ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.7
Committed: Mon Mar 3 15:26:23 2003 UTC (21 years, 2 months ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_3_3, LIBSTATGRAB_0_3_2, LIBSTATGRAB_0_3_1, LIBSTATGRAB_0_3
Changes since 1.6: +32 -4 lines
Log Message:
Added linux support for cpu stats

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 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.
10 *
11 * This program 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.
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <time.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
36 static cpu_states_t cpu_now;
37 static int cpu_now_uninit=1;
38
39 cpu_states_t *get_cpu_totals(){
40
41 #ifdef SOLARIS
42 kstat_ctl_t *kc;
43 kstat_t *ksp;
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 }
65 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
66 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
67 if (kstat_read(kc, ksp, &cs) == -1) {
68 continue;
69 }
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
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 return NULL;
92 }
93
94 fclose(f);
95
96 cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
97 #endif
98
99 cpu_now.systime=time(NULL);
100 cpu_now_uninit=0;
101
102
103 return &cpu_now;
104 }
105
106 cpu_states_t *get_cpu_diff(){
107 static cpu_states_t cpu_diff;
108 cpu_states_t cpu_then, *cpu_tmp;
109
110 if (cpu_now_uninit){
111 if((cpu_tmp=get_cpu_totals())==NULL){
112 /* Should get_cpu_totals fail */
113 return NULL;
114 }
115 return cpu_tmp;
116 }
117
118
119 cpu_then.user=cpu_now.user;
120 cpu_then.kernel=cpu_now.kernel;
121 cpu_then.idle=cpu_now.idle;
122 cpu_then.iowait=cpu_now.iowait;
123 cpu_then.swap=cpu_now.swap;
124 cpu_then.nice=cpu_now.nice;
125 cpu_then.total=cpu_now.total;
126 cpu_then.systime=cpu_now.systime;
127
128 if((cpu_tmp=get_cpu_totals())==NULL){
129 return NULL;
130 }
131
132 cpu_diff.user = cpu_now.user - cpu_then.user;
133 cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
134 cpu_diff.idle = cpu_now.idle - cpu_then.idle;
135 cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
136 cpu_diff.swap = cpu_now.swap - cpu_then.swap;
137 cpu_diff.nice = cpu_now.nice - cpu_then.nice;
138 cpu_diff.total = cpu_now.total - cpu_then.total;
139 cpu_diff.systime = cpu_now.systime - cpu_then.systime;
140
141 return &cpu_diff;
142 }
143
144 cpu_percent_t *cpu_percent_usage(){
145 static cpu_percent_t cpu_usage;
146 cpu_states_t *cs_ptr;
147
148 cs_ptr=get_cpu_diff();
149 if(cs_ptr==NULL){
150 return NULL;
151 }
152
153 cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
154 cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
155 cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
156 cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
157 cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
158 cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
159 cpu_usage.time_taken = cs_ptr->systime;
160
161 return &cpu_usage;
162
163 }
164