3 * Contains OpenBSD specific stuff
7 #include <sys/dkstat.h>
9 #include <sys/resource.h>
10 #include <sys/socket.h>
11 #include <sys/sysctl.h>
13 #include <sys/types.h>
14 #include <sys/vmmeter.h>
16 #include <sys/ioctl.h>
17 #include <sys/sensors.h>
18 #include <sys/malloc.h>
23 #include <net/if_media.h>
24 #include <netinet/in.h>
35 #include <machine/apmvar.h>
37 #include <net80211/ieee80211.h>
38 #include <net80211/ieee80211_ioctl.h>
42 #define MAXSHOWDEVS 16
45 #define pagetok(size) ((size) << pageshift)
47 inline void proc_find_top(struct process **cpu, struct process **mem);
49 static short cpu_setup = 0;
52 struct ifmibdata *data = NULL;
63 kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
65 fprintf(stderr, "error opening kvm\n");
71 /* note: swapmode taken from 'top' source */
73 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
74 * to be based on the new swapctl(2) system call.
77 swapmode(int *used, int *total)
79 struct swapent *swdev;
82 nswap = swapctl(SWAP_NSWAP, 0, 0);
86 swdev = malloc(nswap * sizeof(*swdev));
90 rnswap = swapctl(SWAP_STATS, swdev, nswap);
94 /* if rnswap != nswap, then what? */
98 for (i = 0; i < nswap; i++) {
99 if (swdev[i].se_flags & SWF_ENABLE) {
100 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
101 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
111 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
112 struct timeval boottime;
114 size_t size = sizeof (boottime);
116 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) &&
117 (boottime.tv_sec != 0)) {
119 info.uptime = now - boottime.tv_sec;
121 fprintf(stderr, "Could not get uptime\n");
129 static int mib[2] = {CTL_VM, VM_METER};
130 struct vmtotal vmtotal;
132 int pagesize, pageshift, swap_avail, swap_used;
134 pagesize = getpagesize();
136 while (pagesize > 1) {
141 /* we only need the amount of log(2)1024 for our conversion */
142 pageshift -= LOG1024;
144 /* get total -- systemwide main memory usage structure */
145 size = sizeof(vmtotal);
146 if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
147 warn("sysctl failed");
148 bzero(&vmtotal, sizeof(vmtotal));
151 info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
152 info.mem = pagetok(vmtotal.t_rm);
154 if ((swapmode(&swap_used, &swap_avail)) >= 0) {
155 info.swapmax = swap_avail;
156 info.swap = swap_used;
168 long long r, t, last_recv, last_trans;
169 struct ifaddrs *ifap, *ifa;
174 delta = current_update_time - last_update_time;
178 if (getifaddrs(&ifap) < 0)
181 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
182 ns = get_net_stat((const char *) ifa->ifa_name);
184 if (ifa->ifa_flags & IFF_UP) {
185 struct ifaddrs *iftmp;
189 last_recv = ns->recv;
190 last_trans = ns->trans;
192 if (ifa->ifa_addr->sa_family != AF_LINK)
195 for (iftmp = ifa->ifa_next; iftmp != NULL &&
196 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
197 iftmp = iftmp->ifa_next)
198 if (iftmp->ifa_addr->sa_family == AF_INET)
199 memcpy(&(ns->addr), iftmp->ifa_addr,
200 iftmp->ifa_addr->sa_len);
202 ifd = (struct if_data *) ifa->ifa_data;
206 if (r < ns->last_read_recv)
208 ((long long) 4294967295U -
209 ns->last_read_recv) + r;
211 ns->recv += (r - ns->last_read_recv);
213 ns->last_read_recv = r;
215 if (t < ns->last_read_trans)
217 ((long long) 4294967295U -
218 ns->last_read_trans) + t;
220 ns->trans += (t - ns->last_read_trans);
222 ns->last_read_trans = t;
224 /* calculate speeds */
225 ns->recv_speed = (ns->recv - last_recv) / delta;
226 ns->trans_speed = (ns->trans - last_trans) / delta;
237 update_total_processes()
242 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
244 info.procs = n_processes;
248 update_running_processes()
250 struct kinfo_proc2 *p;
255 int max_size = sizeof(struct kinfo_proc2);
256 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
257 for (i = 0; i < n_processes; i++) {
258 if (p[i].p_stat == SRUN)
262 info.run_procs = cnt;
265 struct cpu_load_struct {
266 unsigned long load[5];
269 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
270 long cpu_used, oldtotal, oldused;
276 * FIXME: is it possible to get per cpu stats with openbsd?
280 int mib[2] = { CTL_HW, HW_NCPU };
281 size_t len = sizeof(cpu_count);
282 if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) == 0)
283 info.cpu_count = cpu_count;
284 else /* last resort, 1 cpu */
288 info.cpu_usage = malloc(info.cpu_count * sizeof (float));
289 if (info.cpu_usage == NULL)
296 int mib[2] = { CTL_KERN, KERN_CPTIME };
298 long cp_time[CPUSTATES];
299 size_t len = sizeof (cp_time);
301 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
302 if ((cpu_setup == 0) || (!info.cpu_usage)) {
307 if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
308 (void) fprintf(stderr, "Cannot get kern.cp_time");
311 fresh.load[0] = cp_time[CP_USER];
312 fresh.load[1] = cp_time[CP_NICE];
313 fresh.load[2] = cp_time[CP_SYS];
314 fresh.load[3] = cp_time[CP_IDLE];
315 fresh.load[4] = cp_time[CP_IDLE];
317 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
319 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
321 if ((total - oldtotal) != 0) {
322 info.cpu_usage[0] = ((double) (used - oldused)) /
323 (double) (total - oldtotal);
325 info.cpu_usage[0] = 0;
333 update_load_average()
338 info.loadavg[0] = (float) v[0];
339 info.loadavg[1] = (float) v[1];
340 info.loadavg[2] = (float) v[2];
343 /* read sensors from sysctl */
344 void update_obsd_sensors()
346 int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
347 struct sensor sensor;
348 struct sensordev sensordev;
350 enum sensor_type type;
352 slen = sizeof(sensor);
353 sdlen = sizeof(sensordev);
357 dev = obsd_sensors.device; // FIXME: read more than one device
359 /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
361 if(sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
367 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
369 for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
371 if (sysctl(mib, 5, &sensor, &slen, NULL, 0)
377 if (sensor.flags & SENSOR_FINVALID)
382 //printf("num: %i value: %.2f\n", sensor.numt, (sensor.value - 273150000) / 1000000.0);
383 obsd_sensors.temp[dev][sensor.numt] = (sensor.value - 273150000) / 1000000.0;
386 //printf("num: %i value: %i\n", sensor.numt, sensor.value);
387 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
389 case SENSOR_VOLTS_DC:
390 obsd_sensors.volt[dev][sensor.numt] = sensor.value/1000000.0;
405 void get_obsd_vendor(char *buf, size_t client_buffer_size)
411 size_t size = sizeof(vendor);
412 if(sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
413 fprintf(stderr, "error reading vendor");
414 snprintf(buf, client_buffer_size, "unknown");
416 snprintf(buf, client_buffer_size, "%s", vendor);
421 void get_obsd_product(char *buf, size_t client_buffer_size)
427 size_t size = sizeof(product);
428 if(sysctl(mib, 2, product, &size, NULL, 0) == -1) {
429 fprintf(stderr, "error reading product");
430 snprintf(buf, client_buffer_size, "unknown");
432 snprintf(buf, client_buffer_size, "%s", product);
436 /* rdtsc() and get_freq_dynamic() copied from linux.c */
438 #if defined(__i386) || defined(__x86_64)
439 __inline__ unsigned long long int
442 unsigned long long int x;
443 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
448 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
450 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
451 char *p_format, int divisor)
453 #if defined(__i386) || defined(__x86_64)
455 struct timeval tvstart, tvstop;
456 unsigned long long cycles[2]; /* gotta be 64 bit */
457 unsigned int microseconds; /* total time taken */
459 memset(&tz, 0, sizeof (tz));
461 /* get this function in cached memory */
462 gettimeofday(&tvstart, &tz);
464 gettimeofday(&tvstart, &tz);
466 /* we don't trust that this is any specific length of time */
469 gettimeofday(&tvstop, &tz);
470 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
471 (tvstop.tv_usec - tvstart.tv_usec);
473 snprintf(p_client_buffer, client_buffer_size, p_format,
474 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
476 get_freq(p_client_buffer, client_buffer_size, p_format, divisor);
482 get_freq(char *p_client_buffer, size_t client_buffer_size,
483 char *p_format, int divisor, unsigned int cpu)
486 int mib[2] = { CTL_HW, HW_CPUSPEED };
488 if (!p_client_buffer || client_buffer_size <= 0 ||
489 !p_format || divisor <= 0)
492 size_t size = sizeof(freq);
493 if(sysctl(mib, 2, &freq, &size, NULL, 0) == 0)
494 snprintf(p_client_buffer, client_buffer_size,
495 p_format, (float)freq/divisor);
497 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
505 proc_find_top(info.cpu, info.memu);
511 struct net_stat * ns;
512 struct ifaddrs *ifap, *ifa;
513 struct ifmediareq ifmr;
514 struct ieee80211_nodereq nr;
515 struct ieee80211_bssid bssid;
521 if (getifaddrs(&ifap) < 0)
524 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
525 ns = get_net_stat((const char *) ifa->ifa_name);
527 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
530 bzero(&ifmr, sizeof(ifmr));
531 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
532 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
536 * We can monitor only wireless interfaces
537 * which not in hostap mode
539 if ((ifmr.ifm_active & IFM_IEEE80211) &&
540 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
543 memset(&bssid, 0, sizeof(bssid));
544 strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
545 ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
547 bzero(&nr, sizeof(nr));
548 bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
549 strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
551 if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
552 //if (nr.nr_max_rssi)
553 // printf(" %u%%", IEEE80211_NODEREQ_RSSI(&nr));
555 ns->linkstatus = nr.nr_rssi;
565 return; /* XXX implement? hifi: not sure how */
569 * While topless is obviously better, top is also not bad.
573 comparecpu(const void *a, const void *b)
575 if (((struct process *)a)->amount > ((struct process *)b)->amount)
578 if (((struct process *)a)->amount < ((struct process *)b)->amount)
585 comparemem(const void *a, const void *b)
587 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
590 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
597 proc_find_top(struct process **cpu, struct process **mem)
599 struct kinfo_proc2 *p;
602 struct process *processes;
606 int pagesize = getpagesize();
608 /* we get total pages count again to be sure it is up to date */
611 size_t size = sizeof(total_pages);
612 if(sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1)
613 fprintf(stderr, "error reading nmempages\n");
615 int max_size = sizeof(struct kinfo_proc2);
616 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
617 processes = malloc(n_processes * sizeof (struct process));
620 for (i = 0; i < n_processes; i++) {
621 if (!((p[i].p_flag & P_SYSTEM)) &&
622 p[i].p_comm != NULL) {
623 processes[j].pid = p[i].p_pid;
624 processes[j].name = strdup(p[i].p_comm);
625 processes[j].amount = 100.0 *
626 p[i].p_pctcpu / FSCALE;
627 processes[j].totalmem = (float)(p[i].p_vm_rssize * pagesize /
628 (float)total_pages) * 100.0;
633 qsort(processes, j - 1, sizeof (struct process), comparemem);
634 for (i = 0; i < 10; i++) {
635 struct process *tmp, *ttmp;
637 tmp = malloc(sizeof (struct process));
638 tmp->pid = processes[i].pid;
639 tmp->amount = processes[i].amount;
640 tmp->totalmem = processes[i].totalmem;
641 tmp->name = strdup(processes[i].name);
651 qsort(processes, j - 1, sizeof (struct process), comparecpu);
652 for (i = 0; i < 10; i++) {
653 struct process *tmp, *ttmp;
655 tmp = malloc(sizeof (struct process));
656 tmp->pid = processes[i].pid;
657 tmp->amount = processes[i].amount;
658 tmp->totalmem = processes[i].totalmem;
659 tmp->name = strdup(processes[i].name);
669 for (i = 0; i < j; free(processes[i++].name));
673 #if defined(i386) || defined(__i386__)
674 #define APMDEV "/dev/apm"
675 #define APM_UNKNOWN 255
678 apm_getinfo(int fd, void *null)
680 if (ioctl(fd, APM_IOC_GETPOWER) == -1)
690 struct apm_power_info info;
692 fd = open(APMDEV, O_RDONLY);
696 if (apm_getinfo(fd, &info) != 0) {
702 switch (info.ac_state) {
707 if (info.battery_state == APM_BATT_CHARGING)
719 *get_apm_battery_life()
723 struct apm_power_info info;
726 out = (char *)calloc(16, sizeof (char));
728 fd = open(APMDEV, O_RDONLY);
730 strncpy(out, "ERR", 16);
734 if (apm_getinfo(fd, &info) != 0) {
736 strncpy(out, "ERR", 16);
741 batt_life = info.battery_life;
742 if (batt_life <= 100) {
743 snprintf(out, 16, "%d%%", batt_life);
746 strncpy(out, "ERR", 16);
752 *get_apm_battery_time()
757 struct apm_power_info info;
760 out = (char *)calloc(16, sizeof (char));
762 fd = open(APMDEV, O_RDONLY);
764 strncpy(out, "ERR", 16);
768 if (apm_getinfo(fd, &info) != 0) {
770 strncpy(out, "ERR", 16);
775 batt_time = info.minutes_left;
778 strncpy(out, "unknown", 16);
782 snprintf(out, 16, "%2d:%02d", h, m);
790 /* empty stubs so conky links */
792 get_battery_perct(const char *bat)
798 get_battery_perct_bar(const char *bar)
809 void update_entropy (void)
815 free_all_processes(void)
821 get_acpi_temperature(int fd)
827 get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
833 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
840 get_i2c_info(int *fd, int arg, char *devtype, char *type)
846 open_acpi_temperature(const char *name)
852 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
858 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
864 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
870 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)