wmclockmon: update change-log
[dockapps.git] / wmcpuload / src / cpu_linux.c
blob64d378ae6ef8b98089b7efd1b7d0c285c8408ba3
1 /*
2 * wmcpuload
3 * GNU/Linux specific part
5 * Copyright (C) 2001, 2002, 2005 Seiichi SATO <ssato@sh.rim.or.jp>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "cpu.h"
33 #include <dirent.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <linux/limits.h>
38 static int is_linux26;
40 static void
41 skip_line(FILE *fp)
43 int c;
45 while ((c = fgetc(fp)) != '\n')
46 if (c == EOF) break;
49 void
50 cpu_init(void)
52 unsigned long long softirq;
53 FILE *fp;
55 if (!(fp = fopen("/proc/stat", "r"))) {
56 perror("fopen");
57 exit(1);
60 is_linux26 = fscanf(fp, "%*s %*u %*u %*u %*u %*u %*u %llu",
61 &softirq);
63 fclose(fp);
65 return;
68 /* returns current cpu usage in percent */
69 int
70 cpu_get_usage(cpu_options * opts)
72 unsigned long long user, nice, system, idle, iowait, irq, softirq;
73 unsigned long long used, total;
74 static unsigned long long pre_used = 0, pre_total = 0;
75 int result;
77 FILE *fp;
78 if (!(fp = fopen("/proc/stat", "r"))) {
79 perror("fopen");
80 exit(1);
84 if (opts->cpu_number == -1) {
85 if (is_linux26)
86 fscanf(fp, "%*s %llu %llu %llu %llu %llu %llu %llu",
87 &user, &nice, &system, &idle, &iowait,
88 &irq, &softirq);
89 else
90 fscanf(fp, "%*s %llu %llu %llu %llu",
91 &user, &nice, &system, &idle);
92 } else {
93 char cpu_name[20];
94 int i;
96 for (i = 0; i <= opts->cpu_number; i++)
97 skip_line(fp);
99 if (is_linux26)
100 fscanf(fp, "%s %llu %llu %llu %llu %llu %llu %llu",
101 cpu_name, &user, &nice, &system, &idle, &iowait,
102 &irq, &softirq);
103 else
104 fscanf(fp, "%s %llu %llu %llu %llu",
105 cpu_name, &user, &nice, &system,
106 &idle);
108 if (cpu_name[3] != '0' + opts->cpu_number) {
109 fprintf(stderr, "Could not find cpu%d.\n",
110 opts->cpu_number);
111 exit(1);
115 fclose(fp);
117 used = user + system;
118 if (!opts->ignore_nice)
119 used += nice;
120 total = user + nice + system + idle;
121 if (is_linux26)
122 total += iowait + irq + softirq;
124 result = (100 * (double)(used - pre_used)) / (double)(total - pre_total);
126 pre_total = total;
127 pre_used = used;
129 return result;