LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
Search this Thread
Old 06-09-2004, 03:20 AM   #1
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12

Rep: Reputation: 0
finding cpu usage with /proc


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
 
Old 06-09-2004, 06:32 AM   #2
zaphod666
LQ Newbie
 
Registered: Jun 2004
Location: Meerbusch, Germany
Distribution: freeBSD 5.1 Slackware 9.1
Posts: 10

Rep: Reputation: 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
 
Old 06-09-2004, 06:44 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
Old 06-14-2004, 01:02 AM   #4
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12

Original Poster
Rep: Reputation: 0
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?
 
Old 06-14-2004, 10:09 AM   #5
zaphod666
LQ Newbie
 
Registered: Jun 2004
Location: Meerbusch, Germany
Distribution: freeBSD 5.1 Slackware 9.1
Posts: 10

Rep: Reputation: 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
 
Old 06-14-2004, 10:49 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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/
 
Old 06-17-2004, 03:54 PM   #7
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843
Blog Entries: 1

Rep: Reputation: 30
How, in c++, can I retrieve a specific PID cpu usage?
in C++
not bash

thanks
 
Old 06-17-2004, 10:08 PM   #8
breadcrust
LQ Newbie
 
Registered: Jun 2003
Location: Australia
Distribution: Mostly Debian (and Ubuntu)
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 06-18-2004, 08:28 AM   #9
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,341

Rep: Reputation: 73
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 08:31 AM.
 
Old 06-18-2004, 09:25 AM   #10
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843
Blog Entries: 1

Rep: Reputation: 30
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!
 
Old 06-18-2004, 09:41 AM   #11
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,341

Rep: Reputation: 73
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.
 
Old 06-18-2004, 12:27 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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 12:28 PM.
 
Old 06-18-2004, 12:55 PM   #13
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843
Blog Entries: 1

Rep: Reputation: 30
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
 
Old 06-18-2004, 02:01 PM   #14
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
www.advancedlinuxprogramming.com has a chapter (pdf file) on working with the proc filesystem. It has example code in C.
 
Old 06-18-2004, 02:21 PM   #15
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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 02:26 PM.
 
  


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
/proc/stat file problem for cpu usage arunka Programming 3 04-23-2014 01:08 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
Comuting CPU usage from /proc ziggyboy Linux - Software 1 09-27-2004 12: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 04:45 AM
Calculating total %cpu usage from /proc/stat gaijin Programming 1 07-07-2004 02:45 AM


All times are GMT -5. The time now is 03:47 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration