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>
64 #define MAXSHOWDEVS 16
67 #define pagetok(size) ((size) << pageshift)
69 inline void proc_find_top(struct process **cpu, struct process **mem);
71 static short cpu_setup = 0;
74 struct ifmibdata *data = NULL;
86 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
88 ERR("error opening kvm");
96 /* note: swapmode taken from 'top' source */
97 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
98 * to be based on the new swapctl(2) system call. */
99 static int swapmode(int *used, int *total)
101 struct swapent *swdev;
102 int nswap, rnswap, i;
104 nswap = swapctl(SWAP_NSWAP, 0, 0);
109 swdev = malloc(nswap * sizeof(*swdev));
114 rnswap = swapctl(SWAP_STATS, swdev, nswap);
119 /* if rnswap != nswap, then what? */
121 /* Total things up */
123 for (i = 0; i < nswap; i++) {
124 if (swdev[i].se_flags & SWF_ENABLE) {
125 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
126 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
133 int check_mount(char *s)
141 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
142 struct timeval boottime;
144 size_t size = sizeof(boottime);
146 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
147 && (boottime.tv_sec != 0)) {
149 info.uptime = now - boottime.tv_sec;
151 ERR("Could not get uptime");
156 void update_meminfo()
158 static int mib[2] = { CTL_VM, VM_METER };
159 struct vmtotal vmtotal;
161 int pagesize, pageshift, swap_avail, swap_used;
163 pagesize = getpagesize();
165 while (pagesize > 1) {
170 /* we only need the amount of log(2)1024 for our conversion */
171 pageshift -= LOG1024;
173 /* get total -- systemwide main memory usage structure */
174 size = sizeof(vmtotal);
175 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
176 warn("sysctl failed");
177 bzero(&vmtotal, sizeof(vmtotal));
180 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
181 info.mem = pagetok(vmtotal.t_rm);
182 info.memeasyfree = info.memfree = info.memmax - info.mem;
184 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
185 info.swapmax = swap_avail;
186 info.swap = swap_used;
193 void update_net_stats()
197 long long r, t, last_recv, last_trans;
198 struct ifaddrs *ifap, *ifa;
202 delta = current_update_time - last_update_time;
203 if (delta <= 0.0001 && total_updates != 0) {
207 if (getifaddrs(&ifap) < 0) {
211 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
212 ns = get_net_stat((const char *) ifa->ifa_name);
214 if (ifa->ifa_flags & IFF_UP) {
215 struct ifaddrs *iftmp;
218 last_recv = ns->recv;
219 last_trans = ns->trans;
221 if (ifa->ifa_addr->sa_family != AF_LINK) {
225 for (iftmp = ifa->ifa_next;
226 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
227 iftmp = iftmp->ifa_next) {
228 if (iftmp->ifa_addr->sa_family == AF_INET) {
229 memcpy(&(ns->addr), iftmp->ifa_addr,
230 iftmp->ifa_addr->sa_len);
234 ifd = (struct if_data *) ifa->ifa_data;
238 if (r < ns->last_read_recv) {
239 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
241 ns->recv += (r - ns->last_read_recv);
244 ns->last_read_recv = r;
246 if (t < ns->last_read_trans) {
247 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
249 ns->trans += (t - ns->last_read_trans);
252 ns->last_read_trans = t;
254 /* calculate speeds */
255 ns->recv_speed = (ns->recv - last_recv) / delta;
256 ns->trans_speed = (ns->trans - last_trans) / delta;
265 void update_total_processes()
270 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
272 info.procs = n_processes;
275 void update_running_processes()
277 struct kinfo_proc2 *p;
282 int max_size = sizeof(struct kinfo_proc2);
284 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
285 for (i = 0; i < n_processes; i++) {
286 if (p[i].p_stat == SRUN) {
291 info.run_procs = cnt;
294 /* new SMP code can be enabled by commenting the following line */
298 struct cpu_load_struct {
299 unsigned long load[5];
302 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
303 long cpu_used, oldtotal, oldused;
306 int64_t *fresh = NULL;
308 /* XXX is 8 enough? - What's the constant for MAXCPU? */
309 /* allocate this with malloc would be better */
310 int64_t oldtotal[8], oldused[8];
315 int cpu_count = 1; /* default to 1 cpu */
317 int mib[2] = { CTL_HW, HW_NCPU };
318 size_t len = sizeof(cpu_count);
320 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
321 ERR("error getting cpu count, defaulting to 1");
324 info.cpu_count = cpu_count;
326 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
327 if (info.cpu_usage == NULL) {
332 assert(fresh == NULL); /* XXX Is this leaking memory? */
333 /* XXX Where shall I free this? */
334 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
340 void update_cpu_usage()
343 int mib[2] = { CTL_KERN, KERN_CPTIME };
345 long cp_time[CPUSTATES];
346 size_t len = sizeof(cp_time);
352 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
353 if ((cpu_setup == 0) || (!info.cpu_usage)) {
359 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
360 ERR("Cannot get kern.cp_time");
363 fresh.load[0] = cp_time[CP_USER];
364 fresh.load[1] = cp_time[CP_NICE];
365 fresh.load[2] = cp_time[CP_SYS];
366 fresh.load[3] = cp_time[CP_IDLE];
367 fresh.load[4] = cp_time[CP_IDLE];
369 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
370 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
372 if ((total - oldtotal) != 0) {
373 info.cpu_usage[0] = ((double) (used - oldused)) /
374 (double) (total - oldtotal);
376 info.cpu_usage[0] = 0;
382 if (info.cpu_count > 1) {
383 size = CPUSTATES * sizeof(int64_t);
384 for (i = 0; i < info.cpu_count; i++) {
385 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
386 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
388 ERR("sysctl kern.cp_time2 failed");
392 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
393 long cp_time_tmp[CPUSTATES];
395 size = sizeof(cp_time_tmp);
396 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
397 ERR("sysctl kern.cp_time failed");
400 for (i = 0; i < CPUSTATES; i++) {
401 fresh[i] = (int64_t) cp_time_tmp[i];
405 /* XXX Do sg with this int64_t => long => double ? float hell. */
406 for (i = 0; i < info.cpu_count; i++) {
408 int at = i * CPUSTATES;
410 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
411 total = used + fresh[at + CP_IDLE];
413 if ((total - oldtotal[i]) != 0) {
414 info.cpu_usage[i] = ((double) (used - oldused[i])) /
415 (double) (total - oldtotal[i]);
417 info.cpu_usage[i] = 0;
426 void update_load_average()
432 info.loadavg[0] = (float) v[0];
433 info.loadavg[1] = (float) v[1];
434 info.loadavg[2] = (float) v[2];
437 /* read sensors from sysctl */
438 void update_obsd_sensors()
440 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
441 struct sensor sensor;
442 struct sensordev sensordev;
444 enum sensor_type type;
446 slen = sizeof(sensor);
447 sdlen = sizeof(sensordev);
451 dev = obsd_sensors.device; // FIXME: read more than one device
453 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
455 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
456 if (errno != ENOENT) {
462 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
464 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
466 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
467 if (errno != ENOENT) {
472 if (sensor.flags & SENSOR_FINVALID) {
478 obsd_sensors.temp[dev][sensor.numt] =
479 (sensor.value - 273150000) / 1000000.0;
482 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
484 case SENSOR_VOLTS_DC:
485 obsd_sensors.volt[dev][sensor.numt] =
486 sensor.value / 1000000.0;
501 void get_obsd_vendor(char *buf, size_t client_buffer_size)
508 size_t size = sizeof(vendor);
510 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
511 ERR("error reading vendor");
512 snprintf(buf, client_buffer_size, "unknown");
514 snprintf(buf, client_buffer_size, "%s", vendor);
519 void get_obsd_product(char *buf, size_t client_buffer_size)
526 size_t size = sizeof(product);
528 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
529 ERR("error reading product");
530 snprintf(buf, client_buffer_size, "unknown");
532 snprintf(buf, client_buffer_size, "%s", product);
536 /* rdtsc() and get_freq_dynamic() copied from linux.c */
538 #if defined(__i386) || defined(__x86_64)
539 __inline__ unsigned long long int rdtsc()
541 unsigned long long int x;
543 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
548 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
549 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
550 const char *p_format, int divisor)
552 #if defined(__i386) || defined(__x86_64)
554 struct timeval tvstart, tvstop;
555 unsigned long long cycles[2]; /* gotta be 64 bit */
556 unsigned int microseconds; /* total time taken */
558 memset(&tz, 0, sizeof(tz));
560 /* get this function in cached memory */
561 gettimeofday(&tvstart, &tz);
563 gettimeofday(&tvstart, &tz);
565 /* we don't trust that this is any specific length of time */
568 gettimeofday(&tvstop, &tz);
569 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
570 (tvstop.tv_usec - tvstart.tv_usec);
572 snprintf(p_client_buffer, client_buffer_size, p_format,
573 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
575 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
580 char get_freq(char *p_client_buffer, size_t client_buffer_size,
581 const char *p_format, int divisor, unsigned int cpu)
584 int mib[2] = { CTL_HW, HW_CPUSPEED };
586 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
591 size_t size = sizeof(freq);
593 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
594 snprintf(p_client_buffer, client_buffer_size, p_format,
595 (float) freq / divisor);
597 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
606 proc_find_top(info.cpu, info.memu);
610 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
611 void update_wifi_stats()
614 struct ifaddrs *ifap, *ifa;
615 struct ifmediareq ifmr;
616 struct ieee80211_nodereq nr;
617 struct ieee80211_bssid bssid;
620 /* Get iface table */
621 if (getifaddrs(&ifap) < 0) {
625 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
626 ns = get_net_stat((const char *) ifa->ifa_name);
628 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
631 bzero(&ifmr, sizeof(ifmr));
632 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
633 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
638 /* We can monitor only wireless interfaces
639 * which are not in hostap mode */
640 if ((ifmr.ifm_active & IFM_IEEE80211)
641 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
644 memset(&bssid, 0, sizeof(bssid));
645 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
646 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
648 bzero(&nr, sizeof(nr));
649 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
650 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
652 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
653 ns->linkstatus = nr.nr_rssi;
662 void clear_diskio_stats()
666 struct diskio_stat *prepare_diskio_stat(const char *s)
672 return; /* XXX: implement? hifi: not sure how */
675 /* While topless is obviously better, top is also not bad. */
677 int comparecpu(const void *a, const void *b)
679 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
683 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
690 int comparemem(const void *a, const void *b)
692 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
696 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
703 inline void proc_find_top(struct process **cpu, struct process **mem)
705 struct kinfo_proc2 *p;
708 struct process *processes;
713 int pagesize = getpagesize();
715 /* we get total pages count again to be sure it is up to date */
717 mib[1] = HW_USERMEM64;
718 size_t size = sizeof(usermem);
720 if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
721 ERR("error reading usermem");
724 /* translate bytes into page count */
725 total_pages = usermem / pagesize;
727 int max_size = sizeof(struct kinfo_proc2);
729 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
730 processes = malloc(n_processes * sizeof(struct process));
732 for (i = 0; i < n_processes; i++) {
733 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
734 processes[j].pid = p[i].p_pid;
735 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
736 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
737 processes[j].totalmem = (float) (p[i].p_vm_rssize /
738 (float) total_pages) * 100.0;
743 qsort(processes, j - 1, sizeof(struct process), comparemem);
744 for (i = 0; i < 10; i++) {
745 struct process *tmp, *ttmp;
747 tmp = malloc(sizeof(struct process));
748 tmp->pid = processes[i].pid;
749 tmp->amount = processes[i].amount;
750 tmp->totalmem = processes[i].totalmem;
751 tmp->name = strndup(processes[i].name, text_buffer_size);
761 qsort(processes, j - 1, sizeof(struct process), comparecpu);
762 for (i = 0; i < 10; i++) {
763 struct process *tmp, *ttmp;
765 tmp = malloc(sizeof(struct process));
766 tmp->pid = processes[i].pid;
767 tmp->amount = processes[i].amount;
768 tmp->totalmem = processes[i].totalmem;
769 tmp->name = strndup(processes[i].name, text_buffer_size);
779 for (i = 0; i < j; i++) {
780 free(processes[i].name);
785 #if defined(i386) || defined(__i386__)
786 #define APMDEV "/dev/apm"
787 #define APM_UNKNOWN 255
789 int apm_getinfo(int fd, apm_info_t aip)
791 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
798 char *get_apm_adapter()
801 struct apm_power_info info;
804 out = (char *) calloc(16, sizeof(char));
806 fd = open(APMDEV, O_RDONLY);
808 strncpy(out, "ERR", 16);
812 if (apm_getinfo(fd, &info) != 0) {
814 strncpy(out, "ERR", 16);
819 switch (info.ac_state) {
821 strncpy(out, "off-line", 16);
825 if (info.battery_state == APM_BATT_CHARGING) {
826 strncpy(out, "charging", 16);
829 strncpy(out, "on-line", 16);
834 strncpy(out, "unknown", 16);
840 char *get_apm_battery_life()
844 struct apm_power_info info;
847 out = (char *) calloc(16, sizeof(char));
849 fd = open(APMDEV, O_RDONLY);
851 strncpy(out, "ERR", 16);
855 if (apm_getinfo(fd, &info) != 0) {
857 strncpy(out, "ERR", 16);
862 batt_life = info.battery_life;
863 if (batt_life <= 100) {
864 snprintf(out, 16, "%d%%", batt_life);
867 strncpy(out, "ERR", 16);
873 char *get_apm_battery_time()
878 struct apm_power_info info;
881 out = (char *) calloc(16, sizeof(char));
883 fd = open(APMDEV, O_RDONLY);
885 strncpy(out, "ERR", 16);
889 if (apm_getinfo(fd, &info) != 0) {
891 strncpy(out, "ERR", 16);
896 batt_time = info.minutes_left;
898 if (batt_time == -1) {
899 strncpy(out, "unknown", 16);
903 snprintf(out, 16, "%2d:%02d", h, m);
911 /* empty stubs so conky links */
912 void prepare_update()
916 void update_entropy(void)
920 void free_all_processes(void)