2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
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/>.
28 #include <sys/dkstat.h>
29 #include <sys/param.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
34 #include <sys/types.h>
35 #include <sys/vmmeter.h>
37 #include <sys/ioctl.h>
40 #include <net/if_mib.h>
41 #include <net/if_media.h>
42 #include <net/if_var.h>
43 #include <netinet/in.h>
54 #include <dev/wi/if_wavelan_ieee.h>
58 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof (var))
59 #define KELVTOC(x) ((x - 2732) / 10.0)
60 #define MAXSHOWDEVS 16
66 inline void proc_find_top(struct process **cpu, struct process **mem);
68 u_int64_t diskio_prev = 0;
69 static short cpu_setup = 0;
70 static short diskio_setup = 0;
72 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(int *retavail, int *retfree)
92 int 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)
104 *retavail = CONVERT(swapary[0].ksw_total);
105 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
107 n = (int) ((double) swapary[0].ksw_used * 100.0 /
108 (double) swapary[0].ksw_total);
121 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
122 struct timeval boottime;
124 size_t size = sizeof (boottime);
126 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) &&
127 (boottime.tv_sec != 0)) {
129 info.uptime = now - boottime.tv_sec;
131 fprintf(stderr, "Could not get uptime\n");
136 int check_mount(char *s)
138 struct statfs *mntbuf;
141 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
142 for (i = mntsize - 1; i >= 0; i--)
143 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
152 int total_pages, inactive_pages, free_pages;
153 int swap_avail, swap_free;
155 int pagesize = getpagesize();
157 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
159 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
161 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
163 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
165 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
167 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
169 info.memmax = (total_pages * pagesize) >> 10;
171 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
174 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
175 info.swapmax = swap_avail;
176 info.swap = (swap_avail - swap_free);
188 long long r, t, last_recv, last_trans;
189 struct ifaddrs *ifap, *ifa;
194 delta = current_update_time - last_update_time;
198 if (getifaddrs(&ifap) < 0)
201 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
202 ns = get_net_stat((const char *) ifa->ifa_name);
204 if (ifa->ifa_flags & IFF_UP) {
205 struct ifaddrs *iftmp;
208 last_recv = ns->recv;
209 last_trans = ns->trans;
211 if (ifa->ifa_addr->sa_family != AF_LINK)
214 for (iftmp = ifa->ifa_next; iftmp != NULL &&
215 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
216 iftmp = iftmp->ifa_next)
217 if (iftmp->ifa_addr->sa_family == AF_INET)
218 memcpy(&(ns->addr), iftmp->ifa_addr,
219 iftmp->ifa_addr->sa_len);
221 ifd = (struct if_data *) ifa->ifa_data;
225 if (r < ns->last_read_recv)
227 ((long long) 4294967295U -
228 ns->last_read_recv) + r;
230 ns->recv += (r - ns->last_read_recv);
232 ns->last_read_recv = r;
234 if (t < ns->last_read_trans)
236 ((long long) 4294967295U -
237 ns->last_read_trans) + t;
239 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;
255 update_total_processes()
259 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
261 info.procs = n_processes;
265 update_running_processes()
267 struct kinfo_proc *p;
271 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
272 for (i = 0; i < n_processes; i++) {
273 #if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
274 if (p[i].kp_proc.p_stat == SRUN)
276 if (p[i].ki_stat == SRUN)
281 info.run_procs = cnt;
284 struct cpu_load_struct {
285 unsigned long load[5];
288 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
289 long cpu_used, oldtotal, oldused;
294 /* int cpu_count = 0; */
298 * FreeBSD doesn't allow to get per CPU load stats
299 * on SMP machines. It's possible to get a CPU count,
300 * but as we fulfil only info.cpu_usage[0], it's better
301 * to report there's only one CPU. It should fix some bugs
305 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
306 info.cpu_count = cpu_count;
310 info.cpu_usage = malloc(info.cpu_count * sizeof (float));
311 if (info.cpu_usage == NULL)
315 /* XXX: SMP support */
320 long cp_time[CPUSTATES];
321 size_t len = sizeof (cp_time);
323 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
324 if ((cpu_setup == 0) || (!info.cpu_usage)) {
329 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
330 (void) fprintf(stderr, "Cannot get kern.cp_time");
333 fresh.load[0] = cp_time[CP_USER];
334 fresh.load[1] = cp_time[CP_NICE];
335 fresh.load[2] = cp_time[CP_SYS];
336 fresh.load[3] = cp_time[CP_IDLE];
337 fresh.load[4] = cp_time[CP_IDLE];
339 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
341 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
343 if ((total - oldtotal) != 0) {
344 info.cpu_usage[0] = ((double) (used - oldused)) /
345 (double) (total - oldtotal);
347 info.cpu_usage[0] = 0;
355 get_sysfs_info(int *fd, int arg, char *devtype, char *type)
361 update_load_average()
366 info.loadavg[0] = (float) v[0];
367 info.loadavg[1] = (float) v[1];
368 info.loadavg[2] = (float) v[2];
372 get_acpi_temperature(int fd)
376 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
378 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
382 return (KELVTOC(temp));
386 get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
388 int battime, batcapacity, batstate, ac;
389 char battery_status[64];
390 char battery_time[64];
392 if (GETSYSCTL("hw.acpi.battery.time", battime))
393 (void) fprintf(stderr,
394 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
395 if (GETSYSCTL("hw.acpi.battery.life", batcapacity))
396 (void) fprintf(stderr,
397 "Cannot read sysctl \"hw.acpi.battery.life\"\n");
399 if (GETSYSCTL("hw.acpi.battery.state", batstate))
400 (void) fprintf(stderr,
401 "Cannot read sysctl \"hw.acpi.battery.state\"\n");
403 if (GETSYSCTL("hw.acpi.acline", ac))
404 (void) fprintf(stderr,
405 "Cannot read sysctl \"hw.acpi.acline\"\n");
409 snprintf (battery_status, sizeof(battery_status)-1,
410 "remaining %d%%", batcapacity);
411 snprintf (battery_time, sizeof(battery_time)-1,
412 "%d:%2.2d", battime / 60, battime % 60);
414 snprintf(buf, n, "remaining %d%% (%d:%2.2d)",
415 batcapacity, battime / 60, battime % 60);
419 /* no time estimate available yet */
420 snprintf(battery_status, sizeof(battery_status)-1,
421 "remaining %d%%", batcapacity);
423 snprintf(buf, n, "remaining %d%%",
427 (void) fprintf(stderr, "Discharging while on AC!\n");
429 snprintf (battery_status, sizeof(battery_status)-1,
430 batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
432 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
434 if (batstate != 2 && batstate != 0)
435 (void) fprintf(stderr, "Unknown battery state %d!\n", batstate);
437 (void) fprintf(stderr, "Charging while not on AC!\n");
443 snprintf(buf, n, "%s", battery_status);
448 snprintf(buf, n, "%s", battery_time);
458 get_battery_perct(const char *bat)
460 /* not implemented */
465 get_battery_perct_bar(const char *bar)
467 /* not implemented */
472 open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n, int *div, char *devtype)
478 open_acpi_temperature(const char *name)
484 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
488 if (!p_client_buffer || client_buffer_size <= 0)
491 if (GETSYSCTL("hw.acpi.acline", state)) {
493 "Cannot read sysctl \"hw.acpi.acline\"\n");
499 strncpy(p_client_buffer, "Running on AC Power",
502 strncpy(p_client_buffer, "Running on battery",
508 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
510 if (!p_client_buffer || client_buffer_size <= 0)
513 /* not implemented */
514 memset(p_client_buffer, 0, client_buffer_size);
518 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
520 if (!p_client_buffer || client_buffer_size <= 0)
523 /* not implemented */
524 memset(p_client_buffer, 0, client_buffer_size);
528 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
530 if (!p_client_buffer || client_buffer_size <= 0)
533 /* not implemented */
534 memset(p_client_buffer, 0, client_buffer_size);
537 /* rdtsc() and get_freq_dynamic() copied from linux.c */
539 #if defined(__i386) || defined(__x86_64)
540 __inline__ unsigned long long int
543 unsigned long long int x;
544 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
549 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
551 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
552 char *p_format, int divisor)
554 #if defined(__i386) || defined(__x86_64)
556 struct timeval tvstart, tvstop;
557 unsigned long long cycles[2]; /* gotta be 64 bit */
558 unsigned int microseconds; /* total time taken */
560 memset(&tz, 0, sizeof (tz));
562 /* get this function in cached memory */
563 gettimeofday(&tvstart, &tz);
565 gettimeofday(&tvstart, &tz);
567 /* we don't trust that this is any specific length of time */
570 gettimeofday(&tvstop, &tz);
571 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
572 (tvstop.tv_usec - tvstart.tv_usec);
574 snprintf(p_client_buffer, client_buffer_size, p_format,
575 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
577 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
583 get_freq(char *p_client_buffer, size_t client_buffer_size,
584 char *p_format, int divisor, unsigned int cpu)
589 freq_sysctl = (char *)calloc(16, sizeof(char));
590 if (freq_sysctl == NULL)
593 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
595 if (!p_client_buffer || client_buffer_size <= 0 ||
596 !p_format || divisor <= 0)
599 if (GETSYSCTL(freq_sysctl, freq) == 0)
600 snprintf(p_client_buffer, client_buffer_size,
601 p_format, (float)freq/divisor);
603 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
612 proc_find_top(info.cpu, info.memu);
619 struct ifreq ifr; /* interface stats */
621 struct net_stat * ns;
622 struct ifaddrs *ifap, *ifa;
623 struct ifmediareq ifmr;
629 if (getifaddrs(&ifap) < 0)
632 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
633 ns = get_net_stat((const char *) ifa->ifa_name);
635 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
638 bzero(&ifmr, sizeof(ifmr));
639 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
640 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
644 * We can monitor only wireless interfaces
645 * which not in hostap mode
647 if ((ifmr.ifm_active & IFM_IEEE80211) &&
648 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
650 bzero(&ifr, sizeof(ifr));
651 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
652 wireq.wi_type = WI_RID_COMMS_QUALITY;
653 wireq.wi_len = WI_MAX_DATALEN;
654 ifr.ifr_data = (void *) &wireq;
656 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
657 perror("ioctl (getting wi status)");
662 * wi_val[0] = quality
666 ns->linkstatus = (int) wireq.wi_val[1];
677 int devs_count, num_selected, num_selections, i;
678 struct device_selection *dev_select = NULL;
679 long select_generation;
681 static struct statinfo statinfo_cur;
682 u_int64_t diskio_current = 0;
684 bzero(&statinfo_cur, sizeof (statinfo_cur));
685 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
686 bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
688 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
691 devs_count = statinfo_cur.dinfo->numdevs;
692 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
693 &select_generation, statinfo_cur.dinfo->generation,
694 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
695 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
696 for (dn = 0; dn < devs_count; ++dn) {
700 di = dev_select[dn].position;
701 dev = &statinfo_cur.dinfo->devices[di];
703 diskio_current += dev->bytes[DEVSTAT_READ] +
704 dev->bytes[DEVSTAT_WRITE];
706 for (i = 0; i < MAX_DISKIO_STATS; i++) {
707 if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
708 diskio_stats[i].dev) == 0) {
709 diskio_stats[i].current =
710 (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE] -
711 diskio_stats[i].last) / 1024;
712 diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
713 diskio_stats[i].last_read) / 1024;
714 diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
715 diskio_stats[i].last_write) / 1024;
716 if (dev->bytes[DEVSTAT_READ] +
717 dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last) {
718 diskio_stats[i].current = 0;
720 if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
721 diskio_stats[i].current_read = 0;
722 diskio_stats[i].current = diskio_stats[i].current_write;
724 if (writes < diskio_stats[i].last_write) {
725 diskio_stats[i].current_write = 0;
726 diskio_stats[i].current = diskio_stats[i].current_read;
728 diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
729 dev->bytes[DEVSTAT_WRITE];
730 diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
731 diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
740 * Since we return (diskio_total_current - diskio_total_old), first
741 * frame will be way too high (it will be equal to
742 * diskio_total_current, i.e. all disk I/O since boot). That's why
743 * it is better to return 0 first time;
745 if (diskio_setup == 0) {
749 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
751 diskio_prev = diskio_current;
753 free(statinfo_cur.dinfo);
757 * While topless is obviously better, top is also not bad.
761 comparecpu(const void *a, const void *b)
763 if (((struct process *)a)->amount > ((struct process *)b)->amount)
766 if (((struct process *)a)->amount < ((struct process *)b)->amount)
773 comparemem(const void *a, const void *b)
775 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
778 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
785 proc_find_top(struct process **cpu, struct process **mem)
787 struct kinfo_proc *p;
790 struct process *processes;
794 /* we get total pages count again to be sure it is up to date */
795 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
796 CRIT_ERR("Cannot read sysctl"
797 "\"vm.stats.vm.v_page_count\"");
799 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
800 processes = malloc(n_processes * sizeof (struct process));
802 for (i = 0; i < n_processes; i++) {
803 if (!((p[i].ki_flag & P_SYSTEM)) &&
804 p[i].ki_comm != NULL) {
805 processes[j].pid = p[i].ki_pid;
806 processes[j].name = strdup(p[i].ki_comm);
807 processes[j].amount = 100.0 *
808 p[i].ki_pctcpu / FSCALE;
809 processes[j].totalmem = (float)(p[i].ki_rssize /
810 (float)total_pages) * 100.0;
815 qsort(processes, j - 1, sizeof (struct process), comparemem);
816 for (i = 0; i < 10 && i < n_processes; i++) {
817 struct process *tmp, *ttmp;
819 tmp = malloc(sizeof (struct process));
820 tmp->pid = processes[i].pid;
821 tmp->amount = processes[i].amount;
822 tmp->totalmem = processes[i].totalmem;
823 tmp->name = strdup(processes[i].name);
833 qsort(processes, j - 1, sizeof (struct process), comparecpu);
834 for (i = 0; i < 10 && i < n_processes; i++) {
835 struct process *tmp, *ttmp;
837 tmp = malloc(sizeof (struct process));
838 tmp->pid = processes[i].pid;
839 tmp->amount = processes[i].amount;
840 tmp->totalmem = processes[i].totalmem;
841 tmp->name = strdup(processes[i].name);
851 #if defined(FREEBSD_DEBUG)
852 printf("=====\nmem\n");
853 for (i = 0; i < 10; i++) {
854 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
855 mem[i]->pid, mem[i]->totalmem);
859 for (i = 0; i < j; free(processes[i++].name));
863 #if defined(i386) || defined(__i386__)
864 #define APMDEV "/dev/apm"
865 #define APM_UNKNOWN 255
868 apm_getinfo(int fd, apm_info_t aip)
870 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
880 struct apm_info info;
883 out = (char *)calloc(16, sizeof (char));
885 fd = open(APMDEV, O_RDONLY);
887 strncpy(out, "ERR", 16);
891 if (apm_getinfo(fd, &info) != 0) {
893 strncpy(out, "ERR", 16);
898 switch (info.ai_acline) {
900 strncpy(out, "off-line", 16);
904 if (info.ai_batt_stat == 3) {
905 strncpy(out, "charging", 16);
908 strncpy(out, "on-line", 16);
913 strncpy(out, "unknown", 16);
920 *get_apm_battery_life()
924 struct apm_info info;
927 out = (char *)calloc(16, sizeof (char));
929 fd = open(APMDEV, O_RDONLY);
931 strncpy(out, "ERR", 16);
935 if (apm_getinfo(fd, &info) != 0) {
937 strncpy(out, "ERR", 16);
942 batt_life = info.ai_batt_life;
943 if (batt_life == APM_UNKNOWN)
944 strncpy(out, "unknown", 16);
945 else if (batt_life <= 100) {
946 snprintf(out, 16, "%d%%", batt_life);
949 strncpy(out, "ERR", 16);
955 *get_apm_battery_time()
960 struct apm_info info;
963 out = (char *)calloc(16, sizeof (char));
965 fd = open(APMDEV, O_RDONLY);
967 strncpy(out, "ERR", 16);
971 if (apm_getinfo(fd, &info) != 0) {
973 strncpy(out, "ERR", 16);
978 batt_time = info.ai_batt_time;
981 strncpy(out, "unknown", 16);
988 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
996 void update_entropy (void)
998 /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
1001 /* empty stub so conky links */
1003 free_all_processes(void)