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.1 by pajs, Tue Feb 18 19:28:30 2003 UTC vs.
Revision 1.13 by ats, Sun Oct 19 02:03:02 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License
# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include <stdio.h>
26 #include <ukcprog.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
25   #include <time.h>
31 #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 LINUX
33 + #include <stdio.h>
34 + #endif
35   #ifdef FREEBSD
36   #include <sys/sysctl.h>
37   #include <sys/dkstat.h>
38   #endif
39 + #ifdef NETBSD
40 + #include <sys/types.h>
41 + #include <sys/param.h>
42 + #include <sys/sysctl.h>
43 + #include <sys/sched.h>
44 + #endif
45  
46   static cpu_states_t cpu_now;
47   static int cpu_now_uninit=1;
48  
45
49   cpu_states_t *get_cpu_totals(){
50  
51   #ifdef SOLARIS
52          kstat_ctl_t *kc;
53          kstat_t *ksp;
54 <        cpu_stat_t cs;
55 <
54 >        cpu_stat_t cs;
55 > #endif
56 > #ifdef LINUX
57 >        FILE *f;
58 > #endif
59 > #ifdef ALLBSD
60 > #ifndef FREEBSD
61 >        int mib[2];
62 > #endif
63 > #ifdef NETBSD
64 >        u_int64_t cp_time[CPUSTATES];
65 > #else
66 >        long cp_time[CPUSTATES];
67 > #endif
68 >        size_t size;
69 > #endif
70 >        
71          cpu_now.user=0;
72 +        /* Not stored in linux or freebsd */
73          cpu_now.iowait=0;
74          cpu_now.kernel=0;
75          cpu_now.idle=0;
76 +        /* Not stored in linux or freebsd */
77          cpu_now.swap=0;
78          cpu_now.total=0;
79          /* Not stored in solaris */
80          cpu_now.nice=0;
81  
82 + #ifdef SOLARIS
83          if ((kc = kstat_open()) == NULL) {
84                  return NULL;
85          }
# Line 67 | Line 88 | cpu_states_t *get_cpu_totals(){
88                  if (kstat_read(kc, ksp, &cs) == -1) {
89                          continue;
90                  }
91 <                cpu_now.user+=cs.cpu_sysinfo.cpu[CPU_USER];
92 <                cpu_now.iowait+=cs.cpu_sysinfo.cpu[CPU_WAIT];
93 <                cpu_now.kernel+=cs.cpu_sysinfo.cpu[CPU_KERNEL];
94 <                cpu_now.idle+=cs.cpu_sysinfo.cpu[CPU_IDLE];
95 <                cpu_now.swap+=cs.cpu_sysinfo.cpu[CPU_STATES];
91 >                cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
92 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
93 >                cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
94 >                cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
95 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
96          }
97  
98          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
99 <        cpu_now_uninit=0;
99 >        
100 >        kstat_close(kc);
101 > #endif
102 > #ifdef LINUX
103 >        if ((f=fopen("/proc/stat", "r" ))==NULL) {
104 >                return NULL;
105 >        }
106 >        /* The very first line should be cpu */
107 >        if((fscanf(f, "cpu %lld %lld %lld %lld", \
108 >                &cpu_now.user, \
109 >                &cpu_now.nice, \
110 >                &cpu_now.kernel, \
111 >                &cpu_now.idle)) != 4){
112 >                fclose(f);
113 >                return NULL;
114 >        }
115  
116 <        if((kstat_close(kc)) != 0){
81 <                return NULL;
82 <        }
116 >        fclose(f);
117  
118 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
119   #endif
120 + #ifdef ALLBSD
121 + #ifdef FREEBSD
122 +        size = sizeof cp_time;
123 +        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
124 +                return NULL;
125 +        }
126 + #else
127 +        mib[0] = CTL_KERN;
128 +        mib[1] = KERN_CP_TIME;
129 +        size = sizeof cp_time;
130 +        if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
131 +                return NULL;
132 +        }
133 + #endif
134  
135 +        cpu_now.user=cp_time[CP_USER];
136 +        cpu_now.nice=cp_time[CP_NICE];
137 +        cpu_now.kernel=cp_time[CP_SYS];
138 +        cpu_now.idle=cp_time[CP_IDLE];
139 +        
140 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
141 +
142 + #endif
143 +
144          cpu_now.systime=time(NULL);
145 +        cpu_now_uninit=0;
146 +
147  
148          return &cpu_now;
149   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines