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