3 * Contains FreeBSD specific stuff
8 #include <sys/dkstat.h>
10 #include <sys/resource.h>
11 #include <sys/socket.h>
12 #include <sys/sysctl.h>
14 #include <sys/types.h>
15 #include <sys/vmmeter.h>
19 #include <net/if_mib.h>
32 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof (var))
33 #define KELVTOC(x) ((x - 2732) / 10.0)
34 #define MAXSHOWDEVS 16
40 inline void proc_find_top(struct process **cpu, struct process **mem);
42 u_int64_t diskio_prev = 0;
43 static short cpu_setup = 0;
44 static short diskio_setup = 0;
46 static int getsysctl(char *name, void *ptr, size_t len)
49 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
60 struct ifmibdata *data = NULL;
63 static int swapmode(int *retavail, int *retfree)
66 int pagesize = getpagesize();
67 struct kvm_swap swapary[1];
72 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
74 n = kvm_getswapinfo(kd, swapary, 1, 0);
75 if (n < 0 || swapary[0].ksw_total == 0)
78 *retavail = CONVERT(swapary[0].ksw_total);
79 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
81 n = (int) ((double) swapary[0].ksw_used * 100.0 /
82 (double) swapary[0].ksw_total);
95 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
96 struct timeval boottime;
98 size_t size = sizeof (boottime);
100 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
101 && (boottime.tv_sec != 0)) {
103 info.uptime = now - boottime.tv_sec;
105 fprintf(stderr, "Could not get uptime\n");
113 int total_pages, inactive_pages, free_pages;
114 int swap_avail, swap_free;
116 int pagesize = getpagesize();
118 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
120 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
122 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
124 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
126 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
128 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
130 info.memmax = (total_pages * pagesize) >> 10;
132 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
135 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
136 info.swapmax = swap_avail;
137 info.swap = (swap_avail - swap_free);
149 long long r, t, last_recv, last_trans;
150 struct ifaddrs *ifap, *ifa;
155 delta = current_update_time - last_update_time;
159 if (getifaddrs(&ifap) < 0)
162 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
163 ns = get_net_stat((const char *) ifa->ifa_name);
165 if (ifa->ifa_flags & IFF_UP) {
166 last_recv = ns->recv;
167 last_trans = ns->trans;
169 if (ifa->ifa_addr->sa_family != AF_LINK)
172 ifd = (struct if_data *) ifa->ifa_data;
176 if (r < ns->last_read_recv)
178 ((long long) 4294967295U -
179 ns->last_read_recv) + r;
181 ns->recv += (r - ns->last_read_recv);
183 ns->last_read_recv = r;
185 if (t < ns->last_read_trans)
187 ((long long) 4294967295U -
188 ns->last_read_trans) + t;
190 ns->trans += (t - ns->last_read_trans);
192 ns->last_read_trans = t;
195 /* calculate speeds */
196 ns->recv_speed = (ns->recv - last_recv) / delta;
197 ns->trans_speed = (ns->trans - last_trans) / delta;
205 update_total_processes()
209 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
211 info.procs = n_processes;
215 update_running_processes()
217 struct kinfo_proc *p;
221 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
222 for (i = 0; i < n_processes; i++) {
224 if (p[i].kp_proc.p_stat == SRUN)
226 if (p[i].ki_stat == SRUN)
231 info.run_procs = cnt;
234 struct cpu_load_struct {
235 unsigned long load[5];
238 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
239 long cpu_used, oldtotal, oldused;
244 /* int cpu_count = 0; */
248 * FreeBSD doesn't allow to get per CPU load stats
249 * on SMP machines. It's possible to get a CPU count,
250 * but as we fulfil only info.cpu_usage[0], it's better
251 * to report there's only one CPU. It should fix some bugs
255 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
256 info.cpu_count = cpu_count;
260 info.cpu_usage = malloc(info.cpu_count * sizeof (float));
261 if (info.cpu_usage == NULL)
265 /* XXX: SMP support */
270 long cp_time[CPUSTATES];
271 size_t len = sizeof (cp_time);
273 if (cpu_setup == 0) {
278 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
279 (void) fprintf(stderr, "Cannot get kern.cp_time");
282 fresh.load[0] = cp_time[CP_USER];
283 fresh.load[1] = cp_time[CP_NICE];
284 fresh.load[2] = cp_time[CP_SYS];
285 fresh.load[3] = cp_time[CP_IDLE];
286 fresh.load[4] = cp_time[CP_IDLE];
288 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
290 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
292 if ((total - oldtotal) != 0) {
293 info.cpu_usage[0] = ((double) (used - oldused)) /
294 (double) (total - oldtotal);
296 info.cpu_usage[0] = 0;
304 get_i2c_info(int *fd, int arg, char *devtype, char *type)
310 update_load_average()
315 info.loadavg[0] = (float) v[0];
316 info.loadavg[1] = (float) v[1];
317 info.loadavg[2] = (float) v[2];
321 get_acpi_temperature(int fd)
325 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
327 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
331 return (KELVTOC(temp));
335 get_battery_stuff(char *buf, unsigned int n, const char *bat)
339 if (GETSYSCTL("hw.acpi.battery.time", battime))
340 (void) fprintf(stderr,
341 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
344 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
345 battime / 60, battime % 60);
347 snprintf(buf, n, "Battery is charging");
351 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
358 open_acpi_temperature(const char *name)
364 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
368 if (!p_client_buffer || client_buffer_size <= 0)
371 if (GETSYSCTL("hw.acpi.acline", state)) {
373 "Cannot read sysctl \"hw.acpi.acline\"\n");
379 strncpy(p_client_buffer, "Running on AC Power",
382 strncpy(p_client_buffer, "Running on battery",
388 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
390 if (!p_client_buffer || client_buffer_size <= 0)
393 /* not implemented */
394 memset(p_client_buffer, 0, client_buffer_size);
398 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
400 if (!p_client_buffer || client_buffer_size <= 0)
403 /* not implemented */
404 memset(p_client_buffer, 0, client_buffer_size);
408 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
410 if (!p_client_buffer || client_buffer_size <= 0)
413 /* not implemented */
414 memset(p_client_buffer, 0, client_buffer_size);
417 /* rdtsc() and get_freq_dynamic() copied from linux.c */
419 #if defined(__i386) || defined(__x86_64)
420 __inline__ unsigned long long int
423 unsigned long long int x;
424 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
429 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
431 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
432 char *p_format, int divisor)
434 #if defined(__i386) || defined(__x86_64)
436 struct timeval tvstart, tvstop;
437 unsigned long long cycles[2]; /* gotta be 64 bit */
438 unsigned int microseconds; /* total time taken */
440 memset(&tz, 0, sizeof (tz));
442 /* get this function in cached memory */
443 gettimeofday(&tvstart, &tz);
445 gettimeofday(&tvstart, &tz);
447 /* we don't trust that this is any specific length of time */
450 gettimeofday(&tvstop, &tz);
451 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
452 (tvstop.tv_usec - tvstart.tv_usec);
454 snprintf(p_client_buffer, client_buffer_size, p_format,
455 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
457 get_freq(p_client_buffer, client_buffer_size, p_format, divisor);
461 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
463 get_freq(char *p_client_buffer, size_t client_buffer_size,
464 char *p_format, int divisor)
468 if (!p_client_buffer || client_buffer_size <= 0
469 || !p_format || divisor <= 0)
472 if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
473 snprintf(p_client_buffer, client_buffer_size,
474 p_format, freq/divisor);
476 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
482 proc_find_top(info.cpu, info.memu);
496 struct device_selection *dev_select = NULL;
497 long select_generation;
499 static struct statinfo statinfo_cur;
500 u_int64_t diskio_current = 0;
502 bzero(&statinfo_cur, sizeof (statinfo_cur));
503 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
504 bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
506 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
509 devs_count = statinfo_cur.dinfo->numdevs;
510 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
511 &select_generation, statinfo_cur.dinfo->generation,
512 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
513 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
514 for (dn = 0; dn < devs_count; ++dn) {
518 di = dev_select[dn].position;
519 dev = &statinfo_cur.dinfo->devices[di];
521 diskio_current += dev->bytes[DEVSTAT_READ] +
522 dev->bytes[DEVSTAT_WRITE];
529 * Since we return (diskio_total_current - diskio_total_old), first
530 * frame will be way too high (it will be equal to
531 * diskio_total_current, i.e. all disk I/O since boot). That's why
532 * it is better to return 0 first time;
534 if (diskio_setup == 0) {
538 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
540 diskio_prev = diskio_current;
542 free(statinfo_cur.dinfo);
546 * While topless is obviously better, top is also not bad.
550 comparecpu(const void *a, const void *b)
552 if (((struct process *)a)->amount > ((struct process *)b)->amount)
555 if (((struct process *)a)->amount < ((struct process *)b)->amount)
562 comparemem(const void *a, const void *b)
564 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
567 if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
574 proc_find_top(struct process **cpu, struct process **mem)
576 struct kinfo_proc *p;
579 struct process *processes;
583 /* we get total pages count again to be sure it is up to date */
584 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
585 CRIT_ERR("Cannot read sysctl"
586 "\"vm.stats.vm.v_page_count\"");
588 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
589 processes = malloc(n_processes * sizeof (struct process));
591 for (i = 0; i < n_processes; i++) {
592 if (!((p[i].ki_flag & P_SYSTEM)) &&
593 p[i].ki_comm != NULL) {
594 processes[j].pid = p[i].ki_pid;
595 processes[j].name = strdup(p[i].ki_comm);
596 processes[j].amount = 100.0 *
597 p[i].ki_pctcpu / FSCALE;
598 processes[j].totalmem = (float)(p[i].ki_rssize /
599 (float)total_pages) * 100.0;
604 qsort(processes, j - 1, sizeof (struct process), comparemem);
605 for (i = 0; i < 10; i++) {
606 struct process *tmp, *ttmp;
608 tmp = malloc(sizeof (struct process));
609 tmp->pid = processes[i].pid;
610 tmp->amount = processes[i].amount;
611 tmp->totalmem = processes[i].totalmem;
612 tmp->name = strdup(processes[i].name);
622 qsort(processes, j - 1, sizeof (struct process), comparecpu);
623 for (i = 0; i < 10; i++) {
624 struct process *tmp, *ttmp;
626 tmp = malloc(sizeof (struct process));
627 tmp->pid = processes[i].pid;
628 tmp->amount = processes[i].amount;
629 tmp->totalmem = processes[i].totalmem;
630 tmp->name = strdup(processes[i].name);
640 #if defined(FREEBSD_DEBUG)
641 printf("=====\nmem\n");
642 for (i = 0; i < 10; i++) {
643 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
644 mem[i]->pid, mem[i]->totalmem);
648 for (i = 0; i < j; free(processes[i++].name));
652 #if defined(i386) || defined(__i386__)
653 #define APMDEV "/dev/apm"
654 #define APM_UNKNOWN 255
657 apm_getinfo(int fd, apm_info_t aip)
659 if (ioctl(fd, APMIO_GETINFO, aip) == -1)
669 struct apm_info info;
671 fd = open(APMDEV, O_RDONLY);
675 if (apm_getinfo(fd, &info) != 0) {
681 switch (info.ai_acline) {
686 if (info.ai_batt_stat == 3)
698 *get_apm_battery_life()
702 struct apm_info info;
705 out = (char *)calloc(16, sizeof (char));
707 fd = open(APMDEV, O_RDONLY);
709 strncpy(out, "ERR", 16);
713 if (apm_getinfo(fd, &info) != 0) {
715 strncpy(out, "ERR", 16);
720 batt_life = info.ai_batt_life;
721 if (batt_life == APM_UNKNOWN)
722 strncpy(out, "unknown", 16);
723 else if (batt_life <= 100) {
724 snprintf(out, 16, "%d%%", batt_life);
727 strncpy(out, "ERR", 16);
733 *get_apm_battery_time()
738 struct apm_info info;
741 out = (char *)calloc(16, sizeof (char));
743 fd = open(APMDEV, O_RDONLY);
745 strncpy(out, "ERR", 16);
749 if (apm_getinfo(fd, &info) != 0) {
751 strncpy(out, "ERR", 16);
756 batt_time = info.ai_batt_time;
759 strncpy(out, "unknown", 16);
766 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
774 /* empty stub so conky links */
776 free_all_processes(void)