* Getting rid of goto usage throughout code.
[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                         close(s);
611                         return;
612                 }
613
614                 /* We can monitor only wireless interfaces
615                  * which are not in hostap mode */
616                 if ((ifmr.ifm_active & IFM_IEEE80211)
617                                 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
618                         /* Get wi status */
619                         bzero(&ifr, sizeof(ifr));
620                         strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
621                         wireq.wi_type = WI_RID_COMMS_QUALITY;
622                         wireq.wi_len = WI_MAX_DATALEN;
623                         ifr.ifr_data = (void *) &wireq;
624
625                         if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
626                                 perror("ioctl (getting wi status)");
627                                 exit(1);
628                         }
629
630                         /* wi_val[0] = quality
631                          * wi_val[1] = signal
632                          * wi_val[2] = noise */
633                         ns->linkstatus = (int) wireq.wi_val[1];
634                 }
635 cleanup:
636                 close(s);
637         }
638 }
639 #endif
640
641 void update_diskio()
642 {
643         int devs_count, num_selected, num_selections, i;
644         struct device_selection *dev_select = NULL;
645         long select_generation;
646         int dn;
647         static struct statinfo statinfo_cur;
648         u_int64_t diskio_current = 0;
649         u_int64_t writes = 0;
650
651         bzero(&statinfo_cur, sizeof(statinfo_cur));
652         statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
653         bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
654
655         if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
656                 return;
657         }
658
659         devs_count = statinfo_cur.dinfo->numdevs;
660         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
661                         &select_generation, statinfo_cur.dinfo->generation,
662                         statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
663                         DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
664                 for (dn = 0; dn < devs_count; ++dn) {
665                         int di;
666                         struct devstat *dev;
667
668                         di = dev_select[dn].position;
669                         dev = &statinfo_cur.dinfo->devices[di];
670
671                         diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
672
673                         for (i = 0; i < MAX_DISKIO_STATS; i++) {
674                                 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
675                                                 diskio_stats[i].dev) == 0) {
676                                         diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
677                                                 dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
678                                         diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
679                                                 diskio_stats[i].last_read) / 1024;
680                                         diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
681                                                 diskio_stats[i].last_write) / 1024;
682                                         if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
683                                                         < diskio_stats[i].last) {
684                                                 diskio_stats[i].current = 0;
685                                         }
686                                         if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
687                                                 diskio_stats[i].current_read = 0;
688                                                 diskio_stats[i].current = diskio_stats[i].current_write;
689                                         }
690                                         if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
691                                                 diskio_stats[i].current_write = 0;
692                                                 diskio_stats[i].current = diskio_stats[i].current_read;
693                                         }
694                                         diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
695                                                 dev->bytes[DEVSTAT_WRITE];
696                                         diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
697                                         diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
698                                 }
699                         }
700                 }
701
702                 free(dev_select);
703         }
704
705         /* Since we return (diskio_total_current - diskio_total_old),
706          * the first frame will be way too high
707          * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
708          *  That's why it is better to return 0 first time; */
709         if (diskio_setup == 0) {
710                 diskio_setup = 1;
711                 diskio_value = 0;
712         } else {
713                 diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
714         }
715         diskio_prev = diskio_current;
716
717         free(statinfo_cur.dinfo);
718 }
719
720 /* While topless is obviously better, top is also not bad. */
721
722 int comparecpu(const void *a, const void *b)
723 {
724         if (((struct process *)a)->amount > ((struct process *)b)->amount) {
725                 return -1;
726         } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
727                 return 1;
728         } else {
729                 return 0;
730         }
731 }
732
733 int comparemem(const void *a, const void *b)
734 {
735         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
736                 return -1;
737         } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
738                 return 1;
739         } else {
740                 return 0;
741         }
742 }
743
744 inline void proc_find_top(struct process **cpu, struct process **mem)
745 {
746         struct kinfo_proc *p;
747         int n_processes;
748         int i, j = 0;
749         struct process *processes;
750
751         int total_pages;
752
753         /* we get total pages count again to be sure it is up to date */
754         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
755                 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
756         }
757
758         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
759         processes = malloc(n_processes * sizeof(struct process));
760
761         for (i = 0; i < n_processes; i++) {
762                 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
763                         processes[j].pid = p[i].ki_pid;
764                         processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
765                         processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
766                         processes[j].totalmem = (float) (p[i].ki_rssize /
767                                 (float) total_pages) * 100.0;
768                         processes[j].vsize = p[i].ki_size;
769                         processes[j].rss = (p[i].ki_rssize * getpagesize());
770                         j++;
771                 }
772         }
773
774         qsort(processes, j - 1, sizeof(struct process), comparemem);
775         for (i = 0; i < 10 && i < n_processes; i++) {
776                 struct process *tmp, *ttmp;
777
778                 tmp = malloc(sizeof(struct process));
779                 tmp->pid = processes[i].pid;
780                 tmp->amount = processes[i].amount;
781                 tmp->totalmem = processes[i].totalmem;
782                 tmp->name = strndup(processes[i].name, text_buffer_size);
783                 tmp->rss = processes[i].rss;
784                 tmp->vsize = processes[i].vsize;
785
786                 ttmp = mem[i];
787                 mem[i] = tmp;
788                 if (ttmp != NULL) {
789                         free(ttmp->name);
790                         free(ttmp);
791                 }
792         }
793
794         qsort(processes, j - 1, sizeof(struct process), comparecpu);
795         for (i = 0; i < 10 && i < n_processes; i++) {
796                 struct process *tmp, *ttmp;
797
798                 tmp = malloc(sizeof(struct process));
799                 tmp->pid = processes[i].pid;
800                 tmp->amount = processes[i].amount;
801                 tmp->totalmem = processes[i].totalmem;
802                 tmp->name = strndup(processes[i].name, text_buffer_size);
803                 tmp->rss = processes[i].rss;
804                 tmp->vsize = processes[i].vsize;
805
806                 ttmp = cpu[i];
807                 cpu[i] = tmp;
808                 if (ttmp != NULL) {
809                         free(ttmp->name);
810                         free(ttmp);
811                 }
812         }
813
814 #if defined(FREEBSD_DEBUG)
815         printf("=====\nmem\n");
816         for (i = 0; i < 10; i++) {
817                 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
818                                 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
819         }
820 #endif
821
822         for (i = 0; i < j; i++) {
823                 free(processes[i].name);
824         }
825         free(processes);
826 }
827
828 #if     defined(i386) || defined(__i386__)
829 #define APMDEV          "/dev/apm"
830 #define APM_UNKNOWN     255
831
832 int apm_getinfo(int fd, apm_info_t aip)
833 {
834         if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
835                 return -1;
836         }
837
838         return 0;
839 }
840
841 char *get_apm_adapter()
842 {
843         int fd;
844         struct apm_info info;
845         char *out;
846
847         out = (char *) calloc(16, sizeof(char));
848
849         fd = open(APMDEV, O_RDONLY);
850         if (fd < 0) {
851                 strncpy(out, "ERR", 16);
852                 return out;
853         }
854
855         if (apm_getinfo(fd, &info) != 0) {
856                 close(fd);
857                 strncpy(out, "ERR", 16);
858                 return out;
859         }
860         close(fd);
861
862         switch (info.ai_acline) {
863                 case 0:
864                         strncpy(out, "off-line", 16);
865                         return out;
866                         break;
867                 case 1:
868                         if (info.ai_batt_stat == 3) {
869                                 strncpy(out, "charging", 16);
870                                 return out;
871                         } else {
872                                 strncpy(out, "on-line", 16);
873                                 return out;
874                         }
875                         break;
876                 default:
877                         strncpy(out, "unknown", 16);
878                         return out;
879                         break;
880         }
881 }
882
883 char *get_apm_battery_life()
884 {
885         int fd;
886         u_int batt_life;
887         struct apm_info info;
888         char *out;
889
890         out = (char *) calloc(16, sizeof(char));
891
892         fd = open(APMDEV, O_RDONLY);
893         if (fd < 0) {
894                 strncpy(out, "ERR", 16);
895                 return out;
896         }
897
898         if (apm_getinfo(fd, &info) != 0) {
899                 close(fd);
900                 strncpy(out, "ERR", 16);
901                 return out;
902         }
903         close(fd);
904
905         batt_life = info.ai_batt_life;
906         if (batt_life == APM_UNKNOWN) {
907                 strncpy(out, "unknown", 16);
908         } else if (batt_life <= 100) {
909                 snprintf(out, 16, "%d%%", batt_life);
910                 return out;
911         } else {
912                 strncpy(out, "ERR", 16);
913         }
914
915         return out;
916 }
917
918 char *get_apm_battery_time()
919 {
920         int fd;
921         int batt_time;
922         int h, m, s;
923         struct apm_info info;
924         char *out;
925
926         out = (char *) calloc(16, sizeof(char));
927
928         fd = open(APMDEV, O_RDONLY);
929         if (fd < 0) {
930                 strncpy(out, "ERR", 16);
931                 return out;
932         }
933
934         if (apm_getinfo(fd, &info) != 0) {
935                 close(fd);
936                 strncpy(out, "ERR", 16);
937                 return out;
938         }
939         close(fd);
940
941         batt_time = info.ai_batt_time;
942
943         if (batt_time == -1) {
944                 strncpy(out, "unknown", 16);
945         } else {
946                 h = batt_time;
947                 s = h % 60;
948                 h /= 60;
949                 m = h % 60;
950                 h /= 60;
951                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
952         }
953
954         return out;
955 }
956
957 #endif
958
959 void update_entropy(void)
960 {
961         /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
962 }
963
964 /* empty stub so conky links */
965 void free_all_processes(void)
966 {
967 }