Initial revision
[monky] / netbsd.c
1 /* NetBSD port */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <err.h>
10 #include <limits.h>
11 #include <paths.h>
12
13 #include <kvm.h>
14 #include <nlist.h>
15
16 #include <sys/time.h>
17 #include <sys/param.h>
18 #include <sys/sysctl.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/user.h>
22 #include <sys/socket.h>
23 #include <sys/swap.h>
24 #include <sys/sched.h>
25 #include <sys/envsys.h>
26
27 #include <net/if.h>
28
29 #include <uvm/uvm_extern.h>
30
31 #include <machine/param.h>
32
33 #include "conky.h"
34
35
36 static kvm_t *kd = NULL;
37 int kd_init = 0, nkd_init = 0;
38 u_int32_t sensvalue;
39 char errbuf[_POSIX2_LINE_MAX];
40
41 static int init_kvm(void)
42 {
43         if (kd_init)
44                 return 0;
45
46         kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
47         if (kd == NULL) {
48                 (void) warnx("cannot kvm_openfiles: %s", errbuf);
49                 return -1;
50         }
51         kd_init = 1;
52         return 0;
53 }
54
55 static int swapmode(int *retavail, int *retfree)
56 {
57         int n;
58         struct swapent *sep;
59
60         *retavail = 0;
61         *retfree = 0;
62
63         n = swapctl(SWAP_NSWAP, 0, 0);
64
65         if (n < 1) {
66                 (void) warn("could not get swap information");
67                 return 0;
68         }
69
70         sep = (struct swapent *) malloc(n * (sizeof(*sep)));
71
72         if (sep == NULL) {
73                 (void) warn("memory allocation failed");
74                 return 0;
75         }
76
77         if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
78                 (void) warn("could not get swap stats");
79                 return 0;
80         }
81         for (; n > 0; n--) {
82                 *retavail += (int) dbtob(sep[n - 1].se_nblks);
83                 *retfree +=
84                     (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
85         }
86         *retavail = (int) (*retavail / 1024);
87         *retfree = (int) (*retfree / 1024);
88
89         return 1;
90 }
91
92
93 void prepare_update()
94 {
95 }
96
97 void update_uptime()
98 {
99         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
100         struct timeval boottime;
101         time_t now;
102         int size = sizeof(boottime);
103
104         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
105             && (boottime.tv_sec != 0)) {
106                 (void) time(&now);
107                 info.uptime = now - boottime.tv_sec;
108         } else {
109                 (void) warn("could not get uptime");
110                 info.uptime = 0;
111         }
112 }
113
114
115 void update_meminfo()
116 {
117         int mib[] = { CTL_VM, VM_UVMEXP2 };
118         int total_pages, inactive_pages, free_pages;
119         int swap_avail, swap_free;
120         const int pagesize = getpagesize();
121         struct uvmexp_sysctl uvmexp;
122         size_t size = sizeof(uvmexp);
123
124         info.memmax = info.mem = 0;
125         info.swapmax = info.swap = 0;
126
127
128         if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
129                 warn("could not get memory info");
130                 return;
131         }
132
133         total_pages = uvmexp.npages;
134         free_pages = uvmexp.free;
135         inactive_pages = uvmexp.inactive;
136
137         info.memmax = (total_pages * pagesize) >> 10;
138         info.mem =
139             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
140
141         if (swapmode(&swap_avail, &swap_free) >= 0) {
142                 info.swapmax = swap_avail;
143                 info.swap = (swap_avail - swap_free);
144         }
145 }
146
147 void update_net_stats()
148 {
149         int i;
150         double delta;
151         struct ifnet ifnet;
152         struct ifnet_head ifhead;       /* interfaces are in a tail queue */
153         u_long ifnetaddr;
154         static struct nlist namelist[] = {
155                 {"_ifnet"},
156                 {NULL},
157         };
158         static kvm_t *nkd;
159
160         if (!nkd_init) {
161                 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
162                 if (nkd == NULL) {
163                         (void) warnx("cannot kvm_openfiles: %s", errbuf);
164                         (void)
165                             warnx
166                             ("maybe you need to setgid kmem this program?");
167                         return;
168                 } else if (kvm_nlist(nkd, namelist) != 0) {
169                         (void) warn("cannot kvm_nlist");
170                         return;
171                 } else
172                         nkd_init = 1;
173         }
174
175         if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
176                      sizeof(ifhead)) < 0) {
177                 (void) warn("cannot kvm_read");
178                 return;
179         }
180
181         /* get delta */
182         delta = current_update_time - last_update_time;
183         if (delta <= 0.0001)
184                 return;
185
186         for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
187              ifnet.if_list.tqe_next && i < 16;
188              ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
189
190                 struct net_stat *ns;
191                 long long last_recv, last_trans;
192
193                 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
194                                 sizeof(ifnet));
195                 ns = get_net_stat(ifnet.if_xname);
196                 ns->up = 1;
197                 last_recv = ns->recv;
198                 last_trans = ns->trans;
199
200                 if (ifnet.if_ibytes < ns->last_read_recv)
201                         ns->recv +=
202                             ((long long) 4294967295U -
203                              ns->last_read_recv) + ifnet.if_ibytes;
204                 else
205                         ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
206
207                 ns->last_read_recv = ifnet.if_ibytes;
208
209                 if (ifnet.if_obytes < ns->last_read_trans)
210                         ns->trans +=
211                             ((long long) 4294967295U -
212                              ns->last_read_trans) + ifnet.if_obytes;
213                 else
214                         ns->trans +=
215                             (ifnet.if_obytes - ns->last_read_trans);
216
217                 ns->last_read_trans = ifnet.if_obytes;
218
219                 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
220                 ns->last_read_recv = ifnet.if_ibytes;
221                 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
222                 ns->last_read_trans = ifnet.if_obytes;
223
224                 ns->recv_speed = (ns->recv - last_recv) / delta;
225                 ns->trans_speed = (ns->trans - last_trans) / delta;
226         }
227 }
228
229 void update_total_processes()
230 {
231         /* It's easier to use kvm here than sysctl */
232
233         int n_processes;
234
235         info.procs = 0;
236
237         if (init_kvm() < 0)
238                 return;
239         else
240                 kvm_getproc2(kd, KERN_PROC_ALL, 0,
241                              sizeof(struct kinfo_proc2), &n_processes);
242
243         info.procs = n_processes;
244 }
245
246 void update_running_processes()
247 {
248         struct kinfo_proc2 *p;
249         int n_processes;
250         int i, cnt = 0;
251
252         info.run_procs = 0;
253
254         if (init_kvm() < 0)
255                 return;
256         else {
257                 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
258                                  sizeof(struct kinfo_proc2), &n_processes);
259                 for (i = 0; i < n_processes; i++)
260                         if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
261                             p[i].p_stat == LSONPROC)
262                                 cnt++;
263         }
264
265         info.run_procs = cnt;
266 }
267
268 struct cpu_load_struct {
269         unsigned long load[5];
270 };
271
272 struct cpu_load_struct fresh = {
273         {0, 0, 0, 0, 0}
274 };
275
276 long cpu_used, oldtotal, oldused;
277
278 void update_cpu_usage()
279 {
280         long used, total;
281         static u_int64_t cp_time[CPUSTATES];
282         size_t len = sizeof(cp_time);
283
284         info.cpu_usage = 0;
285
286         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
287                 (void) warn("cannot get kern.cp_time");
288
289
290         fresh.load[0] = cp_time[CP_USER];
291         fresh.load[1] = cp_time[CP_NICE];
292         fresh.load[2] = cp_time[CP_SYS];
293         fresh.load[3] = cp_time[CP_IDLE];
294         fresh.load[4] = cp_time[CP_IDLE];
295
296         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
297         total =
298             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
299
300         if ((total - oldtotal) != 0)
301                 info.cpu_usage =
302                     ((double) (used - oldused)) / (double) (total -
303                                                             oldtotal);
304         else
305                 info.cpu_usage = 0;
306
307         oldused = used;
308         oldtotal = total;
309
310 }
311
312 double get_i2c_info(int *fd, int div, char *devtype)
313 {
314         return -1;
315 }
316
317 void update_load_average()
318 {
319         double v[3];
320         getloadavg(v, 3);
321
322         info.loadavg[0] = (float) v[0];
323         info.loadavg[1] = (float) v[1];
324         info.loadavg[2] = (float) v[2];
325 }
326
327 double get_acpi_temperature(int fd)
328 {
329         return -1;
330 }
331
332 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
333 {
334 }
335
336 int
337 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
338                 char *devtype)
339 {
340         return -1;
341 }
342
343 int open_acpi_temperature(const char *name)
344 {
345         return -1;
346 }
347
348 char *get_acpi_ac_adapter(void)
349 {
350         return "N/A";
351 }
352
353 char *get_acpi_fan()
354 {
355         return "N/A";
356 }