2 * Contains FreeBSD specific stuff
13 #include <sys/param.h>
14 #include <sys/types.h>
16 #include <sys/resource.h>
17 #include <sys/sysctl.h>
18 #include <sys/vmmeter.h>
19 #include <sys/dkstat.h>
22 #include <sys/socket.h>
24 #include <net/if_mib.h>
25 #include <sys/socket.h>
31 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
32 #define KELVTOC(x) ((x - 2732) / 10.0)
33 #define MAXSHOWDEVS 16
35 inline void proc_find_top(struct process **cpu, struct process **mem);
37 u_int64_t diskio_prev = 0;
38 static short cpu_setup = 0;
39 static short diskio_setup = 0;
41 static int getsysctl(char *name, void *ptr, size_t len)
44 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
55 static kvm_t *kd = NULL;
56 struct ifmibdata *data = NULL;
59 static int swapmode(int *retavail, int *retfree)
62 int pagesize = getpagesize();
63 struct kvm_swap swapary[1];
64 static int kd_init = 1;
68 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
69 O_RDONLY, "kvm_open")) == NULL) {
70 (void) fprintf(stderr, "Cannot read kvm.");
82 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
84 n = kvm_getswapinfo(kd, swapary, 1, 0);
85 if (n < 0 || swapary[0].ksw_total == 0)
88 *retavail = CONVERT(swapary[0].ksw_total);
89 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
91 n = (int) ((double) swapary[0].ksw_used * 100.0 /
92 (double) swapary[0].ksw_total);
103 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
104 struct timeval boottime;
106 size_t size = sizeof(boottime);
108 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
109 && (boottime.tv_sec != 0)) {
111 info.uptime = now - boottime.tv_sec;
113 (void) fprintf(stderr, "Could not get uptime\n");
118 void update_meminfo()
120 int total_pages, inactive_pages, free_pages;
121 int swap_avail, swap_free;
123 int pagesize = getpagesize();
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\"");
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\"");
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\"");
137 info.memmax = (total_pages * pagesize) >> 10;
139 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
142 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
143 info.swapmax = swap_avail;
144 info.swap = (swap_avail - swap_free);
151 void update_net_stats()
155 long long r, t, last_recv, last_trans;
156 struct ifaddrs *ifap, *ifa;
161 delta = current_update_time - last_update_time;
165 if (getifaddrs(&ifap) < 0)
168 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
169 ns = get_net_stat((const char *) ifa->ifa_name);
171 if (ifa->ifa_flags & IFF_UP) {
172 last_recv = ns->recv;
173 last_trans = ns->trans;
175 if (ifa->ifa_addr->sa_family != AF_LINK)
178 ifd = (struct if_data *) ifa->ifa_data;
182 if (r < ns->last_read_recv)
184 ((long long) 4294967295U -
185 ns->last_read_recv) + r;
187 ns->recv += (r - ns->last_read_recv);
189 ns->last_read_recv = r;
191 if (t < ns->last_read_trans)
193 ((long long) 4294967295U -
194 ns->last_read_trans) + t;
196 ns->trans += (t - ns->last_read_trans);
198 ns->last_read_trans = t;
201 /* calculate speeds */
202 ns->recv_speed = (ns->recv - last_recv) / delta;
203 ns->trans_speed = (ns->trans - last_trans) / delta;
210 void update_total_processes()
213 static int kd_init = 1;
217 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
218 O_RDONLY, "kvm_open")) == NULL) {
219 (void) fprintf(stderr, "Cannot read kvm.");
226 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
230 info.procs = n_processes;
233 void update_running_processes()
235 static int kd_init = 1;
236 struct kinfo_proc *p;
243 kvm_open("/dev/null", "/dev/null", "/dev/null",
244 O_RDONLY, "kvm_open")) == NULL) {
245 (void) fprintf(stderr, "Cannot read kvm.");
250 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
251 for (i = 0; i < n_processes; i++) {
253 if (p[i].kp_proc.p_stat == SRUN)
255 if (p[i].ki_stat == SRUN)
262 info.run_procs = cnt;
265 struct cpu_load_struct {
266 unsigned long load[5];
269 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
270 long cpu_used, oldtotal, oldused;
277 * FreeBSD doesn't allow to get per CPU load stats
278 * on SMP machines. It's possible to get a CPU count,
279 * but as we fulfil only info.cpu_usage[0], it's better
280 * to report there's only one CPU. It should fix some bugs
284 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
285 info.cpu_count = cpu_count;
289 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
290 if (info.cpu_usage == NULL)
294 /* XXX: SMP support */
295 void update_cpu_usage()
298 long cp_time[CPUSTATES];
299 size_t len = sizeof(cp_time);
301 if (cpu_setup == 0) {
306 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
307 (void) fprintf(stderr, "Cannot get kern.cp_time");
310 fresh.load[0] = cp_time[CP_USER];
311 fresh.load[1] = cp_time[CP_NICE];
312 fresh.load[2] = cp_time[CP_SYS];
313 fresh.load[3] = cp_time[CP_IDLE];
314 fresh.load[4] = cp_time[CP_IDLE];
316 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
318 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
320 if ((total - oldtotal) != 0) {
321 info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal);
323 info.cpu_usage[0] = 0;
330 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
335 void update_load_average()
340 info.loadavg[0] = (float) v[0];
341 info.loadavg[1] = (float) v[1];
342 info.loadavg[2] = (float) v[2];
345 double get_acpi_temperature(int fd)
349 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
350 (void) fprintf(stderr,
351 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
355 return KELVTOC(temp);
358 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
362 if (GETSYSCTL("hw.acpi.battery.time", battime))
363 (void) fprintf(stderr,
364 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
367 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
368 battime / 60, battime % 60);
370 snprintf(buf, n, "Battery is charging");
374 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
380 int open_acpi_temperature(const char *name)
385 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
389 if ( !p_client_buffer || client_buffer_size <= 0 )
392 if (GETSYSCTL("hw.acpi.acline", state)) {
393 (void) fprintf(stderr,
394 "Cannot read sysctl \"hw.acpi.acline\"\n");
400 strncpy( p_client_buffer, "Running on AC Power", client_buffer_size );
402 strncpy( p_client_buffer, "Running on battery", client_buffer_size );
407 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
409 if ( !p_client_buffer || client_buffer_size <= 0 )
412 /* not implemented */
413 memset(p_client_buffer,0,client_buffer_size);
418 void get_adt746x_cpu( char * p_client_buffer, size_t client_buffer_size )
420 if ( !p_client_buffer || client_buffer_size <= 0 )
423 /* not implemented */
424 memset(p_client_buffer,0,client_buffer_size);
429 void get_adt746x_fan( char * p_client_buffer, size_t client_buffer_size )
431 if ( !p_client_buffer || client_buffer_size <= 0 )
434 /* not implemented */
435 memset(p_client_buffer,0,client_buffer_size);
440 /* rdtsc() and get_freq_dynamic() copied from linux.c */
442 #if defined(__i386) || defined(__x86_64)
443 __inline__ unsigned long long int rdtsc()
445 unsigned long long int x;
446 __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
451 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
452 void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
454 #if defined(__i386) || defined(__x86_64)
456 struct timeval tvstart, tvstop;
457 unsigned long long cycles[2]; /* gotta be 64 bit */
458 unsigned int microseconds; /* total time taken */
460 memset(&tz, 0, sizeof(tz));
462 /* get this function in cached memory */
463 gettimeofday(&tvstart, &tz);
465 gettimeofday(&tvstart, &tz);
467 /* we don't trust that this is any specific length of time */
470 gettimeofday(&tvstop, &tz);
471 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
472 (tvstop.tv_usec - tvstart.tv_usec);
474 snprintf( p_client_buffer, client_buffer_size, p_format, (float)((cycles[1] - cycles[0]) / microseconds) / divisor );
477 get_freq( p_client_buffer, client_buffer_size, p_format, divisor );
482 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
483 void get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
487 if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
490 if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
492 snprintf( p_client_buffer, client_buffer_size, p_format, freq/divisor );
496 snprintf( p_client_buffer, client_buffer_size, p_format, (float)0 );
504 proc_find_top(info.cpu, info.memu);
507 void update_wifi_stats()
516 struct device_selection *dev_select = NULL;
517 long select_generation;
519 static struct statinfo statinfo_cur;
520 u_int64_t diskio_current = 0;
522 bzero(&statinfo_cur, sizeof(statinfo_cur));
523 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
524 bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
526 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
529 devs_count = statinfo_cur.dinfo->numdevs;
530 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
531 &select_generation, statinfo_cur.dinfo->generation,
532 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
533 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
534 for (dn = 0; dn < devs_count; ++dn) {
538 di = dev_select[dn].position;
539 dev = &statinfo_cur.dinfo->devices[di];
541 diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
548 * Since we return (diskio_total_current - diskio_total_old), first
549 * frame will be way too high (it will be equal to diskio_total_current, i.e.
550 * all disk I/O since boot). That's why it is better to return 0 first time;
552 if (diskio_setup == 0) {
556 diskio_value = (unsigned int)((diskio_current - diskio_prev)/1024);
557 diskio_prev = diskio_current;
559 free(statinfo_cur.dinfo);
563 * While topless is obviously better, top is also not bad.
566 int comparecpu(const void * a, const void * b)
568 if (((struct process *)a)->amount > ((struct process *)b)->amount)
571 if (((struct process *)a)->amount < ((struct process *)b)->amount)
577 int comparemem(const void * a, const void * b)
579 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
582 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
588 inline void proc_find_top(struct process **cpu, struct process **mem)
590 static int kd_init = 1;
591 struct kinfo_proc *p;
594 struct process *processes;
599 kvm_open("/dev/null", "/dev/null", "/dev/null",
600 O_RDONLY, "kvm_open")) == NULL) {
601 (void) fprintf(stderr, "Cannot read kvm.");
608 /* we get total pages count again to be sure it is up to date */
609 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
610 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
612 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
613 processes = malloc(n_processes * sizeof(struct process));
615 for (i = 0; i < n_processes; i++) {
616 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
617 processes[j].pid = p[i].ki_pid;
618 processes[j].name = strdup(p[i].ki_comm);
619 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
620 processes[j].totalmem = (float)(p[i].ki_rssize / (float)total_pages) * 100.0;
625 qsort(processes, j, sizeof(struct process), comparemem);
626 for (i = 0; i < 10; mem[i] = &processes[i], i++);
628 qsort(processes, j, sizeof(struct process), comparecpu);
629 for (i = 0; i < 10; cpu[i] = &processes[i], i++);
636 #if defined(i386) || defined(__i386__)
637 #define APMDEV "/dev/apm"
638 #define APM_UNKNOWN 255
640 int apm_getinfo(int fd, apm_info_t aip)
642 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
648 char *get_apm_adapter()
651 struct apm_info info;
653 fd = open(APMDEV, O_RDONLY);
657 if(apm_getinfo(fd, &info) != 0 ) {
663 switch(info.ai_acline) {
668 if(info.ai_batt_stat == 3)
679 char *get_apm_battery_life()
683 struct apm_info info;
686 out = (char *)calloc(16, sizeof(char));
689 fd = open(APMDEV, O_RDONLY);
691 strncpy(out, "ERR", 16);
695 if(apm_getinfo(fd, &info) != 0 ) {
697 strncpy(out, "ERR", 16);
702 batt_life = info.ai_batt_life;
703 if (batt_life == APM_UNKNOWN)
704 strncpy(out, "unknown", 16);
705 else if (batt_life <= 100) {
706 snprintf(out, 20,"%d%%", batt_life);
710 strncpy(out, "ERR", 16);
715 char *get_apm_battery_time()
720 struct apm_info info;
723 out = (char *)calloc(16, sizeof(char));
725 fd = open(APMDEV, O_RDONLY);
727 strncpy(out, "ERR", 16);
731 if(apm_getinfo(fd, &info) != 0 ) {
733 strncpy(out, "ERR", 16);
738 batt_time = info.ai_batt_time;
741 strncpy(out, "unknown", 16);
748 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
756 /* empty stub so conky links */
757 void free_all_processes(void)