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
34 #include <sys/types.h>
36 #include <linux/limits.h>
38 static int is_linux26
;
45 while ((c
= fgetc(fp
)) != '\n')
52 unsigned long long softirq
;
55 if (!(fp
= fopen("/proc/stat", "r"))) {
60 is_linux26
= fscanf(fp
, "%*s %*u %*u %*u %*u %*u %*u %llu",
68 /* returns current cpu usage in percent */
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;
78 if (!(fp
= fopen("/proc/stat", "r"))) {
84 if (opts
->cpu_number
== -1) {
86 fscanf(fp
, "%*s %llu %llu %llu %llu %llu %llu %llu",
87 &user
, &nice
, &system
, &idle
, &iowait
,
90 fscanf(fp
, "%*s %llu %llu %llu %llu",
91 &user
, &nice
, &system
, &idle
);
96 for (i
= 0; i
<= opts
->cpu_number
; i
++)
100 fscanf(fp
, "%s %llu %llu %llu %llu %llu %llu %llu",
101 cpu_name
, &user
, &nice
, &system
, &idle
, &iowait
,
104 fscanf(fp
, "%s %llu %llu %llu %llu",
105 cpu_name
, &user
, &nice
, &system
,
108 if (cpu_name
[3] != '0' + opts
->cpu_number
) {
109 fprintf(stderr
, "Could not find cpu%d.\n",
117 used
= user
+ system
;
118 if (!opts
->ignore_nice
)
120 total
= user
+ nice
+ system
+ idle
;
122 total
+= iowait
+ irq
+ softirq
;
124 result
= (100 * (double)(used
- pre_used
)) / (double)(total
- pre_total
);