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.3 by tdb, Thu Feb 20 13:19:52 2003 UTC vs.
Revision 1.8 by pajs, Mon Mar 31 11:30:54 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
25   #include <time.h>
30 #include <string.h>
26   #include "statgrab.h"
27   #ifdef SOLARIS
28   #include <kstat.h>
29   #include <sys/sysinfo.h>
30 + #include <string.h>
31   #endif
32 < #ifdef FREEBSD
33 < #include <sys/sysctl.h>
38 < #include <sys/dkstat.h>
32 > #ifdef LINUX
33 > #include <stdio.h>
34   #endif
35  
36   static cpu_states_t cpu_now;
# Line 46 | 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 67 | cpu_states_t *get_cpu_totals(){
67                  if (kstat_read(kc, ksp, &cs) == -1) {
68                          continue;
69                  }
70 <                cpu_now.user+=cs.cpu_sysinfo.cpu[CPU_USER];
71 <                cpu_now.iowait+=cs.cpu_sysinfo.cpu[CPU_WAIT];
72 <                cpu_now.kernel+=cs.cpu_sysinfo.cpu[CPU_KERNEL];
73 <                cpu_now.idle+=cs.cpu_sysinfo.cpu[CPU_IDLE];
74 <                cpu_now.swap+=cs.cpu_sysinfo.cpu[CPU_STATES];
70 >                cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
71 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
72 >                cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
73 >                cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
74 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
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;
77 <
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 +                fclose(f);
92 +                return NULL;
93 +        }
94  
95 +        fclose(f);
96 +
97 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
98 + #endif
99 +
100          cpu_now.systime=time(NULL);
101 +        cpu_now_uninit=0;
102 +
103  
104          return &cpu_now;
105   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines