LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 06-09-2004, 04:20 AM   #1
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12
Thanked: 0
finding cpu usage with /proc


[Log in to get rid of this advertisement]
hi, im just wondering how i can find the cpu usage of a program (as a percentage or a figure i can turn into a percentage) using /proc file

thankx in advance
breadcrust is offline     Reply With Quote
Old 06-09-2004, 07:32 AM   #2
zaphod666
LQ Newbie
 
Registered: Jun 2004
Location: Meerbusch, Germany
Distribution: freeBSD 5.1 Slackware 9.1
Posts: 10
Thanked: 0
Hi,

why don't use ps? It gives you all information about cpuusage, you can define the informations ps writes to stout, just read the manpage.

Bis Bald
zaphod666 is offline     Reply With Quote
Old 06-09-2004, 07:44 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
I'm not very sure this is the most correct way, but from a little browsing through "man proc" I'd say something like:

- First get the PID of the process you want to get the CPU percentage of.
- Then parse /proc/<PID>/stat and get the fields "utime" and "stime" from that file.
- Add those two values and call the result "pidcpu", i.e.: pidcpu = utime + stime
- Parse /proc/stat and add the first 3 fields from the first line ("cpu") together, call this result "totalcpu"
- finally: percentage = 100 * pidcpu / totalcpu
Hko is offline     Reply With Quote
Old 06-14-2004, 02:02 AM   #4
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12
Thanked: 0

Original Poster
hmm, thanx hko, but that doesnt seem to work.

i might end up using ps if i cant figure out how to do this through /proc. i had a quick look at the man page for ps but i couldnt figure out how to show cpu usages, how can this be done?
breadcrust is offline     Reply With Quote
Old 06-14-2004, 11:09 AM   #5
zaphod666
LQ Newbie
 
Registered: Jun 2004
Location: Meerbusch, Germany
Distribution: freeBSD 5.1 Slackware 9.1
Posts: 10
Thanked: 0
Hi,

look for this in the manpage:

STANDARD FORMAT SPECIFIERS
These may be used to control both output format and sort-
ing.
For example: ps -eo pid,user,args --sort user

CODE HEADER
() ()

%cpu %CPU
%mem %MEM
alarm ALARM
args COMMAND
blocked BLOCKED
bsdstart START
bsdtime TIME
c C
caught CAUGHT


Bis Bald
zaphod666 is offline     Reply With Quote
Old 06-14-2004, 11:49 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
Quote:
hmm, thanx hko, but that doesnt seem to work.
Sorry about that.
Quote:
i might end up using ps if i cant figure out how to do this through /proc. i had a quick look at the man page for ps but i couldnt figure out how to show cpu usages, how can this be done?
I'd try having a look at the source of 'ps' instead of the man page. 'ps' uses /proc for sure. You can get it here: http://procps.sourceforge.net/
Hko is offline     Reply With Quote
Old 06-17-2004, 04:54 PM   #7
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Main: Ubuntu 7.04, Secondary: DamnSmallLinux, Server: Slackware 12.0
Posts: 841
Blog Entries: 1
Thanked: 0
How, in c++, can I retrieve a specific PID cpu usage?
in C++
not bash

thanks
mimithebrain is offline     Reply With Quote
Old 06-17-2004, 11:08 PM   #8
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12
Thanked: 0

Original Poster
Quote:
Originally posted by mimithebrain
How, in c++, can I retrieve a specific PID cpu usage?
in C++ not bash
thats what im trying to figure out, using /proc files. this isnt a shell script im doing, its a tcl/tk application.
breadcrust is offline     Reply With Quote
Old 06-18-2004, 09:28 AM   #9
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Thanked: 1
hko is right, I don't see an other good solution than open virtual file /proc/<PID>/stat and analyse it ever in C++ or tcl/tk.

Try cat some of these files and you will see.

Last edited by keefaz; 06-18-2004 at 09:31 AM..
keefaz is offline     Reply With Quote
Old 06-18-2004, 10:25 AM   #10
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Main: Ubuntu 7.04, Secondary: DamnSmallLinux, Server: Slackware 12.0
Posts: 841
Blog Entries: 1
Thanked: 0
That does not help,

What I need for my project (and his different project) is how to access that cpu/kernel filesystem and access this is C++,
If nobody knows, say so.
Like that ramdom number thing, I'll have to figure out a formula myself!

Well, can't get everything fresh baked on a plate without working for it!
mimithebrain is offline     Reply With Quote
Old 06-18-2004, 10:41 AM   #11
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Thanked: 1
Well apparently you don't know read...I will add one more thing
/proc/<PID>/stat is a virtual file but you can open it for read like any regular file.
keefaz is offline     Reply With Quote
Old 06-18-2004, 01:27 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
Quote:
Originally posted by mimithebrain
That does not help,
You didn't try.

Quote:
What I need for my project (and his different project) is how to access that cpu/kernel filesystem and access this is C++,
The answers in this thread were already referring to C(++).

Quote:
If nobody knows, say so.
If you don't know how to open a file in C(++)....say so, instead of re-asking the original question, which is quite a different from "how to read a file in C(++)?"

Last edited by Hko; 06-18-2004 at 01:28 PM..
Hko is offline     Reply With Quote
Old 06-18-2004, 01:55 PM   #13
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Main: Ubuntu 7.04, Secondary: DamnSmallLinux, Server: Slackware 12.0
Posts: 841
Blog Entries: 1
Thanked: 0
by the quoting, I see I might have offended you.
Sorry, with sugar on it...

what would be the code to access and assimilate the infomation in /proc ?
When I attempt to read file in proc with "kwrite", it's blank.

Thank you
mimithebrain is offline     Reply With Quote
Old 06-18-2004, 03:01 PM   #14
jim mcnamara
Member
 
Registered: May 2002
Posts: 964
Thanked: 0
www.advancedlinuxprogramming.com has a chapter (pdf file) on working with the proc filesystem. It has example code in C.
jim mcnamara is offline     Reply With Quote
Old 06-18-2004, 03:21 PM   #15
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
Quote:
Originally posted by mimithebrain
by the quoting, I see I might have offended you.
True.

Quote:
what would be the code to access and assimilate the infomation in /proc
Basically:
Code:
#include <fstream>
#include <iostream>

#define MAXLEN 100

int main()
{
	 std::ifstream f;
	 char line[MAXLEN + 1];

	 f.open("/proc/stat");
	 f.get(line, MAXLEN);
	 std::cout << line << std::endl;
	 return 0;
}
Quote:
When I attempt to read file in proc with "kwrite", it's blank.
Some files in /proc may be empty, to other files you may have not persmission to read. But the files you need for cpu-usage will be readable by normal users. For example, /proc/self/stat should not be empty, and readable. If it is still empty, I can only think of only one other reason: your kernel does not support the /proc file-system, or it isn't mounted. This not very likely for the main linux distributions though.

Last edited by Hko; 06-18-2004 at 03:26 PM..
Hko is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
/proc/stat file problem for cpu usage arunka Programming 2 06-20-2009 11:49 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 09:43 PM
Comuting CPU usage from /proc ziggyboy Linux - Software 1 09-27-2004 01:52 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 05:45 AM
Calculating total %cpu usage from /proc/stat gaijin Programming 1 07-07-2004 03:45 AM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration