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.17 by tdb, Mon Jan 19 16:49:21 2004 UTC

# Line 1 | Line 1
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-2004 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
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 > *
20 > * $Id$
21   */
22  
23   #ifdef HAVE_CONFIG_H
24   #include "config.h"
25   #endif
26  
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
27   #include <time.h>
30 #include <string.h>
28   #include "statgrab.h"
29   #ifdef SOLARIS
30   #include <kstat.h>
31   #include <sys/sysinfo.h>
32 + #include <string.h>
33   #endif
34 + #if defined(LINUX) || defined(CYGWIN)
35 + #include <stdio.h>
36 + #endif
37   #ifdef FREEBSD
38   #include <sys/sysctl.h>
39   #include <sys/dkstat.h>
40   #endif
41 + #ifdef NETBSD
42 + #include <sys/types.h>
43 + #include <sys/param.h>
44 + #include <sys/sysctl.h>
45 + #include <sys/sched.h>
46 + #endif
47  
48   static cpu_states_t cpu_now;
49   static int cpu_now_uninit=1;
# Line 46 | Line 53 | cpu_states_t *get_cpu_totals(){
53   #ifdef SOLARIS
54          kstat_ctl_t *kc;
55          kstat_t *ksp;
56 <        cpu_stat_t cs;
57 <
56 >        cpu_stat_t cs;
57 > #endif
58 > #if defined(LINUX) || defined(CYGWIN)
59 >        FILE *f;
60 > #endif
61 > #ifdef ALLBSD
62 > #ifndef FREEBSD
63 >        int mib[2];
64 > #endif
65 > #ifdef NETBSD
66 >        u_int64_t cp_time[CPUSTATES];
67 > #else
68 >        long cp_time[CPUSTATES];
69 > #endif
70 >        size_t size;
71 > #endif
72 >        
73          cpu_now.user=0;
74 +        /* Not stored in linux or freebsd */
75          cpu_now.iowait=0;
76          cpu_now.kernel=0;
77          cpu_now.idle=0;
78 +        /* Not stored in linux or freebsd */
79          cpu_now.swap=0;
80          cpu_now.total=0;
81          /* Not stored in solaris */
82          cpu_now.nice=0;
83  
84 + #ifdef SOLARIS
85          if ((kc = kstat_open()) == NULL) {
86                  return NULL;
87          }
# Line 65 | Line 90 | cpu_states_t *get_cpu_totals(){
90                  if (kstat_read(kc, ksp, &cs) == -1) {
91                          continue;
92                  }
93 <                cpu_now.user+=cs.cpu_sysinfo.cpu[CPU_USER];
94 <                cpu_now.iowait+=cs.cpu_sysinfo.cpu[CPU_WAIT];
95 <                cpu_now.kernel+=cs.cpu_sysinfo.cpu[CPU_KERNEL];
96 <                cpu_now.idle+=cs.cpu_sysinfo.cpu[CPU_IDLE];
97 <                cpu_now.swap+=cs.cpu_sysinfo.cpu[CPU_STATES];
93 >                cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
94 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
95 >                cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
96 >                cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
97 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
98          }
99  
100          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
101 <        cpu_now_uninit=0;
77 <
101 >        
102          kstat_close(kc);
103   #endif
104 + #if defined(LINUX) || defined(CYGWIN)
105 +        if ((f=fopen("/proc/stat", "r" ))==NULL) {
106 +                return NULL;
107 +        }
108 +        /* The very first line should be cpu */
109 +        if((fscanf(f, "cpu %lld %lld %lld %lld", \
110 +                &cpu_now.user, \
111 +                &cpu_now.nice, \
112 +                &cpu_now.kernel, \
113 +                &cpu_now.idle)) != 4){
114 +                fclose(f);
115 +                return NULL;
116 +        }
117  
118 +        fclose(f);
119 +
120 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
121 + #endif
122 + #ifdef ALLBSD
123 + #ifdef FREEBSD
124 +        size = sizeof cp_time;
125 +        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
126 +                return NULL;
127 +        }
128 + #else
129 +        mib[0] = CTL_KERN;
130 +        mib[1] = KERN_CP_TIME;
131 +        size = sizeof cp_time;
132 +        if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
133 +                return NULL;
134 +        }
135 + #endif
136 +
137 +        cpu_now.user=cp_time[CP_USER];
138 +        cpu_now.nice=cp_time[CP_NICE];
139 +        cpu_now.kernel=cp_time[CP_SYS];
140 +        cpu_now.idle=cp_time[CP_IDLE];
141 +        
142 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
143 +
144 + #endif
145 +
146          cpu_now.systime=time(NULL);
147 +        cpu_now_uninit=0;
148 +
149  
150          return &cpu_now;
151   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines