3 * Contains FreeBSD specific stuff
8 #include <sys/dkstat.h>
10 #include <sys/resource.h>
11 #include <sys/socket.h>
12 #include <sys/sysctl.h>
14 #include <sys/types.h>
15 #include <sys/vmmeter.h>
17 #include <sys/ioctl.h>
20 #include <net/if_mib.h>
21 #include <net/if_media.h>
22 #include <net/if_var.h>
23 #include <netinet/in.h>
34 #include <dev/wi/if_wavelan_ieee.h>
38 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof (var))
39 #define KELVTOC(x) ((x - 2732) / 10.0)
40 #define MAXSHOWDEVS 16
46 inline void proc_find_top(struct process **cpu, struct process **mem);
48 u_int64_t diskio_prev = 0;
49 static short cpu_setup = 0;
50 static short diskio_setup = 0;
52 static int getsysctl(char *name, void *ptr, size_t len)
55 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
66 struct ifmibdata *data = NULL;
69 static int swapmode(int *retavail, int *retfree)
72 int pagesize = getpagesize();
73 struct kvm_swap swapary[1];
78 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
80 n = kvm_getswapinfo(kd, swapary, 1, 0);
81 if (n < 0 || swapary[0].ksw_total == 0)
84 *retavail = CONVERT(swapary[0].ksw_total);
85 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
87 n = (int) ((double) swapary[0].ksw_used * 100.0 /
88 (double) swapary[0].ksw_total);
101 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
102 struct timeval boottime;
104 size_t size = sizeof (boottime);
106 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) &&
107 (boottime.tv_sec != 0)) {
109 info.uptime = now - boottime.tv_sec;
111 fprintf(stderr, "Could not get uptime\n");
119 int total_pages, inactive_pages, free_pages;
120 int swap_avail, swap_free;
122 int pagesize = getpagesize();
124 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
126 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
128 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
130 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
132 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
134 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
136 info.memmax = (total_pages * pagesize) >> 10;
138 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
141 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
142 info.swapmax = swap_avail;
143 info.swap = (swap_avail - swap_free);
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 struct ifaddrs *iftmp;
176 last_recv = ns->recv;
177 last_trans = ns->trans;
179 if (ifa->ifa_addr->sa_family != AF_LINK)
182 for (iftmp = ifa->ifa_next; iftmp != NULL &&
183 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
184 iftmp = iftmp->ifa_next)
185 if (iftmp->ifa_addr->sa_family == AF_INET)
186 memcpy(&(ns->addr), iftmp->ifa_addr,
187 iftmp->ifa_addr->sa_len);
189 ifd = (struct if_data *) ifa->ifa_data;
193 if (r < ns->last_read_recv)
195 ((long long) 4294967295U -
196 ns->last_read_recv) + r;
198 ns->recv += (r - ns->last_read_recv);
200 ns->last_read_recv = r;
202 if (t < ns->last_read_trans)
204 ((long long) 4294967295U -
205 ns->last_read_trans) + t;
207 ns->trans += (t - ns->last_read_trans);
209 ns->last_read_trans = t;
211 /* calculate speeds */
212 ns->recv_speed = (ns->recv - last_recv) / delta;
213 ns->trans_speed = (ns->trans - last_trans) / delta;
224 update_total_processes()
228 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
230 info.procs = n_processes;
234 update_running_processes()
236 struct kinfo_proc *p;
240 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
241 for (i = 0; i < n_processes; i++) {
243 if (p[i].kp_proc.p_stat == SRUN)
245 if (p[i].ki_stat == SRUN)
250 info.run_procs = cnt;
253 struct cpu_load_struct {
254 unsigned long load[5];
257 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
258 long cpu_used, oldtotal, oldused;
263 /* int cpu_count = 0; */
267 * FreeBSD doesn't allow to get per CPU load stats
268 * on SMP machines. It's possible to get a CPU count,
269 * but as we fulfil only info.cpu_usage[0], it's better
270 * to report there's only one CPU. It should fix some bugs
274 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
275 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 */
289 long cp_time[CPUSTATES];
290 size_t len = sizeof (cp_time);
292 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
293 if ((cpu_setup == 0) || (!info.cpu_usage)) {
298 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
299 (void) fprintf(stderr, "Cannot get kern.cp_time");
302 fresh.load[0] = cp_time[CP_USER];
303 fresh.load[1] = cp_time[CP_NICE];
304 fresh.load[2] = cp_time[CP_SYS];
305 fresh.load[3] = cp_time[CP_IDLE];
306 fresh.load[4] = cp_time[CP_IDLE];
308 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
310 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
312 if ((total - oldtotal) != 0) {
313 info.cpu_usage[0] = ((double) (used - oldused)) /
314 (double) (total - oldtotal);
316 info.cpu_usage[0] = 0;
324 get_i2c_info(int *fd, int arg, char *devtype, char *type)
330 update_load_average()
335 info.loadavg[0] = (float) v[0];
336 info.loadavg[1] = (float) v[1];
337 info.loadavg[2] = (float) v[2];
341 get_acpi_temperature(int fd)
345 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
347 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
351 return (KELVTOC(temp));
355 get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
357 int battime, batcapacity, batstate, ac;
358 char battery_status[64];
359 char battery_time[64];
361 if (GETSYSCTL("hw.acpi.battery.time", battime))
362 (void) fprintf(stderr,
363 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
364 if (GETSYSCTL("hw.acpi.battery.life", batcapacity))
365 (void) fprintf(stderr,
366 "Cannot read sysctl \"hw.acpi.battery.life\"\n");
368 if (GETSYSCTL("hw.acpi.battery.state", batstate))
369 (void) fprintf(stderr,
370 "Cannot read sysctl \"hw.acpi.battery.state\"\n");
372 if (GETSYSCTL("hw.acpi.acline", ac))
373 (void) fprintf(stderr,
374 "Cannot read sysctl \"hw.acpi.acline\"\n");
378 snprintf (battery_status, sizeof(battery_status)-1,
379 "remaining %d%%", batcapacity);
380 snprintf (battery_time, sizeof(battery_time)-1,
381 "%d:%2.2d", battime / 60, battime % 60);
383 snprintf(buf, n, "remaining %d%% (%d:%2.2d)",
384 batcapacity, battime / 60, battime % 60);
388 /* no time estimate available yet */
389 snprintf(battery_status, sizeof(battery_status)-1,
390 "remaining %d%%", batcapacity);
392 snprintf(buf, n, "remaining %d%%",
396 (void) fprintf(stderr, "Discharging while on AC!\n");
398 snprintf (battery_status, sizeof(battery_status)-1,
399 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
401 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
403 if (batstate != 2 && batstate != 0)
404 (void) fprintf(stderr, "Unknown battery state %d!\n", batstate);
406 (void) fprintf(stderr, "Charging while not on AC!\n");
412 snprintf(buf, n, "%s", battery_status);
417 snprintf(buf, n, "%s", battery_time);
427 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
434 open_acpi_temperature(const char *name)
440 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
444 if (!p_client_buffer || client_buffer_size <= 0)
447 if (GETSYSCTL("hw.acpi.acline", state)) {
449 "Cannot read sysctl \"hw.acpi.acline\"\n");
455 strncpy(p_client_buffer, "Running on AC Power",
458 strncpy(p_client_buffer, "Running on battery",
464 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
466 if (!p_client_buffer || client_buffer_size <= 0)
469 /* not implemented */
470 memset(p_client_buffer, 0, client_buffer_size);
474 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
476 if (!p_client_buffer || client_buffer_size <= 0)
479 /* not implemented */
480 memset(p_client_buffer, 0, client_buffer_size);
484 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
486 if (!p_client_buffer || client_buffer_size <= 0)
489 /* not implemented */
490 memset(p_client_buffer, 0, client_buffer_size);
493 /* rdtsc() and get_freq_dynamic() copied from linux.c */
495 #if defined(__i386) || defined(__x86_64)
496 __inline__ unsigned long long int
499 unsigned long long int x;
500 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
505 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
507 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
508 char *p_format, int divisor)
510 #if defined(__i386) || defined(__x86_64)
512 struct timeval tvstart, tvstop;
513 unsigned long long cycles[2]; /* gotta be 64 bit */
514 unsigned int microseconds; /* total time taken */
516 memset(&tz, 0, sizeof (tz));
518 /* get this function in cached memory */
519 gettimeofday(&tvstart, &tz);
521 gettimeofday(&tvstart, &tz);
523 /* we don't trust that this is any specific length of time */
526 gettimeofday(&tvstop, &tz);
527 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
528 (tvstop.tv_usec - tvstart.tv_usec);
530 snprintf(p_client_buffer, client_buffer_size, p_format,
531 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
533 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
539 get_freq(char *p_client_buffer, size_t client_buffer_size,
540 char *p_format, int divisor, unsigned int cpu)
545 freq_sysctl = (char *)calloc(16, sizeof(char));
546 if (freq_sysctl == NULL)
549 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
551 if (!p_client_buffer || client_buffer_size <= 0 ||
552 !p_format || divisor <= 0)
555 if (GETSYSCTL(freq_sysctl, freq) == 0)
556 snprintf(p_client_buffer, client_buffer_size,
557 p_format, (float)freq/divisor);
559 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
568 proc_find_top(info.cpu, info.memu);
574 struct ifreq ifr; /* interface stats */
576 struct net_stat * ns;
577 struct ifaddrs *ifap, *ifa;
578 struct ifmediareq ifmr;
584 if (getifaddrs(&ifap) < 0)
587 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
588 ns = get_net_stat((const char *) ifa->ifa_name);
590 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
593 bzero(&ifmr, sizeof(ifmr));
594 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
595 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
599 * We can monitor only wireless interfaces
600 * which not in hostap mode
602 if ((ifmr.ifm_active & IFM_IEEE80211) &&
603 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
605 bzero(&ifr, sizeof(ifr));
606 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
607 wireq.wi_type = WI_RID_COMMS_QUALITY;
608 wireq.wi_len = WI_MAX_DATALEN;
609 ifr.ifr_data = (void *) &wireq;
611 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
612 perror("ioctl (getting wi status)");
617 * wi_val[0] = quality
621 ns->linkstatus = (int) wireq.wi_val[1];
633 struct device_selection *dev_select = NULL;
634 long select_generation;
636 static struct statinfo statinfo_cur;
637 u_int64_t diskio_current = 0;
639 bzero(&statinfo_cur, sizeof (statinfo_cur));
640 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
641 bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
643 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
646 devs_count = statinfo_cur.dinfo->numdevs;
647 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
648 &select_generation, statinfo_cur.dinfo->generation,
649 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
650 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
651 for (dn = 0; dn < devs_count; ++dn) {
655 di = dev_select[dn].position;
656 dev = &statinfo_cur.dinfo->devices[di];
658 diskio_current += dev->bytes[DEVSTAT_READ] +
659 dev->bytes[DEVSTAT_WRITE];
666 * Since we return (diskio_total_current - diskio_total_old), first
667 * frame will be way too high (it will be equal to
668 * diskio_total_current, i.e. all disk I/O since boot). That's why
669 * it is better to return 0 first time;
671 if (diskio_setup == 0) {
675 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
677 diskio_prev = diskio_current;
679 free(statinfo_cur.dinfo);
683 * While topless is obviously better, top is also not bad.
687 comparecpu(const void *a, const void *b)
689 if (((struct process *)a)->amount > ((struct process *)b)->amount)
692 if (((struct process *)a)->amount < ((struct process *)b)->amount)
699 comparemem(const void *a, const void *b)
701 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
704 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
711 proc_find_top(struct process **cpu, struct process **mem)
713 struct kinfo_proc *p;
716 struct process *processes;
720 /* we get total pages count again to be sure it is up to date */
721 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
722 CRIT_ERR("Cannot read sysctl"
723 "\"vm.stats.vm.v_page_count\"");
725 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
726 processes = malloc(n_processes * sizeof (struct process));
728 for (i = 0; i < n_processes; i++) {
729 if (!((p[i].ki_flag & P_SYSTEM)) &&
730 p[i].ki_comm != NULL) {
731 processes[j].pid = p[i].ki_pid;
732 processes[j].name = strdup(p[i].ki_comm);
733 processes[j].amount = 100.0 *
734 p[i].ki_pctcpu / FSCALE;
735 processes[j].totalmem = (float)(p[i].ki_rssize /
736 (float)total_pages) * 100.0;
741 qsort(processes, j - 1, sizeof (struct process), comparemem);
742 for (i = 0; i < 10; i++) {
743 struct process *tmp, *ttmp;
745 tmp = malloc(sizeof (struct process));
746 tmp->pid = processes[i].pid;
747 tmp->amount = processes[i].amount;
748 tmp->totalmem = processes[i].totalmem;
749 tmp->name = strdup(processes[i].name);
759 qsort(processes, j - 1, sizeof (struct process), comparecpu);
760 for (i = 0; i < 10; i++) {
761 struct process *tmp, *ttmp;
763 tmp = malloc(sizeof (struct process));
764 tmp->pid = processes[i].pid;
765 tmp->amount = processes[i].amount;
766 tmp->totalmem = processes[i].totalmem;
767 tmp->name = strdup(processes[i].name);
777 #if defined(FREEBSD_DEBUG)
778 printf("=====\nmem\n");
779 for (i = 0; i < 10; i++) {
780 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
781 mem[i]->pid, mem[i]->totalmem);
785 for (i = 0; i < j; free(processes[i++].name));
789 #if defined(i386) || defined(__i386__)
790 #define APMDEV "/dev/apm"
791 #define APM_UNKNOWN 255
794 apm_getinfo(int fd, apm_info_t aip)
796 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
806 struct apm_info info;
808 fd = open(APMDEV, O_RDONLY);
812 if (apm_getinfo(fd, &info) != 0) {
818 switch (info.ai_acline) {
823 if (info.ai_batt_stat == 3)
835 *get_apm_battery_life()
839 struct apm_info info;
842 out = (char *)calloc(16, sizeof (char));
844 fd = open(APMDEV, O_RDONLY);
846 strncpy(out, "ERR", 16);
850 if (apm_getinfo(fd, &info) != 0) {
852 strncpy(out, "ERR", 16);
857 batt_life = info.ai_batt_life;
858 if (batt_life == APM_UNKNOWN)
859 strncpy(out, "unknown", 16);
860 else if (batt_life <= 100) {
861 snprintf(out, 16, "%d%%", batt_life);
864 strncpy(out, "ERR", 16);
870 *get_apm_battery_time()
875 struct apm_info info;
878 out = (char *)calloc(16, sizeof (char));
880 fd = open(APMDEV, O_RDONLY);
882 strncpy(out, "ERR", 16);
886 if (apm_getinfo(fd, &info) != 0) {
888 strncpy(out, "ERR", 16);
893 batt_time = info.ai_batt_time;
896 strncpy(out, "unknown", 16);
903 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
911 void update_entropy (void)
913 /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
916 /* empty stub so conky links */
918 free_all_processes(void)