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/>.
26 * vim: ts=4 sw=4 noet ai cindent syntax=c
30 #include <sys/dkstat.h>
31 #include <sys/param.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
36 #include <sys/types.h>
37 #include <sys/vmmeter.h>
39 #include <sys/ioctl.h>
40 #include <sys/sensors.h>
41 #include <sys/malloc.h>
46 #include <net/if_media.h>
47 #include <netinet/in.h>
55 #include <machine/apmvar.h>
57 #include <net80211/ieee80211.h>
58 #include <net80211/ieee80211_ioctl.h>
66 #define MAXSHOWDEVS 16
69 #define pagetok(size) ((size) << pageshift)
71 inline void proc_find_top(struct process **cpu, struct process **mem);
73 static short cpu_setup = 0;
76 struct ifmibdata *data = NULL;
88 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
90 ERR("error opening kvm");
98 /* note: swapmode taken from 'top' source */
99 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
100 * to be based on the new swapctl(2) system call. */
101 static int swapmode(int *used, int *total)
103 struct swapent *swdev;
104 int nswap, rnswap, i;
106 nswap = swapctl(SWAP_NSWAP, 0, 0);
111 swdev = malloc(nswap * sizeof(*swdev));
116 rnswap = swapctl(SWAP_STATS, swdev, nswap);
121 /* if rnswap != nswap, then what? */
123 /* Total things up */
125 for (i = 0; i < nswap; i++) {
126 if (swdev[i].se_flags & SWF_ENABLE) {
127 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
128 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
135 int check_mount(char *s)
143 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
144 struct timeval boottime;
146 size_t size = sizeof(boottime);
148 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
149 && (boottime.tv_sec != 0)) {
151 info.uptime = now - boottime.tv_sec;
153 ERR("Could not get uptime");
158 void update_meminfo()
160 static int mib[2] = { CTL_VM, VM_METER };
161 struct vmtotal vmtotal;
163 int pagesize, pageshift, swap_avail, swap_used;
165 pagesize = getpagesize();
167 while (pagesize > 1) {
172 /* we only need the amount of log(2)1024 for our conversion */
173 pageshift -= LOG1024;
175 /* get total -- systemwide main memory usage structure */
176 size = sizeof(vmtotal);
177 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
178 warn("sysctl failed");
179 bzero(&vmtotal, sizeof(vmtotal));
182 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
183 info.mem = pagetok(vmtotal.t_rm);
184 info.memeasyfree = info.memfree = info.memmax - info.mem;
186 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
187 info.swapmax = swap_avail;
188 info.swap = swap_used;
189 info.swapfree = swap_avail - swap_used;
197 void update_net_stats()
201 long long r, t, last_recv, last_trans;
202 struct ifaddrs *ifap, *ifa;
206 delta = current_update_time - last_update_time;
207 if (delta <= 0.0001) {
211 if (getifaddrs(&ifap) < 0) {
215 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
216 ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
218 if (ifa->ifa_flags & IFF_UP) {
219 struct ifaddrs *iftmp;
222 last_recv = ns->recv;
223 last_trans = ns->trans;
225 if (ifa->ifa_addr->sa_family != AF_LINK) {
229 for (iftmp = ifa->ifa_next;
230 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
231 iftmp = iftmp->ifa_next) {
232 if (iftmp->ifa_addr->sa_family == AF_INET) {
233 memcpy(&(ns->addr), iftmp->ifa_addr,
234 iftmp->ifa_addr->sa_len);
238 ifd = (struct if_data *) ifa->ifa_data;
242 if (r < ns->last_read_recv) {
243 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
245 ns->recv += (r - ns->last_read_recv);
248 ns->last_read_recv = r;
250 if (t < ns->last_read_trans) {
251 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
253 ns->trans += (t - ns->last_read_trans);
256 ns->last_read_trans = t;
258 /* calculate speeds */
259 ns->recv_speed = (ns->recv - last_recv) / delta;
260 ns->trans_speed = (ns->trans - last_trans) / delta;
269 void update_total_processes()
274 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
276 info.procs = n_processes;
279 void update_running_processes()
281 struct kinfo_proc2 *p;
286 int max_size = sizeof(struct kinfo_proc2);
288 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
289 for (i = 0; i < n_processes; i++) {
290 if (p[i].p_stat == SRUN) {
295 info.run_procs = cnt;
298 /* new SMP code can be enabled by commenting the following line */
302 struct cpu_load_struct {
303 unsigned long load[5];
306 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
307 long cpu_used, oldtotal, oldused;
310 int64_t *fresh = NULL;
312 /* XXX is 8 enough? - What's the constant for MAXCPU? */
313 /* allocate this with malloc would be better */
314 int64_t oldtotal[8], oldused[8];
319 int cpu_count = 1; /* default to 1 cpu */
321 int mib[2] = { CTL_HW, HW_NCPU };
322 size_t len = sizeof(cpu_count);
324 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
325 ERR("error getting cpu count, defaulting to 1");
328 info.cpu_count = cpu_count;
330 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
331 if (info.cpu_usage == NULL) {
332 CRIT_ERR(NULL, NULL, "malloc");
336 assert(fresh == NULL); /* XXX Is this leaking memory? */
337 /* XXX Where shall I free this? */
338 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
339 CRIT_ERR(NULL, NULL, "calloc");
344 void update_cpu_usage()
347 int mib[2] = { CTL_KERN, KERN_CPTIME };
349 long cp_time[CPUSTATES];
350 size_t len = sizeof(cp_time);
356 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
357 if ((cpu_setup == 0) || (!info.cpu_usage)) {
363 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
364 ERR("Cannot get kern.cp_time");
367 fresh.load[0] = cp_time[CP_USER];
368 fresh.load[1] = cp_time[CP_NICE];
369 fresh.load[2] = cp_time[CP_SYS];
370 fresh.load[3] = cp_time[CP_IDLE];
371 fresh.load[4] = cp_time[CP_IDLE];
373 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
374 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
376 if ((total - oldtotal) != 0) {
377 info.cpu_usage[0] = ((double) (used - oldused)) /
378 (double) (total - oldtotal);
380 info.cpu_usage[0] = 0;
386 if (info.cpu_count > 1) {
387 size = CPUSTATES * sizeof(int64_t);
388 for (i = 0; i < info.cpu_count; i++) {
389 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
390 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
392 ERR("sysctl kern.cp_time2 failed");
396 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
397 long cp_time_tmp[CPUSTATES];
399 size = sizeof(cp_time_tmp);
400 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
401 ERR("sysctl kern.cp_time failed");
404 for (i = 0; i < CPUSTATES; i++) {
405 fresh[i] = (int64_t) cp_time_tmp[i];
409 /* XXX Do sg with this int64_t => long => double ? float hell. */
410 for (i = 0; i < info.cpu_count; i++) {
412 int at = i * CPUSTATES;
414 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
415 total = used + fresh[at + CP_IDLE];
417 if ((total - oldtotal[i]) != 0) {
418 info.cpu_usage[i] = ((double) (used - oldused[i])) /
419 (double) (total - oldtotal[i]);
421 info.cpu_usage[i] = 0;
430 void update_load_average()
436 info.loadavg[0] = (float) v[0];
437 info.loadavg[1] = (float) v[1];
438 info.loadavg[2] = (float) v[2];
441 /* read sensors from sysctl */
442 void update_obsd_sensors()
444 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
445 struct sensor sensor;
446 struct sensordev sensordev;
448 enum sensor_type type;
450 slen = sizeof(sensor);
451 sdlen = sizeof(sensordev);
455 dev = obsd_sensors.device; // FIXME: read more than one device
457 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
459 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
460 if (errno != ENOENT) {
466 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
468 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
470 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
471 if (errno != ENOENT) {
476 if (sensor.flags & SENSOR_FINVALID) {
482 obsd_sensors.temp[dev][sensor.numt] =
483 (sensor.value - 273150000) / 1000000.0;
486 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
488 case SENSOR_VOLTS_DC:
489 obsd_sensors.volt[dev][sensor.numt] =
490 sensor.value / 1000000.0;
505 void get_obsd_vendor(char *buf, size_t client_buffer_size)
512 size_t size = sizeof(vendor);
514 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
515 ERR("error reading vendor");
516 snprintf(buf, client_buffer_size, "unknown");
518 snprintf(buf, client_buffer_size, "%s", vendor);
523 void get_obsd_product(char *buf, size_t client_buffer_size)
530 size_t size = sizeof(product);
532 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
533 ERR("error reading product");
534 snprintf(buf, client_buffer_size, "unknown");
536 snprintf(buf, client_buffer_size, "%s", product);
540 /* rdtsc() and get_freq_dynamic() copied from linux.c */
542 #if defined(__i386) || defined(__x86_64)
543 __inline__ unsigned long long int rdtsc()
545 unsigned long long int x;
547 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
552 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
553 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
554 const char *p_format, int divisor)
556 #if defined(__i386) || defined(__x86_64)
558 struct timeval tvstart, tvstop;
559 unsigned long long cycles[2]; /* gotta be 64 bit */
560 unsigned int microseconds; /* total time taken */
562 memset(&tz, 0, sizeof(tz));
564 /* get this function in cached memory */
565 gettimeofday(&tvstart, &tz);
567 gettimeofday(&tvstart, &tz);
569 /* we don't trust that this is any specific length of time */
572 gettimeofday(&tvstop, &tz);
573 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
574 (tvstop.tv_usec - tvstart.tv_usec);
576 snprintf(p_client_buffer, client_buffer_size, p_format,
577 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
579 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
584 char get_freq(char *p_client_buffer, size_t client_buffer_size,
585 const char *p_format, int divisor, unsigned int cpu)
588 int mib[2] = { CTL_HW, HW_CPUSPEED };
590 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
595 size_t size = sizeof(freq);
597 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
598 snprintf(p_client_buffer, client_buffer_size, p_format,
599 (float) freq / divisor);
601 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
610 proc_find_top(info.cpu, info.memu);
614 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
615 void update_wifi_stats()
618 struct ifaddrs *ifap, *ifa;
619 struct ifmediareq ifmr;
620 struct ieee80211_nodereq nr;
621 struct ieee80211_bssid bssid;
624 /* Get iface table */
625 if (getifaddrs(&ifap) < 0) {
629 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
630 ns = get_net_stat((const char *) ifa->ifa_name);
632 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
635 bzero(&ifmr, sizeof(ifmr));
636 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
637 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
642 /* We can monitor only wireless interfaces
643 * which are not in hostap mode */
644 if ((ifmr.ifm_active & IFM_IEEE80211)
645 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
648 memset(&bssid, 0, sizeof(bssid));
649 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
650 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
652 bzero(&nr, sizeof(nr));
653 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
654 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
656 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
657 ns->linkstatus = nr.nr_rssi;
666 void clear_diskio_stats()
670 struct diskio_stat *prepare_diskio_stat(const char *s)
676 return; /* XXX: implement? hifi: not sure how */
679 /* While topless is obviously better, top is also not bad. */
681 int comparecpu(const void *a, const void *b)
683 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
687 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
694 int comparemem(const void *a, const void *b)
696 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
700 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
707 inline void proc_find_top(struct process **cpu, struct process **mem)
709 struct kinfo_proc2 *p;
712 struct process *processes;
717 int pagesize = getpagesize();
719 /* we get total pages count again to be sure it is up to date */
721 mib[1] = HW_USERMEM64;
722 size_t size = sizeof(usermem);
724 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
725 ERR("error reading usermem");
728 /* translate bytes into page count */
729 total_pages = usermem / pagesize;
731 int max_size = sizeof(struct kinfo_proc2);
733 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
734 processes = malloc(n_processes * sizeof(struct process));
736 for (i = 0; i < n_processes; i++) {
737 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
738 processes[j].pid = p[i].p_pid;
739 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
740 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
741 processes[j].totalmem = (float) (p[i].p_vm_rssize /
742 (float) total_pages) * 100.0;
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->totalmem = processes[i].totalmem;
755 tmp->name = strndup(processes[i].name, text_buffer_size);
765 qsort(processes, j - 1, sizeof(struct process), comparecpu);
766 for (i = 0; i < 10; i++) {
767 struct process *tmp, *ttmp;
769 tmp = malloc(sizeof(struct process));
770 tmp->pid = processes[i].pid;
771 tmp->amount = processes[i].amount;
772 tmp->totalmem = processes[i].totalmem;
773 tmp->name = strndup(processes[i].name, text_buffer_size);
783 for (i = 0; i < j; i++) {
784 free(processes[i].name);
789 #if defined(i386) || defined(__i386__)
790 #define APMDEV "/dev/apm"
791 #define APM_UNKNOWN 255
793 int apm_getinfo(int fd, apm_info_t aip)
795 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
802 char *get_apm_adapter()
805 struct apm_power_info info;
808 out = (char *) calloc(16, sizeof(char));
810 fd = open(APMDEV, O_RDONLY);
812 strncpy(out, "ERR", 16);
816 if (apm_getinfo(fd, &info) != 0) {
818 strncpy(out, "ERR", 16);
823 switch (info.ac_state) {
825 strncpy(out, "off-line", 16);
829 if (info.battery_state == APM_BATT_CHARGING) {
830 strncpy(out, "charging", 16);
833 strncpy(out, "on-line", 16);
838 strncpy(out, "unknown", 16);
844 char *get_apm_battery_life()
848 struct apm_power_info info;
851 out = (char *) calloc(16, sizeof(char));
853 fd = open(APMDEV, O_RDONLY);
855 strncpy(out, "ERR", 16);
859 if (apm_getinfo(fd, &info) != 0) {
861 strncpy(out, "ERR", 16);
866 batt_life = info.battery_life;
867 if (batt_life <= 100) {
868 snprintf(out, 16, "%d%%", batt_life);
871 strncpy(out, "ERR", 16);
877 char *get_apm_battery_time()
882 struct apm_power_info info;
885 out = (char *) calloc(16, sizeof(char));
887 fd = open(APMDEV, O_RDONLY);
889 strncpy(out, "ERR", 16);
893 if (apm_getinfo(fd, &info) != 0) {
895 strncpy(out, "ERR", 16);
900 batt_time = info.minutes_left;
902 if (batt_time == -1) {
903 strncpy(out, "unknown", 16);
907 snprintf(out, 16, "%2d:%02d", h, m);
915 /* empty stubs so conky links */
916 void prepare_update()
920 void update_entropy(void)
924 void free_all_processes(void)