Added
Link Here
|
1 |
--- plugins/cpu.c~ Tue May 16 19:23:18 2006 |
2 |
+++ plugins/cpu.c Tue May 16 19:00:07 2006 |
3 |
@@ -18,14 +18,24 @@ |
4 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
5 |
* |
6 |
*/ |
7 |
-/*A little bug fixed by Mykola <mykola@2ka.mipt.ru>:) */ |
8 |
+/* |
9 |
+ * A little bug fixed by Mykola <mykola@2ka.mipt.ru> :) |
10 |
+ * FreeBSD support added by Andreas Wiese <aw@instandbesetzt.net> |
11 |
+ */ |
12 |
|
13 |
|
14 |
#include <string.h> |
15 |
#include <sys/time.h> |
16 |
#include <time.h> |
17 |
-#include <sys/sysinfo.h> |
18 |
+#ifdef __FreeBSD__ |
19 |
+# include <sys/types.h> |
20 |
+# include <sys/resource.h> |
21 |
+# include <sys/sysctl.h> |
22 |
+#else |
23 |
+# include <sys/sysinfo.h> |
24 |
+#endif |
25 |
#include <stdlib.h> |
26 |
+#include <stdio.h> |
27 |
|
28 |
#include "plugin.h" |
29 |
#include "panel.h" |
30 |
@@ -60,6 +70,37 @@ |
31 |
struct cpu_stat cpu_anterior; |
32 |
} cpu_t; |
33 |
|
34 |
+#ifdef __FreeBSD__ |
35 |
+static void |
36 |
+get_procstat(unsigned long *u, unsigned long *n, unsigned long *s, |
37 |
+ unsigned long *i) |
38 |
+{ |
39 |
+ static int mib[2] = { -1, -1 }, init = 0, j, realhz; |
40 |
+ long ct[CPUSTATES]; |
41 |
+ |
42 |
+ |
43 |
+ if(init == 0) { |
44 |
+ struct clockinfo ci; |
45 |
+ j = sizeof(ci); |
46 |
+ sysctlbyname("kern.clockrate", &ci, &j, NULL, 0); |
47 |
+ realhz = ci.stathz ? ci.stathz : ci.hz; |
48 |
+ |
49 |
+ j = 2; |
50 |
+ sysctlnametomib("kern.cp_time", mib, &j); |
51 |
+ |
52 |
+ init = 1; |
53 |
+ j = sizeof(ct); |
54 |
+ } |
55 |
+ |
56 |
+ sysctl(mib, 2, ct, &j, NULL, 0); |
57 |
+ *u = ct[CP_USER] / realhz; |
58 |
+ *n = ct[CP_NICE] / realhz; |
59 |
+ *s = ct[CP_SYS] / realhz; |
60 |
+ *i = ct[CP_IDLE] / realhz; |
61 |
+ |
62 |
+ return; |
63 |
+} |
64 |
+#endif |
65 |
|
66 |
static int |
67 |
cpu_update(cpu_t *c) |
68 |
@@ -67,18 +108,24 @@ |
69 |
int cpu_u=0, cpu_s=0, cpu_n=0, cpu_i=100; |
70 |
unsigned int i; |
71 |
struct cpu_stat cpu, cpu_r; |
72 |
+#ifndef __FreeBSD__ |
73 |
FILE *stat; |
74 |
+#endif |
75 |
float total; |
76 |
|
77 |
ENTER; |
78 |
if(!c->pixmap) |
79 |
RET(TRUE); |
80 |
|
81 |
+#ifdef __FreeBSD__ |
82 |
+ get_procstat(&cpu.u, &cpu.n, &cpu.s, &cpu.i); |
83 |
+#else |
84 |
stat = fopen("/proc/stat", "r"); |
85 |
if(!stat) |
86 |
RET(TRUE); |
87 |
fscanf(stat, "cpu %lu %lu %lu %lu", &cpu.u, &cpu.n, &cpu.s, &cpu.i); |
88 |
fclose(stat); |
89 |
+#endif |
90 |
|
91 |
cpu_r.u = cpu.u - c->cpu_anterior.u; |
92 |
cpu_r.n = cpu.n - c->cpu_anterior.n; |