doc cleanup
[monky] / src / freebsd.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  * $Id$ */
26
27 #include <sys/dkstat.h>
28 #include <sys/param.h>
29 #include <sys/resource.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/vmmeter.h>
35 #include <sys/user.h>
36 #include <sys/ioctl.h>
37
38 #include <net/if.h>
39 #include <net/if_mib.h>
40 #include <net/if_media.h>
41 #include <net/if_var.h>
42 #include <netinet/in.h>
43
44 #include <devstat.h>
45 #include <fcntl.h>
46 #include <ifaddrs.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include <dev/wi/if_wavelan_ieee.h>
54
55 #include "conky.h"
56
57 #define GETSYSCTL(name, var)    getsysctl(name, &(var), sizeof(var))
58 #define KELVTOC(x)                              ((x - 2732) / 10.0)
59 #define MAXSHOWDEVS                             16
60
61 #if 0
62 #define FREEBSD_DEBUG
63 #endif
64
65 inline void proc_find_top(struct process **cpu, struct process **mem);
66
67 u_int64_t diskio_prev = 0;
68 static short cpu_setup = 0;
69 static short diskio_setup = 0;
70
71 static int getsysctl(char *name, void *ptr, size_t len)
72 {
73         size_t nlen = len;
74
75         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
76                 return -1;
77         }
78
79         if (nlen != len) {
80                 return -1;
81         }
82
83         return 0;
84 }
85
86 struct ifmibdata *data = NULL;
87 size_t len = 0;
88
89 static int swapmode(unsigned long *retavail, unsigned long *retfree)
90 {
91         int n;
92         unsigned long pagesize = getpagesize();
93         struct kvm_swap swapary[1];
94
95         *retavail = 0;
96         *retfree = 0;
97
98 #define CONVERT(v)      ((quad_t)(v) * (pagesize / 1024))
99
100         n = kvm_getswapinfo(kd, swapary, 1, 0);
101         if (n < 0 || swapary[0].ksw_total == 0) {
102                 return 0;
103         }
104
105         *retavail = CONVERT(swapary[0].ksw_total);
106         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
107
108         n = (int) ((double) swapary[0].ksw_used * 100.0 /
109                 (double) swapary[0].ksw_total);
110
111         return n;
112 }
113
114 void prepare_update()
115 {
116 }
117
118 void update_uptime()
119 {
120         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
121         struct timeval boottime;
122         time_t now;
123         size_t size = sizeof(boottime);
124
125         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
126                         && (boottime.tv_sec != 0)) {
127                 time(&now);
128                 info.uptime = now - boottime.tv_sec;
129         } else {
130                 fprintf(stderr, "Could not get uptime\n");
131                 info.uptime = 0;
132         }
133 }
134
135 int check_mount(char *s)
136 {
137         struct statfs *mntbuf;
138         int i, mntsize;
139
140         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
141         for (i = mntsize - 1; i >= 0; i--) {
142                 if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
143                         return 1;
144                 }
145         }
146
147         return 0;
148 }
149
150 void update_meminfo()
151 {
152         unsigned long total_pages, inactive_pages, free_pages;
153         unsigned long swap_avail, swap_free;
154
155         int pagesize = getpagesize();
156
157         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
158                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
159         }
160
161         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
162                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
163         }
164
165         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
166                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
167         }
168
169         info.memmax = total_pages * (pagesize >> 10);
170         info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
171
172         if ((swapmode(&swap_avail, &swap_free)) >= 0) {
173                 info.swapmax = swap_avail;
174                 info.swap = (swap_avail - swap_free);
175         } else {
176                 info.swapmax = 0;
177                 info.swap = 0;
178         }
179 }
180
181 void update_net_stats()
182 {
183         struct net_stat *ns;
184         double delta;
185         long long r, t, last_recv, last_trans;
186         struct ifaddrs *ifap, *ifa;
187         struct if_data *ifd;
188
189         /* get delta */
190         delta = current_update_time - last_update_time;
191         if (delta <= 0.0001) {
192                 return;
193         }
194
195         if (getifaddrs(&ifap) < 0) {
196                 return;
197         }
198
199         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
200                 ns = get_net_stat((const char *) ifa->ifa_name);
201
202                 if (ifa->ifa_flags & IFF_UP) {
203                         struct ifaddrs *iftmp;
204
205                         ns->up = 1;
206                         last_recv = ns->recv;
207                         last_trans = ns->trans;
208
209                         if (ifa->ifa_addr->sa_family != AF_LINK) {
210                                 continue;
211                         }
212
213                         for (iftmp = ifa->ifa_next;
214                                         iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
215                                         iftmp = iftmp->ifa_next) {
216                                 if (iftmp->ifa_addr->sa_family == AF_INET) {
217                                         memcpy(&(ns->addr), iftmp->ifa_addr,
218                                                 iftmp->ifa_addr->sa_len);
219                                 }
220                         }
221
222                         ifd = (struct if_data *) ifa->ifa_data;
223                         r = ifd->ifi_ibytes;
224                         t = ifd->ifi_obytes;
225
226                         if (r < ns->last_read_recv) {
227                                 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
228                         } else {
229                                 ns->recv += (r - ns->last_read_recv);
230                         }
231
232                         ns->last_read_recv = r;
233
234                         if (t < ns->last_read_trans) {
235                                 ns->trans += ((long long) 4294967295U -
236                                         ns->last_read_trans) + t;
237                         } else {
238                                 ns->trans += (t - ns->last_read_trans);
239                         }
240
241                         ns->last_read_trans = t;
242
243                         /* calculate speeds */
244                         ns->recv_speed = (ns->recv - last_recv) / delta;
245                         ns->trans_speed = (ns->trans - last_trans) / delta;
246                 } else {
247                         ns->up = 0;
248                 }
249         }
250
251         freeifaddrs(ifap);
252 }
253
254 void update_total_processes()
255 {
256         int n_processes;
257
258         kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
259
260         info.procs = n_processes;
261 }
262
263 void update_running_processes()
264 {
265         struct kinfo_proc *p;
266         int n_processes;
267         int i, cnt = 0;
268
269         p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
270         for (i = 0; i < n_processes; i++) {
271 #if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
272                 if (p[i].kp_proc.p_stat == SRUN) {
273 #else
274                 if (p[i].ki_stat == SRUN) {
275 #endif
276                         cnt++;
277                 }
278         }
279
280         info.run_procs = cnt;
281 }
282
283 struct cpu_load_struct {
284         unsigned long load[5];
285 };
286
287 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
288 long cpu_used, oldtotal, oldused;
289
290 void get_cpu_count()
291 {
292         /* int cpu_count = 0; */
293
294         /* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
295          * It's possible to get a CPU count, but as we fulfill only
296          * info.cpu_usage[0], it's better to report there's only one CPU.
297          * It should fix some bugs (e.g. cpugraph) */
298 #if 0
299         if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
300                 info.cpu_count = cpu_count;
301         }
302 #endif
303         info.cpu_count = 1;
304
305         info.cpu_usage = malloc(info.cpu_count * sizeof(float));
306         if (info.cpu_usage == NULL) {
307                 CRIT_ERR("malloc");
308         }
309 }
310
311 /* XXX: SMP support */
312 void update_cpu_usage()
313 {
314         long used, total;
315         long cp_time[CPUSTATES];
316         size_t len = sizeof(cp_time);
317
318         /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
319         if ((cpu_setup == 0) || (!info.cpu_usage)) {
320                 get_cpu_count();
321                 cpu_setup = 1;
322         }
323
324         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
325                 fprintf(stderr, "Cannot get kern.cp_time");
326         }
327
328         fresh.load[0] = cp_time[CP_USER];
329         fresh.load[1] = cp_time[CP_NICE];
330         fresh.load[2] = cp_time[CP_SYS];
331         fresh.load[3] = cp_time[CP_IDLE];
332         fresh.load[4] = cp_time[CP_IDLE];
333
334         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
335         total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
336
337         if ((total - oldtotal) != 0) {
338                 info.cpu_usage[0] = ((double) (used - oldused)) /
339                         (double) (total - oldtotal);
340         } else {
341                 info.cpu_usage[0] = 0;
342         }
343
344         oldused = used;
345         oldtotal = total;
346 }
347
348 double get_sysfs_info(int *fd, int arg, char *devtype, char *type)
349 {
350         return 0.0;
351 }
352
353 void update_load_average()
354 {
355         double v[3];
356
357         getloadavg(v, 3);
358
359         info.loadavg[0] = (double) v[0];
360         info.loadavg[1] = (double) v[1];
361         info.loadavg[2] = (double) v[2];
362 }
363
364 double get_acpi_temperature(int fd)
365 {
366         int temp;
367
368         if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
369                 fprintf(stderr,
370                         "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
371                 return 0.0;
372         }
373
374         return KELVTOC(temp);
375 }
376
377 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
378 {
379         int battime, batcapacity, batstate, ac;
380         char battery_status[64];
381         char battery_time[64];
382
383         if (GETSYSCTL("hw.acpi.battery.time", battime)) {
384                 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
385         }
386         if (GETSYSCTL("hw.acpi.battery.life", batcapacity)) {
387                 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
388         }
389
390         if (GETSYSCTL("hw.acpi.battery.state", batstate)) {
391                 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
392         }
393
394         if (GETSYSCTL("hw.acpi.acline", ac)) {
395                 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
396         }
397
398         if (batstate == 1) {
399                 if (battime != -1) {
400                         snprintf(battery_status, sizeof(battery_status) - 1,
401                                 "remaining %d%%", batcapacity);
402                         snprintf(battery_time, sizeof(battery_time) - 1, "%d:%2.2d",
403                                 battime / 60, battime % 60);
404                         /* snprintf(buf, n, "remaining %d%% (%d:%2.2d)", batcapacity,
405                                 battime / 60, battime % 60); */
406                 } else {
407                         /* no time estimate available yet */
408                         snprintf(battery_status, sizeof(battery_status) - 1,
409                                 "remaining %d%%", batcapacity);
410                 }
411                 /* snprintf(buf, n, "remaining %d%%", batcapacity); */
412                 if (ac == 1) {
413                         fprintf(stderr, "Discharging while on AC!\n");
414                 }
415         } else {
416                 snprintf(battery_status, sizeof(battery_status) - 1,
417                         batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
418                 /* snprintf(buf, n,
419                         batstate == 2 ? "charging (%d%%)" : "charged (%d%%)",
420                         batcapacity); */
421                 if (batstate != 2 && batstate != 0) {
422                         fprintf(stderr, "Unknown battery state %d!\n", batstate);
423                 }
424                 if (ac == 0) {
425                         fprintf(stderr, "Charging while not on AC!\n");
426                 }
427         }
428
429         switch (item) {
430                 case BATTERY_STATUS:
431                         snprintf(buf, n, "%s", battery_status);
432                         break;
433                 case BATTERY_TIME:
434                         snprintf(buf, n, "%s", battery_time);
435                         break;
436                 default:
437                         break;
438         }
439 }
440
441 int get_battery_perct(const char *bat)
442 {
443         /* not implemented */
444         return 0;
445 }
446
447 int get_battery_perct_bar(const char *bar)
448 {
449         /* not implemented */
450         return 0;
451 }
452
453 int open_sysfs_sensor(const char *dir, const char *dev, const char *type,
454                 int n, int *div, char *devtype)
455 {
456         return 0;
457 }
458
459 int open_acpi_temperature(const char *name)
460 {
461         return 0;
462 }
463
464 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
465 {
466         int state;
467
468         if (!p_client_buffer || client_buffer_size <= 0) {
469                 return;
470         }
471
472         if (GETSYSCTL("hw.acpi.acline", state)) {
473                 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
474                 return;
475         }
476
477         if (state) {
478                 strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
479         } else {
480                 strncpy(p_client_buffer, "Running on battery", client_buffer_size);
481         }
482 }
483
484 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
485 {
486         /* not implemented */
487         if (p_client_buffer && client_buffer_size > 0) {
488                 memset(p_client_buffer, 0, client_buffer_size);
489         }
490 }
491
492 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
493 {
494         /* not implemented */
495         if (p_client_buffer && client_buffer_size > 0) {
496                 memset(p_client_buffer, 0, client_buffer_size);
497         }
498 }
499
500 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
501 {
502         /* not implemented */
503         if (p_client_buffer && client_buffer_size > 0) {
504                 memset(p_client_buffer, 0, client_buffer_size);
505         }
506 }
507
508 /* rdtsc() and get_freq_dynamic() copied from linux.c */
509
510 #if  defined(__i386) || defined(__x86_64)
511 __inline__ unsigned long long int rdtsc()
512 {
513         unsigned long long int x;
514
515         __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
516         return x;
517 }
518 #endif
519
520 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
521 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
522                 char *p_format, int divisor)
523 {
524 #if  defined(__i386) || defined(__x86_64)
525         struct timezone tz;
526         struct timeval tvstart, tvstop;
527         unsigned long long cycles[2];   /* gotta be 64 bit */
528         unsigned int microseconds;      /* total time taken */
529
530         memset(&tz, 0, sizeof(tz));
531
532         /* get this function in cached memory */
533         gettimeofday(&tvstart, &tz);
534         cycles[0] = rdtsc();
535         gettimeofday(&tvstart, &tz);
536
537         /* we don't trust that this is any specific length of time */
538         usleep(100);
539         cycles[1] = rdtsc();
540         gettimeofday(&tvstop, &tz);
541         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
542                 (tvstop.tv_usec - tvstart.tv_usec);
543
544         snprintf(p_client_buffer, client_buffer_size, p_format,
545                 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
546 #else
547         get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
548 #endif
549 }
550
551 /* void */
552 char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
553                 int divisor, unsigned int cpu)
554 {
555         int freq;
556         char *freq_sysctl;
557
558         freq_sysctl = (char *) calloc(16, sizeof(char));
559         if (freq_sysctl == NULL) {
560                 exit(-1);
561         }
562
563         snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
564
565         if (!p_client_buffer || client_buffer_size <= 0 || !p_format
566                         || divisor <= 0) {
567                 return 0;
568         }
569
570         if (GETSYSCTL(freq_sysctl, freq) == 0) {
571                 snprintf(p_client_buffer, client_buffer_size, p_format,
572                         (float) freq / divisor);
573         } else {
574                 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
575         }
576
577         free(freq_sysctl);
578         return 1;
579 }
580
581 void update_top()
582 {
583         proc_find_top(info.cpu, info.memu);
584 }
585
586 #if 0
587 void update_wifi_stats()
588 {
589         struct ifreq ifr;               /* interface stats */
590         struct wi_req wireq;
591         struct net_stat *ns;
592         struct ifaddrs *ifap, *ifa;
593         struct ifmediareq ifmr;
594         int s;
595
596         /* Get iface table */
597         if (getifaddrs(&ifap) < 0) {
598                 return;
599         }
600
601         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
602                 ns = get_net_stat((const char *) ifa->ifa_name);
603
604                 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
605
606                 /* Get media type */
607                 bzero(&ifmr, sizeof(ifmr));
608                 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
609                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
610                         goto cleanup;
611                 }
612
613                 /* We can monitor only wireless interfaces
614                  * which are not in hostap mode */
615                 if ((ifmr.ifm_active & IFM_IEEE80211)
616                                 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
617                         /* Get wi status */
618                         bzero(&ifr, sizeof(ifr));
619                         strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
620                         wireq.wi_type = WI_RID_COMMS_QUALITY;
621                         wireq.wi_len = WI_MAX_DATALEN;
622                         ifr.ifr_data = (void *) &wireq;
623
624                         if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
625                                 perror("ioctl (getting wi status)");
626                                 exit(1);
627                         }
628
629                         /* wi_val[0] = quality
630                          * wi_val[1] = signal
631                          * wi_val[2] = noise */
632                         ns->linkstatus = (int) wireq.wi_val[1];
633                 }
634 cleanup:
635                 close(s);
636         }
637 }
638 #endif
639
640 void update_diskio()
641 {
642         int devs_count, num_selected, num_selections, i;
643         struct device_selection *dev_select = NULL;
644         long select_generation;
645         int dn;
646         static struct statinfo statinfo_cur;
647         u_int64_t diskio_current = 0;
648         u_int64_t writes = 0;
649
650         bzero(&statinfo_cur, sizeof(statinfo_cur));
651         statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
652         bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
653
654         if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
655                 return;
656         }
657
658         devs_count = statinfo_cur.dinfo->numdevs;
659         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
660                         &select_generation, statinfo_cur.dinfo->generation,
661                         statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
662                         DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
663                 for (dn = 0; dn < devs_count; ++dn) {
664                         int di;
665                         struct devstat *dev;
666
667                         di = dev_select[dn].position;
668                         dev = &statinfo_cur.dinfo->devices[di];
669
670                         diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
671
672                         for (i = 0; i < MAX_DISKIO_STATS; i++) {
673                                 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
674                                                 diskio_stats[i].dev) == 0) {
675                                         diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
676                                                 dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
677                                         diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
678                                                 diskio_stats[i].last_read) / 1024;
679                                         diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
680                                                 diskio_stats[i].last_write) / 1024;
681                                         if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
682                                                         < diskio_stats[i].last) {
683                                                 diskio_stats[i].current = 0;
684                                         }
685                                         if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
686                                                 diskio_stats[i].current_read = 0;
687                                                 diskio_stats[i].current = diskio_stats[i].current_write;
688                                         }
689                                         if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
690                                                 diskio_stats[i].current_write = 0;
691                                                 diskio_stats[i].current = diskio_stats[i].current_read;
692                                         }
693                                         diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
694                                                 dev->bytes[DEVSTAT_WRITE];
695                                         diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
696                                         diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
697                                 }
698                         }
699                 }
700
701                 free(dev_select);
702         }
703
704         /* Since we return (diskio_total_current - diskio_total_old),
705          * the first frame will be way too high
706          * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
707          *  That's why it is better to return 0 first time; */
708         if (diskio_setup == 0) {
709                 diskio_setup = 1;
710                 diskio_value = 0;
711         } else {
712                 diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
713         }
714         diskio_prev = diskio_current;
715
716         free(statinfo_cur.dinfo);
717 }
718
719 /* While topless is obviously better, top is also not bad. */
720
721 int comparecpu(const void *a, const void *b)
722 {
723         if (((struct process *)a)->amount > ((struct process *)b)->amount) {
724                 return -1;
725         } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
726                 return 1;
727         } else {
728                 return 0;
729         }
730 }
731
732 int comparemem(const void *a, const void *b)
733 {
734         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
735                 return -1;
736         } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
737                 return 1;
738         } else {
739                 return 0;
740         }
741 }
742
743 inline void proc_find_top(struct process **cpu, struct process **mem)
744 {
745         struct kinfo_proc *p;
746         int n_processes;
747         int i, j = 0;
748         struct process *processes;
749
750         int total_pages;
751
752         /* we get total pages count again to be sure it is up to date */
753         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
754                 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
755         }
756
757         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
758         processes = malloc(n_processes * sizeof(struct process));
759
760         for (i = 0; i < n_processes; i++) {
761                 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
762                         processes[j].pid = p[i].ki_pid;
763                         processes[j].name = strdup(p[i].ki_comm);
764                         processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
765                         processes[j].totalmem = (float) (p[i].ki_rssize /
766                                 (float) total_pages) * 100.0;
767                         processes[j].vsize = p[i].ki_size;
768                         processes[j].rss = (p[i].ki_rssize * getpagesize());
769                         j++;
770                 }
771         }
772
773         qsort(processes, j - 1, sizeof(struct process), comparemem);
774         for (i = 0; i < 10 && i < n_processes; i++) {
775                 struct process *tmp, *ttmp;
776
777                 tmp = malloc(sizeof(struct process));
778                 tmp->pid = processes[i].pid;
779                 tmp->amount = processes[i].amount;
780                 tmp->totalmem = processes[i].totalmem;
781                 tmp->name = strdup(processes[i].name);
782                 tmp->rss = processes[i].rss;
783                 tmp->vsize = processes[i].vsize;
784
785                 ttmp = mem[i];
786                 mem[i] = tmp;
787                 if (ttmp != NULL) {
788                         free(ttmp->name);
789                         free(ttmp);
790                 }
791         }
792
793         qsort(processes, j - 1, sizeof(struct process), comparecpu);
794         for (i = 0; i < 10 && i < n_processes; i++) {
795                 struct process *tmp, *ttmp;
796
797                 tmp = malloc(sizeof(struct process));
798                 tmp->pid = processes[i].pid;
799                 tmp->amount = processes[i].amount;
800                 tmp->totalmem = processes[i].totalmem;
801                 tmp->name = strdup(processes[i].name);
802                 tmp->rss = processes[i].rss;
803                 tmp->vsize = processes[i].vsize;
804
805                 ttmp = cpu[i];
806                 cpu[i] = tmp;
807                 if (ttmp != NULL) {
808                         free(ttmp->name);
809                         free(ttmp);
810                 }
811         }
812
813 #if defined(FREEBSD_DEBUG)
814         printf("=====\nmem\n");
815         for (i = 0; i < 10; i++) {
816                 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
817                                 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
818         }
819 #endif
820
821         for (i = 0; i < j; i++) {
822                 free(processes[i].name);
823         }
824         free(processes);
825 }
826
827 #if     defined(i386) || defined(__i386__)
828 #define APMDEV          "/dev/apm"
829 #define APM_UNKNOWN     255
830
831 int apm_getinfo(int fd, apm_info_t aip)
832 {
833         if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
834                 return -1;
835         }
836
837         return 0;
838 }
839
840 char *get_apm_adapter()
841 {
842         int fd;
843         struct apm_info info;
844         char *out;
845
846         out = (char *) calloc(16, sizeof(char));
847
848         fd = open(APMDEV, O_RDONLY);
849         if (fd < 0) {
850                 strncpy(out, "ERR", 16);
851                 return out;
852         }
853
854         if (apm_getinfo(fd, &info) != 0) {
855                 close(fd);
856                 strncpy(out, "ERR", 16);
857                 return out;
858         }
859         close(fd);
860
861         switch (info.ai_acline) {
862                 case 0:
863                         strncpy(out, "off-line", 16);
864                         return out;
865                         break;
866                 case 1:
867                         if (info.ai_batt_stat == 3) {
868                                 strncpy(out, "charging", 16);
869                                 return out;
870                         } else {
871                                 strncpy(out, "on-line", 16);
872                                 return out;
873                         }
874                         break;
875                 default:
876                         strncpy(out, "unknown", 16);
877                         return out;
878                         break;
879         }
880 }
881
882 char *get_apm_battery_life()
883 {
884         int fd;
885         u_int batt_life;
886         struct apm_info info;
887         char *out;
888
889         out = (char *) calloc(16, sizeof(char));
890
891         fd = open(APMDEV, O_RDONLY);
892         if (fd < 0) {
893                 strncpy(out, "ERR", 16);
894                 return out;
895         }
896
897         if (apm_getinfo(fd, &info) != 0) {
898                 close(fd);
899                 strncpy(out, "ERR", 16);
900                 return out;
901         }
902         close(fd);
903
904         batt_life = info.ai_batt_life;
905         if (batt_life == APM_UNKNOWN) {
906                 strncpy(out, "unknown", 16);
907         } else if (batt_life <= 100) {
908                 snprintf(out, 16, "%d%%", batt_life);
909                 return out;
910         } else {
911                 strncpy(out, "ERR", 16);
912         }
913
914         return out;
915 }
916
917 char *get_apm_battery_time()
918 {
919         int fd;
920         int batt_time;
921         int h, m, s;
922         struct apm_info info;
923         char *out;
924
925         out = (char *) calloc(16, sizeof(char));
926
927         fd = open(APMDEV, O_RDONLY);
928         if (fd < 0) {
929                 strncpy(out, "ERR", 16);
930                 return out;
931         }
932
933         if (apm_getinfo(fd, &info) != 0) {
934                 close(fd);
935                 strncpy(out, "ERR", 16);
936                 return out;
937         }
938         close(fd);
939
940         batt_time = info.ai_batt_time;
941
942         if (batt_time == -1) {
943                 strncpy(out, "unknown", 16);
944         } else {
945                 h = batt_time;
946                 s = h % 60;
947                 h /= 60;
948                 m = h % 60;
949                 h /= 60;
950                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
951         }
952
953         return out;
954 }
955
956 #endif
957
958 void update_entropy(void)
959 {
960         /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
961 }
962
963 /* empty stub so conky links */
964 void free_all_processes(void)
965 {
966 }