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-2009 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);
585 /* rdtsc() and get_freq_dynamic() copied from linux.c */
587 #if defined(__i386) || defined(__x86_64)
588 __inline__ unsigned long long int rdtsc()
590 unsigned long long int x;
592 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
597 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
598 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
599 const char *p_format, int divisor)
601 #if defined(__i386) || defined(__x86_64)
603 struct timeval tvstart, tvstop;
604 unsigned long long cycles[2]; /* gotta be 64 bit */
605 unsigned int microseconds; /* total time taken */
607 memset(&tz, 0, sizeof(tz));
609 /* get this function in cached memory */
610 gettimeofday(&tvstart, &tz);
612 gettimeofday(&tvstart, &tz);
614 /* we don't trust that this is any specific length of time */
617 gettimeofday(&tvstop, &tz);
618 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
619 (tvstop.tv_usec - tvstart.tv_usec);
621 snprintf(p_client_buffer, client_buffer_size, p_format,
622 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
624 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
629 char get_freq(char *p_client_buffer, size_t client_buffer_size,
630 const char *p_format, int divisor, unsigned int cpu)
633 int mib[2] = { CTL_HW, HW_CPUSPEED };
635 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
640 size_t size = sizeof(freq);
642 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
643 snprintf(p_client_buffer, client_buffer_size, p_format,
644 (float) freq / divisor);
646 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
655 proc_find_top(info.cpu, info.memu);
659 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
660 void update_wifi_stats()
663 struct ifaddrs *ifap, *ifa;
664 struct ifmediareq ifmr;
665 struct ieee80211_nodereq nr;
666 struct ieee80211_bssid bssid;
669 /* Get iface table */
670 if (getifaddrs(&ifap) < 0) {
674 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
675 ns = get_net_stat((const char *) ifa->ifa_name);
677 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
680 bzero(&ifmr, sizeof(ifmr));
681 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
682 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
687 /* We can monitor only wireless interfaces
688 * which are not in hostap mode */
689 if ((ifmr.ifm_active & IFM_IEEE80211)
690 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
693 memset(&bssid, 0, sizeof(bssid));
694 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
695 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
697 bzero(&nr, sizeof(nr));
698 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
699 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
701 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
702 ns->linkstatus = nr.nr_rssi;
711 void clear_diskio_stats()
715 struct diskio_stat *prepare_diskio_stat(const char *s)
721 return; /* XXX: implement? hifi: not sure how */
724 /* While topless is obviously better, top is also not bad. */
726 int comparecpu(const void *a, const void *b)
728 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
732 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
739 int comparemem(const void *a, const void *b)
741 if (((struct process *) a)->rss > ((struct process *) b)->rss) {
745 if (((struct process *) a)->rss < ((struct process *) b)->rss) {
752 inline void proc_find_top(struct process **cpu, struct process **mem)
754 struct kinfo_proc2 *p;
757 struct process *processes;
762 int pagesize = getpagesize();
764 /* we get total pages count again to be sure it is up to date */
766 mib[1] = HW_USERMEM64;
767 size_t size = sizeof(usermem);
769 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
770 NORM_ERR("error reading usermem");
773 /* translate bytes into page count */
774 total_pages = usermem / pagesize;
776 int max_size = sizeof(struct kinfo_proc2);
778 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
779 processes = malloc(n_processes * sizeof(struct process));
781 for (i = 0; i < n_processes; i++) {
782 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
783 processes[j].pid = p[i].p_pid;
784 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
785 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
790 qsort(processes, j - 1, sizeof(struct process), comparemem);
791 for (i = 0; i < 10; i++) {
792 struct process *tmp, *ttmp;
794 tmp = malloc(sizeof(struct process));
795 tmp->pid = processes[i].pid;
796 tmp->amount = processes[i].amount;
797 tmp->name = strndup(processes[i].name, text_buffer_size);
807 qsort(processes, j - 1, sizeof(struct process), comparecpu);
808 for (i = 0; i < 10; i++) {
809 struct process *tmp, *ttmp;
811 tmp = malloc(sizeof(struct process));
812 tmp->pid = processes[i].pid;
813 tmp->amount = processes[i].amount;
814 tmp->name = strndup(processes[i].name, text_buffer_size);
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, APM_IOC_GETPOWER, aip) == -1) {
843 char *get_apm_adapter()
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 switch (info.ac_state) {
866 strncpy(out, "off-line", 16);
870 if (info.battery_state == APM_BATT_CHARGING) {
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_power_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.battery_life;
908 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_power_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.minutes_left;
943 if (batt_time == -1) {
944 strncpy(out, "unknown", 16);
948 snprintf(out, 16, "%2d:%02d", h, m);
956 /* empty stubs so conky links */
957 void prepare_update()
961 void update_entropy(void)
965 void free_all_processes(void)