2
* Copyright (C) 2002,2003,2004 Mattia Dongili<dongili@supereva.it>
3
* George Staikos <staikos@0wned.org>
6
* - fixed /proc/stat reads in 2.6 kernels by Holger Ruckdeschel
7
* - detect kernel version (usefull??) by Mattia Dongili
10
* - added support for cpu monitoring, base code by Dietz Proepper and minor
11
* fixes by Mattia Dongili
13
* This program is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
void libsys_preinit(void (*in_log) (const int prio, const char *fmt, ...), general *config);
31
static unsigned int old_weighted_activity = 0,
34
static int get_kversion(void) {
39
fp = fopen ("/proc/version", "r");
41
cp_log(LOG_ERR, "get_kversion(): %s: %s\n", "/proc/version", strerror(errno));
45
f = fscanf (fp, "Linux version %s", kver);
50
cp_log(LOG_INFO, "get_kversion(): read kernel version %s.\n", kver);
52
if (strstr(kver, "2.6") == kver) {
53
cp_log(LOG_INFO, "get_kversion(): kernel version is 2.6.\n");
55
} else if (strstr(kver, "2.4") == kver) {
56
cp_log(LOG_INFO, "get_kversion(): kernel version is 2.4.\n");
59
cp_log(LOG_WARNING, "Unknown kernel version let's try to continue assuming a 2.6 kernel.\n");
65
void libsys_preinit ( void (*in_log) (const int prio, const char *fmt, ...), general *config ) {
67
configuration = config;
68
config->kver = get_kversion();
69
cp_log(LOG_INFO, "libsys_preinit(): pre-initialization done.\n");
73
int get_cpu(sys_info *si) {
78
unsigned int c_user, c_nice, c_sys, c_time, delta_time, delta_activity, weighted_activity, perc;
79
unsigned long int c_idle;
80
unsigned long int c_iowait=0, c_irq=0, c_softirq=0; /* for linux 2.6 only */
82
/* read raw jiffies... */
83
fp = fopen ("/proc/stat", "r");
85
cp_log(LOG_ERR, "get_cpu(): %s: %s\n", "/proc/stat", strerror(errno));
90
"cpu %u %u %u %lu %lu %lu %lu",
91
&c_user, &c_nice, &c_sys, &c_idle, &c_iowait, &c_irq, &c_softirq);
93
} while ((f!=4 && configuration->kver==KVER_24) || (f!=7 && configuration->kver==KVER_26));
97
"get_cpu(): CPU c_user=%d c_nice=%d c_sys=%d c_idle=%d c_iowait=%d c_irq=%d c_softirq=%d.\n",
98
c_user, c_nice, c_sys, c_idle, c_iowait, c_irq, c_softirq);
99
/* calculate total jiffies, weight them and save */
100
c_sys += c_irq + c_softirq;
102
c_time = c_user + c_nice + c_sys + c_idle;
103
delta_time = c_time - old_time;
107
si->cur_sys_activity = c_user + c_sys;
108
si->cur_nice_activity = c_nice;
111
weighted_activity = c_user + c_nice / 3 + c_sys;
112
delta_activity = weighted_activity - old_weighted_activity;
113
old_weighted_activity = weighted_activity;
116
"get_cpu(): CPU delta_activity=%d delta_time=%d weighted_activity=%d.\n",
117
delta_activity, delta_time, weighted_activity);
119
if ( delta_activity > delta_time || delta_time <= 0) {
122
perc = delta_activity * 100 / delta_time;
124
cp_log(LOG_INFO, "get_cpu(): CPU usage = %d.\n", perc);