- Fix `make dist` without having HAVE_DOCSTUFF defined
[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 u_int64_t diskio_prev = 0;
36 static short cpu_setup = 0;
37
38 static int getsysctl(char *name, void *ptr, size_t len)
39 {
40         size_t nlen = len;
41         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
42                 return -1;
43         }
44
45         if (nlen != len) {
46                 return -1;
47         }
48
49         return 0;
50 }
51
52 static kvm_t *kd = NULL;
53 struct ifmibdata *data = NULL;
54 size_t len = 0;
55
56 static int swapmode(int *retavail, int *retfree)
57 {
58         int n;
59         int pagesize = getpagesize();
60         struct kvm_swap swapary[1];
61         static int kd_init = 1;
62
63         if (kd_init) {
64                 kd_init = 0;
65                 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
66                                    O_RDONLY, "kvm_open")) == NULL) {
67                         (void) fprintf(stderr, "Cannot read kvm.");
68                         return -1;
69                 }
70         }
71
72         if (kd == NULL) {
73                 return -1;
74         }
75
76         *retavail = 0;
77         *retfree = 0;
78
79 #define CONVERT(v)      ((quad_t)(v) * pagesize / 1024)
80
81         n = kvm_getswapinfo(kd, swapary, 1, 0);
82         if (n < 0 || swapary[0].ksw_total == 0)
83                 return (0);
84
85         *retavail = CONVERT(swapary[0].ksw_total);
86         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
87
88         n = (int) ((double) swapary[0].ksw_used * 100.0 /
89                    (double) swapary[0].ksw_total);
90
91         return n;
92 }
93
94 void prepare_update()
95 {
96 }
97
98 void update_uptime()
99 {
100         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
101         struct timeval boottime;
102         time_t now;
103         size_t size = sizeof(boottime);
104
105         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
106             && (boottime.tv_sec != 0)) {
107                 time(&now);
108                 info.uptime = now - boottime.tv_sec;
109         } else {
110                 (void) fprintf(stderr, "Could not get uptime\n");
111                 info.uptime = 0;
112         }
113 }
114
115 void update_meminfo()
116 {
117         int total_pages, inactive_pages, free_pages;
118         int swap_avail, swap_free;
119
120         int pagesize = getpagesize();
121
122         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
123                 (void) fprintf(stderr,
124                                "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
125
126         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
127                 (void) fprintf(stderr,
128                                "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
129
130         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
131                 (void) fprintf(stderr,
132                                "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
133
134         info.memmax = (total_pages * pagesize) >> 10;
135         info.mem =
136             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
137
138
139         if ((swapmode(&swap_avail, &swap_free)) >= 0) {
140                 info.swapmax = swap_avail;
141                 info.swap = (swap_avail - swap_free);
142         } else {
143                 info.swapmax = 0;
144                 info.swap = 0;
145         }
146 }
147
148 void update_net_stats()
149 {
150         struct net_stat *ns;
151         double delta;
152         long long r, t, last_recv, last_trans;
153         struct ifaddrs *ifap, *ifa;
154         struct if_data *ifd;
155
156
157         /* get delta */
158         delta = current_update_time - last_update_time;
159         if (delta <= 0.0001)
160                 return;
161
162         if (getifaddrs(&ifap) < 0)
163                 return;
164
165         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
166                 ns = get_net_stat((const char *) ifa->ifa_name);
167
168                 if (ifa->ifa_flags & IFF_UP) {
169                         last_recv = ns->recv;
170                         last_trans = ns->trans;
171
172                         if (ifa->ifa_addr->sa_family != AF_LINK)
173                                 continue;
174
175                         ifd = (struct if_data *) ifa->ifa_data;
176                         r = ifd->ifi_ibytes;
177                         t = ifd->ifi_obytes;
178
179                         if (r < ns->last_read_recv)
180                                 ns->recv +=
181                                     ((long long) 4294967295U -
182                                      ns->last_read_recv) + r;
183                         else
184                                 ns->recv += (r - ns->last_read_recv);
185
186                         ns->last_read_recv = r;
187
188                         if (t < ns->last_read_trans)
189                                 ns->trans +=
190                                     ((long long) 4294967295U -
191                                      ns->last_read_trans) + t;
192                         else
193                                 ns->trans += (t - ns->last_read_trans);
194
195                         ns->last_read_trans = t;
196
197
198                         /* calculate speeds */
199                         ns->recv_speed = (ns->recv - last_recv) / delta;
200                         ns->trans_speed = (ns->trans - last_trans) / delta;
201                 }
202         }
203
204         freeifaddrs(ifap);
205 }
206
207 void update_total_processes()
208 {
209         int n_processes;
210         static int kd_init = 1;
211
212         if (kd_init) {
213                 kd_init = 0;
214                 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
215                                    O_RDONLY, "kvm_open")) == NULL) {
216                         (void) fprintf(stderr, "Cannot read kvm.");
217                         return;
218                 }
219         }
220
221
222         if (kd != NULL)
223                 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
224         else
225                 return;
226
227         info.procs = n_processes;
228 }
229
230 void update_running_processes()
231 {
232         static int kd_init = 1;
233         struct kinfo_proc *p;
234         int n_processes;
235         int i, cnt = 0;
236
237         if (kd_init) {
238                 kd_init = 0;
239                 if ((kd =
240                      kvm_open("/dev/null", "/dev/null", "/dev/null",
241                               O_RDONLY, "kvm_open")) == NULL) {
242                         (void) fprintf(stderr, "Cannot read kvm.");
243                 }
244         }
245
246         if (kd != NULL) {
247                 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
248                 for (i = 0; i < n_processes; i++) {
249 #if __FreeBSD__ < 5
250                         if (p[i].kp_proc.p_stat == SRUN)
251 #else
252                         if (p[i].ki_stat == SRUN)
253 #endif
254                                 cnt++;
255                 }
256         } else
257                 return;
258
259         info.run_procs = cnt;
260 }
261
262 struct cpu_load_struct {
263         unsigned long load[5];
264 };
265
266 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
267 long cpu_used, oldtotal, oldused;
268
269 void get_cpu_count()
270 {
271         int cpu_count = 0;      
272
273         if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
274                 info.cpu_count = cpu_count;
275
276         info.cpu_usage = malloc(info.cpu_count * sizeof(float));
277         if (info.cpu_usage == NULL)
278                 CRIT_ERR("malloc");
279 }
280
281 void update_cpu_usage()
282 {
283         long used, total;
284         long cp_time[CPUSTATES];
285         size_t len = sizeof(cp_time);
286         
287         if (cpu_setup == 0) {
288                 get_cpu_count();
289                 cpu_setup = 1;
290         }
291         
292         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
293                 (void) fprintf(stderr, "Cannot get kern.cp_time");
294         }
295
296         fresh.load[0] = cp_time[CP_USER];
297         fresh.load[1] = cp_time[CP_NICE];
298         fresh.load[2] = cp_time[CP_SYS];
299         fresh.load[3] = cp_time[CP_IDLE];
300         fresh.load[4] = cp_time[CP_IDLE];
301
302         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
303         total =
304             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
305
306         if ((total - oldtotal) != 0) {
307                 info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal);
308         } else {
309                 info.cpu_usage[0] = 0;
310         }
311
312         oldused = used;
313         oldtotal = total;
314 }
315
316 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
317 {
318         return 0;
319 }
320
321 void update_load_average()
322 {
323         double v[3];
324         getloadavg(v, 3);
325
326         info.loadavg[0] = (float) v[0];
327         info.loadavg[1] = (float) v[1];
328         info.loadavg[2] = (float) v[2];
329 }
330
331 double get_acpi_temperature(int fd)
332 {
333         int temp;
334
335         if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
336                 (void) fprintf(stderr,
337                                "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
338                 return 0.0;
339         }
340
341         return KELVTOC(temp);
342 }
343
344 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
345 {
346         int battime;
347
348         if (GETSYSCTL("hw.acpi.battery.time", battime))
349                 (void) fprintf(stderr,
350                                "Cannot read sysctl \"hw.acpi.battery.time\"\n");
351
352         if (battime != -1)
353                 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
354                          battime / 60, battime % 60);
355         else
356                 snprintf(buf, n, "Battery is charging");
357 }
358
359 int
360 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
361                 char *devtype)
362 {
363         return 0;
364 }
365
366 int open_acpi_temperature(const char *name)
367 {
368         return 0;
369 }
370
371 char *get_acpi_ac_adapter(void)
372 {
373         int state;
374         char *acstate = (char *) malloc(100);
375
376         if (GETSYSCTL("hw.acpi.acline", state)) {
377                 (void) fprintf(stderr,
378                                "Cannot read sysctl \"hw.acpi.acline\"\n");
379                 return "n\\a";
380         }
381
382
383         if (state)
384                 strcpy(acstate, "Running on AC Power");
385         else
386                 strcpy(acstate, "Running on battery");
387
388         return acstate;
389 }
390
391 char *get_acpi_fan()
392 {
393         return "";
394 }
395
396 char *get_adt746x_cpu()
397 {
398         return "";
399 }
400
401 char *get_adt746x_fan()
402 {
403         return "";
404 }
405
406 /* rdtsc() and get_freq_dynamic() copied from linux.c */
407
408 #if  defined(__i386) || defined(__x86_64)
409 __inline__ unsigned long long int rdtsc()
410 {
411         unsigned long long int x;
412         __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
413         return x;
414 }
415 #endif
416
417 float get_freq_dynamic()
418 {
419 #if  defined(__i386) || defined(__x86_64)
420         struct timezone tz;
421         struct timeval tvstart, tvstop;
422         unsigned long long cycles[2];   /* gotta be 64 bit */
423         unsigned int microseconds;      /* total time taken */
424
425         memset(&tz, 0, sizeof(tz));
426
427         /* get this function in cached memory */
428         gettimeofday(&tvstart, &tz);
429         cycles[0] = rdtsc();
430         gettimeofday(&tvstart, &tz);
431          
432         /* we don't trust that this is any specific length of time */
433         usleep(100);
434         cycles[1] = rdtsc();
435         gettimeofday(&tvstop, &tz);
436         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
437             (tvstop.tv_usec - tvstart.tv_usec);
438                              
439         return (cycles[1] - cycles[0]) / microseconds;
440 #else
441         return get_freq();
442 #endif
443 }
444
445 float get_freq()
446 {
447         int freq;
448         
449         if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
450                 return (float)freq;
451         else
452                 return (float)0;
453 }
454
455 void update_top()
456 {
457         /* XXX */
458 }
459
460 void update_wifi_stats()
461 {
462         /* XXX */
463 }
464 void update_diskio()
465 {
466         int devs_count,
467             num_selected,
468             num_selections;
469         struct device_selection *dev_select = NULL;
470         long select_generation;
471         int dn;
472         static struct statinfo  statinfo_cur;
473         u_int64_t diskio_current = 0;
474
475         bzero(&statinfo_cur, sizeof(statinfo_cur));
476         statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
477         bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
478         
479         if (devstat_getdevs(NULL, &statinfo_cur) < 0)
480                 return;
481
482         devs_count = statinfo_cur.dinfo->numdevs;
483         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
484                         &select_generation, statinfo_cur.dinfo->generation,
485                         statinfo_cur.dinfo->devices, devs_count, NULL, 0, 
486                         NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
487                 for (dn = 0; dn < devs_count; ++dn) {
488                         int di;
489                         struct devstat  *dev;
490
491                         di = dev_select[dn].position;
492                         dev = &statinfo_cur.dinfo->devices[di];
493
494                         diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
495                 }
496                 
497                 free(dev_select);
498         }
499
500         diskio_value = (unsigned int)((diskio_current - diskio_prev)/1024);
501         diskio_prev = diskio_current;
502 }