Disk i/o support on FreeBSD patch.
[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  */
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 static struct diskio_stat diskio_stats_[MAX_DISKIO_STATS];
68 struct diskio_stat *diskio_stats = diskio_stats_;
69
70 static int getsysctl(char *name, void *ptr, size_t len)
71 {
72         size_t nlen = len;
73
74         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
75                 return -1;
76         }
77
78         if (nlen != len) {
79                 return -1;
80         }
81
82         return 0;
83 }
84
85 struct ifmibdata *data = NULL;
86 size_t len = 0;
87
88 static int swapmode(unsigned long *retavail, unsigned long *retfree)
89 {
90         int n;
91         unsigned long pagesize = getpagesize();
92         struct kvm_swap swapary[1];
93
94         *retavail = 0;
95         *retfree = 0;
96
97 #define CONVERT(v)      ((quad_t)(v) * (pagesize / 1024))
98
99         n = kvm_getswapinfo(kd, swapary, 1, 0);
100         if (n < 0 || swapary[0].ksw_total == 0) {
101                 return 0;
102         }
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 prepare_update()
114 {
115 }
116
117 void update_uptime()
118 {
119         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
120         struct timeval boottime;
121         time_t now;
122         size_t size = sizeof(boottime);
123
124         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
125                         && (boottime.tv_sec != 0)) {
126                 time(&now);
127                 info.uptime = now - boottime.tv_sec;
128         } else {
129                 fprintf(stderr, "Could not get uptime\n");
130                 info.uptime = 0;
131         }
132 }
133
134 int check_mount(char *s)
135 {
136         struct statfs *mntbuf;
137         int i, mntsize;
138
139         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
140         for (i = mntsize - 1; i >= 0; i--) {
141                 if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
142                         return 1;
143                 }
144         }
145
146         return 0;
147 }
148
149 void update_meminfo()
150 {
151         u_int total_pages, inactive_pages, free_pages;
152         unsigned long swap_avail, swap_free;
153
154         int pagesize = getpagesize();
155
156         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
157                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
158         }
159
160         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
161                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
162         }
163
164         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
165                 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
166         }
167
168         info.memmax = total_pages * (pagesize >> 10);
169         info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
170         info.memeasyfree = info.memfree = info.memmax - info.mem;
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                 const 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, const char *p_format,
553                 int divisor, unsigned int cpu)
554 {
555         int freq;
556         char *freq_sysctl;
557
558         freq_sysctl = (char *) calloc(16, sizeof(char));
559         if (freq_sysctl == NULL) {
560                 exit(-1);
561         }
562
563         snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
564
565         if (!p_client_buffer || client_buffer_size <= 0 || !p_format
566                         || divisor <= 0) {
567                 return 0;
568         }
569
570         if (GETSYSCTL(freq_sysctl, freq) == 0) {
571                 snprintf(p_client_buffer, client_buffer_size, p_format,
572                         (float) freq / divisor);
573         } else {
574                 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
575         }
576
577         free(freq_sysctl);
578         return 1;
579 }
580
581 void update_top()
582 {
583         proc_find_top(info.cpu, info.memu);
584 }
585
586 #if 0
587 void update_wifi_stats()
588 {
589         struct ifreq ifr;               /* interface stats */
590         struct wi_req wireq;
591         struct net_stat *ns;
592         struct ifaddrs *ifap, *ifa;
593         struct ifmediareq ifmr;
594         int s;
595
596         /* Get iface table */
597         if (getifaddrs(&ifap) < 0) {
598                 return;
599         }
600
601         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
602                 ns = get_net_stat((const char *) ifa->ifa_name);
603
604                 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
605
606                 /* Get media type */
607                 bzero(&ifmr, sizeof(ifmr));
608                 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
609                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
610                         close(s);
611                         return;
612                 }
613
614                 /* We can monitor only wireless interfaces
615                  * which are not in hostap mode */
616                 if ((ifmr.ifm_active & IFM_IEEE80211)
617                                 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
618                         /* Get wi status */
619                         bzero(&ifr, sizeof(ifr));
620                         strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
621                         wireq.wi_type = WI_RID_COMMS_QUALITY;
622                         wireq.wi_len = WI_MAX_DATALEN;
623                         ifr.ifr_data = (void *) &wireq;
624
625                         if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
626                                 perror("ioctl (getting wi status)");
627                                 exit(1);
628                         }
629
630                         /* wi_val[0] = quality
631                          * wi_val[1] = signal
632                          * wi_val[2] = noise */
633                         ns->linkstatus = (int) wireq.wi_val[1];
634                 }
635 cleanup:
636                 close(s);
637         }
638 }
639 #endif
640
641 void update_diskio()
642 {
643         int devs_count, num_selected, num_selections, i;
644         struct device_selection *dev_select = NULL;
645         long select_generation;
646         int dn;
647         static struct statinfo statinfo_cur;
648         u_int64_t diskio_current = 0;
649         u_int64_t writes = 0;
650
651         bzero(&statinfo_cur, sizeof(statinfo_cur));
652         statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
653         bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
654
655         if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
656                 return;
657         }
658
659         devs_count = statinfo_cur.dinfo->numdevs;
660         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
661                         &select_generation, statinfo_cur.dinfo->generation,
662                         statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
663                         DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
664                 for (dn = 0; dn < devs_count; ++dn) {
665                         int di;
666                         struct devstat *dev;
667
668                         di = dev_select[dn].position;
669                         dev = &statinfo_cur.dinfo->devices[di];
670
671                         diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
672
673                         for (i = 0; i < MAX_DISKIO_STATS; i++) {
674                                 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
675                                                 diskio_stats[i].dev) == 0) {
676                                         diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
677                                                 dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
678                                         diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
679                                                 diskio_stats[i].last_read) / 1024;
680                                         diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
681                                                 diskio_stats[i].last_write) / 1024;
682                                         if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
683                                                         < diskio_stats[i].last) {
684                                                 diskio_stats[i].current = 0;
685                                         }
686                                         if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
687                                                 diskio_stats[i].current_read = 0;
688                                                 diskio_stats[i].current = diskio_stats[i].current_write;
689                                         }
690                                         if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
691                                                 diskio_stats[i].current_write = 0;
692                                                 diskio_stats[i].current = diskio_stats[i].current_read;
693                                         }
694                                         diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
695                                                 dev->bytes[DEVSTAT_WRITE];
696                                         diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
697                                         diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
698                                 }
699                         }
700                 }
701
702                 free(dev_select);
703         }
704
705         /* Since we return (diskio_total_current - diskio_total_old),
706          * the first frame will be way too high
707          * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
708          *  That's why it is better to return 0 first time; */
709         if (diskio_setup == 0) {
710                 diskio_setup = 1;
711                 info.diskio_value = 0;
712         } else {
713                 info.diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
714         }
715         diskio_prev = diskio_current;
716
717         free(statinfo_cur.dinfo);
718 }
719
720 void clear_diskio_stats()
721 {
722         unsigned i;
723         for(i = 0; i < MAX_DISKIO_STATS; i++) {
724                 if (diskio_stats[i].dev) {
725                         free(diskio_stats[i].dev);
726                         diskio_stats[i].dev = 0;
727                 }
728         }
729 }
730
731 struct diskio_stat *prepare_diskio_stat(const char *s)
732 {
733         struct diskio_stat *new = 0;
734         struct stat sb;
735         unsigned i;
736         FILE *fp;
737         int found = 0;
738         char device[text_buffer_size], fbuf[text_buffer_size];
739         static int rep = 0;
740         /* lookup existing or get new */
741         for (i = 0; i < MAX_DISKIO_STATS; i++) {
742                 if (diskio_stats[i].dev) {
743                         if (strcmp(diskio_stats[i].dev, s) == 0) {
744                                 return &diskio_stats[i];
745                         }
746                 } else {
747                         new = &diskio_stats[i];
748                         break;
749                 }
750         }
751         /* new dev */
752         if (!new) {
753                 ERR("too many diskio stats");
754                 return 0;
755         }
756         if (new->dev) {
757                 free(new->dev);
758                 new->dev = 0;
759         }
760         if (strncmp(s, "/dev/", 5) == 0) {
761                 // supplied a /dev/device arg, so cut off the /dev part
762                 new->dev = strndup(s + 5, text_buffer_size);
763         } else {
764                 new->dev = strndup(s, text_buffer_size);
765         }
766         /*
767          * check that device actually exists
768          */
769         snprintf(device, text_buffer_size, "/dev/%s", new->dev);
770
771         if (stat(device, &sb)) {
772                 ERR("diskio device '%s' does not exist", s);
773                 return 0;
774         }
775         new->current = 0;
776         new->current_read = 0;
777         new ->current_write = 0;
778         new->last = UINT_MAX;
779         new->last_read = UINT_MAX;
780         new->last_write = UINT_MAX;
781         return new;
782 }
783
784 /* While topless is obviously better, top is also not bad. */
785
786 int comparecpu(const void *a, const void *b)
787 {
788         if (((struct process *)a)->amount > ((struct process *)b)->amount) {
789                 return -1;
790         } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
791                 return 1;
792         } else {
793                 return 0;
794         }
795 }
796
797 int comparemem(const void *a, const void *b)
798 {
799         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
800                 return -1;
801         } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
802                 return 1;
803         } else {
804                 return 0;
805         }
806 }
807
808 inline void proc_find_top(struct process **cpu, struct process **mem)
809 {
810         struct kinfo_proc *p;
811         int n_processes;
812         int i, j = 0;
813         struct process *processes;
814
815         int total_pages;
816
817         /* we get total pages count again to be sure it is up to date */
818         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
819                 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
820         }
821
822         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
823         processes = malloc(n_processes * sizeof(struct process));
824
825         for (i = 0; i < n_processes; i++) {
826                 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
827                         processes[j].pid = p[i].ki_pid;
828                         processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
829                         processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
830                         processes[j].totalmem = (float) (p[i].ki_rssize /
831                                 (float) total_pages) * 100.0;
832                         processes[j].vsize = p[i].ki_size;
833                         processes[j].rss = (p[i].ki_rssize * getpagesize());
834                         j++;
835                 }
836         }
837
838         qsort(processes, j - 1, sizeof(struct process), comparemem);
839         for (i = 0; i < 10 && i < n_processes; i++) {
840                 struct process *tmp, *ttmp;
841
842                 tmp = malloc(sizeof(struct process));
843                 tmp->pid = processes[i].pid;
844                 tmp->amount = processes[i].amount;
845                 tmp->totalmem = processes[i].totalmem;
846                 tmp->name = strndup(processes[i].name, text_buffer_size);
847                 tmp->rss = processes[i].rss;
848                 tmp->vsize = processes[i].vsize;
849
850                 ttmp = mem[i];
851                 mem[i] = tmp;
852                 if (ttmp != NULL) {
853                         free(ttmp->name);
854                         free(ttmp);
855                 }
856         }
857
858         qsort(processes, j - 1, sizeof(struct process), comparecpu);
859         for (i = 0; i < 10 && i < n_processes; i++) {
860                 struct process *tmp, *ttmp;
861
862                 tmp = malloc(sizeof(struct process));
863                 tmp->pid = processes[i].pid;
864                 tmp->amount = processes[i].amount;
865                 tmp->totalmem = processes[i].totalmem;
866                 tmp->name = strndup(processes[i].name, text_buffer_size);
867                 tmp->rss = processes[i].rss;
868                 tmp->vsize = processes[i].vsize;
869
870                 ttmp = cpu[i];
871                 cpu[i] = tmp;
872                 if (ttmp != NULL) {
873                         free(ttmp->name);
874                         free(ttmp);
875                 }
876         }
877
878 #if defined(FREEBSD_DEBUG)
879         printf("=====\nmem\n");
880         for (i = 0; i < 10; i++) {
881                 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
882                                 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
883         }
884 #endif
885
886         for (i = 0; i < j; i++) {
887                 free(processes[i].name);
888         }
889         free(processes);
890 }
891
892 #if     defined(i386) || defined(__i386__)
893 #define APMDEV          "/dev/apm"
894 #define APM_UNKNOWN     255
895
896 int apm_getinfo(int fd, apm_info_t aip)
897 {
898         if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
899                 return -1;
900         }
901
902         return 0;
903 }
904
905 char *get_apm_adapter()
906 {
907         int fd;
908         struct apm_info info;
909         char *out;
910
911         out = (char *) calloc(16, sizeof(char));
912
913         fd = open(APMDEV, O_RDONLY);
914         if (fd < 0) {
915                 strncpy(out, "ERR", 16);
916                 return out;
917         }
918
919         if (apm_getinfo(fd, &info) != 0) {
920                 close(fd);
921                 strncpy(out, "ERR", 16);
922                 return out;
923         }
924         close(fd);
925
926         switch (info.ai_acline) {
927                 case 0:
928                         strncpy(out, "off-line", 16);
929                         return out;
930                         break;
931                 case 1:
932                         if (info.ai_batt_stat == 3) {
933                                 strncpy(out, "charging", 16);
934                                 return out;
935                         } else {
936                                 strncpy(out, "on-line", 16);
937                                 return out;
938                         }
939                         break;
940                 default:
941                         strncpy(out, "unknown", 16);
942                         return out;
943                         break;
944         }
945 }
946
947 char *get_apm_battery_life()
948 {
949         int fd;
950         u_int batt_life;
951         struct apm_info info;
952         char *out;
953
954         out = (char *) calloc(16, sizeof(char));
955
956         fd = open(APMDEV, O_RDONLY);
957         if (fd < 0) {
958                 strncpy(out, "ERR", 16);
959                 return out;
960         }
961
962         if (apm_getinfo(fd, &info) != 0) {
963                 close(fd);
964                 strncpy(out, "ERR", 16);
965                 return out;
966         }
967         close(fd);
968
969         batt_life = info.ai_batt_life;
970         if (batt_life == APM_UNKNOWN) {
971                 strncpy(out, "unknown", 16);
972         } else if (batt_life <= 100) {
973                 snprintf(out, 16, "%d%%", batt_life);
974                 return out;
975         } else {
976                 strncpy(out, "ERR", 16);
977         }
978
979         return out;
980 }
981
982 char *get_apm_battery_time()
983 {
984         int fd;
985         int batt_time;
986         int h, m, s;
987         struct apm_info info;
988         char *out;
989
990         out = (char *) calloc(16, sizeof(char));
991
992         fd = open(APMDEV, O_RDONLY);
993         if (fd < 0) {
994                 strncpy(out, "ERR", 16);
995                 return out;
996         }
997
998         if (apm_getinfo(fd, &info) != 0) {
999                 close(fd);
1000                 strncpy(out, "ERR", 16);
1001                 return out;
1002         }
1003         close(fd);
1004
1005         batt_time = info.ai_batt_time;
1006
1007         if (batt_time == -1) {
1008                 strncpy(out, "unknown", 16);
1009         } else {
1010                 h = batt_time;
1011                 s = h % 60;
1012                 h /= 60;
1013                 m = h % 60;
1014                 h /= 60;
1015                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
1016         }
1017
1018         return out;
1019 }
1020
1021 #endif
1022
1023 void update_entropy(void)
1024 {
1025         /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
1026 }
1027
1028 /* empty stub so conky links */
1029 void free_all_processes(void)
1030 {
1031 }