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>
50 #include <dev/wi/if_wavelan_ieee.h>
54 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
55 #define KELVTOC(x) ((x - 2732) / 10.0)
56 #define MAXSHOWDEVS 16
62 inline void proc_find_top(struct process **cpu, struct process **mem);
64 u_int64_t diskio_prev = 0;
65 static short cpu_setup = 0;
66 static short diskio_setup = 0;
68 static int getsysctl(char *name, void *ptr, size_t len)
72 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
83 struct ifmibdata *data = NULL;
86 static int swapmode(unsigned long *retavail, unsigned long *retfree)
89 unsigned long pagesize = getpagesize();
90 struct kvm_swap swapary[1];
95 #define CONVERT(v) ((quad_t)(v) * (pagesize / 1024))
97 n = kvm_getswapinfo(kd, swapary, 1, 0);
98 if (n < 0 || swapary[0].ksw_total == 0) {
102 *retavail = CONVERT(swapary[0].ksw_total);
103 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
105 n = (int) ((double) swapary[0].ksw_used * 100.0 /
106 (double) swapary[0].ksw_total);
111 void prepare_update()
117 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
118 struct timeval boottime;
120 size_t size = sizeof(boottime);
122 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
123 && (boottime.tv_sec != 0)) {
125 info.uptime = now - boottime.tv_sec;
127 fprintf(stderr, "Could not get uptime\n");
132 int check_mount(char *s)
134 struct statfs *mntbuf;
137 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
138 for (i = mntsize - 1; i >= 0; i--) {
139 if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
147 void update_meminfo()
149 u_int total_pages, inactive_pages, free_pages;
150 unsigned long swap_avail, swap_free;
152 int pagesize = getpagesize();
154 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
155 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
158 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
159 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
162 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
163 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
166 info.memmax = total_pages * (pagesize >> 10);
167 info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
168 info.memeasyfree = info.memfree = info.memmax - info.mem;
170 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
171 info.swapmax = swap_avail;
172 info.swap = (swap_avail - swap_free);
179 void update_net_stats()
183 long long r, t, last_recv, last_trans;
184 struct ifaddrs *ifap, *ifa;
188 delta = current_update_time - last_update_time;
189 if (delta <= 0.0001) {
193 if (getifaddrs(&ifap) < 0) {
197 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
198 ns = get_net_stat((const char *) ifa->ifa_name);
200 if (ifa->ifa_flags & IFF_UP) {
201 struct ifaddrs *iftmp;
204 last_recv = ns->recv;
205 last_trans = ns->trans;
207 if (ifa->ifa_addr->sa_family != AF_LINK) {
211 for (iftmp = ifa->ifa_next;
212 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
213 iftmp = iftmp->ifa_next) {
214 if (iftmp->ifa_addr->sa_family == AF_INET) {
215 memcpy(&(ns->addr), iftmp->ifa_addr,
216 iftmp->ifa_addr->sa_len);
220 ifd = (struct if_data *) ifa->ifa_data;
224 if (r < ns->last_read_recv) {
225 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
227 ns->recv += (r - ns->last_read_recv);
230 ns->last_read_recv = r;
232 if (t < ns->last_read_trans) {
233 ns->trans += ((long long) 4294967295U -
234 ns->last_read_trans) + t;
236 ns->trans += (t - ns->last_read_trans);
239 ns->last_read_trans = t;
241 /* calculate speeds */
242 ns->recv_speed = (ns->recv - last_recv) / delta;
243 ns->trans_speed = (ns->trans - last_trans) / delta;
252 void update_total_processes()
256 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
258 info.procs = n_processes;
261 void update_running_processes()
263 struct kinfo_proc *p;
267 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
268 for (i = 0; i < n_processes; i++) {
269 #if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
270 if (p[i].kp_proc.p_stat == SRUN) {
272 if (p[i].ki_stat == SRUN) {
278 info.run_procs = cnt;
281 struct cpu_load_struct {
282 unsigned long load[5];
285 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
286 long cpu_used, oldtotal, oldused;
290 /* int cpu_count = 0; */
292 /* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
293 * It's possible to get a CPU count, but as we fulfill only
294 * info.cpu_usage[0], it's better to report there's only one CPU.
295 * It should fix some bugs (e.g. cpugraph) */
297 if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
298 info.cpu_count = cpu_count;
303 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
304 if (info.cpu_usage == NULL) {
309 /* XXX: SMP support */
310 void update_cpu_usage()
313 long cp_time[CPUSTATES];
314 size_t len = sizeof(cp_time);
316 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
317 if ((cpu_setup == 0) || (!info.cpu_usage)) {
322 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
323 fprintf(stderr, "Cannot get kern.cp_time");
326 fresh.load[0] = cp_time[CP_USER];
327 fresh.load[1] = cp_time[CP_NICE];
328 fresh.load[2] = cp_time[CP_SYS];
329 fresh.load[3] = cp_time[CP_IDLE];
330 fresh.load[4] = cp_time[CP_IDLE];
332 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
333 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
335 if ((total - oldtotal) != 0) {
336 info.cpu_usage[0] = ((double) (used - oldused)) /
337 (double) (total - oldtotal);
339 info.cpu_usage[0] = 0;
346 double get_sysfs_info(int *fd, int arg, char *devtype, char *type)
351 void update_load_average()
357 info.loadavg[0] = (double) v[0];
358 info.loadavg[1] = (double) v[1];
359 info.loadavg[2] = (double) v[2];
362 double get_acpi_temperature(int fd)
366 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
368 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
372 return KELVTOC(temp);
375 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
377 int battime, batcapacity, batstate, ac;
378 char battery_status[64];
379 char battery_time[64];
381 if (GETSYSCTL("hw.acpi.battery.time", battime)) {
382 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
384 if (GETSYSCTL("hw.acpi.battery.life", batcapacity)) {
385 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
388 if (GETSYSCTL("hw.acpi.battery.state", batstate)) {
389 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
392 if (GETSYSCTL("hw.acpi.acline", ac)) {
393 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
398 snprintf(battery_status, sizeof(battery_status) - 1,
399 "remaining %d%%", batcapacity);
400 snprintf(battery_time, sizeof(battery_time) - 1, "%d:%2.2d",
401 battime / 60, battime % 60);
402 /* snprintf(buf, n, "remaining %d%% (%d:%2.2d)", batcapacity,
403 battime / 60, battime % 60); */
405 /* no time estimate available yet */
406 snprintf(battery_status, sizeof(battery_status) - 1,
407 "remaining %d%%", batcapacity);
409 /* snprintf(buf, n, "remaining %d%%", batcapacity); */
411 fprintf(stderr, "Discharging while on AC!\n");
414 snprintf(battery_status, sizeof(battery_status) - 1,
415 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
417 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)",
419 if (batstate != 2 && batstate != 0) {
420 fprintf(stderr, "Unknown battery state %d!\n", batstate);
423 fprintf(stderr, "Charging while not on AC!\n");
429 snprintf(buf, n, "%s", battery_status);
432 snprintf(buf, n, "%s", battery_time);
439 int get_battery_perct(const char *bat)
441 /* not implemented */
445 int get_battery_perct_bar(const char *bar)
447 /* not implemented */
451 int open_sysfs_sensor(const char *dir, const char *dev, const char *type,
452 int n, int *div, char *devtype)
457 int open_acpi_temperature(const char *name)
462 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
466 if (!p_client_buffer || client_buffer_size <= 0) {
470 if (GETSYSCTL("hw.acpi.acline", state)) {
471 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
476 strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
478 strncpy(p_client_buffer, "Running on battery", client_buffer_size);
482 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
484 /* not implemented */
485 if (p_client_buffer && client_buffer_size > 0) {
486 memset(p_client_buffer, 0, client_buffer_size);
490 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
492 /* not implemented */
493 if (p_client_buffer && client_buffer_size > 0) {
494 memset(p_client_buffer, 0, client_buffer_size);
498 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
500 /* not implemented */
501 if (p_client_buffer && client_buffer_size > 0) {
502 memset(p_client_buffer, 0, client_buffer_size);
506 /* rdtsc() and get_freq_dynamic() copied from linux.c */
508 #if defined(__i386) || defined(__x86_64)
509 __inline__ unsigned long long int rdtsc()
511 unsigned long long int x;
513 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
518 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
519 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
520 const char *p_format, int divisor)
522 #if defined(__i386) || defined(__x86_64)
524 struct timeval tvstart, tvstop;
525 unsigned long long cycles[2]; /* gotta be 64 bit */
526 unsigned int microseconds; /* total time taken */
528 memset(&tz, 0, sizeof(tz));
530 /* get this function in cached memory */
531 gettimeofday(&tvstart, &tz);
533 gettimeofday(&tvstart, &tz);
535 /* we don't trust that this is any specific length of time */
538 gettimeofday(&tvstop, &tz);
539 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
540 (tvstop.tv_usec - tvstart.tv_usec);
542 snprintf(p_client_buffer, client_buffer_size, p_format,
543 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
545 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
550 char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
551 int divisor, unsigned int cpu)
556 freq_sysctl = (char *) calloc(16, sizeof(char));
557 if (freq_sysctl == NULL) {
561 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
563 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
568 if (GETSYSCTL(freq_sysctl, freq) == 0) {
569 snprintf(p_client_buffer, client_buffer_size, p_format,
570 (float) freq / divisor);
572 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
581 proc_find_top(info.cpu, info.memu);
585 void update_wifi_stats()
587 struct ifreq ifr; /* interface stats */
590 struct ifaddrs *ifap, *ifa;
591 struct ifmediareq ifmr;
594 /* Get iface table */
595 if (getifaddrs(&ifap) < 0) {
599 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
600 ns = get_net_stat((const char *) ifa->ifa_name);
602 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
605 bzero(&ifmr, sizeof(ifmr));
606 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
607 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
612 /* We can monitor only wireless interfaces
613 * which are not in hostap mode */
614 if ((ifmr.ifm_active & IFM_IEEE80211)
615 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
617 bzero(&ifr, sizeof(ifr));
618 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
619 wireq.wi_type = WI_RID_COMMS_QUALITY;
620 wireq.wi_len = WI_MAX_DATALEN;
621 ifr.ifr_data = (void *) &wireq;
623 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
624 perror("ioctl (getting wi status)");
628 /* wi_val[0] = quality
630 * wi_val[2] = noise */
631 ns->linkstatus = (int) wireq.wi_val[1];
641 int devs_count, num_selected, num_selections, i;
642 struct device_selection *dev_select = NULL;
643 long select_generation;
645 static struct statinfo statinfo_cur;
646 u_int64_t diskio_current = 0;
647 u_int64_t writes = 0;
649 bzero(&statinfo_cur, sizeof(statinfo_cur));
650 statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
651 bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
653 if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
657 devs_count = statinfo_cur.dinfo->numdevs;
658 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
659 &select_generation, statinfo_cur.dinfo->generation,
660 statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
661 DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
662 for (dn = 0; dn < devs_count; ++dn) {
666 di = dev_select[dn].position;
667 dev = &statinfo_cur.dinfo->devices[di];
669 diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
671 for (i = 0; i < MAX_DISKIO_STATS; i++) {
672 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
673 diskio_stats[i].dev) == 0) {
674 diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
675 dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
676 diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
677 diskio_stats[i].last_read) / 1024;
678 diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
679 diskio_stats[i].last_write) / 1024;
680 if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
681 < diskio_stats[i].last) {
682 diskio_stats[i].current = 0;
684 if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
685 diskio_stats[i].current_read = 0;
686 diskio_stats[i].current = diskio_stats[i].current_write;
688 if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
689 diskio_stats[i].current_write = 0;
690 diskio_stats[i].current = diskio_stats[i].current_read;
692 diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
693 dev->bytes[DEVSTAT_WRITE];
694 diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
695 diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
703 /* Since we return (diskio_total_current - diskio_total_old),
704 * the first frame will be way too high
705 * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
706 * That's why it is better to return 0 first time; */
707 if (diskio_setup == 0) {
709 info.diskio_value = 0;
711 info.diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
713 diskio_prev = diskio_current;
715 free(statinfo_cur.dinfo);
718 void clear_diskio_stats()
722 /* While topless is obviously better, top is also not bad. */
724 int comparecpu(const void *a, const void *b)
726 if (((struct process *)a)->amount > ((struct process *)b)->amount) {
728 } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
735 int comparemem(const void *a, const void *b)
737 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
739 } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
746 inline void proc_find_top(struct process **cpu, struct process **mem)
748 struct kinfo_proc *p;
751 struct process *processes;
755 /* we get total pages count again to be sure it is up to date */
756 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
757 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
760 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
761 processes = malloc(n_processes * sizeof(struct process));
763 for (i = 0; i < n_processes; i++) {
764 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
765 processes[j].pid = p[i].ki_pid;
766 processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
767 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
768 processes[j].totalmem = (float) (p[i].ki_rssize /
769 (float) total_pages) * 100.0;
770 processes[j].vsize = p[i].ki_size;
771 processes[j].rss = (p[i].ki_rssize * getpagesize());
776 qsort(processes, j - 1, sizeof(struct process), comparemem);
777 for (i = 0; i < 10 && i < n_processes; i++) {
778 struct process *tmp, *ttmp;
780 tmp = malloc(sizeof(struct process));
781 tmp->pid = processes[i].pid;
782 tmp->amount = processes[i].amount;
783 tmp->totalmem = processes[i].totalmem;
784 tmp->name = strndup(processes[i].name, text_buffer_size);
785 tmp->rss = processes[i].rss;
786 tmp->vsize = processes[i].vsize;
796 qsort(processes, j - 1, sizeof(struct process), comparecpu);
797 for (i = 0; i < 10 && i < n_processes; i++) {
798 struct process *tmp, *ttmp;
800 tmp = malloc(sizeof(struct process));
801 tmp->pid = processes[i].pid;
802 tmp->amount = processes[i].amount;
803 tmp->totalmem = processes[i].totalmem;
804 tmp->name = strndup(processes[i].name, text_buffer_size);
805 tmp->rss = processes[i].rss;
806 tmp->vsize = processes[i].vsize;
816 #if defined(FREEBSD_DEBUG)
817 printf("=====\nmem\n");
818 for (i = 0; i < 10; i++) {
819 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
820 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
824 for (i = 0; i < j; i++) {
825 free(processes[i].name);
830 #if defined(i386) || defined(__i386__)
831 #define APMDEV "/dev/apm"
832 #define APM_UNKNOWN 255
834 int apm_getinfo(int fd, apm_info_t aip)
836 if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
843 char *get_apm_adapter()
846 struct apm_info info;
849 out = (char *) calloc(16, sizeof(char));
851 fd = open(APMDEV, O_RDONLY);
853 strncpy(out, "ERR", 16);
857 if (apm_getinfo(fd, &info) != 0) {
859 strncpy(out, "ERR", 16);
864 switch (info.ai_acline) {
866 strncpy(out, "off-line", 16);
870 if (info.ai_batt_stat == 3) {
871 strncpy(out, "charging", 16);
874 strncpy(out, "on-line", 16);
879 strncpy(out, "unknown", 16);
885 char *get_apm_battery_life()
889 struct apm_info info;
892 out = (char *) calloc(16, sizeof(char));
894 fd = open(APMDEV, O_RDONLY);
896 strncpy(out, "ERR", 16);
900 if (apm_getinfo(fd, &info) != 0) {
902 strncpy(out, "ERR", 16);
907 batt_life = info.ai_batt_life;
908 if (batt_life == APM_UNKNOWN) {
909 strncpy(out, "unknown", 16);
910 } else if (batt_life <= 100) {
911 snprintf(out, 16, "%d%%", batt_life);
914 strncpy(out, "ERR", 16);
920 char *get_apm_battery_time()
925 struct apm_info info;
928 out = (char *) calloc(16, sizeof(char));
930 fd = open(APMDEV, O_RDONLY);
932 strncpy(out, "ERR", 16);
936 if (apm_getinfo(fd, &info) != 0) {
938 strncpy(out, "ERR", 16);
943 batt_time = info.ai_batt_time;
945 if (batt_time == -1) {
946 strncpy(out, "unknown", 16);
953 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
961 void update_entropy(void)
963 /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
966 /* empty stub so conky links */
967 void free_all_processes(void)