ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.4
Committed: Fri Feb 28 22:59:35 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.3: +0 -5 lines
Log Message:
Tidy up of configure script, and includes.

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