Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I am trying to write a program in perl to calculate the cpu and processor utilization of a particular process (for example : mozilla). The output of the program ...
  1. #1
    Just Joined! suresh_v_2k2's Avatar
    Join Date
    May 2007
    Location
    Mumbai
    Posts
    4

    %CPU & %MEM utilization

    Hi, I am trying to write a program in perl to calculate the cpu and processor utilization of a particular process (for example : mozilla). The output of the program must be just like the result obtained by ps command under linux, but i do not want to use the ps command in my program to get the cpu and memory details but use the /proc filesystem to calculate the cpu and memory utilization of process.

    Can you help me how do i calculate the cpu and memory in percentage, utilized by a particular process from the /proc filesystem. I have learnt from various resources that ps command too uses /proc filesystem to get the information about processes, but i could not find any information of how to calculate the percentage utilization of cpu and memory of particular process.

    I hope you could help me out of this problem.

    Thank you.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by suresh_v_2k2 View Post
    ..
    but i could not find any information of how to calculate the percentage utilization of cpu and memory of particular process.....
    if you give the ps command the aux option, it shows %CPU and %MEM used.
    Code:
    # ps aux | awk 'BEGIN{ printf "%-10s%s\n","CPU","MEM"}/mozilla/{printf "%-10s%s\n",$3,$4}'
    CPU       MEM
    3.2       5.5
    0.0       0.0

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, /proc/meminfo has information such as the total memory of the box.
    Code:
    alex@danu /proc $ cat meminfo  
    MemTotal:      1295048 kB
    MemFree:        141768 kB
    Buffers:        187912 kB
    Cached:         338496 kB
    SwapCached:          0 kB
    ...
    /proc also has subdirectories for each running process. Each of these has a 'mem' file. Now, I seem to be unable to read mine, but these likely have information related to the memory usage of a particular process. If you can find a guide through /proc/PID/status, this may also have some info.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined! suresh_v_2k2's Avatar
    Join Date
    May 2007
    Location
    Mumbai
    Posts
    4
    Quote Originally Posted by ghostdog74 View Post
    if you give the ps command the aux option, it shows %CPU and %MEM used.
    Code:
    # ps aux | awk 'BEGIN{ printf "%-10s%s\n","CPU","MEM"}/mozilla/{printf "%-10s%s\n",$3,$4}'
    CPU       MEM
    3.2       5.5
    0.0       0.0
    i already tried the ps command but i do not want to use ps command but to calculate my self from the information avilable in /proc/[PID]/stat or mem or cpu etc.,

    any way thanks for your reply.

  5. #5
    Just Joined! suresh_v_2k2's Avatar
    Join Date
    May 2007
    Location
    Mumbai
    Posts
    4
    Quote Originally Posted by Cabhan View Post
    Well, /proc/meminfo has information such as the total memory of the box.
    Code:
    alex@danu /proc $ cat meminfo  
    MemTotal:      1295048 kB
    MemFree:        141768 kB
    Buffers:        187912 kB
    Cached:         338496 kB
    SwapCached:          0 kB
    ...
    /proc also has subdirectories for each running process. Each of these has a 'mem' file. Now, I seem to be unable to read mine, but these likely have information related to the memory usage of a particular process. If you can find a guide through /proc/PID/status, this may also have some info.
    hi, i think you got my point. i found the information under /proc/PID/status but do you know how to i convert those values in to %values do you have any formlua to find the %cpu and %mem utilization using the values avilable in /proc/PID/stat or cpu or mem etc.,

    thanks for you relpy

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Alrighty, well, the proc man page has a great deal of information. In particular, it discusses the /proc/PID/stat file, where the 23rd entry is:
    Code:
    vsize %lu
           Virtual memory size in bytes.
    If you combine that bit of information with /proc/meminfo, you should be able to figure out percentages.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...