File: cpu.c

package info (click to toggle)
psensor 0.6.2.17-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,388 kB
  • sloc: ansic: 4,994; sh: 4,385; perl: 2,498; makefile: 245; sed: 16
file content (107 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download
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
/*
 * Copyright (C) 2010-2012 jeanfi@gmail.com
 *
 * 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 <locale.h>
#include <libintl.h>
#define _(str) gettext(str)

#include <string.h>

#include <glibtop/cpu.h>

#include "cpu.h"

static glibtop_cpu *cpu;
static float last_used;
static float last_total;

struct psensor *create_cpu_usage_sensor(int measures_len)
{
	char *label;
	int type;
	char *id;
	struct psensor *psensor;

	id = strdup("cpu usage");
	label = strdup("cpu usage");
	type = SENSOR_TYPE_CPU_USAGE;

	psensor = psensor_create(id, label, type, measures_len);

	return psensor;
}

struct psensor * *
cpu_psensor_list_add(struct psensor **sensors, int measures_len)
{
	struct psensor *s;

	s = create_cpu_usage_sensor(measures_len);

	return psensor_list_add(sensors, s);
}

static double get_usage()
{
	unsigned long int used = 0;
	unsigned long int dt;
	double cpu_rate = UNKNOWN_DBL_VALUE;

	if (!cpu)
		cpu = malloc(sizeof(glibtop_cpu));

	glibtop_get_cpu(cpu);

	used = cpu->user + cpu->nice + cpu->sys;

	dt = cpu->total - last_total;

	if (dt)
		cpu_rate = 100 * (used - last_used) / dt;

	last_used = used;
	last_total = cpu->total;

	return cpu_rate;
}

void cpu_usage_sensor_update(struct psensor *s)
{
	psensor_set_current_value(s, get_usage());
}

void cpu_psensor_list_update(struct psensor **sensors)
{
	struct psensor **ss, *s;

	ss = sensors;
	while (*ss) {
		s = *ss;

		if (s->type == SENSOR_TYPE_CPU_USAGE)
			cpu_usage_sensor_update(s);

		ss++;
	}
}

void cpu_cleanup()
{
	if (cpu)
		free(cpu);
}