1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2007 Toni Spets
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/dkstat.h>
32 #include <sys/param.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <sys/types.h>
38 #include <sys/vmmeter.h>
40 #include <sys/ioctl.h>
41 #include <sys/sensors.h>
42 #include <sys/malloc.h>
47 #include <net/if_media.h>
48 #include <netinet/in.h>
56 #include <machine/apmvar.h>
58 #include <net80211/ieee80211.h>
59 #include <net80211/ieee80211_ioctl.h>
68 #define MAXSHOWDEVS 16
71 #define pagetok(size) ((size) << pageshift)
73 inline void proc_find_top(struct process **cpu, struct process **mem);
75 static short cpu_setup = 0;
78 struct ifmibdata *data = NULL;
90 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
92 NORM_ERR("error opening kvm");
100 /* note: swapmode taken from 'top' source */
101 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
102 * to be based on the new swapctl(2) system call. */
103 static int swapmode(int *used, int *total)
105 struct swapent *swdev;
106 int nswap, rnswap, i;
108 nswap = swapctl(SWAP_NSWAP, 0, 0);
113 swdev = malloc(nswap * sizeof(*swdev));
118 rnswap = swapctl(SWAP_STATS, swdev, nswap);
123 /* if rnswap != nswap, then what? */
125 /* Total things up */
127 for (i = 0; i < nswap; i++) {
128 if (swdev[i].se_flags & SWF_ENABLE) {
129 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
130 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
137 int check_mount(char *s)
145 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
146 struct timeval boottime;
148 size_t size = sizeof(boottime);
150 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
151 && (boottime.tv_sec != 0)) {
153 info.uptime = now - boottime.tv_sec;
155 NORM_ERR("Could not get uptime");
160 void update_meminfo()
162 static int mib[2] = { CTL_VM, VM_METER };
163 struct vmtotal vmtotal;
165 int pagesize, pageshift, swap_avail, swap_used;
167 pagesize = getpagesize();
169 while (pagesize > 1) {
174 /* we only need the amount of log(2)1024 for our conversion */
175 pageshift -= LOG1024;
177 /* get total -- systemwide main memory usage structure */
178 size = sizeof(vmtotal);
179 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
180 warn("sysctl failed");
181 bzero(&vmtotal, sizeof(vmtotal));
184 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
185 info.mem = pagetok(vmtotal.t_rm);
186 info.memeasyfree = info.memfree = info.memmax - info.mem;
188 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
189 info.swapmax = swap_avail;
190 info.swap = swap_used;
191 info.swapfree = swap_avail - swap_used;
199 void update_net_stats()
203 long long r, t, last_recv, last_trans;
204 struct ifaddrs *ifap, *ifa;
208 delta = current_update_time - last_update_time;
209 if (delta <= 0.0001) {
213 if (getifaddrs(&ifap) < 0) {
217 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
218 ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
220 if (ifa->ifa_flags & IFF_UP) {
221 struct ifaddrs *iftmp;
224 last_recv = ns->recv;
225 last_trans = ns->trans;
227 if (ifa->ifa_addr->sa_family != AF_LINK) {
231 for (iftmp = ifa->ifa_next;
232 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
233 iftmp = iftmp->ifa_next) {
234 if (iftmp->ifa_addr->sa_family == AF_INET) {
235 memcpy(&(ns->addr), iftmp->ifa_addr,
236 iftmp->ifa_addr->sa_len);
240 ifd = (struct if_data *) ifa->ifa_data;
244 if (r < ns->last_read_recv) {
245 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
247 ns->recv += (r - ns->last_read_recv);
250 ns->last_read_recv = r;
252 if (t < ns->last_read_trans) {
253 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
255 ns->trans += (t - ns->last_read_trans);
258 ns->last_read_trans = t;
260 /* calculate speeds */
261 ns->recv_speed = (ns->recv - last_recv) / delta;
262 ns->trans_speed = (ns->trans - last_trans) / delta;
271 void update_total_processes()
276 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
278 info.procs = n_processes;
281 void update_running_processes()
283 struct kinfo_proc2 *p;
288 int max_size = sizeof(struct kinfo_proc2);
290 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
291 for (i = 0; i < n_processes; i++) {
292 if (p[i].p_stat == SRUN) {
297 info.run_procs = cnt;
300 /* new SMP code can be enabled by commenting the following line */
304 struct cpu_load_struct {
305 unsigned long load[5];
308 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
309 long cpu_used, oldtotal, oldused;
312 int64_t *fresh = NULL;
314 /* XXX is 8 enough? - What's the constant for MAXCPU? */
315 /* allocate this with malloc would be better */
316 int64_t oldtotal[8], oldused[8];
321 int cpu_count = 1; /* default to 1 cpu */
323 int mib[2] = { CTL_HW, HW_NCPU };
324 size_t len = sizeof(cpu_count);
326 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
327 NORM_ERR("error getting cpu count, defaulting to 1");
330 info.cpu_count = cpu_count;
332 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
333 if (info.cpu_usage == NULL) {
334 CRIT_ERR(NULL, NULL, "malloc");
338 assert(fresh == NULL); /* XXX Is this leaking memory? */
339 /* XXX Where shall I free this? */
340 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
341 CRIT_ERR(NULL, NULL, "calloc");
346 void update_cpu_usage()
349 int mib[2] = { CTL_KERN, KERN_CPTIME };
351 long cp_time[CPUSTATES];
352 size_t len = sizeof(cp_time);
358 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
359 if ((cpu_setup == 0) || (!info.cpu_usage)) {
365 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
366 NORM_ERR("Cannot get kern.cp_time");
369 fresh.load[0] = cp_time[CP_USER];
370 fresh.load[1] = cp_time[CP_NICE];
371 fresh.load[2] = cp_time[CP_SYS];
372 fresh.load[3] = cp_time[CP_IDLE];
373 fresh.load[4] = cp_time[CP_IDLE];
375 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
376 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
378 if ((total - oldtotal) != 0) {
379 info.cpu_usage[0] = ((double) (used - oldused)) /
380 (double) (total - oldtotal);
382 info.cpu_usage[0] = 0;
388 if (info.cpu_count > 1) {
389 size = CPUSTATES * sizeof(int64_t);
390 for (i = 0; i < info.cpu_count; i++) {
391 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
392 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
394 NORM_ERR("sysctl kern.cp_time2 failed");
398 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
399 long cp_time_tmp[CPUSTATES];
401 size = sizeof(cp_time_tmp);
402 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
403 NORM_ERR("sysctl kern.cp_time failed");
406 for (i = 0; i < CPUSTATES; i++) {
407 fresh[i] = (int64_t) cp_time_tmp[i];
411 /* XXX Do sg with this int64_t => long => double ? float hell. */
412 for (i = 0; i < info.cpu_count; i++) {
414 int at = i * CPUSTATES;
416 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
417 total = used + fresh[at + CP_IDLE];
419 if ((total - oldtotal[i]) != 0) {
420 info.cpu_usage[i] = ((double) (used - oldused[i])) /
421 (double) (total - oldtotal[i]);
423 info.cpu_usage[i] = 0;
432 void update_load_average()
438 info.loadavg[0] = (float) v[0];
439 info.loadavg[1] = (float) v[1];
440 info.loadavg[2] = (float) v[2];
443 #define OBSD_MAX_SENSORS 256
444 static struct obsd_sensors_struct {
446 float temp[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
447 unsigned int fan[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
448 float volt[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
451 /* read sensors from sysctl */
452 void update_obsd_sensors()
454 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
455 struct sensor sensor;
456 struct sensordev sensordev;
458 enum sensor_type type;
460 slen = sizeof(sensor);
461 sdlen = sizeof(sensordev);
465 dev = obsd_sensors.device; // FIXME: read more than one device
467 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
469 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
470 if (errno != ENOENT) {
476 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
478 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
480 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
481 if (errno != ENOENT) {
486 if (sensor.flags & SENSOR_FINVALID) {
492 obsd_sensors.temp[dev][sensor.numt] =
493 (sensor.value - 273150000) / 1000000.0;
496 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
498 case SENSOR_VOLTS_DC:
499 obsd_sensors.volt[dev][sensor.numt] =
500 sensor.value / 1000000.0;
514 void parse_obsd_sensor(struct text_object *obj, const char *arg)
516 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
517 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
519 NORM_ERR("Invalid sensor number!");
521 obj->data.l = atoi(&arg[0]);
524 void print_obsd_sensors_temp(struct text_object *obj, char *p, int p_max_size)
526 obsd_sensors.device = sensor_device;
527 update_obsd_sensors();
528 temp_print(p, p_max_size,
529 obsd_sensors.temp[obsd_sensors.device][obj->data.l],
533 void print_obsd_sensors_fan(struct text_object *obj, char *p, int p_max_size)
535 obsd_sensors.device = sensor_device;
536 update_obsd_sensors();
537 snprintf(p, p_max_size, "%d",
538 obsd_sensors.fan[obsd_sensors.device][obj->data.l]);
541 void print_obsd_sensors_volt(struct text_object *obj, char *p, int p_max_size)
543 obsd_sensors.device = sensor_device;
544 update_obsd_sensors();
545 snprintf(p, p_max_size, "%.2f",
546 obsd_sensors.volt[obsd_sensors.device][obj->data.l]);
550 void get_obsd_vendor(char *buf, size_t client_buffer_size)
557 size_t size = sizeof(vendor);
559 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
560 NORM_ERR("error reading vendor");
561 snprintf(buf, client_buffer_size, "unknown");
563 snprintf(buf, client_buffer_size, "%s", vendor);
568 void get_obsd_product(char *buf, size_t client_buffer_size)
575 size_t size = sizeof(product);
577 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
578 NORM_ERR("error reading product");
579 snprintf(buf, client_buffer_size, "unknown");
581 snprintf(buf, client_buffer_size, "%s", product);
586 char get_freq(char *p_client_buffer, size_t client_buffer_size,
587 const char *p_format, int divisor, unsigned int cpu)
590 int mib[2] = { CTL_HW, HW_CPUSPEED };
592 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
597 size_t size = sizeof(freq);
599 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
600 snprintf(p_client_buffer, client_buffer_size, p_format,
601 (float) freq / divisor);
603 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
612 proc_find_top(info.cpu, info.memu);
616 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
617 void update_wifi_stats()
620 struct ifaddrs *ifap, *ifa;
621 struct ifmediareq ifmr;
622 struct ieee80211_nodereq nr;
623 struct ieee80211_bssid bssid;
626 /* Get iface table */
627 if (getifaddrs(&ifap) < 0) {
631 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
632 ns = get_net_stat((const char *) ifa->ifa_name);
634 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
637 bzero(&ifmr, sizeof(ifmr));
638 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
639 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
644 /* We can monitor only wireless interfaces
645 * which are not in hostap mode */
646 if ((ifmr.ifm_active & IFM_IEEE80211)
647 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
650 memset(&bssid, 0, sizeof(bssid));
651 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
652 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
654 bzero(&nr, sizeof(nr));
655 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
656 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
658 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
659 ns->linkstatus = nr.nr_rssi;
668 void clear_diskio_stats()
672 struct diskio_stat *prepare_diskio_stat(const char *s)
678 return; /* XXX: implement? hifi: not sure how */
681 /* While topless is obviously better, top is also not bad. */
683 int comparecpu(const void *a, const void *b)
685 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
689 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
696 int comparemem(const void *a, const void *b)
698 if (((struct process *) a)->rss > ((struct process *) b)->rss) {
702 if (((struct process *) a)->rss < ((struct process *) b)->rss) {
709 inline void proc_find_top(struct process **cpu, struct process **mem)
711 struct kinfo_proc2 *p;
714 struct process *processes;
719 int pagesize = getpagesize();
721 /* we get total pages count again to be sure it is up to date */
723 mib[1] = HW_USERMEM64;
724 size_t size = sizeof(usermem);
726 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
727 NORM_ERR("error reading usermem");
730 /* translate bytes into page count */
731 total_pages = usermem / pagesize;
733 int max_size = sizeof(struct kinfo_proc2);
735 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
736 processes = malloc(n_processes * sizeof(struct process));
738 for (i = 0; i < n_processes; i++) {
739 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
740 processes[j].pid = p[i].p_pid;
741 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
742 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
747 qsort(processes, j - 1, sizeof(struct process), comparemem);
748 for (i = 0; i < 10; i++) {
749 struct process *tmp, *ttmp;
751 tmp = malloc(sizeof(struct process));
752 tmp->pid = processes[i].pid;
753 tmp->amount = processes[i].amount;
754 tmp->name = strndup(processes[i].name, text_buffer_size);
764 qsort(processes, j - 1, sizeof(struct process), comparecpu);
765 for (i = 0; i < 10; i++) {
766 struct process *tmp, *ttmp;
768 tmp = malloc(sizeof(struct process));
769 tmp->pid = processes[i].pid;
770 tmp->amount = processes[i].amount;
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 int get_entropy_avail(unsigned int *val)
923 int get_entropy_poolsize(unsigned int *val)
928 void free_all_processes(void)