LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-26-2009, 08:35 AM   #1
NilesBor
LQ Newbie
 
Registered: Apr 2009
Posts: 7

Rep: Reputation: 0
How to retrieve CPU, Disk and Memory utilization in linux userspace code


Hi,

I would like to write userspace code that will retrieve the CPU utilization, Disk utilization and Memory utilization at a given time (all in %). I know that various linux commands will help (like "top", "free", "mpstat", "sar", etc) but they do not help me when writing C code in userspace as I will not be running commands on the command line.

At first I thought I should investigate what system calls these linux commands make and try to follow similar guide to writing my code to retrieve the utilization parms I mentioned above.

Then, upon further googling, some users say just to use the linux commands in your C code but just precede it by "system" and/or "popen" and parse/retrieve the desired results. Eg:

<code>
system("ps aux | grep 28164 | awk '{print $6}'");
</code>

...prints the 6th field of process ID 28164 (but unfortunately does not store it in a C variable).

<code>
FILE *get_ps;
get_mac = popen("ifconfig eth0 | grep HWaddr | awk '{print $5}'", "r");
</code>

...gets a particular MAC address (stores it in get_mac).


Has anyone had experience doing this? Any recommendations?

Thanks for any help.
 
Old 06-17-2009, 08:13 AM   #2
sreeharsha.t
Member
 
Registered: Jan 2009
Location: Mumbai, India
Distribution: Fedora 9, 10,11, RHEL 5
Posts: 67

Rep: Reputation: 18
Talking

Quote:
Originally Posted by NilesBor View Post
Hi,

I would like to write userspace code that will retrieve the CPU utilization, Disk utilization and Memory utilization at a given time (all in %). I know that various linux commands will help (like "top", "free", "mpstat", "sar", etc) but they do not help me when writing C code in userspace as I will not be running commands on the command line.
The commands like top, free, mpstat, etc. run in userspace.. and the term 'open source' means you can look at their source code.. This will be the easiest way to address your problem..
 
Old 06-17-2009, 08:27 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
The data you need are all probably exposed via /proc or sysfs. Simply read from there - some math will be required; that is where calling the above programs is beneficial. Calculating CPU usage (for example) requires determining both a usage and time delta, then doing the math for percentage, taking into account how many processors/cores are in use.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
process memory,CPU utilization kbarani Linux - Software 1 01-07-2009 03:50 AM
Sending alert if CPU or RAM or Disk reaches 90% utilization dhanju Linux - Server 2 08-18-2008 10:07 AM
Linux desktop GUI tool for CPU utilization and bandwidth utilization karimasif Linux - Software 4 03-03-2008 01:09 PM
cpu and memory utilization laxmi_cs Linux - General 2 08-29-2005 04:06 PM
CPU & memory utilization rgerhards Linux - Newbie 2 12-03-2003 11:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration