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.7 by pajs, Mon Mar 3 15:26:23 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
# Line 29 | Line 31
31   #include <sys/sysinfo.h>
32   #include <string.h>
33   #endif
34 < #ifdef LINUX
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 43 | Line 55 | cpu_states_t *get_cpu_totals(){
55          kstat_t *ksp;
56          cpu_stat_t cs;
57   #endif
58 < #ifdef LINUX
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 */
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 */
78 >        /* Not stored in linux or freebsd */
79          cpu_now.swap=0;
80          cpu_now.total=0;
81          /* Not stored in solaris */
# Line 78 | Line 101 | cpu_states_t *get_cpu_totals(){
101          
102          kstat_close(kc);
103   #endif
104 < #ifdef LINUX
104 > #if defined(LINUX) || defined(CYGWIN)
105          if ((f=fopen("/proc/stat", "r" ))==NULL) {
106                  return NULL;
107          }
# Line 88 | Line 111 | cpu_states_t *get_cpu_totals(){
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines