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;
276 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
277 info.cpu_count = cpu_count;
279 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
280 if (info.cpu_usage == NULL)
284 /* XXX: SMP support */
285 void update_cpu_usage()
288 long cp_time[CPUSTATES];
289 size_t len = sizeof(cp_time);
291 if (cpu_setup == 0) {
296 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
297 (void) fprintf(stderr, "Cannot get kern.cp_time");
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];
306 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
308 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
310 if ((total - oldtotal) != 0) {
311 info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal);
313 info.cpu_usage[0] = 0;
320 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
325 void update_load_average()
330 info.loadavg[0] = (float) v[0];
331 info.loadavg[1] = (float) v[1];
332 info.loadavg[2] = (float) v[2];
335 double get_acpi_temperature(int fd)
339 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
340 (void) fprintf(stderr,
341 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
345 return KELVTOC(temp);
348 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
352 if (GETSYSCTL("hw.acpi.battery.time", battime))
353 (void) fprintf(stderr,
354 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
357 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
358 battime / 60, battime % 60);
360 snprintf(buf, n, "Battery is charging");
364 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
370 int open_acpi_temperature(const char *name)
375 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
379 if ( !p_client_buffer || client_buffer_size <= 0 )
382 if (GETSYSCTL("hw.acpi.acline", state)) {
383 (void) fprintf(stderr,
384 "Cannot read sysctl \"hw.acpi.acline\"\n");
390 strncpy( p_client_buffer, "Running on AC Power", client_buffer_size );
392 strncpy( p_client_buffer, "Running on battery", client_buffer_size );
397 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
399 if ( !p_client_buffer || client_buffer_size <= 0 )
402 /* not implemented */
403 memset(p_client_buffer,0,client_buffer_size);
408 void get_adt746x_cpu( char * p_client_buffer, size_t client_buffer_size )
410 if ( !p_client_buffer || client_buffer_size <= 0 )
413 /* not implemented */
414 memset(p_client_buffer,0,client_buffer_size);
419 void get_adt746x_fan( char * p_client_buffer, size_t client_buffer_size )
421 if ( !p_client_buffer || client_buffer_size <= 0 )
424 /* not implemented */
425 memset(p_client_buffer,0,client_buffer_size);
430 /* rdtsc() and get_freq_dynamic() copied from linux.c */
432 #if defined(__i386) || defined(__x86_64)
433 __inline__ unsigned long long int rdtsc()
435 unsigned long long int x;
436 __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
441 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
442 void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
444 #if defined(__i386) || defined(__x86_64)
446 struct timeval tvstart, tvstop;
447 unsigned long long cycles[2]; /* gotta be 64 bit */
448 unsigned int microseconds; /* total time taken */
450 memset(&tz, 0, sizeof(tz));
452 /* get this function in cached memory */
453 gettimeofday(&tvstart, &tz);
455 gettimeofday(&tvstart, &tz);
457 /* we don't trust that this is any specific length of time */
460 gettimeofday(&tvstop, &tz);
461 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
462 (tvstop.tv_usec - tvstart.tv_usec);
464 snprintf( p_client_buffer, client_buffer_size, p_format, (float)((cycles[1] - cycles[0]) / microseconds) / divisor );
467 get_freq( p_client_buffer, client_buffer_size, p_format, divisor );
472 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
473 void get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
477 if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
480 if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
482 snprintf( p_client_buffer, client_buffer_size, p_format, freq/divisor );
486 snprintf( p_client_buffer, client_buffer_size, p_format, (float)0 );
494 proc_find_top(info.cpu, info.memu);
497 void update_wifi_stats()
506 struct device_selection *dev_select = NULL;
507 long select_generation;
509 static struct statinfo statinfo_cur;
510 u_int64_t diskio_current = 0;
512 bzero(&statinfo_cur, sizeof(statinfo_cur));
513 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
514 bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
516 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
519 devs_count = statinfo_cur.dinfo->numdevs;
520 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
521 &select_generation, statinfo_cur.dinfo->generation,
522 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
523 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
524 for (dn = 0; dn < devs_count; ++dn) {
528 di = dev_select[dn].position;
529 dev = &statinfo_cur.dinfo->devices[di];
531 diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
538 * Since we return (diskio_total_current - diskio_total_old), first
539 * frame will be way too high (it will be equal to diskio_total_current, i.e.
540 * all disk I/O since boot). That's why it is better to return 0 first time;
542 if (diskio_setup == 0) {
546 diskio_value = (unsigned int)((diskio_current - diskio_prev)/1024);
547 diskio_prev = diskio_current;
549 free(statinfo_cur.dinfo);
553 * While topless is obviously better, top is also not bad.
556 int comparecpu(const void * a, const void * b)
558 if (((struct process *)a)->amount > ((struct process *)b)->amount)
561 if (((struct process *)a)->amount < ((struct process *)b)->amount)
567 int comparemem(const void * a, const void * b)
569 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
572 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
578 inline void proc_find_top(struct process **cpu, struct process **mem)
580 static int kd_init = 1;
581 struct kinfo_proc *p;
584 struct process *processes;
589 kvm_open("/dev/null", "/dev/null", "/dev/null",
590 O_RDONLY, "kvm_open")) == NULL) {
591 (void) fprintf(stderr, "Cannot read kvm.");
598 /* we get total pages count again to be sure it is up to date */
599 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
600 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
602 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
603 processes = malloc(n_processes * sizeof(struct process));
605 for (i = 0; i < n_processes; i++) {
606 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
607 processes[j].pid = p[i].ki_pid;
608 processes[j].name = strdup(p[i].ki_comm);
609 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
610 processes[j].totalmem = (float)(p[i].ki_rssize / (float)total_pages) * 100.0;
615 qsort(processes, j, sizeof(struct process), comparemem);
616 for (i = 0; i < 10; mem[i] = &processes[i], i++);
618 qsort(processes, j, sizeof(struct process), comparecpu);
619 for (i = 0; i < 10; cpu[i] = &processes[i], i++);
626 #if defined(i386) || defined(__i386__)
627 #define APMDEV "/dev/apm"
628 #define APM_UNKNOWN 255
630 int apm_getinfo(int fd, apm_info_t aip)
632 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
638 char *get_apm_adapter()
641 struct apm_info info;
643 fd = open(APMDEV, O_RDONLY);
647 if(apm_getinfo(fd, &info) != 0 ) {
653 switch(info.ai_acline) {
658 if(info.ai_batt_stat == 3)
669 char *get_apm_battery_life()
673 struct apm_info info;
676 out = (char *)calloc(16, sizeof(char));
679 fd = open(APMDEV, O_RDONLY);
681 strncpy(out, "ERR", 16);
685 if(apm_getinfo(fd, &info) != 0 ) {
687 strncpy(out, "ERR", 16);
692 batt_life = info.ai_batt_life;
693 if (batt_life == APM_UNKNOWN)
694 strncpy(out, "unknown", 16);
695 else if (batt_life <= 100) {
696 snprintf(out, 20,"%d%%", batt_life);
700 strncpy(out, "ERR", 16);
705 char *get_apm_battery_time()
710 struct apm_info info;
713 out = (char *)calloc(16, sizeof(char));
715 fd = open(APMDEV, O_RDONLY);
717 strncpy(out, "ERR", 16);
721 if(apm_getinfo(fd, &info) != 0 ) {
723 strncpy(out, "ERR", 16);
728 batt_time = info.ai_batt_time;
731 strncpy(out, "unknown", 16);
738 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
744 /* empty stub so conky links */
745 void free_all_processes( void )