ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/cpu_stats.c (file contents):
Revision 1.6 by pajs, Mon Mar 3 14:37:24 2003 UTC vs.
Revision 1.7 by pajs, Mon Mar 3 15:26:23 2003 UTC

# Line 29 | Line 29
29   #include <sys/sysinfo.h>
30   #include <string.h>
31   #endif
32 + #ifdef LINUX
33 + #include <stdio.h>
34 + #endif
35  
36   static cpu_states_t cpu_now;
37   static int cpu_now_uninit=1;
# Line 38 | Line 41 | cpu_states_t *get_cpu_totals(){
41   #ifdef SOLARIS
42          kstat_ctl_t *kc;
43          kstat_t *ksp;
44 <        cpu_stat_t cs;
45 <
44 >        cpu_stat_t cs;
45 > #endif
46 > #ifdef LINUX
47 >        FILE *f;
48 > #endif
49 >        
50          cpu_now.user=0;
51 +        /* Not stored in linux */
52          cpu_now.iowait=0;
53          cpu_now.kernel=0;
54          cpu_now.idle=0;
55 +        /* Not stored in linux */
56          cpu_now.swap=0;
57          cpu_now.total=0;
58          /* Not stored in solaris */
59          cpu_now.nice=0;
60  
61 + #ifdef SOLARIS
62          if ((kc = kstat_open()) == NULL) {
63                  return NULL;
64          }
# Line 65 | Line 75 | cpu_states_t *get_cpu_totals(){
75          }
76  
77          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
78 <        cpu_now_uninit=0;
69 <
78 >        
79          kstat_close(kc);
80   #endif
81 + #ifdef LINUX
82 +        if ((f=fopen("/proc/stat", "r" ))==NULL) {
83 +                return NULL;
84 +        }
85 +        /* The very first line should be cpu */
86 +        if((fscanf(f, "cpu %lld %lld %lld %lld", \
87 +                &cpu_now.user, \
88 +                &cpu_now.nice, \
89 +                &cpu_now.kernel, \
90 +                &cpu_now.idle)) != 4){
91 +                return NULL;
92 +        }
93  
94 +        fclose(f);
95 +
96 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
97 + #endif
98 +
99          cpu_now.systime=time(NULL);
100 +        cpu_now_uninit=0;
101 +
102  
103          return &cpu_now;
104   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines