1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11 * All rights reserved.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/dkstat.h>
28 #include <sys/param.h>
29 #include <sys/resource.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
33 #include <sys/types.h>
34 #include <sys/vmmeter.h>
36 #include <sys/ioctl.h>
39 #include <net/if_mib.h>
40 #include <net/if_media.h>
41 #include <net/if_var.h>
42 #include <netinet/in.h>
53 #include <dev/wi/if_wavelan_ieee.h>
57 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
58 #define KELVTOC(x) ((x - 2732) / 10.0)
59 #define MAXSHOWDEVS 16
65 inline void proc_find_top(struct process **cpu, struct process **mem);
67 u_int64_t diskio_prev = 0;
68 static short cpu_setup = 0;
69 static short diskio_setup = 0;
71 static int getsysctl(char *name, void *ptr, size_t len)
75 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
86 struct ifmibdata *data = NULL;
89 static int swapmode(unsigned long *retavail, unsigned long *retfree)
92 unsigned long pagesize = getpagesize();
93 struct kvm_swap swapary[1];
98 #define CONVERT(v) ((quad_t)(v) * (pagesize / 1024))
100 n = kvm_getswapinfo(kd, swapary, 1, 0);
101 if (n < 0 || swapary[0].ksw_total == 0) {
105 *retavail = CONVERT(swapary[0].ksw_total);
106 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
108 n = (int) ((double) swapary[0].ksw_used * 100.0 /
109 (double) swapary[0].ksw_total);
114 void prepare_update()
120 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
121 struct timeval boottime;
123 size_t size = sizeof(boottime);
125 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
126 && (boottime.tv_sec != 0)) {
128 info.uptime = now - boottime.tv_sec;
130 fprintf(stderr, "Could not get uptime\n");
135 int check_mount(char *s)
137 struct statfs *mntbuf;
140 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
141 for (i = mntsize - 1; i >= 0; i--) {
142 if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
150 void update_meminfo()
152 unsigned long total_pages, inactive_pages, free_pages;
153 unsigned long swap_avail, swap_free;
155 int pagesize = getpagesize();
157 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
158 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
161 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
162 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
165 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
166 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
169 info.memmax = total_pages * (pagesize >> 10);
170 info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
172 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
173 info.swapmax = swap_avail;
174 info.swap = (swap_avail - swap_free);
181 void update_net_stats()
185 long long r, t, last_recv, last_trans;
186 struct ifaddrs *ifap, *ifa;
190 delta = current_update_time - last_update_time;
191 if (delta <= 0.0001) {
195 if (getifaddrs(&ifap) < 0) {
199 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
200 ns = get_net_stat((const char *) ifa->ifa_name);
202 if (ifa->ifa_flags & IFF_UP) {
203 struct ifaddrs *iftmp;
206 last_recv = ns->recv;
207 last_trans = ns->trans;
209 if (ifa->ifa_addr->sa_family != AF_LINK) {
213 for (iftmp = ifa->ifa_next;
214 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
215 iftmp = iftmp->ifa_next) {
216 if (iftmp->ifa_addr->sa_family == AF_INET) {
217 memcpy(&(ns->addr), iftmp->ifa_addr,
218 iftmp->ifa_addr->sa_len);
222 ifd = (struct if_data *) ifa->ifa_data;
226 if (r < ns->last_read_recv) {
227 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
229 ns->recv += (r - ns->last_read_recv);
232 ns->last_read_recv = r;
234 if (t < ns->last_read_trans) {
235 ns->trans += ((long long) 4294967295U -
236 ns->last_read_trans) + t;
238 ns->trans += (t - ns->last_read_trans);
241 ns->last_read_trans = t;
243 /* calculate speeds */
244 ns->recv_speed = (ns->recv - last_recv) / delta;
245 ns->trans_speed = (ns->trans - last_trans) / delta;
254 void update_total_processes()
258 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
260 info.procs = n_processes;
263 void update_running_processes()
265 struct kinfo_proc *p;
269 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
270 for (i = 0; i < n_processes; i++) {
271 #if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
272 if (p[i].kp_proc.p_stat == SRUN) {
274 if (p[i].ki_stat == SRUN) {
280 info.run_procs = cnt;
283 struct cpu_load_struct {
284 unsigned long load[5];
287 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
288 long cpu_used, oldtotal, oldused;
292 /* int cpu_count = 0; */
294 /* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
295 * It's possible to get a CPU count, but as we fulfill only
296 * info.cpu_usage[0], it's better to report there's only one CPU.
297 * It should fix some bugs (e.g. cpugraph) */
299 if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
300 info.cpu_count = cpu_count;
305 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
306 if (info.cpu_usage == NULL) {
311 /* XXX: SMP support */
312 void update_cpu_usage()
315 long cp_time[CPUSTATES];
316 size_t len = sizeof(cp_time);
318 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
319 if ((cpu_setup == 0) || (!info.cpu_usage)) {
324 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
325 fprintf(stderr, "Cannot get kern.cp_time");
328 fresh.load[0] = cp_time[CP_USER];
329 fresh.load[1] = cp_time[CP_NICE];
330 fresh.load[2] = cp_time[CP_SYS];
331 fresh.load[3] = cp_time[CP_IDLE];
332 fresh.load[4] = cp_time[CP_IDLE];
334 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
335 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
337 if ((total - oldtotal) != 0) {
338 info.cpu_usage[0] = ((double) (used - oldused)) /
339 (double) (total - oldtotal);
341 info.cpu_usage[0] = 0;
348 double get_sysfs_info(int *fd, int arg, char *devtype, char *type)
353 void update_load_average()
359 info.loadavg[0] = (double) v[0];
360 info.loadavg[1] = (double) v[1];
361 info.loadavg[2] = (double) v[2];
364 double get_acpi_temperature(int fd)
368 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
370 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
374 return KELVTOC(temp);
377 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
379 int battime, batcapacity, batstate, ac;
380 char battery_status[64];
381 char battery_time[64];
383 if (GETSYSCTL("hw.acpi.battery.time", battime)) {
384 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
386 if (GETSYSCTL("hw.acpi.battery.life", batcapacity)) {
387 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
390 if (GETSYSCTL("hw.acpi.battery.state", batstate)) {
391 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
394 if (GETSYSCTL("hw.acpi.acline", ac)) {
395 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
400 snprintf(battery_status, sizeof(battery_status) - 1,
401 "remaining %d%%", batcapacity);
402 snprintf(battery_time, sizeof(battery_time) - 1, "%d:%2.2d",
403 battime / 60, battime % 60);
404 /* snprintf(buf, n, "remaining %d%% (%d:%2.2d)", batcapacity,
405 battime / 60, battime % 60); */
407 /* no time estimate available yet */
408 snprintf(battery_status, sizeof(battery_status) - 1,
409 "remaining %d%%", batcapacity);
411 /* snprintf(buf, n, "remaining %d%%", batcapacity); */
413 fprintf(stderr, "Discharging while on AC!\n");
416 snprintf(battery_status, sizeof(battery_status) - 1,
417 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
419 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)",
421 if (batstate != 2 && batstate != 0) {
422 fprintf(stderr, "Unknown battery state %d!\n", batstate);
425 fprintf(stderr, "Charging while not on AC!\n");
431 snprintf(buf, n, "%s", battery_status);
434 snprintf(buf, n, "%s", battery_time);
441 int get_battery_perct(const char *bat)
443 /* not implemented */
447 int get_battery_perct_bar(const char *bar)
449 /* not implemented */
453 int open_sysfs_sensor(const char *dir, const char *dev, const char *type,
454 int n, int *div, char *devtype)
459 int open_acpi_temperature(const char *name)
464 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
468 if (!p_client_buffer || client_buffer_size <= 0) {
472 if (GETSYSCTL("hw.acpi.acline", state)) {
473 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
478 strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
480 strncpy(p_client_buffer, "Running on battery", client_buffer_size);
484 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
486 /* not implemented */
487 if (p_client_buffer && client_buffer_size > 0) {
488 memset(p_client_buffer, 0, client_buffer_size);
492 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
494 /* not implemented */
495 if (p_client_buffer && client_buffer_size > 0) {
496 memset(p_client_buffer, 0, client_buffer_size);
500 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
502 /* not implemented */
503 if (p_client_buffer && client_buffer_size > 0) {
504 memset(p_client_buffer, 0, client_buffer_size);
508 /* rdtsc() and get_freq_dynamic() copied from linux.c */
510 #if defined(__i386) || defined(__x86_64)
511 __inline__ unsigned long long int rdtsc()
513 unsigned long long int x;
515 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
520 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
521 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
522 char *p_format, int divisor)
524 #if defined(__i386) || defined(__x86_64)
526 struct timeval tvstart, tvstop;
527 unsigned long long cycles[2]; /* gotta be 64 bit */
528 unsigned int microseconds; /* total time taken */
530 memset(&tz, 0, sizeof(tz));
532 /* get this function in cached memory */
533 gettimeofday(&tvstart, &tz);
535 gettimeofday(&tvstart, &tz);
537 /* we don't trust that this is any specific length of time */
540 gettimeofday(&tvstop, &tz);
541 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
542 (tvstop.tv_usec - tvstart.tv_usec);
544 snprintf(p_client_buffer, client_buffer_size, p_format,
545 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
547 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
552 char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
553 int divisor, unsigned int cpu)
558 freq_sysctl = (char *) calloc(16, sizeof(char));
559 if (freq_sysctl == NULL) {
563 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
565 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
570 if (GETSYSCTL(freq_sysctl, freq) == 0) {
571 snprintf(p_client_buffer, client_buffer_size, p_format,
572 (float) freq / divisor);
574 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
583 proc_find_top(info.cpu, info.memu);
587 void update_wifi_stats()
589 struct ifreq ifr; /* interface stats */
592 struct ifaddrs *ifap, *ifa;
593 struct ifmediareq ifmr;
596 /* Get iface table */
597 if (getifaddrs(&ifap) < 0) {
601 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
602 ns = get_net_stat((const char *) ifa->ifa_name);
604 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
607 bzero(&ifmr, sizeof(ifmr));
608 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
609 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
614 /* We can monitor only wireless interfaces
615 * which are not in hostap mode */
616 if ((ifmr.ifm_active & IFM_IEEE80211)
617 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
619 bzero(&ifr, sizeof(ifr));
620 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
621 wireq.wi_type = WI_RID_COMMS_QUALITY;
622 wireq.wi_len = WI_MAX_DATALEN;
623 ifr.ifr_data = (void *) &wireq;
625 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
626 perror("ioctl (getting wi status)");
630 /* wi_val[0] = quality
632 * wi_val[2] = noise */
633 ns->linkstatus = (int) wireq.wi_val[1];
643 int devs_count, num_selected, num_selections, i;
644 struct device_selection *dev_select = NULL;
645 long select_generation;
647 static struct statinfo statinfo_cur;
648 u_int64_t diskio_current = 0;
649 u_int64_t writes = 0;
651 bzero(&statinfo_cur, sizeof(statinfo_cur));
652 statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
653 bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
655 if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
659 devs_count = statinfo_cur.dinfo->numdevs;
660 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
661 &select_generation, statinfo_cur.dinfo->generation,
662 statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
663 DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
664 for (dn = 0; dn < devs_count; ++dn) {
668 di = dev_select[dn].position;
669 dev = &statinfo_cur.dinfo->devices[di];
671 diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
673 for (i = 0; i < MAX_DISKIO_STATS; i++) {
674 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
675 diskio_stats[i].dev) == 0) {
676 diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
677 dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
678 diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
679 diskio_stats[i].last_read) / 1024;
680 diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
681 diskio_stats[i].last_write) / 1024;
682 if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
683 < diskio_stats[i].last) {
684 diskio_stats[i].current = 0;
686 if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
687 diskio_stats[i].current_read = 0;
688 diskio_stats[i].current = diskio_stats[i].current_write;
690 if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
691 diskio_stats[i].current_write = 0;
692 diskio_stats[i].current = diskio_stats[i].current_read;
694 diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
695 dev->bytes[DEVSTAT_WRITE];
696 diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
697 diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
705 /* Since we return (diskio_total_current - diskio_total_old),
706 * the first frame will be way too high
707 * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
708 * That's why it is better to return 0 first time; */
709 if (diskio_setup == 0) {
713 diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
715 diskio_prev = diskio_current;
717 free(statinfo_cur.dinfo);
720 /* While topless is obviously better, top is also not bad. */
722 int comparecpu(const void *a, const void *b)
724 if (((struct process *)a)->amount > ((struct process *)b)->amount) {
726 } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
733 int comparemem(const void *a, const void *b)
735 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
737 } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
744 inline void proc_find_top(struct process **cpu, struct process **mem)
746 struct kinfo_proc *p;
749 struct process *processes;
753 /* we get total pages count again to be sure it is up to date */
754 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
755 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
758 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
759 processes = malloc(n_processes * sizeof(struct process));
761 for (i = 0; i < n_processes; i++) {
762 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
763 processes[j].pid = p[i].ki_pid;
764 processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
765 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
766 processes[j].totalmem = (float) (p[i].ki_rssize /
767 (float) total_pages) * 100.0;
768 processes[j].vsize = p[i].ki_size;
769 processes[j].rss = (p[i].ki_rssize * getpagesize());
774 qsort(processes, j - 1, sizeof(struct process), comparemem);
775 for (i = 0; i < 10 && i < n_processes; i++) {
776 struct process *tmp, *ttmp;
778 tmp = malloc(sizeof(struct process));
779 tmp->pid = processes[i].pid;
780 tmp->amount = processes[i].amount;
781 tmp->totalmem = processes[i].totalmem;
782 tmp->name = strndup(processes[i].name, text_buffer_size);
783 tmp->rss = processes[i].rss;
784 tmp->vsize = processes[i].vsize;
794 qsort(processes, j - 1, sizeof(struct process), comparecpu);
795 for (i = 0; i < 10 && i < n_processes; i++) {
796 struct process *tmp, *ttmp;
798 tmp = malloc(sizeof(struct process));
799 tmp->pid = processes[i].pid;
800 tmp->amount = processes[i].amount;
801 tmp->totalmem = processes[i].totalmem;
802 tmp->name = strndup(processes[i].name, text_buffer_size);
803 tmp->rss = processes[i].rss;
804 tmp->vsize = processes[i].vsize;
814 #if defined(FREEBSD_DEBUG)
815 printf("=====\nmem\n");
816 for (i = 0; i < 10; i++) {
817 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
818 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
822 for (i = 0; i < j; i++) {
823 free(processes[i].name);
828 #if defined(i386) || defined(__i386__)
829 #define APMDEV "/dev/apm"
830 #define APM_UNKNOWN 255
832 int apm_getinfo(int fd, apm_info_t aip)
834 if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
841 char *get_apm_adapter()
844 struct apm_info info;
847 out = (char *) calloc(16, sizeof(char));
849 fd = open(APMDEV, O_RDONLY);
851 strncpy(out, "ERR", 16);
855 if (apm_getinfo(fd, &info) != 0) {
857 strncpy(out, "ERR", 16);
862 switch (info.ai_acline) {
864 strncpy(out, "off-line", 16);
868 if (info.ai_batt_stat == 3) {
869 strncpy(out, "charging", 16);
872 strncpy(out, "on-line", 16);
877 strncpy(out, "unknown", 16);
883 char *get_apm_battery_life()
887 struct apm_info info;
890 out = (char *) calloc(16, sizeof(char));
892 fd = open(APMDEV, O_RDONLY);
894 strncpy(out, "ERR", 16);
898 if (apm_getinfo(fd, &info) != 0) {
900 strncpy(out, "ERR", 16);
905 batt_life = info.ai_batt_life;
906 if (batt_life == APM_UNKNOWN) {
907 strncpy(out, "unknown", 16);
908 } else if (batt_life <= 100) {
909 snprintf(out, 16, "%d%%", batt_life);
912 strncpy(out, "ERR", 16);
918 char *get_apm_battery_time()
923 struct apm_info info;
926 out = (char *) calloc(16, sizeof(char));
928 fd = open(APMDEV, O_RDONLY);
930 strncpy(out, "ERR", 16);
934 if (apm_getinfo(fd, &info) != 0) {
936 strncpy(out, "ERR", 16);
941 batt_time = info.ai_batt_time;
943 if (batt_time == -1) {
944 strncpy(out, "unknown", 16);
951 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
959 void update_entropy(void)
961 /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
964 /* empty stub so conky links */
965 void free_all_processes(void)