* Applied 12 patches:
[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-2007 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(int *retavail, int *retfree)
90 {
91         int n;
92         int 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         int total_pages, inactive_pages, free_pages;
153         int 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
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                 diskio_value = 0;
710         } else {
711                 diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
712         }
713         diskio_prev = diskio_current;
714
715         free(statinfo_cur.dinfo);
716 }
717
718 /* While topless is obviously better, top is also not bad. */
719
720 int comparecpu(const void *a, const void *b)
721 {
722         if (((struct process *)a)->amount > ((struct process *)b)->amount) {
723                 return -1;
724         } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
725                 return 1;
726         } else {
727                 return 0;
728         }
729 }
730
731 int comparemem(const void *a, const void *b)
732 {
733         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
734                 return -1;
735         } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
736                 return 1;
737         } else {
738                 return 0;
739         }
740 }
741
742 inline void proc_find_top(struct process **cpu, struct process **mem)
743 {
744         struct kinfo_proc *p;
745         int n_processes;
746         int i, j = 0;
747         struct process *processes;
748
749         int total_pages;
750
751         /* we get total pages count again to be sure it is up to date */
752         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
753                 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
754         }
755
756         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
757         processes = malloc(n_processes * sizeof(struct process));
758
759         for (i = 0; i < n_processes; i++) {
760                 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
761                         processes[j].pid = p[i].ki_pid;
762                         processes[j].name = strdup(p[i].ki_comm);
763                         processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
764                         processes[j].totalmem = (float) (p[i].ki_rssize /
765                                 (float) total_pages) * 100.0;
766                         j++;
767                 }
768         }
769
770         qsort(processes, j - 1, sizeof(struct process), comparemem);
771         for (i = 0; i < 10 && i < n_processes; i++) {
772                 struct process *tmp, *ttmp;
773
774                 tmp = malloc(sizeof(struct process));
775                 tmp->pid = processes[i].pid;
776                 tmp->amount = processes[i].amount;
777                 tmp->totalmem = processes[i].totalmem;
778                 tmp->name = strdup(processes[i].name);
779
780                 ttmp = mem[i];
781                 mem[i] = tmp;
782                 if (ttmp != NULL) {
783                         free(ttmp->name);
784                         free(ttmp);
785                 }
786         }
787
788         qsort(processes, j - 1, sizeof(struct process), comparecpu);
789         for (i = 0; i < 10 && i < n_processes; i++) {
790                 struct process *tmp, *ttmp;
791
792                 tmp = malloc(sizeof(struct process));
793                 tmp->pid = processes[i].pid;
794                 tmp->amount = processes[i].amount;
795                 tmp->totalmem = processes[i].totalmem;
796                 tmp->name = strdup(processes[i].name);
797
798                 ttmp = cpu[i];
799                 cpu[i] = tmp;
800                 if (ttmp != NULL) {
801                         free(ttmp->name);
802                         free(ttmp);
803                 }
804         }
805
806 #if defined(FREEBSD_DEBUG)
807         printf("=====\nmem\n");
808         for (i = 0; i < 10; i++) {
809                 printf("%d: %s(%d) %.2f\n", i, mem[i]->name, mem[i]->pid,
810                         mem[i]->totalmem);
811         }
812 #endif
813
814         for (i = 0; i < j; i++) {
815                 free(processes[i].name);
816         }
817         free(processes);
818 }
819
820 #if     defined(i386) || defined(__i386__)
821 #define APMDEV          "/dev/apm"
822 #define APM_UNKNOWN     255
823
824 int apm_getinfo(int fd, apm_info_t aip)
825 {
826         if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
827                 return -1;
828         }
829
830         return 0;
831 }
832
833 char *get_apm_adapter()
834 {
835         int fd;
836         struct apm_info info;
837         char *out;
838
839         out = (char *) calloc(16, sizeof(char));
840
841         fd = open(APMDEV, O_RDONLY);
842         if (fd < 0) {
843                 strncpy(out, "ERR", 16);
844                 return out;
845         }
846
847         if (apm_getinfo(fd, &info) != 0) {
848                 close(fd);
849                 strncpy(out, "ERR", 16);
850                 return out;
851         }
852         close(fd);
853
854         switch (info.ai_acline) {
855                 case 0:
856                         strncpy(out, "off-line", 16);
857                         return out;
858                         break;
859                 case 1:
860                         if (info.ai_batt_stat == 3) {
861                                 strncpy(out, "charging", 16);
862                                 return out;
863                         } else {
864                                 strncpy(out, "on-line", 16);
865                                 return out;
866                         }
867                         break;
868                 default:
869                         strncpy(out, "unknown", 16);
870                         return out;
871                         break;
872         }
873 }
874
875 char *get_apm_battery_life()
876 {
877         int fd;
878         u_int batt_life;
879         struct apm_info info;
880         char *out;
881
882         out = (char *) calloc(16, sizeof(char));
883
884         fd = open(APMDEV, O_RDONLY);
885         if (fd < 0) {
886                 strncpy(out, "ERR", 16);
887                 return out;
888         }
889
890         if (apm_getinfo(fd, &info) != 0) {
891                 close(fd);
892                 strncpy(out, "ERR", 16);
893                 return out;
894         }
895         close(fd);
896
897         batt_life = info.ai_batt_life;
898         if (batt_life == APM_UNKNOWN) {
899                 strncpy(out, "unknown", 16);
900         } else if (batt_life <= 100) {
901                 snprintf(out, 16, "%d%%", batt_life);
902                 return out;
903         } else {
904                 strncpy(out, "ERR", 16);
905         }
906
907         return out;
908 }
909
910 char *get_apm_battery_time()
911 {
912         int fd;
913         int batt_time;
914         int h, m, s;
915         struct apm_info info;
916         char *out;
917
918         out = (char *) calloc(16, sizeof(char));
919
920         fd = open(APMDEV, O_RDONLY);
921         if (fd < 0) {
922                 strncpy(out, "ERR", 16);
923                 return out;
924         }
925
926         if (apm_getinfo(fd, &info) != 0) {
927                 close(fd);
928                 strncpy(out, "ERR", 16);
929                 return out;
930         }
931         close(fd);
932
933         batt_time = info.ai_batt_time;
934
935         if (batt_time == -1) {
936                 strncpy(out, "unknown", 16);
937         } else {
938                 h = batt_time;
939                 s = h % 60;
940                 h /= 60;
941                 m = h % 60;
942                 h /= 60;
943                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
944         }
945
946         return out;
947 }
948
949 #endif
950
951 void update_entropy(void)
952 {
953         /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
954 }
955
956 /* empty stub so conky links */
957 void free_all_processes(void)
958 {
959 }