2 * Contains FreeBSD specific stuff
13 #include <sys/param.h>
14 #include <sys/types.h>
16 #include <sys/resource.h>
17 #include <sys/sysctl.h>
18 #include <sys/vmmeter.h>
19 #include <sys/dkstat.h>
22 #include <sys/socket.h>
24 #include <net/if_mib.h>
25 #include <sys/socket.h>
31 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
32 #define KELVTOC(x) ((x - 2732) / 10.0)
33 #define MAXSHOWDEVS 16
35 u_int64_t diskio_prev = 0;
36 static short cpu_setup = 0;
38 static int getsysctl(char *name, void *ptr, size_t len)
41 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
52 static kvm_t *kd = NULL;
53 struct ifmibdata *data = NULL;
56 static int swapmode(int *retavail, int *retfree)
59 int pagesize = getpagesize();
60 struct kvm_swap swapary[1];
61 static int kd_init = 1;
65 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
66 O_RDONLY, "kvm_open")) == NULL) {
67 (void) fprintf(stderr, "Cannot read kvm.");
79 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
81 n = kvm_getswapinfo(kd, swapary, 1, 0);
82 if (n < 0 || swapary[0].ksw_total == 0)
85 *retavail = CONVERT(swapary[0].ksw_total);
86 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
88 n = (int) ((double) swapary[0].ksw_used * 100.0 /
89 (double) swapary[0].ksw_total);
100 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
101 struct timeval boottime;
103 size_t size = sizeof(boottime);
105 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
106 && (boottime.tv_sec != 0)) {
108 info.uptime = now - boottime.tv_sec;
110 (void) fprintf(stderr, "Could not get uptime\n");
115 void update_meminfo()
117 int total_pages, inactive_pages, free_pages;
118 int swap_avail, swap_free;
120 int pagesize = getpagesize();
122 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
123 (void) fprintf(stderr,
124 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
126 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
127 (void) fprintf(stderr,
128 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
130 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
131 (void) fprintf(stderr,
132 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
134 info.memmax = (total_pages * pagesize) >> 10;
136 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
139 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
140 info.swapmax = swap_avail;
141 info.swap = (swap_avail - swap_free);
148 void update_net_stats()
152 long long r, t, last_recv, last_trans;
153 struct ifaddrs *ifap, *ifa;
158 delta = current_update_time - last_update_time;
162 if (getifaddrs(&ifap) < 0)
165 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
166 ns = get_net_stat((const char *) ifa->ifa_name);
168 if (ifa->ifa_flags & IFF_UP) {
169 last_recv = ns->recv;
170 last_trans = ns->trans;
172 if (ifa->ifa_addr->sa_family != AF_LINK)
175 ifd = (struct if_data *) ifa->ifa_data;
179 if (r < ns->last_read_recv)
181 ((long long) 4294967295U -
182 ns->last_read_recv) + r;
184 ns->recv += (r - ns->last_read_recv);
186 ns->last_read_recv = r;
188 if (t < ns->last_read_trans)
190 ((long long) 4294967295U -
191 ns->last_read_trans) + t;
193 ns->trans += (t - ns->last_read_trans);
195 ns->last_read_trans = t;
198 /* calculate speeds */
199 ns->recv_speed = (ns->recv - last_recv) / delta;
200 ns->trans_speed = (ns->trans - last_trans) / delta;
207 void update_total_processes()
210 static int kd_init = 1;
214 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
215 O_RDONLY, "kvm_open")) == NULL) {
216 (void) fprintf(stderr, "Cannot read kvm.");
223 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
227 info.procs = n_processes;
230 void update_running_processes()
232 static int kd_init = 1;
233 struct kinfo_proc *p;
240 kvm_open("/dev/null", "/dev/null", "/dev/null",
241 O_RDONLY, "kvm_open")) == NULL) {
242 (void) fprintf(stderr, "Cannot read kvm.");
247 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
248 for (i = 0; i < n_processes; i++) {
250 if (p[i].kp_proc.p_stat == SRUN)
252 if (p[i].ki_stat == SRUN)
259 info.run_procs = cnt;
262 struct cpu_load_struct {
263 unsigned long load[5];
266 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
267 long cpu_used, oldtotal, oldused;
273 if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
274 info.cpu_count = cpu_count;
276 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
277 if (info.cpu_usage == NULL)
281 void update_cpu_usage()
284 long cp_time[CPUSTATES];
285 size_t len = sizeof(cp_time);
287 if (cpu_setup == 0) {
292 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
293 (void) fprintf(stderr, "Cannot get kern.cp_time");
296 fresh.load[0] = cp_time[CP_USER];
297 fresh.load[1] = cp_time[CP_NICE];
298 fresh.load[2] = cp_time[CP_SYS];
299 fresh.load[3] = cp_time[CP_IDLE];
300 fresh.load[4] = cp_time[CP_IDLE];
302 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
304 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
306 if ((total - oldtotal) != 0) {
307 info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal);
309 info.cpu_usage[0] = 0;
316 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
321 void update_load_average()
326 info.loadavg[0] = (float) v[0];
327 info.loadavg[1] = (float) v[1];
328 info.loadavg[2] = (float) v[2];
331 double get_acpi_temperature(int fd)
335 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
336 (void) fprintf(stderr,
337 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
341 return KELVTOC(temp);
344 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
348 if (GETSYSCTL("hw.acpi.battery.time", battime))
349 (void) fprintf(stderr,
350 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
353 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
354 battime / 60, battime % 60);
356 snprintf(buf, n, "Battery is charging");
360 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
366 int open_acpi_temperature(const char *name)
371 char *get_acpi_ac_adapter(void)
374 char *acstate = (char *) malloc(100);
376 if (GETSYSCTL("hw.acpi.acline", state)) {
377 (void) fprintf(stderr,
378 "Cannot read sysctl \"hw.acpi.acline\"\n");
384 strcpy(acstate, "Running on AC Power");
386 strcpy(acstate, "Running on battery");
396 char *get_adt746x_cpu()
401 char *get_adt746x_fan()
406 /* rdtsc() and get_freq_dynamic() copied from linux.c */
408 #if defined(__i386) || defined(__x86_64)
409 __inline__ unsigned long long int rdtsc()
411 unsigned long long int x;
412 __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
417 float get_freq_dynamic()
419 #if defined(__i386) || defined(__x86_64)
421 struct timeval tvstart, tvstop;
422 unsigned long long cycles[2]; /* gotta be 64 bit */
423 unsigned int microseconds; /* total time taken */
425 memset(&tz, 0, sizeof(tz));
427 /* get this function in cached memory */
428 gettimeofday(&tvstart, &tz);
430 gettimeofday(&tvstart, &tz);
432 /* we don't trust that this is any specific length of time */
435 gettimeofday(&tvstop, &tz);
436 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
437 (tvstop.tv_usec - tvstart.tv_usec);
439 return (cycles[1] - cycles[0]) / microseconds;
449 if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
460 void update_wifi_stats()
469 struct device_selection *dev_select = NULL;
470 long select_generation;
472 static struct statinfo statinfo_cur;
473 u_int64_t diskio_current = 0;
475 bzero(&statinfo_cur, sizeof(statinfo_cur));
476 statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
477 bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
479 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
482 devs_count = statinfo_cur.dinfo->numdevs;
483 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
484 &select_generation, statinfo_cur.dinfo->generation,
485 statinfo_cur.dinfo->devices, devs_count, NULL, 0,
486 NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
487 for (dn = 0; dn < devs_count; ++dn) {
491 di = dev_select[dn].position;
492 dev = &statinfo_cur.dinfo->devices[di];
494 diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
500 diskio_value = (unsigned int)((diskio_current - diskio_prev)/1024);
501 diskio_prev = diskio_current;