ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/cpu_stats.c
Revision: 1.1
Committed: Mon May 13 17:10:09 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Log Message:
CPU stat's, currently only works on linux and doesn't print it out usefully.

File Contents

# Content
1 #include <stdio.h>
2 #include <local/ukcprog.h>
3 #include <unistd.h>
4
5 #define WAIT_TIME_IN_SECS 1
6 char *get_cpu_stats(){
7 double u_p, i_p, k_p;
8 #ifdef LINUX
9 long total, user, idle, kernel, nice;
10 long cpu_states[4][2];
11 FILE *f;
12 #endif
13
14 #ifdef LINUX
15 if ((f=fopen("/proc/stat", "r" ))==NULL) {
16 errf("Failed to open /proc/stat (%m)");
17 return NULL;
18 }
19
20 /* cpu stats should be the first line.. */
21 if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][0], &cpu_states[1][0], &cpu_states[2][0] ,&cpu_states[3][0])) != 4){
22 errf("Failed to read in correct number of CPU stats (%m)");
23 return NULL;
24 }
25
26 if ((fclose(f)) != 0) {
27 errf("Failed to close file (%m)");
28 return NULL;
29 }
30
31 sleep(WAIT_TIME_IN_SECS);
32
33 if ((f=fopen("/proc/stat", "r" ))==NULL) {
34 errf("Failed to open /proc/stat (%m)");
35 return NULL;
36 }
37 if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][1], &cpu_states[1][1], &cpu_states[2][1] ,&cpu_states[3][1])) != 4){
38 errf("Failed to read in correct number of CPU stats (%m)");
39 return NULL;
40 }
41
42 if ((fclose(f)) != 0) {
43 errf("Failed to close file (%m)");
44 return NULL;
45 }
46
47 user=cpu_states[0][1]-cpu_states[0][0]+cpu_states[1][1]-cpu_states[1][0];
48 kernel=cpu_states[2][1]-cpu_states[2][0];
49 idle=cpu_states[3][1]-cpu_states[3][0];
50 total=user+kernel+idle;
51
52 u_p=((double)user/(double)total)*100.00;
53 k_p=((double)kernel/(double)total)*100.00;
54 i_p=((double)idle/(double)total)*100.00;
55
56 #endif
57
58 return "jibble";
59 }
60
61 int main(){
62 get_cpu_stats();
63 exit(0);
64 }