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