ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.3
Committed: Thu Feb 20 13:19:52 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +0 -1 lines
Log Message:
Removed references to ukcprog.h.
Fixed a missing * in the disk stats.
And removed a block of linux stuff that shouldn't have been there :-)
All with Pete's approval, of course.

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