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");
116 int check_mount(char *s)
118 struct statfs *mntbuf;
121 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
122 for (i = mntsize - 1; i >= 0; i--)
123 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
132 int total_pages, inactive_pages, free_pages;
133 int swap_avail, swap_free;
135 int pagesize = getpagesize();
137 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
139 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
141 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
143 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
145 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
147 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
149 info.memmax = (total_pages * pagesize) >> 10;
151 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
154 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
155 info.swapmax = swap_avail;
156 info.swap = (swap_avail - swap_free);
168 long long r, t, last_recv, last_trans;
169 struct ifaddrs *ifap, *ifa;
174 delta = current_update_time - last_update_time;
178 if (getifaddrs(&ifap) < 0)
181 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
182 ns = get_net_stat((const char *) ifa->ifa_name);
184 if (ifa->ifa_flags & IFF_UP) {
185 struct ifaddrs *iftmp;
189 last_recv = ns->recv;
190 last_trans = ns->trans;
192 if (ifa->ifa_addr->sa_family != AF_LINK)
195 for (iftmp = ifa->ifa_next; iftmp != NULL &&
196 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
197 iftmp = iftmp->ifa_next)
198 if (iftmp->ifa_addr->sa_family == AF_INET)
199 memcpy(&(ns->addr), iftmp->ifa_addr,
200 iftmp->ifa_addr->sa_len);
202 ifd = (struct if_data *) ifa->ifa_data;
206 if (r < ns->last_read_recv)
208 ((long long) 4294967295U -
209 ns->last_read_recv) + r;
211 ns->recv += (r - ns->last_read_recv);
213 ns->last_read_recv = r;
215 if (t < ns->last_read_trans)
217 ((long long) 4294967295U -
218 ns->last_read_trans) + t;
220 ns->trans += (t - ns->last_read_trans);
222 ns->last_read_trans = t;
224 /* calculate speeds */
225 ns->recv_speed = (ns->recv - last_recv) / delta;
226 ns->trans_speed = (ns->trans - last_trans) / delta;
237 update_total_processes()
241 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
243 info.procs = n_processes;
247 update_running_processes()
249 struct kinfo_proc *p;
253 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
254 for (i = 0; i < n_processes; i++) {
256 if (p[i].kp_proc.p_stat == SRUN)
258 if (p[i].ki_stat == SRUN)
263 info.run_procs = cnt;
266 struct cpu_load_struct {
267 unsigned long load[5];
270 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
271 long cpu_used, oldtotal, oldused;
276 /* int cpu_count = 0; */
280 * FreeBSD doesn't allow to get per CPU load stats
281 * on SMP machines. It's possible to get a CPU count,
282 * but as we fulfil only info.cpu_usage[0], it's better
283 * to report there's only one CPU. It should fix some bugs
287 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
288 info.cpu_count = cpu_count;
292 info.cpu_usage = malloc(info.cpu_count * sizeof (float));
293 if (info.cpu_usage == NULL)
297 /* XXX: SMP support */
302 long cp_time[CPUSTATES];
303 size_t len = sizeof (cp_time);
305 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
306 if ((cpu_setup == 0) || (!info.cpu_usage)) {
311 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
312 (void) fprintf(stderr, "Cannot get kern.cp_time");
315 fresh.load[0] = cp_time[CP_USER];
316 fresh.load[1] = cp_time[CP_NICE];
317 fresh.load[2] = cp_time[CP_SYS];
318 fresh.load[3] = cp_time[CP_IDLE];
319 fresh.load[4] = cp_time[CP_IDLE];
321 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
323 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
325 if ((total - oldtotal) != 0) {
326 info.cpu_usage[0] = ((double) (used - oldused)) /
327 (double) (total - oldtotal);
329 info.cpu_usage[0] = 0;
337 get_i2c_info(int *fd, int arg, char *devtype, char *type)
343 update_load_average()
348 info.loadavg[0] = (float) v[0];
349 info.loadavg[1] = (float) v[1];
350 info.loadavg[2] = (float) v[2];
354 get_acpi_temperature(int fd)
358 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
360 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
364 return (KELVTOC(temp));
368 get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
370 int battime, batcapacity, batstate, ac;
371 char battery_status[64];
372 char battery_time[64];
374 if (GETSYSCTL("hw.acpi.battery.time", battime))
375 (void) fprintf(stderr,
376 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
377 if (GETSYSCTL("hw.acpi.battery.life", batcapacity))
378 (void) fprintf(stderr,
379 "Cannot read sysctl \"hw.acpi.battery.life\"\n");
381 if (GETSYSCTL("hw.acpi.battery.state", batstate))
382 (void) fprintf(stderr,
383 "Cannot read sysctl \"hw.acpi.battery.state\"\n");
385 if (GETSYSCTL("hw.acpi.acline", ac))
386 (void) fprintf(stderr,
387 "Cannot read sysctl \"hw.acpi.acline\"\n");
391 snprintf (battery_status, sizeof(battery_status)-1,
392 "remaining %d%%", batcapacity);
393 snprintf (battery_time, sizeof(battery_time)-1,
394 "%d:%2.2d", battime / 60, battime % 60);
396 snprintf(buf, n, "remaining %d%% (%d:%2.2d)",
397 batcapacity, battime / 60, battime % 60);
401 /* no time estimate available yet */
402 snprintf(battery_status, sizeof(battery_status)-1,
403 "remaining %d%%", batcapacity);
405 snprintf(buf, n, "remaining %d%%",
409 (void) fprintf(stderr, "Discharging while on AC!\n");
411 snprintf (battery_status, sizeof(battery_status)-1,
412 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
414 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
416 if (batstate != 2 && batstate != 0)
417 (void) fprintf(stderr, "Unknown battery state %d!\n", batstate);
419 (void) fprintf(stderr, "Charging while not on AC!\n");
425 snprintf(buf, n, "%s", battery_status);
430 snprintf(buf, n, "%s", battery_time);
440 get_battery_perct(const char *bat)
442 /* not implemented */
447 get_battery_perct_bar(const char *bar)
449 /* not implemented */
454 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
461 open_acpi_temperature(const char *name)
467 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
471 if (!p_client_buffer || client_buffer_size <= 0)
474 if (GETSYSCTL("hw.acpi.acline", state)) {
476 "Cannot read sysctl \"hw.acpi.acline\"\n");
482 strncpy(p_client_buffer, "Running on AC Power",
485 strncpy(p_client_buffer, "Running on battery",
491 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
493 if (!p_client_buffer || client_buffer_size <= 0)
496 /* not implemented */
497 memset(p_client_buffer, 0, client_buffer_size);
501 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
503 if (!p_client_buffer || client_buffer_size <= 0)
506 /* not implemented */
507 memset(p_client_buffer, 0, client_buffer_size);
511 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
513 if (!p_client_buffer || client_buffer_size <= 0)
516 /* not implemented */
517 memset(p_client_buffer, 0, client_buffer_size);
520 /* rdtsc() and get_freq_dynamic() copied from linux.c */
522 #if defined(__i386) || defined(__x86_64)
523 __inline__ unsigned long long int
526 unsigned long long int x;
527 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
532 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
534 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
535 char *p_format, int divisor)
537 #if defined(__i386) || defined(__x86_64)
539 struct timeval tvstart, tvstop;
540 unsigned long long cycles[2]; /* gotta be 64 bit */
541 unsigned int microseconds; /* total time taken */
543 memset(&tz, 0, sizeof (tz));
545 /* get this function in cached memory */
546 gettimeofday(&tvstart, &tz);
548 gettimeofday(&tvstart, &tz);
550 /* we don't trust that this is any specific length of time */
553 gettimeofday(&tvstop, &tz);
554 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
555 (tvstop.tv_usec - tvstart.tv_usec);
557 snprintf(p_client_buffer, client_buffer_size, p_format,
558 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
560 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
566 get_freq(char *p_client_buffer, size_t client_buffer_size,
567 char *p_format, int divisor, unsigned int cpu)
572 freq_sysctl = (char *)calloc(16, sizeof(char));
573 if (freq_sysctl == NULL)
576 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
578 if (!p_client_buffer || client_buffer_size <= 0 ||
579 !p_format || divisor <= 0)
582 if (GETSYSCTL(freq_sysctl, freq) == 0)
583 snprintf(p_client_buffer, client_buffer_size,
584 p_format, (float)freq/divisor);
586 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
595 proc_find_top(info.cpu, info.memu);
602 struct ifreq ifr; /* interface stats */
604 struct net_stat * ns;
605 struct ifaddrs *ifap, *ifa;
606 struct ifmediareq ifmr;
612 if (getifaddrs(&ifap) < 0)
615 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
616 ns = get_net_stat((const char *) ifa->ifa_name);
618 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
621 bzero(&ifmr, sizeof(ifmr));
622 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
623 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
627 * We can monitor only wireless interfaces
628 * which not in hostap mode
630 if ((ifmr.ifm_active & IFM_IEEE80211) &&
631 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
633 bzero(&ifr, sizeof(ifr));
634 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
635 wireq.wi_type = WI_RID_COMMS_QUALITY;
636 wireq.wi_len = WI_MAX_DATALEN;
637 ifr.ifr_data = (void *) &wireq;
639 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
640 perror("ioctl (getting wi status)");
645 * wi_val[0] = quality
649 ns->linkstatus = (int) wireq.wi_val[1];
663 struct device_selection *dev_select = NULL;
664 long select_generation;
666 static struct statinfo statinfo_cur;
667 u_int64_t diskio_current = 0;
669 bzero(&statinfo_cur, sizeof (statinfo_cur));
670 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
671 bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
673 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
676 devs_count = statinfo_cur.dinfo->numdevs;
677 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
678 &select_generation, statinfo_cur.dinfo->generation,
679 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
680 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
681 for (dn = 0; dn < devs_count; ++dn) {
685 di = dev_select[dn].position;
686 dev = &statinfo_cur.dinfo->devices[di];
688 diskio_current += dev->bytes[DEVSTAT_READ] +
689 dev->bytes[DEVSTAT_WRITE];
696 * Since we return (diskio_total_current - diskio_total_old), first
697 * frame will be way too high (it will be equal to
698 * diskio_total_current, i.e. all disk I/O since boot). That's why
699 * it is better to return 0 first time;
701 if (diskio_setup == 0) {
705 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
707 diskio_prev = diskio_current;
709 free(statinfo_cur.dinfo);
713 * While topless is obviously better, top is also not bad.
717 comparecpu(const void *a, const void *b)
719 if (((struct process *)a)->amount > ((struct process *)b)->amount)
722 if (((struct process *)a)->amount < ((struct process *)b)->amount)
729 comparemem(const void *a, const void *b)
731 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
734 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
741 proc_find_top(struct process **cpu, struct process **mem)
743 struct kinfo_proc *p;
746 struct process *processes;
750 /* we get total pages count again to be sure it is up to date */
751 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
752 CRIT_ERR("Cannot read sysctl"
753 "\"vm.stats.vm.v_page_count\"");
755 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
756 processes = malloc(n_processes * sizeof (struct process));
758 for (i = 0; i < n_processes; i++) {
759 if (!((p[i].ki_flag & P_SYSTEM)) &&
760 p[i].ki_comm != NULL) {
761 processes[j].pid = p[i].ki_pid;
762 processes[j].name = strdup(p[i].ki_comm);
763 processes[j].amount = 100.0 *
764 p[i].ki_pctcpu / FSCALE;
765 processes[j].totalmem = (float)(p[i].ki_rssize /
766 (float)total_pages) * 100.0;
771 qsort(processes, j - 1, sizeof (struct process), comparemem);
772 for (i = 0; i < 10 && i < n_processes; i++) {
773 struct process *tmp, *ttmp;
775 tmp = malloc(sizeof (struct process));
776 tmp->pid = processes[i].pid;
777 tmp->amount = processes[i].amount;
778 tmp->totalmem = processes[i].totalmem;
779 tmp->name = strdup(processes[i].name);
789 qsort(processes, j - 1, sizeof (struct process), comparecpu);
790 for (i = 0; i < 10 && i < n_processes; i++) {
791 struct process *tmp, *ttmp;
793 tmp = malloc(sizeof (struct process));
794 tmp->pid = processes[i].pid;
795 tmp->amount = processes[i].amount;
796 tmp->totalmem = processes[i].totalmem;
797 tmp->name = strdup(processes[i].name);
807 #if defined(FREEBSD_DEBUG)
808 printf("=====\nmem\n");
809 for (i = 0; i < 10; i++) {
810 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
811 mem[i]->pid, mem[i]->totalmem);
815 for (i = 0; i < j; free(processes[i++].name));
819 #if defined(i386) || defined(__i386__)
820 #define APMDEV "/dev/apm"
821 #define APM_UNKNOWN 255
824 apm_getinfo(int fd, apm_info_t aip)
826 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
836 struct apm_info info;
838 out = (char *)calloc(16, sizeof (char));
840 fd = open(APMDEV, O_RDONLY);
842 strncpy(out, "ERR", 16);
846 if (apm_getinfo(fd, &info) != 0) {
848 strncpy(out, "ERR", 16);
853 switch (info.ai_acline) {
855 strncpy(out, "off-line", 16);
859 if (info.ai_batt_stat == 3) {
860 strncpy(out, "charging", 16);
863 strncpy(out, "on-line", 16);
868 strncpy(out, "unknown", 16);
875 *get_apm_battery_life()
879 struct apm_info info;
882 out = (char *)calloc(16, sizeof (char));
884 fd = open(APMDEV, O_RDONLY);
886 strncpy(out, "ERR", 16);
890 if (apm_getinfo(fd, &info) != 0) {
892 strncpy(out, "ERR", 16);
897 batt_life = info.ai_batt_life;
898 if (batt_life == APM_UNKNOWN)
899 strncpy(out, "unknown", 16);
900 else if (batt_life <= 100) {
901 snprintf(out, 16, "%d%%", batt_life);
904 strncpy(out, "ERR", 16);
910 *get_apm_battery_time()
915 struct apm_info info;
918 out = (char *)calloc(16, sizeof (char));
920 fd = open(APMDEV, O_RDONLY);
922 strncpy(out, "ERR", 16);
926 if (apm_getinfo(fd, &info) != 0) {
928 strncpy(out, "ERR", 16);
933 batt_time = info.ai_batt_time;
936 strncpy(out, "unknown", 16);
943 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
951 void update_entropy (void)
953 /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
956 /* empty stub so conky links */
958 free_all_processes(void)