ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.8
Committed: Mon Mar 31 11:30:54 2003 UTC (21 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_3_4
Changes since 1.7: +1 -0 lines
Log Message:
Fixed some issues with not closing FILE*'s.

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 fclose(f);
92 return NULL;
93 }
94
95 fclose(f);
96
97 cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
98 #endif
99
100 cpu_now.systime=time(NULL);
101 cpu_now_uninit=0;
102
103
104 return &cpu_now;
105 }
106
107 cpu_states_t *get_cpu_diff(){
108 static cpu_states_t cpu_diff;
109 cpu_states_t cpu_then, *cpu_tmp;
110
111 if (cpu_now_uninit){
112 if((cpu_tmp=get_cpu_totals())==NULL){
113 /* Should get_cpu_totals fail */
114 return NULL;
115 }
116 return cpu_tmp;
117 }
118
119
120 cpu_then.user=cpu_now.user;
121 cpu_then.kernel=cpu_now.kernel;
122 cpu_then.idle=cpu_now.idle;
123 cpu_then.iowait=cpu_now.iowait;
124 cpu_then.swap=cpu_now.swap;
125 cpu_then.nice=cpu_now.nice;
126 cpu_then.total=cpu_now.total;
127 cpu_then.systime=cpu_now.systime;
128
129 if((cpu_tmp=get_cpu_totals())==NULL){
130 return NULL;
131 }
132
133 cpu_diff.user = cpu_now.user - cpu_then.user;
134 cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
135 cpu_diff.idle = cpu_now.idle - cpu_then.idle;
136 cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
137 cpu_diff.swap = cpu_now.swap - cpu_then.swap;
138 cpu_diff.nice = cpu_now.nice - cpu_then.nice;
139 cpu_diff.total = cpu_now.total - cpu_then.total;
140 cpu_diff.systime = cpu_now.systime - cpu_then.systime;
141
142 return &cpu_diff;
143 }
144
145 cpu_percent_t *cpu_percent_usage(){
146 static cpu_percent_t cpu_usage;
147 cpu_states_t *cs_ptr;
148
149 cs_ptr=get_cpu_diff();
150 if(cs_ptr==NULL){
151 return NULL;
152 }
153
154 cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
155 cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
156 cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
157 cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
158 cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
159 cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
160 cpu_usage.time_taken = cs_ptr->systime;
161
162 return &cpu_usage;
163
164 }
165