ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.6
Committed: Mon Mar 3 14:37:24 2003 UTC (21 years, 2 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -4 lines
Log Message:
Removed headers that were not used.

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
33 static cpu_states_t cpu_now;
34 static int cpu_now_uninit=1;
35
36 cpu_states_t *get_cpu_totals(){
37
38 #ifdef SOLARIS
39 kstat_ctl_t *kc;
40 kstat_t *ksp;
41 cpu_stat_t cs;
42
43 cpu_now.user=0;
44 cpu_now.iowait=0;
45 cpu_now.kernel=0;
46 cpu_now.idle=0;
47 cpu_now.swap=0;
48 cpu_now.total=0;
49 /* Not stored in solaris */
50 cpu_now.nice=0;
51
52 if ((kc = kstat_open()) == NULL) {
53 return NULL;
54 }
55 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
56 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
57 if (kstat_read(kc, ksp, &cs) == -1) {
58 continue;
59 }
60 cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
61 cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
62 cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
63 cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
64 cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
65 }
66
67 cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
68 cpu_now_uninit=0;
69
70 kstat_close(kc);
71 #endif
72
73 cpu_now.systime=time(NULL);
74
75 return &cpu_now;
76 }
77
78 cpu_states_t *get_cpu_diff(){
79 static cpu_states_t cpu_diff;
80 cpu_states_t cpu_then, *cpu_tmp;
81
82 if (cpu_now_uninit){
83 if((cpu_tmp=get_cpu_totals())==NULL){
84 /* Should get_cpu_totals fail */
85 return NULL;
86 }
87 return cpu_tmp;
88 }
89
90
91 cpu_then.user=cpu_now.user;
92 cpu_then.kernel=cpu_now.kernel;
93 cpu_then.idle=cpu_now.idle;
94 cpu_then.iowait=cpu_now.iowait;
95 cpu_then.swap=cpu_now.swap;
96 cpu_then.nice=cpu_now.nice;
97 cpu_then.total=cpu_now.total;
98 cpu_then.systime=cpu_now.systime;
99
100 if((cpu_tmp=get_cpu_totals())==NULL){
101 return NULL;
102 }
103
104 cpu_diff.user = cpu_now.user - cpu_then.user;
105 cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
106 cpu_diff.idle = cpu_now.idle - cpu_then.idle;
107 cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
108 cpu_diff.swap = cpu_now.swap - cpu_then.swap;
109 cpu_diff.nice = cpu_now.nice - cpu_then.nice;
110 cpu_diff.total = cpu_now.total - cpu_then.total;
111 cpu_diff.systime = cpu_now.systime - cpu_then.systime;
112
113 return &cpu_diff;
114 }
115
116 cpu_percent_t *cpu_percent_usage(){
117 static cpu_percent_t cpu_usage;
118 cpu_states_t *cs_ptr;
119
120 cs_ptr=get_cpu_diff();
121 if(cs_ptr==NULL){
122 return NULL;
123 }
124
125 cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
126 cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
127 cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
128 cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
129 cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
130 cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
131 cpu_usage.time_taken = cs_ptr->systime;
132
133 return &cpu_usage;
134
135 }
136