summaryrefslogtreecommitdiff
path: root/src/gpm-load.c
blob: 3b0f57214d52e4ac6da4afb9f1033984da24957e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>

#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#if defined(sun) && defined(__SVR4)
#include <kstat.h>
#include <sys/sysinfo.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <glib/gi18n.h>

#include "gpm-common.h"
#include "gpm-marshal.h"
#include "egg-debug.h"

#include "gpm-load.h"

static void     gpm_load_finalize   (GObject	  *object);

#define GPM_LOAD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPM_TYPE_LOAD, GpmLoadPrivate))

struct GpmLoadPrivate
{
	long unsigned	 old_idle;
	long unsigned	 old_total;
};

static gpointer gpm_load_object = NULL;

G_DEFINE_TYPE (GpmLoad, gpm_load, G_TYPE_OBJECT)

/**
 * gpm_load_class_init:
 * @klass: This class instance
 **/
static void
gpm_load_class_init (GpmLoadClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gpm_load_finalize;
	g_type_class_add_private (klass, sizeof (GpmLoadPrivate));
}

#if defined(sun) && defined(__SVR4)

/**
 * gpm_load_get_cpu_values:
 * @cpu_idle: The idle time reported by the CPU
 * @cpu_total: The total time reported by the CPU
 * Return value: Success of reading /proc/stat.
 **/
static gboolean
gpm_load_get_cpu_values (long unsigned *cpu_idle, long unsigned *cpu_total)
{	
	long unsigned cpu_user = 0;
	long unsigned cpu_kernel = 0;
	long unsigned cpu_wait = 0;
	kstat_ctl_t *kc = NULL;
	kstat_named_t *kn = NULL;
	kstat_t *ks = NULL;
	cpu_stat_t  data;
	int ncpus;
	int count;
	   	
	kc = kstat_open();
	if (!kc) {
		egg_warning ("Cannot open kstat!\n");
		return FALSE;
	}

	ks = kstat_lookup(kc, "unix", 0, "system_misc");
  	if (kstat_read(kc, ks, NULL) == -1) {
		egg_warning ("Cannot read kstat on module unix!\n");
		goto out;
	}
	kn = kstat_data_lookup (ks, "ncpus");
	if (!kn) {
		egg_warning ("Cannot get number of cpus in current system!\n");
		goto out;
	}
	ncpus = kn->value.ui32;

	/* 
 	 * To aggresive ticks used of all cpus,
	 * traverse kstat chain to access very cpu_stat instane.
	 */
	for(count = 0, *cpu_idle =0, *cpu_total = 0; count < ncpus; count++){
		
		ks = kstat_lookup(kc, "cpu_stat", count, NULL);
		if (ks == NULL) {
			egg_warning ("Null output for kstat on cpu%d\n", count);
			goto out;
		}
     
		if (kstat_read(kc, ks, &data) == -1) {
			egg_warning ("Cannot read kstat entry on cpu%d\n", count);
			goto out;
		}

		egg_debug ("cpu%d:\t%lu\t%lu\t%lu\t%lu\n", count,
					data.cpu_sysinfo.cpu[CPU_IDLE],
					data.cpu_sysinfo.cpu[CPU_USER],
					data.cpu_sysinfo.cpu[CPU_KERNEL],
					data.cpu_sysinfo.cpu[CPU_WAIT]);

		*cpu_idle += data.cpu_sysinfo.cpu[CPU_IDLE];
		cpu_user += data.cpu_sysinfo.cpu[CPU_USER];
		cpu_kernel += data.cpu_sysinfo.cpu[CPU_KERNEL];
		cpu_wait += data.cpu_sysinfo.cpu[CPU_WAIT];
	}
	kstat_close(kc);
	/*
         * Summing up all these times gives you the system uptime.
         * This is what the uptime command does.
         */
        *cpu_total = cpu_user + cpu_kernel + cpu_wait + *cpu_idle;
        return TRUE;

out:
	kstat_close(kc);
	return FALSE;
}

#else

/**
 * gpm_load_get_cpu_values:
 * @cpu_idle: The idle time reported by the CPU
 * @cpu_total: The total time reported by the CPU
 * Return value: Success of reading /proc/stat.
 **/
static gboolean
gpm_load_get_cpu_values (long unsigned *cpu_idle, long unsigned *cpu_total)
{
	long unsigned cpu_user;
	long unsigned cpu_nice;
	long unsigned cpu_system;
	int len;
	char tmp[5];
	char str[80];
	FILE *fd;
	char *suc;
	gboolean ret = FALSE;

	/* open file */
	fd = fopen("/proc/stat", "r");
	if (!fd)
		goto out;

	/* get data */
	suc = fgets (str, 80, fd);
	if (suc == NULL)
		goto out;

	/* parse */
	len = sscanf (str, "%s %lu %lu %lu %lu", tmp,
		      &cpu_user, &cpu_nice, &cpu_system, cpu_idle);
	if (len != 5)
		goto out;

	/* summing up all these times gives you the system uptime in jiffies */
	*cpu_total = cpu_user + cpu_nice + cpu_system + *cpu_idle;
	ret = TRUE;
out:
	if (fd)
		fclose (fd);
	return ret;
}
#endif /* sun & __SVR4 */

/**
 * gpm_load_get_current:
 * @load: This class instance
 * Return value: The CPU idle load
 **/
gdouble
gpm_load_get_current (GpmLoad *load)
{
	double percentage_load;
	long unsigned cpu_idle;
	long unsigned cpu_total;
	long unsigned diff_idle;
	long unsigned diff_total;
	gboolean ret;

	/* work out the differences */
	ret = gpm_load_get_cpu_values (&cpu_idle, &cpu_total);
	if (!ret)
		return 0.0;

	diff_idle = cpu_idle - load->priv->old_idle;
	diff_total = cpu_total - load->priv->old_total;

	/* If we divide the total time by idle time we get the load. */
	if (diff_idle > 0)
		percentage_load = (double) diff_total / (double) diff_idle;
	else
		percentage_load = 100;

	load->priv->old_idle = cpu_idle;
	load->priv->old_total = cpu_total;

	return percentage_load;
}

/**
 * gpm_load_init:
 */
static void
gpm_load_init (GpmLoad *load)
{
	load->priv = GPM_LOAD_GET_PRIVATE (load);

	load->priv->old_idle = 0;
	load->priv->old_total = 0;

	/* we have to populate the values at startup */
	gpm_load_get_cpu_values (&load->priv->old_idle, &load->priv->old_total);
}

/**
 * gpm_load_coldplug:
 *
 * @object: This load instance
 */
static void
gpm_load_finalize (GObject *object)
{
	GpmLoad *load;
	g_return_if_fail (object != NULL);
	g_return_if_fail (GPM_IS_LOAD (object));
	load = GPM_LOAD (object);
	g_return_if_fail (load->priv != NULL);
	G_OBJECT_CLASS (gpm_load_parent_class)->finalize (object);
}

/**
 * gpm_load_new:
 * Return value: new GpmLoad instance.
 **/
GpmLoad *
gpm_load_new (void)
{
	if (gpm_load_object != NULL) {
		g_object_ref (gpm_load_object);
	} else {
		gpm_load_object = g_object_new (GPM_TYPE_LOAD, NULL);
		g_object_add_weak_pointer (gpm_load_object, &gpm_load_object);
	}
	return GPM_LOAD (gpm_load_object);
}