ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/cpu_stats.c
Revision: 1.2
Committed: Tue Feb 18 23:23:36 2003 UTC (21 years, 3 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -5 lines
Log Message:
Changed the kstat_close to not return NULL in event of a failure. If we
cant close it, well there is nothing i can do about that, so i may as well
at least return something useful since its done all the hardwork by that
point. And anywan, it should never fail to close :)

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