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>
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);
179 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
180 info.swapmax = swap_avail;
181 info.swap = swap_used;
188 void update_net_stats()
192 long long r, t, last_recv, last_trans;
193 struct ifaddrs *ifap, *ifa;
197 delta = current_update_time - last_update_time;
198 if (delta <= 0.0001) {
202 if (getifaddrs(&ifap) < 0) {
206 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
207 ns = get_net_stat((const char *) ifa->ifa_name);
209 if (ifa->ifa_flags & IFF_UP) {
210 struct ifaddrs *iftmp;
213 last_recv = ns->recv;
214 last_trans = ns->trans;
216 if (ifa->ifa_addr->sa_family != AF_LINK) {
220 for (iftmp = ifa->ifa_next;
221 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
222 iftmp = iftmp->ifa_next) {
223 if (iftmp->ifa_addr->sa_family == AF_INET) {
224 memcpy(&(ns->addr), iftmp->ifa_addr,
225 iftmp->ifa_addr->sa_len);
229 ifd = (struct if_data *) ifa->ifa_data;
233 if (r < ns->last_read_recv) {
234 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
236 ns->recv += (r - ns->last_read_recv);
239 ns->last_read_recv = r;
241 if (t < ns->last_read_trans) {
242 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
244 ns->trans += (t - ns->last_read_trans);
247 ns->last_read_trans = t;
249 /* calculate speeds */
250 ns->recv_speed = (ns->recv - last_recv) / delta;
251 ns->trans_speed = (ns->trans - last_trans) / delta;
260 void update_total_processes()
265 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
267 info.procs = n_processes;
270 void update_running_processes()
272 struct kinfo_proc2 *p;
277 int max_size = sizeof(struct kinfo_proc2);
279 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
280 for (i = 0; i < n_processes; i++) {
281 if (p[i].p_stat == SRUN) {
286 info.run_procs = cnt;
289 /* new SMP code can be enabled by commenting the following line */
293 struct cpu_load_struct {
294 unsigned long load[5];
297 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
298 long cpu_used, oldtotal, oldused;
301 int64_t *fresh = NULL;
303 /* XXX is 8 enough? - What's the constant for MAXCPU? */
304 /* allocate this with malloc would be better */
305 int64_t oldtotal[8], oldused[8];
310 int cpu_count = 1; /* default to 1 cpu */
312 int mib[2] = { CTL_HW, HW_NCPU };
313 size_t len = sizeof(cpu_count);
315 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
316 ERR("error getting cpu count, defaulting to 1");
319 info.cpu_count = cpu_count;
321 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
322 if (info.cpu_usage == NULL) {
327 assert(fresh == NULL); /* XXX Is this leaking memory? */
328 /* XXX Where shall I free this? */
329 if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
335 void update_cpu_usage()
338 int mib[2] = { CTL_KERN, KERN_CPTIME };
340 long cp_time[CPUSTATES];
341 size_t len = sizeof(cp_time);
347 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
348 if ((cpu_setup == 0) || (!info.cpu_usage)) {
354 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
355 ERR("Cannot get kern.cp_time");
358 fresh.load[0] = cp_time[CP_USER];
359 fresh.load[1] = cp_time[CP_NICE];
360 fresh.load[2] = cp_time[CP_SYS];
361 fresh.load[3] = cp_time[CP_IDLE];
362 fresh.load[4] = cp_time[CP_IDLE];
364 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
365 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
367 if ((total - oldtotal) != 0) {
368 info.cpu_usage[0] = ((double) (used - oldused)) /
369 (double) (total - oldtotal);
371 info.cpu_usage[0] = 0;
377 if (info.cpu_count > 1) {
378 size = CPUSTATES * sizeof(int64_t);
379 for (i = 0; i < info.cpu_count; i++) {
380 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
381 if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
383 ERR("sysctl kern.cp_time2 failed");
387 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
388 long cp_time_tmp[CPUSTATES];
390 size = sizeof(cp_time_tmp);
391 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
392 ERR("sysctl kern.cp_time failed");
395 for (i = 0; i < CPUSTATES; i++) {
396 fresh[i] = (int64_t) cp_time_tmp[i];
400 /* XXX Do sg with this int64_t => long => double ? float hell. */
401 for (i = 0; i < info.cpu_count; i++) {
403 int at = i * CPUSTATES;
405 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
406 total = used + fresh[at + CP_IDLE];
408 if ((total - oldtotal[i]) != 0) {
409 info.cpu_usage[i] = ((double) (used - oldused[i])) /
410 (double) (total - oldtotal[i]);
412 info.cpu_usage[i] = 0;
421 void update_load_average()
427 info.loadavg[0] = (float) v[0];
428 info.loadavg[1] = (float) v[1];
429 info.loadavg[2] = (float) v[2];
432 /* read sensors from sysctl */
433 void update_obsd_sensors()
435 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
436 struct sensor sensor;
437 struct sensordev sensordev;
439 enum sensor_type type;
441 slen = sizeof(sensor);
442 sdlen = sizeof(sensordev);
446 dev = obsd_sensors.device; // FIXME: read more than one device
448 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
450 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
451 if (errno != ENOENT) {
457 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
459 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
461 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
462 if (errno != ENOENT) {
467 if (sensor.flags & SENSOR_FINVALID) {
473 obsd_sensors.temp[dev][sensor.numt] =
474 (sensor.value - 273150000) / 1000000.0;
477 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
479 case SENSOR_VOLTS_DC:
480 obsd_sensors.volt[dev][sensor.numt] =
481 sensor.value / 1000000.0;
496 void get_obsd_vendor(char *buf, size_t client_buffer_size)
503 size_t size = sizeof(vendor);
505 if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
506 ERR("error reading vendor");
507 snprintf(buf, client_buffer_size, "unknown");
509 snprintf(buf, client_buffer_size, "%s", vendor);
514 void get_obsd_product(char *buf, size_t client_buffer_size)
521 size_t size = sizeof(product);
523 if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
524 ERR("error reading product");
525 snprintf(buf, client_buffer_size, "unknown");
527 snprintf(buf, client_buffer_size, "%s", product);
531 /* rdtsc() and get_freq_dynamic() copied from linux.c */
533 #if defined(__i386) || defined(__x86_64)
534 __inline__ unsigned long long int rdtsc()
536 unsigned long long int x;
538 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
543 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
544 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
545 const char *p_format, int divisor)
547 #if defined(__i386) || defined(__x86_64)
549 struct timeval tvstart, tvstop;
550 unsigned long long cycles[2]; /* gotta be 64 bit */
551 unsigned int microseconds; /* total time taken */
553 memset(&tz, 0, sizeof(tz));
555 /* get this function in cached memory */
556 gettimeofday(&tvstart, &tz);
558 gettimeofday(&tvstart, &tz);
560 /* we don't trust that this is any specific length of time */
563 gettimeofday(&tvstop, &tz);
564 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
565 (tvstop.tv_usec - tvstart.tv_usec);
567 snprintf(p_client_buffer, client_buffer_size, p_format,
568 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
570 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
575 char get_freq(char *p_client_buffer, size_t client_buffer_size,
576 const char *p_format, int divisor, unsigned int cpu)
579 int mib[2] = { CTL_HW, HW_CPUSPEED };
581 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
586 size_t size = sizeof(freq);
588 if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
589 snprintf(p_client_buffer, client_buffer_size, p_format,
590 (float) freq / divisor);
592 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
600 proc_find_top(info.cpu, info.memu);
604 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
605 void update_wifi_stats()
608 struct ifaddrs *ifap, *ifa;
609 struct ifmediareq ifmr;
610 struct ieee80211_nodereq nr;
611 struct ieee80211_bssid bssid;
614 /* Get iface table */
615 if (getifaddrs(&ifap) < 0) {
619 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
620 ns = get_net_stat((const char *) ifa->ifa_name);
622 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
625 bzero(&ifmr, sizeof(ifmr));
626 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
627 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
632 /* We can monitor only wireless interfaces
633 * which are not in hostap mode */
634 if ((ifmr.ifm_active & IFM_IEEE80211)
635 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
638 memset(&bssid, 0, sizeof(bssid));
639 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
640 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
642 bzero(&nr, sizeof(nr));
643 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
644 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
646 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
647 ns->linkstatus = nr.nr_rssi;
658 return; /* XXX: implement? hifi: not sure how */
661 /* While topless is obviously better, top is also not bad. */
663 int comparecpu(const void *a, const void *b)
665 if (((struct process *) a)->amount > ((struct process *) b)->amount) {
669 if (((struct process *) a)->amount < ((struct process *) b)->amount) {
676 int comparemem(const void *a, const void *b)
678 if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
682 if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
689 inline void proc_find_top(struct process **cpu, struct process **mem)
691 struct kinfo_proc2 *p;
694 struct process *processes;
698 int pagesize = getpagesize();
700 /* we get total pages count again to be sure it is up to date */
703 size_t size = sizeof(total_pages);
705 if (sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1) {
706 ERR("error reading nmempages");
709 int max_size = sizeof(struct kinfo_proc2);
711 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
712 processes = malloc(n_processes * sizeof(struct process));
714 for (i = 0; i < n_processes; i++) {
715 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
716 processes[j].pid = p[i].p_pid;
717 processes[j].name = strndup(p[i].p_comm, text_buffer_size);
718 processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
719 processes[j].totalmem = (float) (p[i].p_vm_rssize * pagesize /
720 (float) total_pages) * 100.0;
725 qsort(processes, j - 1, sizeof(struct process), comparemem);
726 for (i = 0; i < 10; i++) {
727 struct process *tmp, *ttmp;
729 tmp = malloc(sizeof(struct process));
730 tmp->pid = processes[i].pid;
731 tmp->amount = processes[i].amount;
732 tmp->totalmem = processes[i].totalmem;
733 tmp->name = strndup(processes[i].name, text_buffer_size);
743 qsort(processes, j - 1, sizeof(struct process), comparecpu);
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 for (i = 0; i < j; i++) {
762 free(processes[i].name);
767 #if defined(i386) || defined(__i386__)
768 #define APMDEV "/dev/apm"
769 #define APM_UNKNOWN 255
771 int apm_getinfo(int fd, apm_info_t aip)
773 if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
780 char *get_apm_adapter()
783 struct apm_power_info info;
786 out = (char *) calloc(16, sizeof(char));
788 fd = open(APMDEV, O_RDONLY);
790 strncpy(out, "ERR", 16);
794 if (apm_getinfo(fd, &info) != 0) {
796 strncpy(out, "ERR", 16);
801 switch (info.ac_state) {
803 strncpy(out, "off-line", 16);
807 if (info.battery_state == APM_BATT_CHARGING) {
808 strncpy(out, "charging", 16);
811 strncpy(out, "on-line", 16);
816 strncpy(out, "unknown", 16);
822 char *get_apm_battery_life()
826 struct apm_power_info info;
829 out = (char *) calloc(16, sizeof(char));
831 fd = open(APMDEV, O_RDONLY);
833 strncpy(out, "ERR", 16);
837 if (apm_getinfo(fd, &info) != 0) {
839 strncpy(out, "ERR", 16);
844 batt_life = info.battery_life;
845 if (batt_life <= 100) {
846 snprintf(out, 16, "%d%%", batt_life);
849 strncpy(out, "ERR", 16);
855 char *get_apm_battery_time()
860 struct apm_power_info info;
863 out = (char *) calloc(16, sizeof(char));
865 fd = open(APMDEV, O_RDONLY);
867 strncpy(out, "ERR", 16);
871 if (apm_getinfo(fd, &info) != 0) {
873 strncpy(out, "ERR", 16);
878 batt_time = info.minutes_left;
880 if (batt_time == -1) {
881 strncpy(out, "unknown", 16);
885 snprintf(out, 16, "%2d:%02d", h, m);
893 /* empty stubs so conky links */
894 void prepare_update()
898 void update_entropy(void)
902 void free_all_processes(void)