LinuxQuestions.org
Review your favorite Linux distribution.
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 10-03-2005, 05:47 AM   #1
arunka
Member
 
Registered: Mar 2005
Posts: 60

Rep: Reputation: 15
/proc/stat file problem for cpu usage


hi all
i am having linux kernel version 2.4.21-138 (suse 8),
some times i observe that in /proc/stat file the count of cpu_idle
field will be geeting decremented if i cat the /proc/stat file two times.
since these are the statistics that will be from the system uptime,
i believe either the value should be same or incremented.
i think it is a bug in kernel code...
Also can any one let me know how top calculates the CPU usage??


Thanks in advance...


-Arun KA
 
Old 10-04-2005, 04:48 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 77
This is from man (5) proc re: /proc/stat:
Quote:
The amount of time, measured in units of USER_HZ (1/100ths of a second on most architecures), that the system spent in
user mode, user mode with low priority (nice), system mode, and the idle task, respectively. The last value should be
USER_HZ times the second entry in the uptime pseudo-file.
This is from proc/sysinfo.c, which is included in the procps package. This is where the source for 'top' includes...

Quote:
* Some values in /proc are expressed in units of 1/HZ seconds, where HZ
* is the kernel clock tick rate. One of these units is called a jiffy.
* The HZ value used in the kernel may vary according to hacker desire.
* According to Linus Torvalds, this is not true. He considers the values
* in /proc as being in architecture-dependant units that have no relation
* to the kernel clock tick rate. Examination of the kernel source code
* reveals that opinion as wishful thinking.
*
* In any case, we need the HZ constant as used in /proc. (the real HZ value
* may differ, but we don't care) There are several ways we could get HZ:
*
* 1. Include the kernel header file. If it changes, recompile this library.
* 2. Use the sysconf() function. When HZ changes, recompile the C library!
* 3. Ask the kernel. This is obviously correct...
*
* Linus Torvalds won't let us ask the kernel, because he thinks we should
* not know the HZ value. Oh well, we don't have to listen to him.
* Someone smuggled out the HZ value. :-)
I recommend you read this source code to see what is going on. You can get the source here:
http://procps.sourceforge.net/

There are however, easier ways to get this info. See man (2) sysinfo, for a system call.

Last edited by bulliver; 10-04-2005 at 05:08 AM.
 
Old 06-20-2009, 10:49 PM   #3
danibel.bonetti
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
C program to calculate CPU Usage using /proc/stat

Hi, try this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

double time_so_far()
{
 struct timeval tp;

 if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
   perror("gettimeofday");
 return ((double) (tp.tv_sec)) +
   (((double) tp.tv_usec) * 0.000001 );
}


int main() {

	FILE *f1;
	double ti, tf;
	
	char c[10];
	int i1,i2,i3,i4,i5,i6;

	ti = time_so_far();
	f1 = fopen("/proc/stat", "r");
	fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);
	fclose(f1);

	usleep(1000000);

	tf = time_so_far();
	f1 = fopen("/proc/stat", "r");
	fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);
	fclose(f1);

	int t = (i4+i5+i6)-(i1+i2+i3);
	printf("cpu usage: %.1f%%\n", (t / ((tf-ti) * 100)) * 100);

	return 0;
}
 
Old 04-23-2014, 01:08 PM   #4
eyas
LQ Newbie
 
Registered: Apr 2014
Posts: 1

Rep: Reputation: Disabled
Thumbs up thanks

it's work carefully
 
  


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
New cpu values in /proc/stat? eldonz Linux - General 1 10-19-2005 12:16 PM
/proc/stat cpu info GDarkBladE Linux - Software 2 10-11-2005 03:16 PM
/proc/stat cpu ahz Programming 1 04-24-2005 07:24 AM
Comuting CPU usage from /proc ziggyboy Linux - Software 1 09-27-2004 12:52 PM
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 04:14 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