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>
60 #define MAXSHOWDEVS 16
63 #define pagetok(size) ((size) << pageshift)
65 inline void proc_find_top(struct process **cpu, struct process **mem);
67 static short cpu_setup = 0;
70 struct ifmibdata *data = NULL;
82 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
84 ERR("error opening kvm");
92 /* note: swapmode taken from 'top' source */
93 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
94 * to be based on the new swapctl(2) system call. */
95 static int swapmode(int *used, int *total)
97 struct swapent *swdev;
100 nswap = swapctl(SWAP_NSWAP, 0, 0);
105 swdev = malloc(nswap * sizeof(*swdev));
110 rnswap = swapctl(SWAP_STATS, swdev, nswap);
115 /* if rnswap != nswap, then what? */
117 /* Total things up */
119 for (i = 0; i < nswap; i++) {
120 if (swdev[i].se_flags & SWF_ENABLE) {
121 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
122 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
129 int check_mount(char *s)
137 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
138 struct timeval boottime;
140 size_t size = sizeof(boottime);
142 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
143 && (boottime.tv_sec != 0)) {
145 info.uptime = now - boottime.tv_sec;
147 ERR("Could not get uptime");
152 void update_meminfo()
154 static int mib[2] = { CTL_VM, VM_METER };
155 struct vmtotal vmtotal;
157 int pagesize, pageshift, swap_avail, swap_used;
159 pagesize = getpagesize();
161 while (pagesize > 1) {
166 /* we only need the amount of log(2)1024 for our conversion */
167 pageshift -= LOG1024;
169 /* get total -- systemwide main memory usage structure */
170 size = sizeof(vmtotal);
171 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
172 warn("sysctl failed");
173 bzero(&vmtotal, sizeof(vmtotal));
176 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
177 info.mem = pagetok(vmtotal.t_rm);
178 info.memeasyfree = info.memfree = info.memmax - info.mem;
180 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
181 info.swapmax = swap_avail;
182 info.swap = swap_used;
189 void update_net_stats()
193 long long r, t, last_recv, last_trans;
194 struct ifaddrs *ifap, *ifa;
198 delta = current_update_time - last_update_time;
199 if (delta <= 0.0001) {
203 if (getifaddrs(&ifap) < 0) {
207 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
208 ns = get_net_stat((const char *) ifa->ifa_name);
210 if (ifa->ifa_flags & IFF_UP) {
211 struct ifaddrs *iftmp;
214 last_recv = ns->recv;
215 last_trans = ns->trans;
217 if (ifa->ifa_addr->sa_family != AF_LINK) {
221 for (iftmp = ifa->ifa_next;
222 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
223 iftmp = iftmp->ifa_next) {
224 if (iftmp->ifa_addr->sa_family == AF_INET) {
225 memcpy(&(ns->addr), iftmp->ifa_addr,
226 iftmp->ifa_addr->sa_len);
230 ifd = (struct if_data *) ifa->ifa_data;
234 if (r < ns->last_read_recv) {
235 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
237 ns->recv += (r - ns->last_read_recv);
240 ns->last_read_recv = r;
242 if (t < ns->last_read_trans) {
243 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
245 ns->trans += (t - ns->last_read_trans);
248 ns->last_read_trans = t;
250 /* calculate speeds */
251 ns->recv_speed = (ns->recv - last_recv) / delta;
252 ns->trans_speed = (ns->trans - last_trans) / delta;
261 void update_total_processes()
266 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
268 info.procs = n_processes;
271 void update_running_processes()
273 struct kinfo_proc2 *p;
278 int max_size = sizeof(struct kinfo_proc2);
280 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
281 for (i = 0; i < n_processes; i++) {
282 if (p[i].p_stat == SRUN) {
287 info.run_procs = cnt;
290 /* new SMP code can be enabled by commenting the following line */
294 struct cpu_load_struct {
295 unsigned long load[5];
298 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
299 long cpu_used, oldtotal, oldused;
302 int64_t *fresh = NULL;
304 /* XXX is 8 enough? - What's the constant for MAXCPU? */
305 /* allocate this with malloc would be better */
306 int64_t oldtotal[8], oldused[8];
311 int cpu_count = 1; /* default to 1 cpu */
313 int mib[2] = { CTL_HW, HW_NCPU };
314 size_t len = sizeof(cpu_count);
316 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
317 ERR("error getting cpu count, defaulting to 1");
320 info.cpu_count = cpu_count;
322 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
323 if (info.cpu_usage == NULL) {
328 assert(fresh == NULL); /* XXX Is this leaking memory? */
329 /* XXX Where shall I free this? */
330 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
336 void update_cpu_usage()
339 int mib[2] = { CTL_KERN, KERN_CPTIME };
341 long cp_time[CPUSTATES];
342 size_t len = sizeof(cp_time);
348 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
349 if ((cpu_setup == 0) || (!info.cpu_usage)) {
355 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
356 ERR("Cannot get kern.cp_time");
359 fresh.load[0] = cp_time[CP_USER];
360 fresh.load[1] = cp_time[CP_NICE];
361 fresh.load[2] = cp_time[CP_SYS];
362 fresh.load[3] = cp_time[CP_IDLE];
363 fresh.load[4] = cp_time[CP_IDLE];
365 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
366 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
368 if ((total - oldtotal) != 0) {
369 info.cpu_usage[0] = ((double) (used - oldused)) /
370 (double) (total - oldtotal);
372 info.cpu_usage[0] = 0;
378 if (info.cpu_count > 1) {
379 size = CPUSTATES * sizeof(int64_t);
380 for (i = 0; i < info.cpu_count; i++) {
381 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
382 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
384 ERR("sysctl kern.cp_time2 failed");
388 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
389 long cp_time_tmp[CPUSTATES];
391 size = sizeof(cp_time_tmp);
392 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
393 ERR("sysctl kern.cp_time failed");
396 for (i = 0; i < CPUSTATES; i++) {
397 fresh[i] = (int64_t) cp_time_tmp[i];
401 /* XXX Do sg with this int64_t => long => double ? float hell. */
402 for (i = 0; i < info.cpu_count; i++) {
404 int at = i * CPUSTATES;
406 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
407 total = used + fresh[at + CP_IDLE];
409 if ((total - oldtotal[i]) != 0) {
410 info.cpu_usage[i] = ((double) (used - oldused[i])) /
411 (double) (total - oldtotal[i]);
413 info.cpu_usage[i] = 0;
422 void update_load_average()
428 info.loadavg[0] = (float) v[0];
429 info.loadavg[1] = (float) v[1];
430 info.loadavg[2] = (float) v[2];
433 /* read sensors from sysctl */
434 void update_obsd_sensors()
436 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
437 struct sensor sensor;
438 struct sensordev sensordev;
440 enum sensor_type type;
442 slen = sizeof(sensor);
443 sdlen = sizeof(sensordev);
447 dev = obsd_sensors.device; // FIXME: read more than one device
449 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
451 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
452 if (errno != ENOENT) {
458 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
460 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
462 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
463 if (errno != ENOENT) {
468 if (sensor.flags & SENSOR_FINVALID) {
474 obsd_sensors.temp[dev][sensor.numt] =
475 (sensor.value - 273150000) / 1000000.0;
478 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
480 case SENSOR_VOLTS_DC:
481 obsd_sensors.volt[dev][sensor.numt] =
482 sensor.value / 1000000.0;
497 void get_obsd_vendor(char *buf, size_t client_buffer_size)
504 size_t size = sizeof(vendor);
506 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
507 ERR("error reading vendor");
508 snprintf(buf, client_buffer_size, "unknown");
510 snprintf(buf, client_buffer_size, "%s", vendor);
515 void get_obsd_product(char *buf, size_t client_buffer_size)
522 size_t size = sizeof(product);
524 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
525 ERR("error reading product");
526 snprintf(buf, client_buffer_size, "unknown");
528 snprintf(buf, client_buffer_size, "%s", product);
532 /* rdtsc() and get_freq_dynamic() copied from linux.c */
534 #if defined(__i386) || defined(__x86_64)
535 __inline__ unsigned long long int rdtsc()
537 unsigned long long int x;
539 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
544 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
545 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
546 const char *p_format, int divisor)
548 #if defined(__i386) || defined(__x86_64)
550 struct timeval tvstart, tvstop;
551 unsigned long long cycles[2]; /* gotta be 64 bit */
552 unsigned int microseconds; /* total time taken */
554 memset(&tz, 0, sizeof(tz));
556 /* get this function in cached memory */
557 gettimeofday(&tvstart, &tz);
559 gettimeofday(&tvstart, &tz);
561 /* we don't trust that this is any specific length of time */
564 gettimeofday(&tvstop, &tz);
565 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
566 (tvstop.tv_usec - tvstart.tv_usec);
568 snprintf(p_client_buffer, client_buffer_size, p_format,
569 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
571 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
576 char get_freq(char *p_client_buffer, size_t client_buffer_size,
577 const char *p_format, int divisor, unsigned int cpu)
580 int mib[2] = { CTL_HW, HW_CPUSPEED };
582 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
587 size_t size = sizeof(freq);
589 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
590 snprintf(p_client_buffer, client_buffer_size, p_format,
591 (float) freq / divisor);
593 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
602 proc_find_top(info.cpu, info.memu);
606 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
607 void update_wifi_stats()
610 struct ifaddrs *ifap, *ifa;
611 struct ifmediareq ifmr;
612 struct ieee80211_nodereq nr;
613 struct ieee80211_bssid bssid;
616 /* Get iface table */
617 if (getifaddrs(&ifap) < 0) {
621 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
622 ns = get_net_stat((const char *) ifa->ifa_name);
624 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
627 bzero(&ifmr, sizeof(ifmr));
628 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
629 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
634 /* We can monitor only wireless interfaces
635 * which are not in hostap mode */
636 if ((ifmr.ifm_active & IFM_IEEE80211)
637 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
640 memset(&bssid, 0, sizeof(bssid));
641 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
642 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
644 bzero(&nr, sizeof(nr));
645 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
646 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
648 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
649 ns->linkstatus = nr.nr_rssi;
658 void clear_diskio_stats()
664 return; /* XXX: implement? hifi: not sure how */
667 /* While topless is obviously better, top is also not bad. */
669 int comparecpu(const void *a, const void *b)
671 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
675 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
682 int comparemem(const void *a, const void *b)
684 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
688 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
695 inline void proc_find_top(struct process **cpu, struct process **mem)
697 struct kinfo_proc2 *p;
700 struct process *processes;
705 int pagesize = getpagesize();
707 /* we get total pages count again to be sure it is up to date */
709 mib[1] = HW_USERMEM64;
710 size_t size = sizeof(usermem);
712 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
713 ERR("error reading usermem");
716 /* translate bytes into page count */
717 total_pages = usermem / pagesize;
719 int max_size = sizeof(struct kinfo_proc2);
721 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
722 processes = malloc(n_processes * sizeof(struct process));
724 for (i = 0; i < n_processes; i++) {
725 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
726 processes[j].pid = p[i].p_pid;
727 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
728 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
729 processes[j].totalmem = (float) (p[i].p_vm_rssize /
730 (float) total_pages) * 100.0;
735 qsort(processes, j - 1, sizeof(struct process), comparemem);
736 for (i = 0; i < 10; i++) {
737 struct process *tmp, *ttmp;
739 tmp = malloc(sizeof(struct process));
740 tmp->pid = processes[i].pid;
741 tmp->amount = processes[i].amount;
742 tmp->totalmem = processes[i].totalmem;
743 tmp->name = strndup(processes[i].name, text_buffer_size);
753 qsort(processes, j - 1, sizeof(struct process), comparecpu);
754 for (i = 0; i < 10; i++) {
755 struct process *tmp, *ttmp;
757 tmp = malloc(sizeof(struct process));
758 tmp->pid = processes[i].pid;
759 tmp->amount = processes[i].amount;
760 tmp->totalmem = processes[i].totalmem;
761 tmp->name = strndup(processes[i].name, text_buffer_size);
771 for (i = 0; i < j; i++) {
772 free(processes[i].name);
777 #if defined(i386) || defined(__i386__)
778 #define APMDEV "/dev/apm"
779 #define APM_UNKNOWN 255
781 int apm_getinfo(int fd, apm_info_t aip)
783 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
790 char *get_apm_adapter()
793 struct apm_power_info info;
796 out = (char *) calloc(16, sizeof(char));
798 fd = open(APMDEV, O_RDONLY);
800 strncpy(out, "ERR", 16);
804 if (apm_getinfo(fd, &info) != 0) {
806 strncpy(out, "ERR", 16);
811 switch (info.ac_state) {
813 strncpy(out, "off-line", 16);
817 if (info.battery_state == APM_BATT_CHARGING) {
818 strncpy(out, "charging", 16);
821 strncpy(out, "on-line", 16);
826 strncpy(out, "unknown", 16);
832 char *get_apm_battery_life()
836 struct apm_power_info info;
839 out = (char *) calloc(16, sizeof(char));
841 fd = open(APMDEV, O_RDONLY);
843 strncpy(out, "ERR", 16);
847 if (apm_getinfo(fd, &info) != 0) {
849 strncpy(out, "ERR", 16);
854 batt_life = info.battery_life;
855 if (batt_life <= 100) {
856 snprintf(out, 16, "%d%%", batt_life);
859 strncpy(out, "ERR", 16);
865 char *get_apm_battery_time()
870 struct apm_power_info info;
873 out = (char *) calloc(16, sizeof(char));
875 fd = open(APMDEV, O_RDONLY);
877 strncpy(out, "ERR", 16);
881 if (apm_getinfo(fd, &info) != 0) {
883 strncpy(out, "ERR", 16);
888 batt_time = info.minutes_left;
890 if (batt_time == -1) {
891 strncpy(out, "unknown", 16);
895 snprintf(out, 16, "%2d:%02d", h, m);
903 /* empty stubs so conky links */
904 void prepare_update()
908 void update_entropy(void)
912 void free_all_processes(void)