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.2 by pajs, Tue Feb 18 23:23:36 2003 UTC vs.
Revision 1.10 by tdb, Sun Aug 24 20:24:09 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>
# Line 47 | Line 45 | cpu_states_t *get_cpu_totals(){
45   #ifdef SOLARIS
46          kstat_ctl_t *kc;
47          kstat_t *ksp;
48 <        cpu_stat_t cs;
49 <
48 >        cpu_stat_t cs;
49 > #endif
50 > #ifdef LINUX
51 >        FILE *f;
52 > #endif
53 > #ifdef FREEBSD
54 >        long cp_time[CPUSTATES];
55 >        size_t size;
56 > #endif
57 >        
58          cpu_now.user=0;
59 +        /* Not stored in linux or freebsd */
60          cpu_now.iowait=0;
61          cpu_now.kernel=0;
62          cpu_now.idle=0;
63 +        /* Not stored in linux or freebsd */
64          cpu_now.swap=0;
65          cpu_now.total=0;
66          /* Not stored in solaris */
67          cpu_now.nice=0;
68  
69 + #ifdef SOLARIS
70          if ((kc = kstat_open()) == NULL) {
71                  return NULL;
72          }
# Line 66 | Line 75 | cpu_states_t *get_cpu_totals(){
75                  if (kstat_read(kc, ksp, &cs) == -1) {
76                          continue;
77                  }
78 <                cpu_now.user+=cs.cpu_sysinfo.cpu[CPU_USER];
79 <                cpu_now.iowait+=cs.cpu_sysinfo.cpu[CPU_WAIT];
80 <                cpu_now.kernel+=cs.cpu_sysinfo.cpu[CPU_KERNEL];
81 <                cpu_now.idle+=cs.cpu_sysinfo.cpu[CPU_IDLE];
82 <                cpu_now.swap+=cs.cpu_sysinfo.cpu[CPU_STATES];
78 >                cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
79 >                cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
80 >                cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
81 >                cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
82 >                cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
83          }
84  
85          cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
86 <        cpu_now_uninit=0;
78 <
86 >        
87          kstat_close(kc);
88   #endif
89 + #ifdef LINUX
90 +        if ((f=fopen("/proc/stat", "r" ))==NULL) {
91 +                return NULL;
92 +        }
93 +        /* The very first line should be cpu */
94 +        if((fscanf(f, "cpu %lld %lld %lld %lld", \
95 +                &cpu_now.user, \
96 +                &cpu_now.nice, \
97 +                &cpu_now.kernel, \
98 +                &cpu_now.idle)) != 4){
99 +                fclose(f);
100 +                return NULL;
101 +        }
102  
103 +        fclose(f);
104 +
105 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
106 + #endif
107 + #ifdef FREEBSD
108 +        if (sysctlbyname("kern.cp_time", NULL, &size, NULL, NULL) < 0){
109 +                return NULL;
110 +        }
111 +        if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, NULL) < 0){
112 +                return NULL;
113 +        }
114 +
115 +        cpu_now.user=cp_time[CP_USER];
116 +        cpu_now.nice=cp_time[CP_NICE];
117 +        cpu_now.kernel=cp_time[CP_SYS];
118 +        cpu_now.idle=cp_time[CP_IDLE];
119 +        
120 +        cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
121 +
122 + #endif
123 +
124          cpu_now.systime=time(NULL);
125 +        cpu_now_uninit=0;
126 +
127  
128          return &cpu_now;
129   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines