[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2528
  • Last Modified:

How to measure CPU/Memory usage?


I'm working on a project that one of its tasks is to measure the average memory and cpu usage for the passed X minutes. The plan is to cover tier 1 Unix/Linux OS,

I'm a programmer and not very familiar with the different technical aspects of cpu and memory but I do know that these is no single number that represents them.
I found out in Unix the command "vmstat" that gives information about memory, cpu, page fault and other system parameters but I'm not sure this is the right tool. I mean, there is user cpu usage and system cpu usage. How do you figure out a single number that represents the overall cpu usage? The same goes for memory, there is phisical memory, and virtual memory and some other memory related parameters.
And what happen if you have 16 cpus? Can you boil it down to a single number?

The design is that a user will define a minimum free cpu usage and a minimum available memory, and if these conditions are met, only then other tasks will be performed. This is to prevent a situation in which a task is submitted to a machine that is already overloaded.

How would you approach such a task?
Do you know of any 3rd parties softwares or utilities that yield such information?

  • 3
  • 2
  • 2
  • +2
4 Solutions
Use top command for real time monitoring ... top -b gives you periodic snapshots ... man top for more info
Also look into time command to time the execution of your program ... and of course there are profilers for memory and CPU e.g. memprof, valgind, gprof etc.
nivo_ZAuthor Commented:
Thanks fpr your reply.
Top is not necessarily installed, the solution should be generic and sould use tools that exist by default.
what all tools are available?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

How about something simple like "ps".
ps has an option  that allows you to format it's output.
online man page example : http://www.netadmintools.com/html/1ps.man.html
or better try : man ps

for instance "ps -eo pid,pcpu,comm,args" gives you the pid , the processor usage, process name and it´s arguments

SNMP agent is existent on all platforms. read host mib.
how to mesure the memory usage of a perl script?

this can not be caught with top, ps and /usr/bin/time -v prints everywhere a 0.

time prints
real    0m0.608s
user    0m0.523s
sys     0m0.033s

/usr/bin/time -v
        Command being timed: "perl app.pl"
        User time (seconds): 0.51
        System time (seconds): 0.03
        Percent of CPU this job got: 81%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.66
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 0
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 3764
        Voluntary context switches: 147
        Involuntary context switches: 180
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

You should use process accounting. most likely psacct package contains sa, sar and accton tools and google will show you enough guidance for your purpose.
Hello, thank zou for the answer. I was googleing for about an hour but couldnät find anzthing usable, also non of the mentioned programs has ever appeared upto page #5 in google :-) so I sould try something you've mentioned google about it and read the man page

thanks and regards

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now