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) 2007 Toni Spets
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * 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>
38 #include <sys/sensors.h>
39 #include <sys/malloc.h>
44 #include <net/if_media.h>
45 #include <netinet/in.h>
53 #include <machine/apmvar.h>
55 #include <net80211/ieee80211.h>
56 #include <net80211/ieee80211_ioctl.h>
64 #define MAXSHOWDEVS 16
67 #define pagetok(size) ((size) << pageshift)
69 inline void proc_find_top(struct process **cpu, struct process **mem);
71 static short cpu_setup = 0;
74 struct ifmibdata *data = NULL;
86 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
88 ERR("error opening kvm");
96 /* note: swapmode taken from 'top' source */
97 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
98 * to be based on the new swapctl(2) system call. */
99 static int swapmode(int *used, int *total)
101 struct swapent *swdev;
102 int nswap, rnswap, i;
104 nswap = swapctl(SWAP_NSWAP, 0, 0);
109 swdev = malloc(nswap * sizeof(*swdev));
114 rnswap = swapctl(SWAP_STATS, swdev, nswap);
119 /* if rnswap != nswap, then what? */
121 /* Total things up */
123 for (i = 0; i < nswap; i++) {
124 if (swdev[i].se_flags & SWF_ENABLE) {
125 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
126 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
133 int check_mount(char *s)
141 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
142 struct timeval boottime;
144 size_t size = sizeof(boottime);
146 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
147 && (boottime.tv_sec != 0)) {
149 info.uptime = now - boottime.tv_sec;
151 ERR("Could not get uptime");
156 void update_meminfo()
158 static int mib[2] = { CTL_VM, VM_METER };
159 struct vmtotal vmtotal;
161 int pagesize, pageshift, swap_avail, swap_used;
163 pagesize = getpagesize();
165 while (pagesize > 1) {
170 /* we only need the amount of log(2)1024 for our conversion */
171 pageshift -= LOG1024;
173 /* get total -- systemwide main memory usage structure */
174 size = sizeof(vmtotal);
175 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
176 warn("sysctl failed");
177 bzero(&vmtotal, sizeof(vmtotal));
180 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
181 info.mem = pagetok(vmtotal.t_rm);
182 info.memeasyfree = info.memfree = info.memmax - info.mem;
184 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
185 info.swapmax = swap_avail;
186 info.swap = swap_used;
187 info.swapfree = swap_avail - swap_used;
195 void update_net_stats()
199 long long r, t, last_recv, last_trans;
200 struct ifaddrs *ifap, *ifa;
204 delta = current_update_time - last_update_time;
205 if (delta <= 0.0001) {
209 if (getifaddrs(&ifap) < 0) {
213 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
214 ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
216 if (ifa->ifa_flags & IFF_UP) {
217 struct ifaddrs *iftmp;
220 last_recv = ns->recv;
221 last_trans = ns->trans;
223 if (ifa->ifa_addr->sa_family != AF_LINK) {
227 for (iftmp = ifa->ifa_next;
228 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
229 iftmp = iftmp->ifa_next) {
230 if (iftmp->ifa_addr->sa_family == AF_INET) {
231 memcpy(&(ns->addr), iftmp->ifa_addr,
232 iftmp->ifa_addr->sa_len);
236 ifd = (struct if_data *) ifa->ifa_data;
240 if (r < ns->last_read_recv) {
241 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
243 ns->recv += (r - ns->last_read_recv);
246 ns->last_read_recv = r;
248 if (t < ns->last_read_trans) {
249 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
251 ns->trans += (t - ns->last_read_trans);
254 ns->last_read_trans = t;
256 /* calculate speeds */
257 ns->recv_speed = (ns->recv - last_recv) / delta;
258 ns->trans_speed = (ns->trans - last_trans) / delta;
267 void update_total_processes()
272 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
274 info.procs = n_processes;
277 void update_running_processes()
279 struct kinfo_proc2 *p;
284 int max_size = sizeof(struct kinfo_proc2);
286 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
287 for (i = 0; i < n_processes; i++) {
288 if (p[i].p_stat == SRUN) {
293 info.run_procs = cnt;
296 /* new SMP code can be enabled by commenting the following line */
300 struct cpu_load_struct {
301 unsigned long load[5];
304 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
305 long cpu_used, oldtotal, oldused;
308 int64_t *fresh = NULL;
310 /* XXX is 8 enough? - What's the constant for MAXCPU? */
311 /* allocate this with malloc would be better */
312 int64_t oldtotal[8], oldused[8];
317 int cpu_count = 1; /* default to 1 cpu */
319 int mib[2] = { CTL_HW, HW_NCPU };
320 size_t len = sizeof(cpu_count);
322 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
323 ERR("error getting cpu count, defaulting to 1");
326 info.cpu_count = cpu_count;
328 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
329 if (info.cpu_usage == NULL) {
330 CRIT_ERR(NULL, NULL, "malloc");
334 assert(fresh == NULL); /* XXX Is this leaking memory? */
335 /* XXX Where shall I free this? */
336 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
337 CRIT_ERR(NULL, NULL, "calloc");
342 void update_cpu_usage()
345 int mib[2] = { CTL_KERN, KERN_CPTIME };
347 long cp_time[CPUSTATES];
348 size_t len = sizeof(cp_time);
354 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
355 if ((cpu_setup == 0) || (!info.cpu_usage)) {
361 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
362 ERR("Cannot get kern.cp_time");
365 fresh.load[0] = cp_time[CP_USER];
366 fresh.load[1] = cp_time[CP_NICE];
367 fresh.load[2] = cp_time[CP_SYS];
368 fresh.load[3] = cp_time[CP_IDLE];
369 fresh.load[4] = cp_time[CP_IDLE];
371 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
372 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
374 if ((total - oldtotal) != 0) {
375 info.cpu_usage[0] = ((double) (used - oldused)) /
376 (double) (total - oldtotal);
378 info.cpu_usage[0] = 0;
384 if (info.cpu_count > 1) {
385 size = CPUSTATES * sizeof(int64_t);
386 for (i = 0; i < info.cpu_count; i++) {
387 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
388 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
390 ERR("sysctl kern.cp_time2 failed");
394 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
395 long cp_time_tmp[CPUSTATES];
397 size = sizeof(cp_time_tmp);
398 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
399 ERR("sysctl kern.cp_time failed");
402 for (i = 0; i < CPUSTATES; i++) {
403 fresh[i] = (int64_t) cp_time_tmp[i];
407 /* XXX Do sg with this int64_t => long => double ? float hell. */
408 for (i = 0; i < info.cpu_count; i++) {
410 int at = i * CPUSTATES;
412 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
413 total = used + fresh[at + CP_IDLE];
415 if ((total - oldtotal[i]) != 0) {
416 info.cpu_usage[i] = ((double) (used - oldused[i])) /
417 (double) (total - oldtotal[i]);
419 info.cpu_usage[i] = 0;
428 void update_load_average()
434 info.loadavg[0] = (float) v[0];
435 info.loadavg[1] = (float) v[1];
436 info.loadavg[2] = (float) v[2];
439 /* read sensors from sysctl */
440 void update_obsd_sensors()
442 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
443 struct sensor sensor;
444 struct sensordev sensordev;
446 enum sensor_type type;
448 slen = sizeof(sensor);
449 sdlen = sizeof(sensordev);
453 dev = obsd_sensors.device; // FIXME: read more than one device
455 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
457 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
458 if (errno != ENOENT) {
464 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
466 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
468 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
469 if (errno != ENOENT) {
474 if (sensor.flags & SENSOR_FINVALID) {
480 obsd_sensors.temp[dev][sensor.numt] =
481 (sensor.value - 273150000) / 1000000.0;
484 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
486 case SENSOR_VOLTS_DC:
487 obsd_sensors.volt[dev][sensor.numt] =
488 sensor.value / 1000000.0;
503 void get_obsd_vendor(char *buf, size_t client_buffer_size)
510 size_t size = sizeof(vendor);
512 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
513 ERR("error reading vendor");
514 snprintf(buf, client_buffer_size, "unknown");
516 snprintf(buf, client_buffer_size, "%s", vendor);
521 void get_obsd_product(char *buf, size_t client_buffer_size)
528 size_t size = sizeof(product);
530 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
531 ERR("error reading product");
532 snprintf(buf, client_buffer_size, "unknown");
534 snprintf(buf, client_buffer_size, "%s", product);
538 /* rdtsc() and get_freq_dynamic() copied from linux.c */
540 #if defined(__i386) || defined(__x86_64)
541 __inline__ unsigned long long int rdtsc()
543 unsigned long long int x;
545 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
550 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
551 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
552 const 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);
582 char get_freq(char *p_client_buffer, size_t client_buffer_size,
583 const char *p_format, int divisor, unsigned int cpu)
586 int mib[2] = { CTL_HW, HW_CPUSPEED };
588 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
593 size_t size = sizeof(freq);
595 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
596 snprintf(p_client_buffer, client_buffer_size, p_format,
597 (float) freq / divisor);
599 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
608 proc_find_top(info.cpu, info.memu);
612 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
613 void update_wifi_stats()
616 struct ifaddrs *ifap, *ifa;
617 struct ifmediareq ifmr;
618 struct ieee80211_nodereq nr;
619 struct ieee80211_bssid bssid;
622 /* Get iface table */
623 if (getifaddrs(&ifap) < 0) {
627 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
628 ns = get_net_stat((const char *) ifa->ifa_name);
630 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
633 bzero(&ifmr, sizeof(ifmr));
634 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
635 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
640 /* We can monitor only wireless interfaces
641 * which are not in hostap mode */
642 if ((ifmr.ifm_active & IFM_IEEE80211)
643 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
646 memset(&bssid, 0, sizeof(bssid));
647 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
648 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
650 bzero(&nr, sizeof(nr));
651 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
652 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
654 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
655 ns->linkstatus = nr.nr_rssi;
664 void clear_diskio_stats()
668 struct diskio_stat *prepare_diskio_stat(const char *s)
674 return; /* XXX: implement? hifi: not sure how */
677 /* While topless is obviously better, top is also not bad. */
679 int comparecpu(const void *a, const void *b)
681 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
685 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
692 int comparemem(const void *a, const void *b)
694 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
698 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
705 inline void proc_find_top(struct process **cpu, struct process **mem)
707 struct kinfo_proc2 *p;
710 struct process *processes;
715 int pagesize = getpagesize();
717 /* we get total pages count again to be sure it is up to date */
719 mib[1] = HW_USERMEM64;
720 size_t size = sizeof(usermem);
722 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
723 ERR("error reading usermem");
726 /* translate bytes into page count */
727 total_pages = usermem / pagesize;
729 int max_size = sizeof(struct kinfo_proc2);
731 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
732 processes = malloc(n_processes * sizeof(struct process));
734 for (i = 0; i < n_processes; i++) {
735 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
736 processes[j].pid = p[i].p_pid;
737 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
738 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
739 processes[j].totalmem = (float) (p[i].p_vm_rssize /
740 (float) total_pages) * 100.0;
745 qsort(processes, j - 1, sizeof(struct process), comparemem);
746 for (i = 0; i < 10; i++) {
747 struct process *tmp, *ttmp;
749 tmp = malloc(sizeof(struct process));
750 tmp->pid = processes[i].pid;
751 tmp->amount = processes[i].amount;
752 tmp->totalmem = processes[i].totalmem;
753 tmp->name = strndup(processes[i].name, text_buffer_size);
763 qsort(processes, j - 1, sizeof(struct process), comparecpu);
764 for (i = 0; i < 10; i++) {
765 struct process *tmp, *ttmp;
767 tmp = malloc(sizeof(struct process));
768 tmp->pid = processes[i].pid;
769 tmp->amount = processes[i].amount;
770 tmp->totalmem = processes[i].totalmem;
771 tmp->name = strndup(processes[i].name, text_buffer_size);
781 for (i = 0; i < j; i++) {
782 free(processes[i].name);
787 #if defined(i386) || defined(__i386__)
788 #define APMDEV "/dev/apm"
789 #define APM_UNKNOWN 255
791 int apm_getinfo(int fd, apm_info_t aip)
793 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
800 char *get_apm_adapter()
803 struct apm_power_info info;
806 out = (char *) calloc(16, sizeof(char));
808 fd = open(APMDEV, O_RDONLY);
810 strncpy(out, "ERR", 16);
814 if (apm_getinfo(fd, &info) != 0) {
816 strncpy(out, "ERR", 16);
821 switch (info.ac_state) {
823 strncpy(out, "off-line", 16);
827 if (info.battery_state == APM_BATT_CHARGING) {
828 strncpy(out, "charging", 16);
831 strncpy(out, "on-line", 16);
836 strncpy(out, "unknown", 16);
842 char *get_apm_battery_life()
846 struct apm_power_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 batt_life = info.battery_life;
865 if (batt_life <= 100) {
866 snprintf(out, 16, "%d%%", batt_life);
869 strncpy(out, "ERR", 16);
875 char *get_apm_battery_time()
880 struct apm_power_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 batt_time = info.minutes_left;
900 if (batt_time == -1) {
901 strncpy(out, "unknown", 16);
905 snprintf(out, 16, "%2d:%02d", h, m);
913 /* empty stubs so conky links */
914 void prepare_update()
918 void update_entropy(void)
922 void free_all_processes(void)