get_acpi_fan() / get_acpi_ac_adapter() interface changes as per bug 1355470
[monky] / src / freebsd.c
1 /** freebsd.c
2  * Contains FreeBSD specific stuff
3  *
4  * $Id$
5  */
6
7 #include <fcntl.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <kvm.h>
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <sys/resource.h>
17 #include <sys/sysctl.h>
18 #include <sys/vmmeter.h>
19 #include <sys/dkstat.h>
20 #include <unistd.h>
21 #include <sys/user.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <net/if_mib.h>
25 #include <sys/socket.h>
26 #include <ifaddrs.h>
27 #include <devstat.h>
28
29 #include "conky.h"
30
31 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
32 #define KELVTOC(x)      ((x - 2732) / 10.0)
33 #define MAXSHOWDEVS     16
34
35 inline void proc_find_top(struct process **cpu, struct process **mem);
36
37 u_int64_t diskio_prev = 0;
38 static short cpu_setup = 0;
39 static short diskio_setup = 0;
40
41 static int getsysctl(char *name, void *ptr, size_t len)
42 {
43         size_t nlen = len;
44         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
45                 return -1;
46         }
47
48         if (nlen != len) {
49                 return -1;
50         }
51
52         return 0;
53 }
54
55 static kvm_t *kd = NULL;
56 struct ifmibdata *data = NULL;
57 size_t len = 0;
58
59 static int swapmode(int *retavail, int *retfree)
60 {
61         int n;
62         int pagesize = getpagesize();
63         struct kvm_swap swapary[1];
64         static int kd_init = 1;
65
66         if (kd_init) {
67                 kd_init = 0;
68                 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
69                                    O_RDONLY, "kvm_open")) == NULL) {
70                         (void) fprintf(stderr, "Cannot read kvm.");
71                         return -1;
72                 }
73         }
74
75         if (kd == NULL) {
76                 return -1;
77         }
78
79         *retavail = 0;
80         *retfree = 0;
81
82 #define CONVERT(v)      ((quad_t)(v) * pagesize / 1024)
83
84         n = kvm_getswapinfo(kd, swapary, 1, 0);
85         if (n < 0 || swapary[0].ksw_total == 0)
86                 return (0);
87
88         *retavail = CONVERT(swapary[0].ksw_total);
89         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
90
91         n = (int) ((double) swapary[0].ksw_used * 100.0 /
92                    (double) swapary[0].ksw_total);
93
94         return n;
95 }
96
97 void prepare_update()
98 {
99 }
100
101 void update_uptime()
102 {
103         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
104         struct timeval boottime;
105         time_t now;
106         size_t size = sizeof(boottime);
107
108         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
109             && (boottime.tv_sec != 0)) {
110                 time(&now);
111                 info.uptime = now - boottime.tv_sec;
112         } else {
113                 (void) fprintf(stderr, "Could not get uptime\n");
114                 info.uptime = 0;
115         }
116 }
117
118 void update_meminfo()
119 {
120         int total_pages, inactive_pages, free_pages;
121         int swap_avail, swap_free;
122
123         int pagesize = getpagesize();
124
125         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
126                 (void) fprintf(stderr,
127                                "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
128
129         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
130                 (void) fprintf(stderr,
131                                "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
132
133         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
134                 (void) fprintf(stderr,
135                                "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
136
137         info.memmax = (total_pages * pagesize) >> 10;
138         info.mem =
139             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
140
141
142         if ((swapmode(&swap_avail, &swap_free)) >= 0) {
143                 info.swapmax = swap_avail;
144                 info.swap = (swap_avail - swap_free);
145         } else {
146                 info.swapmax = 0;
147                 info.swap = 0;
148         }
149 }
150
151 void update_net_stats()
152 {
153         struct net_stat *ns;
154         double delta;
155         long long r, t, last_recv, last_trans;
156         struct ifaddrs *ifap, *ifa;
157         struct if_data *ifd;
158
159
160         /* get delta */
161         delta = current_update_time - last_update_time;
162         if (delta <= 0.0001)
163                 return;
164
165         if (getifaddrs(&ifap) < 0)
166                 return;
167
168         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
169                 ns = get_net_stat((const char *) ifa->ifa_name);
170
171                 if (ifa->ifa_flags & IFF_UP) {
172                         last_recv = ns->recv;
173                         last_trans = ns->trans;
174
175                         if (ifa->ifa_addr->sa_family != AF_LINK)
176                                 continue;
177
178                         ifd = (struct if_data *) ifa->ifa_data;
179                         r = ifd->ifi_ibytes;
180                         t = ifd->ifi_obytes;
181
182                         if (r < ns->last_read_recv)
183                                 ns->recv +=
184                                     ((long long) 4294967295U -
185                                      ns->last_read_recv) + r;
186                         else
187                                 ns->recv += (r - ns->last_read_recv);
188
189                         ns->last_read_recv = r;
190
191                         if (t < ns->last_read_trans)
192                                 ns->trans +=
193                                     ((long long) 4294967295U -
194                                      ns->last_read_trans) + t;
195                         else
196                                 ns->trans += (t - ns->last_read_trans);
197
198                         ns->last_read_trans = t;
199
200
201                         /* calculate speeds */
202                         ns->recv_speed = (ns->recv - last_recv) / delta;
203                         ns->trans_speed = (ns->trans - last_trans) / delta;
204                 }
205         }
206
207         freeifaddrs(ifap);
208 }
209
210 void update_total_processes()
211 {
212         int n_processes;
213         static int kd_init = 1;
214
215         if (kd_init) {
216                 kd_init = 0;
217                 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
218                                    O_RDONLY, "kvm_open")) == NULL) {
219                         (void) fprintf(stderr, "Cannot read kvm.");
220                         return;
221                 }
222         }
223
224
225         if (kd != NULL)
226                 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
227         else
228                 return;
229
230         info.procs = n_processes;
231 }
232
233 void update_running_processes()
234 {
235         static int kd_init = 1;
236         struct kinfo_proc *p;
237         int n_processes;
238         int i, cnt = 0;
239
240         if (kd_init) {
241                 kd_init = 0;
242                 if ((kd =
243                      kvm_open("/dev/null", "/dev/null", "/dev/null",
244                               O_RDONLY, "kvm_open")) == NULL) {
245                         (void) fprintf(stderr, "Cannot read kvm.");
246                 }
247         }
248
249         if (kd != NULL) {
250                 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
251                 for (i = 0; i < n_processes; i++) {
252 #if __FreeBSD__ < 5
253                         if (p[i].kp_proc.p_stat == SRUN)
254 #else
255                         if (p[i].ki_stat == SRUN)
256 #endif
257                                 cnt++;
258                 }
259         } else
260                 return;
261
262         info.run_procs = cnt;
263 }
264
265 struct cpu_load_struct {
266         unsigned long load[5];
267 };
268
269 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
270 long cpu_used, oldtotal, oldused;
271
272 void get_cpu_count()
273 {
274         int cpu_count = 0;      
275
276         if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
277                 info.cpu_count = cpu_count;
278
279         info.cpu_usage = malloc(info.cpu_count * sizeof(float));
280         if (info.cpu_usage == NULL)
281                 CRIT_ERR("malloc");
282 }
283
284 /* XXX: SMP support */
285 void update_cpu_usage()
286 {
287         long used, total;
288         long cp_time[CPUSTATES];
289         size_t len = sizeof(cp_time);
290         
291         if (cpu_setup == 0) {
292                 get_cpu_count();
293                 cpu_setup = 1;
294         }
295         
296         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
297                 (void) fprintf(stderr, "Cannot get kern.cp_time");
298         }
299
300         fresh.load[0] = cp_time[CP_USER];
301         fresh.load[1] = cp_time[CP_NICE];
302         fresh.load[2] = cp_time[CP_SYS];
303         fresh.load[3] = cp_time[CP_IDLE];
304         fresh.load[4] = cp_time[CP_IDLE];
305
306         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
307         total =
308             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
309
310         if ((total - oldtotal) != 0) {
311                 info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal);
312         } else {
313                 info.cpu_usage[0] = 0;
314         }
315
316         oldused = used;
317         oldtotal = total;
318 }
319
320 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
321 {
322         return 0;
323 }
324
325 void update_load_average()
326 {
327         double v[3];
328         getloadavg(v, 3);
329
330         info.loadavg[0] = (float) v[0];
331         info.loadavg[1] = (float) v[1];
332         info.loadavg[2] = (float) v[2];
333 }
334
335 double get_acpi_temperature(int fd)
336 {
337         int temp;
338
339         if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
340                 (void) fprintf(stderr,
341                                "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
342                 return 0.0;
343         }
344
345         return KELVTOC(temp);
346 }
347
348 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
349 {
350         int battime;
351
352         if (GETSYSCTL("hw.acpi.battery.time", battime))
353                 (void) fprintf(stderr,
354                                "Cannot read sysctl \"hw.acpi.battery.time\"\n");
355
356         if (battime != -1)
357                 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
358                          battime / 60, battime % 60);
359         else
360                 snprintf(buf, n, "Battery is charging");
361 }
362
363 int
364 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
365                 char *devtype)
366 {
367         return 0;
368 }
369
370 int open_acpi_temperature(const char *name)
371 {
372         return 0;
373 }
374
375 /*char *get_acpi_ac_adapter(void)*/
376 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
377 {
378         int state;
379         /*char *acstate = (char *) malloc(100);*/
380
381         if ( !p_client_buffer !! client_buffer_size <= 0 )
382                 return;
383
384         if (GETSYSCTL("hw.acpi.acline", state)) {
385                 (void) fprintf(stderr,
386                                "Cannot read sysctl \"hw.acpi.acline\"\n");
387                 /*return "n\\a";*/
388                 return;
389         }
390
391
392         if (state)
393                 /*strcpy(acstate, "Running on AC Power");*/
394                 strncpy( p_client_buffer, client_buffer_size, "Running on AC Power" );
395         else
396                 /*strcpy(acstate, "Running on battery");*/
397                 strncpy( p_client_buffer, client_buffer_size, "Running on battery" );
398
399         /*return ac_state;*/
400         return;
401 }
402
403 /*char *get_acpi_fan()*/
404 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
405 {
406         if ( !p_client_buffer !! client_buffer_size <= 0 )
407                 return;
408
409         /* no implementation */
410
411         return;
412 }
413
414 char *get_adt746x_cpu()
415 {
416         return "";
417 }
418
419 char *get_adt746x_fan()
420 {
421         return "";
422 }
423
424 /* rdtsc() and get_freq_dynamic() copied from linux.c */
425
426 #if  defined(__i386) || defined(__x86_64)
427 __inline__ unsigned long long int rdtsc()
428 {
429         unsigned long long int x;
430         __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
431         return x;
432 }
433 #endif
434
435 float get_freq_dynamic()
436 {
437 #if  defined(__i386) || defined(__x86_64)
438         struct timezone tz;
439         struct timeval tvstart, tvstop;
440         unsigned long long cycles[2];   /* gotta be 64 bit */
441         unsigned int microseconds;      /* total time taken */
442
443         memset(&tz, 0, sizeof(tz));
444
445         /* get this function in cached memory */
446         gettimeofday(&tvstart, &tz);
447         cycles[0] = rdtsc();
448         gettimeofday(&tvstart, &tz);
449          
450         /* we don't trust that this is any specific length of time */
451         usleep(100);
452         cycles[1] = rdtsc();
453         gettimeofday(&tvstop, &tz);
454         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
455             (tvstop.tv_usec - tvstart.tv_usec);
456                              
457         return (cycles[1] - cycles[0]) / microseconds;
458 #else
459         return get_freq();
460 #endif
461 }
462
463 float get_freq()
464 {
465         int freq;
466         
467         if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
468                 return (float)freq;
469         else
470                 return (float)0;
471 }
472
473 void update_top()
474 {
475         proc_find_top(info.cpu, info.memu);
476 }
477
478 void update_wifi_stats()
479 {
480         /* XXX */
481 }
482 void update_diskio()
483 {
484         int devs_count,
485             num_selected,
486             num_selections;
487         struct device_selection *dev_select = NULL;
488         long select_generation;
489         int dn;
490         static struct statinfo  statinfo_cur;
491         u_int64_t diskio_current = 0;
492
493         bzero(&statinfo_cur, sizeof(statinfo_cur));
494         statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
495         bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
496         
497         if (devstat_getdevs(NULL, &statinfo_cur) < 0)
498                 return;
499
500         devs_count = statinfo_cur.dinfo->numdevs;
501         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
502                         &select_generation, statinfo_cur.dinfo->generation,
503                         statinfo_cur.dinfo->devices, devs_count, NULL, 0, 
504                         NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
505                 for (dn = 0; dn < devs_count; ++dn) {
506                         int di;
507                         struct devstat  *dev;
508
509                         di = dev_select[dn].position;
510                         dev = &statinfo_cur.dinfo->devices[di];
511
512                         diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
513                 }
514                 
515                 free(dev_select);
516         }
517
518         /* 
519          * Since we return (diskio_total_current - diskio_total_old), first
520          * frame will be way too high (it will be equal to diskio_total_current, i.e.
521          * all disk I/O since boot). That's why it is better to return 0 first time;
522          */
523         if (diskio_setup == 0) {
524                 diskio_setup = 1;
525                 diskio_value = 0;
526         } else
527                 diskio_value = (unsigned int)((diskio_current - diskio_prev)/1024);
528         diskio_prev = diskio_current;
529
530         free(statinfo_cur.dinfo);
531 }
532
533 /*
534  * While topless is obviously better, top is also not bad.
535  */
536
537 int comparecpu(const void * a, const void * b)
538 {
539         if (((struct process *)a)->amount > ((struct process *)b)->amount)
540                 return -1;
541         
542         if (((struct process *)a)->amount < ((struct process *)b)->amount)
543                 return 1;
544         
545         return 0;
546 }
547
548 int comparemem(const void * a, const void * b)
549 {
550         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
551                 return -1;
552         
553         if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
554                 return 1;
555         
556         return 0;
557 }
558
559 inline void proc_find_top(struct process **cpu, struct process **mem)
560 {
561         static int kd_init = 1;
562         struct kinfo_proc *p;
563         int n_processes;
564         int i, j = 0;
565         struct process *processes;
566         
567         if (kd_init) {
568                 kd_init = 0;
569                 if ((kd =
570                      kvm_open("/dev/null", "/dev/null", "/dev/null",
571                               O_RDONLY, "kvm_open")) == NULL) {
572                         (void) fprintf(stderr, "Cannot read kvm.");
573                 }
574         }
575
576         if (kd != NULL) {
577                 int total_pages;
578
579                 /* we get total pages count again to be sure it is up to date */
580                 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
581                         CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
582                 
583                 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
584                 processes = malloc(n_processes * sizeof(struct process));
585
586                 for (i = 0; i < n_processes; i++) {
587                         if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
588                                 processes[j].pid = p[i].ki_pid;
589                                 processes[j].name =  strdup(p[i].ki_comm);
590                                 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
591                                 processes[j].totalmem = (float)(p[i].ki_rssize / (float)total_pages) * 100.0;
592                                 j++;
593                         }
594                 }
595
596                 qsort(processes, j, sizeof(struct process), comparemem);
597                 for (i = 0; i < 10; mem[i] = &processes[i], i++);
598
599                 qsort(processes, j, sizeof(struct process), comparecpu);
600                 for (i = 0; i < 10; cpu[i] = &processes[i], i++);
601                 
602                 free(processes);
603         } else
604                 return;
605 }
606
607 #if defined(i386) || defined(__i386__)
608 #define APMDEV  "/dev/apm"
609 #define APM_UNKNOWN     255
610
611 int apm_getinfo(int fd, apm_info_t aip)
612 {
613         if (ioctl(fd, APMIO_GETINFO, aip) == -1)
614                 return -1;
615
616         return 0;
617 }
618
619 char *get_apm_adapter()
620 {
621         int fd;
622         struct apm_info info;
623
624         fd = open(APMDEV, O_RDONLY);
625         if(fd < 0) 
626                 return "ERR";
627
628         if(apm_getinfo(fd, &info) != 0 ) {
629                 close(fd);
630                 return "ERR";
631         }
632         close(fd);
633
634         switch(info.ai_acline) {
635                 case 0:
636                         return "off-line";
637                         break;
638                 case 1:
639                         if(info.ai_batt_stat == 3)
640                                 return "charging";
641                         else
642                                 return "on-line";
643                         break;
644                 default:
645                         return "unknown";
646                         break;
647         }
648 }
649
650 char *get_apm_battery_life()
651 {
652         int fd;
653         u_int batt_life;
654         struct apm_info info;
655         char *out;
656
657         out = (char *)calloc(16, sizeof(char));
658         
659
660         fd = open(APMDEV, O_RDONLY);
661         if(fd < 0) {
662                 strncpy(out, "ERR", 16);
663                 return out;
664         }
665
666         if(apm_getinfo(fd, &info) != 0 ) {
667                 close(fd);
668                 strncpy(out, "ERR", 16);
669                 return out;
670         }
671         close(fd);
672
673         batt_life = info.ai_batt_life;
674         if (batt_life == APM_UNKNOWN)
675                 strncpy(out, "unknown", 16);
676         else if (batt_life <= 100) {
677                 snprintf(out, 20,"%d%%", batt_life);
678                 return out;
679         }
680         else
681                 strncpy(out, "ERR", 16);
682
683         return out;
684 }
685
686 char *get_apm_battery_time()
687 {
688         int fd;
689         int batt_time;
690         int h, m, s;
691         struct apm_info info;
692         char *out;
693
694         out = (char *)calloc(16, sizeof(char));
695
696         fd = open(APMDEV, O_RDONLY);
697         if(fd < 0) {
698                 strncpy(out, "ERR", 16);
699                 return out;
700         }
701
702         if(apm_getinfo(fd, &info) != 0 ) {
703                 close(fd);
704                 strncpy(out, "ERR", 16);
705                 return out;
706         }
707         close(fd);
708
709         batt_time = info.ai_batt_time;
710
711         if (batt_time == -1)
712                 strncpy(out, "unknown", 16);
713         else {
714                 h = batt_time;
715                 s = h % 60;
716                 h /= 60;
717                 m = h % 60;
718                 h /= 60;
719                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
720         }
721
722         return out;
723 }
724 #endif