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-2008 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>
56 #include <machine/apmvar.h>
58 #include <net80211/ieee80211.h>
59 #include <net80211/ieee80211_ioctl.h>
63 #define MAXSHOWDEVS 16
66 #define pagetok(size) ((size) << pageshift)
68 inline void proc_find_top(struct process **cpu, struct process **mem);
70 static short cpu_setup = 0;
73 struct ifmibdata *data = NULL;
85 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
87 ERR("error opening kvm");
95 /* note: swapmode taken from 'top' source */
96 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
97 * to be based on the new swapctl(2) system call. */
98 static int swapmode(int *used, int *total)
100 struct swapent *swdev;
101 int nswap, rnswap, i;
103 nswap = swapctl(SWAP_NSWAP, 0, 0);
108 swdev = malloc(nswap * sizeof(*swdev));
113 rnswap = swapctl(SWAP_STATS, swdev, nswap);
118 /* if rnswap != nswap, then what? */
120 /* Total things up */
122 for (i = 0; i < nswap; i++) {
123 if (swdev[i].se_flags & SWF_ENABLE) {
124 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
125 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
132 int check_mount(char *s)
140 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
141 struct timeval boottime;
143 size_t size = sizeof(boottime);
145 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
146 && (boottime.tv_sec != 0)) {
148 info.uptime = now - boottime.tv_sec;
150 ERR("Could not get uptime");
155 void update_meminfo()
157 static int mib[2] = { CTL_VM, VM_METER };
158 struct vmtotal vmtotal;
160 int pagesize, pageshift, swap_avail, swap_used;
162 pagesize = getpagesize();
164 while (pagesize > 1) {
169 /* we only need the amount of log(2)1024 for our conversion */
170 pageshift -= LOG1024;
172 /* get total -- systemwide main memory usage structure */
173 size = sizeof(vmtotal);
174 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
175 warn("sysctl failed");
176 bzero(&vmtotal, sizeof(vmtotal));
179 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
180 info.mem = pagetok(vmtotal.t_rm);
182 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
183 info.swapmax = swap_avail;
184 info.swap = swap_used;
191 void update_net_stats()
195 long long r, t, last_recv, last_trans;
196 struct ifaddrs *ifap, *ifa;
200 delta = current_update_time - last_update_time;
201 if (delta <= 0.0001) {
205 if (getifaddrs(&ifap) < 0) {
209 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
210 ns = get_net_stat((const char *) ifa->ifa_name);
212 if (ifa->ifa_flags & IFF_UP) {
213 struct ifaddrs *iftmp;
216 last_recv = ns->recv;
217 last_trans = ns->trans;
219 if (ifa->ifa_addr->sa_family != AF_LINK) {
223 for (iftmp = ifa->ifa_next;
224 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
225 iftmp = iftmp->ifa_next) {
226 if (iftmp->ifa_addr->sa_family == AF_INET) {
227 memcpy(&(ns->addr), iftmp->ifa_addr,
228 iftmp->ifa_addr->sa_len);
232 ifd = (struct if_data *) ifa->ifa_data;
236 if (r < ns->last_read_recv) {
237 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
239 ns->recv += (r - ns->last_read_recv);
242 ns->last_read_recv = r;
244 if (t < ns->last_read_trans) {
245 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
247 ns->trans += (t - ns->last_read_trans);
250 ns->last_read_trans = t;
252 /* calculate speeds */
253 ns->recv_speed = (ns->recv - last_recv) / delta;
254 ns->trans_speed = (ns->trans - last_trans) / delta;
263 void update_total_processes()
268 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
270 info.procs = n_processes;
273 void update_running_processes()
275 struct kinfo_proc2 *p;
280 int max_size = sizeof(struct kinfo_proc2);
282 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
283 for (i = 0; i < n_processes; i++) {
284 if (p[i].p_stat == SRUN) {
289 info.run_procs = cnt;
292 /* new SMP code can be enabled by commenting the following line */
296 struct cpu_load_struct {
297 unsigned long load[5];
300 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
301 long cpu_used, oldtotal, oldused;
304 int64_t *fresh = NULL;
306 /* XXX is 8 enough? - What's the constant for MAXCPU? */
307 /* allocate this with malloc would be better */
308 int64_t oldtotal[8], oldused[8];
313 int cpu_count = 1; /* default to 1 cpu */
315 int mib[2] = { CTL_HW, HW_NCPU };
316 size_t len = sizeof(cpu_count);
318 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
319 ERR("error getting cpu count, defaulting to 1");
322 info.cpu_count = cpu_count;
324 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
325 if (info.cpu_usage == NULL) {
330 assert(fresh == NULL); /* XXX Is this leaking memory? */
331 /* XXX Where shall I free this? */
332 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
338 void update_cpu_usage()
341 int mib[2] = { CTL_KERN, KERN_CPTIME };
343 long cp_time[CPUSTATES];
344 size_t len = sizeof(cp_time);
350 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
351 if ((cpu_setup == 0) || (!info.cpu_usage)) {
357 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
358 ERR("Cannot get kern.cp_time");
361 fresh.load[0] = cp_time[CP_USER];
362 fresh.load[1] = cp_time[CP_NICE];
363 fresh.load[2] = cp_time[CP_SYS];
364 fresh.load[3] = cp_time[CP_IDLE];
365 fresh.load[4] = cp_time[CP_IDLE];
367 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
368 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
370 if ((total - oldtotal) != 0) {
371 info.cpu_usage[0] = ((double) (used - oldused)) /
372 (double) (total - oldtotal);
374 info.cpu_usage[0] = 0;
380 if (info.cpu_count > 1) {
381 size = CPUSTATES * sizeof(int64_t);
382 for (i = 0; i < info.cpu_count; i++) {
383 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
384 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
386 ERR("sysctl kern.cp_time2 failed");
390 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
391 long cp_time_tmp[CPUSTATES];
393 size = sizeof(cp_time_tmp);
394 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
395 ERR("sysctl kern.cp_time failed");
398 for (i = 0; i < CPUSTATES; i++) {
399 fresh[i] = (int64_t) cp_time_tmp[i];
403 /* XXX Do sg with this int64_t => long => double ? float hell. */
404 for (i = 0; i < info.cpu_count; i++) {
406 int at = i * CPUSTATES;
408 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
409 total = used + fresh[at + CP_IDLE];
411 if ((total - oldtotal[i]) != 0) {
412 info.cpu_usage[i] = ((double) (used - oldused[i])) /
413 (double) (total - oldtotal[i]);
415 info.cpu_usage[i] = 0;
424 void update_load_average()
430 info.loadavg[0] = (float) v[0];
431 info.loadavg[1] = (float) v[1];
432 info.loadavg[2] = (float) v[2];
435 /* read sensors from sysctl */
436 void update_obsd_sensors()
438 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
439 struct sensor sensor;
440 struct sensordev sensordev;
442 enum sensor_type type;
444 slen = sizeof(sensor);
445 sdlen = sizeof(sensordev);
449 dev = obsd_sensors.device; // FIXME: read more than one device
451 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
453 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
454 if (errno != ENOENT) {
460 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
462 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
464 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
465 if (errno != ENOENT) {
470 if (sensor.flags & SENSOR_FINVALID) {
476 obsd_sensors.temp[dev][sensor.numt] =
477 (sensor.value - 273150000) / 1000000.0;
480 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
482 case SENSOR_VOLTS_DC:
483 obsd_sensors.volt[dev][sensor.numt] =
484 sensor.value / 1000000.0;
499 void get_obsd_vendor(char *buf, size_t client_buffer_size)
506 size_t size = sizeof(vendor);
508 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
509 ERR("error reading vendor");
510 snprintf(buf, client_buffer_size, "unknown");
512 snprintf(buf, client_buffer_size, "%s", vendor);
517 void get_obsd_product(char *buf, size_t client_buffer_size)
524 size_t size = sizeof(product);
526 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
527 ERR("error reading product");
528 snprintf(buf, client_buffer_size, "unknown");
530 snprintf(buf, client_buffer_size, "%s", product);
534 /* rdtsc() and get_freq_dynamic() copied from linux.c */
536 #if defined(__i386) || defined(__x86_64)
537 __inline__ unsigned long long int rdtsc()
539 unsigned long long int x;
541 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
546 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
547 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
548 const char *p_format, int divisor)
550 #if defined(__i386) || defined(__x86_64)
552 struct timeval tvstart, tvstop;
553 unsigned long long cycles[2]; /* gotta be 64 bit */
554 unsigned int microseconds; /* total time taken */
556 memset(&tz, 0, sizeof(tz));
558 /* get this function in cached memory */
559 gettimeofday(&tvstart, &tz);
561 gettimeofday(&tvstart, &tz);
563 /* we don't trust that this is any specific length of time */
566 gettimeofday(&tvstop, &tz);
567 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
568 (tvstop.tv_usec - tvstart.tv_usec);
570 snprintf(p_client_buffer, client_buffer_size, p_format,
571 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
573 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
578 char get_freq(char *p_client_buffer, size_t client_buffer_size,
579 const char *p_format, int divisor, unsigned int cpu)
582 int mib[2] = { CTL_HW, HW_CPUSPEED };
584 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
589 size_t size = sizeof(freq);
591 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
592 snprintf(p_client_buffer, client_buffer_size, p_format,
593 (float) freq / divisor);
595 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
603 proc_find_top(info.cpu, info.memu);
607 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
608 void update_wifi_stats()
611 struct ifaddrs *ifap, *ifa;
612 struct ifmediareq ifmr;
613 struct ieee80211_nodereq nr;
614 struct ieee80211_bssid bssid;
617 /* Get iface table */
618 if (getifaddrs(&ifap) < 0) {
622 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
623 ns = get_net_stat((const char *) ifa->ifa_name);
625 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
628 bzero(&ifmr, sizeof(ifmr));
629 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
630 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
635 /* We can monitor only wireless interfaces
636 * which are not in hostap mode */
637 if ((ifmr.ifm_active & IFM_IEEE80211)
638 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
641 memset(&bssid, 0, sizeof(bssid));
642 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
643 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
645 bzero(&nr, sizeof(nr));
646 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
647 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
649 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
650 ns->linkstatus = nr.nr_rssi;
661 return; /* XXX: implement? hifi: not sure how */
664 /* While topless is obviously better, top is also not bad. */
666 int comparecpu(const void *a, const void *b)
668 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
672 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
679 int comparemem(const void *a, const void *b)
681 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
685 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
692 inline void proc_find_top(struct process **cpu, struct process **mem)
694 struct kinfo_proc2 *p;
697 struct process *processes;
701 int pagesize = getpagesize();
703 /* we get total pages count again to be sure it is up to date */
706 size_t size = sizeof(total_pages);
708 if (sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1) {
709 ERR("error reading nmempages");
712 int max_size = sizeof(struct kinfo_proc2);
714 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
715 processes = malloc(n_processes * sizeof(struct process));
717 for (i = 0; i < n_processes; i++) {
718 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
719 processes[j].pid = p[i].p_pid;
720 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
721 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
722 processes[j].totalmem = (float) (p[i].p_vm_rssize * pagesize /
723 (float) total_pages) * 100.0;
728 qsort(processes, j - 1, sizeof(struct process), comparemem);
729 for (i = 0; i < 10; i++) {
730 struct process *tmp, *ttmp;
732 tmp = malloc(sizeof(struct process));
733 tmp->pid = processes[i].pid;
734 tmp->amount = processes[i].amount;
735 tmp->totalmem = processes[i].totalmem;
736 tmp->name = strndup(processes[i].name, text_buffer_size);
746 qsort(processes, j - 1, sizeof(struct process), comparecpu);
747 for (i = 0; i < 10; i++) {
748 struct process *tmp, *ttmp;
750 tmp = malloc(sizeof(struct process));
751 tmp->pid = processes[i].pid;
752 tmp->amount = processes[i].amount;
753 tmp->totalmem = processes[i].totalmem;
754 tmp->name = strndup(processes[i].name, text_buffer_size);
764 for (i = 0; i < j; i++) {
765 free(processes[i].name);
770 #if defined(i386) || defined(__i386__)
771 #define APMDEV "/dev/apm"
772 #define APM_UNKNOWN 255
774 int apm_getinfo(int fd, apm_info_t aip)
776 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
783 char *get_apm_adapter()
786 struct apm_power_info info;
789 out = (char *) calloc(16, sizeof(char));
791 fd = open(APMDEV, O_RDONLY);
793 strncpy(out, "ERR", 16);
797 if (apm_getinfo(fd, &info) != 0) {
799 strncpy(out, "ERR", 16);
804 switch (info.ac_state) {
806 strncpy(out, "off-line", 16);
810 if (info.battery_state == APM_BATT_CHARGING) {
811 strncpy(out, "charging", 16);
814 strncpy(out, "on-line", 16);
819 strncpy(out, "unknown", 16);
825 char *get_apm_battery_life()
829 struct apm_power_info info;
832 out = (char *) calloc(16, sizeof(char));
834 fd = open(APMDEV, O_RDONLY);
836 strncpy(out, "ERR", 16);
840 if (apm_getinfo(fd, &info) != 0) {
842 strncpy(out, "ERR", 16);
847 batt_life = info.battery_life;
848 if (batt_life <= 100) {
849 snprintf(out, 16, "%d%%", batt_life);
852 strncpy(out, "ERR", 16);
858 char *get_apm_battery_time()
863 struct apm_power_info info;
866 out = (char *) calloc(16, sizeof(char));
868 fd = open(APMDEV, O_RDONLY);
870 strncpy(out, "ERR", 16);
874 if (apm_getinfo(fd, &info) != 0) {
876 strncpy(out, "ERR", 16);
881 batt_time = info.minutes_left;
883 if (batt_time == -1) {
884 strncpy(out, "unknown", 16);
888 snprintf(out, 16, "%2d:%02d", h, m);
896 /* empty stubs so conky links */
897 void prepare_update()
901 void update_entropy(void)
905 void free_all_processes(void)