summaryrefslogtreecommitdiffstats
path: root/x11/lxpanel/files/patch-plugins_monitors_monitors.c
blob: 58e1914bc5c34711051b0ae1caf9416f8aa1b636 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
--- plugins/monitors/monitors.c.orig	2016-11-19 18:28:43 UTC
+++ plugins/monitors/monitors.c
@@ -72,6 +72,12 @@
  */
 
 #include <stdlib.h>
+#if defined(__DragonFly__) || (__FreeBSD__)
+#include <unistd.h>
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
 #include <glib/gi18n.h>
 #include <errno.h>
 #include <libfm/fm-gtk.h>
@@ -221,20 +227,46 @@ monitor_set_foreground_color(MonitorsPlu
 /******************************************************************************
  *                                 CPU monitor                                *
  ******************************************************************************/
-typedef unsigned long long CPUTick;/* Value from /proc/stat                   */
 typedef float CPUSample;	   /* Saved CPU utilization value as 0.0..1.0 */
 
+#if defined(__linux__)
+typedef unsigned long long CPUTick;/* Value from /proc/stat                   */
+
 struct cpu_stat {
     CPUTick u, n, s, i;		  /* User, nice, system, idle */
 };
+#elif defined(__DragonFly__) || (__FreeBSD__)
+typedef glong CPUTick;
+
+struct cpu_stat {
+    CPUTick u, n, s, intr, i;
+};
+
+static gint cpu_nb(void)
+{
+    static gint mib[] = { CTL_HW, HW_NCPU };
+    gint res;
+    size_t len = sizeof(gint);
+
+    if (sysctl(mib, 2, &res, &len, NULL, 0) < 0)
+        return 0;
+    else
+        return res;
+}
+#endif
 
 static gboolean
 cpu_update(Monitor * c)
 {
+#if defined(__linux__)
     static struct cpu_stat previous_cpu_stat = { 0, 0, 0, 0 };
+#elif defined(__DragonFly__) || (__FreeBSD__)
+    static struct cpu_stat previous_cpu_stat = { 0, 0, 0, 0, 0 };
+#endif
 
     if ((c->stats != NULL) && (c->pixmap != NULL))
     {
+#if defined(__linux__)
         /* Open statistics file and scan out CPU usage. */
         struct cpu_stat cpu;
         FILE * stat = fopen("/proc/stat", "r");
@@ -269,6 +301,48 @@ cpu_update(Monitor * c)
             /* Redraw with the new sample. */
             redraw_pixmap(c);
         }
+#elif defined(__DragonFly__) || (__FreeBSD__)
+        size_t cp_size = sizeof(glong) * CPUSTATES * cpu_nb();
+        glong *cp_times = malloc(cp_size);
+
+        if (sysctlbyname("kern.cp_times", cp_times, &cp_size, NULL, 0) < 0)
+        {
+            g_free(cp_times);
+            return FALSE;
+        }
+        else
+        {
+            struct cpu_stat cpu;
+            struct cpu_stat cpu_delta;
+
+            cpu.u = cp_times[CP_USER];
+            cpu.n = cp_times[CP_NICE];
+            cpu.s = cp_times[CP_SYS];
+            cpu.intr = cp_times[CP_INTR];
+            cpu.i = cp_times[CP_IDLE];
+
+            g_free(cp_times);
+
+            /* Compute delta from previous statistics. */
+            cpu_delta.u = cpu.u - previous_cpu_stat.u;
+            cpu_delta.n = cpu.n - previous_cpu_stat.n;
+            cpu_delta.s = cpu.s - previous_cpu_stat.s;
+            cpu_delta.intr = cpu.intr - previous_cpu_stat.intr;
+            cpu_delta.i = cpu.i - previous_cpu_stat.i;
+
+            memcpy(&previous_cpu_stat, &cpu, sizeof(struct cpu_stat));
+
+            float cpu_used = cpu_delta.u + cpu_delta.n;
+            float cpu_total = cpu_used + cpu_delta.s + cpu_delta.intr + cpu_delta.i;
+            c->stats[c->ring_cursor] = cpu_used / cpu_total;
+            c->ring_cursor += 1;
+            if (c->ring_cursor >= c->pixmap_width)
+                c->ring_cursor = 0;
+
+            /* Redraw with the new sample. */
+            redraw_pixmap(c);
+        }
+#endif
     }
     return TRUE;
 }
@@ -294,11 +368,38 @@ cpu_tooltip_update (Monitor *m)
 /******************************************************************************
  *                               RAM Monitor                                  *
  ******************************************************************************/
+#if defined(__DragonFly__) || (__FreeBSD__)
+static glong
+mem_get_by_bytes(const gchar *name)
+{
+    glong buf;
+    gsize len = sizeof(glong);
+
+    if (sysctlbyname(name, &buf, &len, NULL, 0) < 0)
+        return 0;
+    else
+        return buf;
+}
+
+static glong
+mem_get_by_pages(const gchar *name)
+{
+    glong res = 0;
+
+    res = mem_get_by_bytes(name);
+    if (res > 0)
+        res = res * getpagesize();
+
+    return res;
+}
+#endif
+
 static gboolean
 mem_update(Monitor * m)
 {
     ENTER;
 
+#if defined(__linux__)
     FILE *meminfo;
     char buf[80];
     long int mem_total = 0;
@@ -343,6 +444,17 @@ mem_update(Monitor * m)
                   "readmask %x", readmask);
         RET(FALSE);
     }
+#elif defined(__DragonFly__) || (__FreeBSD__)
+    if (!m->stats || !m->pixmap)
+        RET(TRUE);
+
+    glong mem_total, mem_free, mem_buffers, mem_cached;
+
+    mem_total = mem_get_by_bytes("hw.physmem");
+    mem_free = mem_get_by_pages("vm.stats.vm.v_free_count");
+    mem_buffers = mem_get_by_bytes("vfs.bufspace");
+    mem_cached = mem_get_by_pages("vm.stats.vm.v_inactive_count");
+#endif
 
     m->total = mem_total;
 
OpenPOWER on IntegriCloud