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>
30 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
31 #define KELVTOC(x) ((x - 2732) / 10.0)
33 static int getsysctl(char *name, void *ptr, size_t len)
36 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
47 static kvm_t *kd = NULL;
48 struct ifmibdata *data = NULL;
51 static int swapmode(int *retavail, int *retfree)
54 int pagesize = getpagesize();
55 struct kvm_swap swapary[1];
56 static int kd_init = 1;
60 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
61 O_RDONLY, "kvm_open")) == NULL) {
62 (void) fprintf(stderr, "Cannot read kvm.");
74 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
76 n = kvm_getswapinfo(kd, swapary, 1, 0);
77 if (n < 0 || swapary[0].ksw_total == 0)
80 *retavail = CONVERT(swapary[0].ksw_total);
81 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
83 n = (int) ((double) swapary[0].ksw_used * 100.0 /
84 (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 (void) fprintf(stderr, "Could not get uptime\n");
110 void update_meminfo()
112 int total_pages, inactive_pages, free_pages;
113 int swap_avail, swap_free;
115 int pagesize = getpagesize();
117 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
118 (void) fprintf(stderr,
119 "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
121 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
122 (void) fprintf(stderr,
123 "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
125 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
126 (void) fprintf(stderr,
127 "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
129 info.memmax = (total_pages * pagesize) >> 10;
131 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
134 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
135 info.swapmax = swap_avail;
136 info.swap = (swap_avail - swap_free);
143 void update_net_stats()
147 long long r, t, last_recv, last_trans;
148 struct ifaddrs *ifap, *ifa;
153 delta = current_update_time - last_update_time;
157 if (getifaddrs(&ifap) < 0)
160 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
161 ns = get_net_stat((const char *) ifa->ifa_name);
163 if (ifa->ifa_flags & IFF_UP) {
164 last_recv = ns->recv;
165 last_trans = ns->trans;
167 if (ifa->ifa_addr->sa_family != AF_LINK)
170 ifd = (struct if_data *) ifa->ifa_data;
174 if (r < ns->last_read_recv)
176 ((long long) 4294967295U -
177 ns->last_read_recv) + r;
179 ns->recv += (r - ns->last_read_recv);
181 ns->last_read_recv = r;
183 if (t < ns->last_read_trans)
185 ((long long) 4294967295U -
186 ns->last_read_trans) + t;
188 ns->trans += (t - ns->last_read_trans);
190 ns->last_read_trans = t;
193 /* calculate speeds */
194 ns->recv_speed = (ns->recv - last_recv) / delta;
195 ns->trans_speed = (ns->trans - last_trans) / delta;
202 void update_total_processes()
205 static int kd_init = 1;
209 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
210 O_RDONLY, "kvm_open")) == NULL) {
211 (void) fprintf(stderr, "Cannot read kvm.");
218 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
222 info.procs = n_processes;
225 void update_running_processes()
227 static int kd_init = 1;
228 struct kinfo_proc *p;
235 kvm_open("/dev/null", "/dev/null", "/dev/null",
236 O_RDONLY, "kvm_open")) == NULL) {
237 (void) fprintf(stderr, "Cannot read kvm.");
242 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
243 for (i = 0; i < n_processes; i++) {
245 if (p[i].kp_proc.p_stat == SRUN)
247 if (p[i].ki_stat == SRUN)
254 info.run_procs = cnt;
257 struct cpu_load_struct {
258 unsigned long load[5];
261 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
262 long cpu_used, oldtotal, oldused;
264 void update_cpu_usage()
267 long cp_time[CPUSTATES];
268 size_t len = sizeof(cp_time);
270 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
271 (void) fprintf(stderr, "Cannot get kern.cp_time");
274 fresh.load[0] = cp_time[CP_USER];
275 fresh.load[1] = cp_time[CP_NICE];
276 fresh.load[2] = cp_time[CP_SYS];
277 fresh.load[3] = cp_time[CP_IDLE];
278 fresh.load[4] = cp_time[CP_IDLE];
280 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
282 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
284 if ((total - oldtotal) != 0) {
286 ((double) (used - oldused)) / (double) (total -
296 double get_i2c_info(int *fd, int arg, char *devtype, char *type)
301 void update_load_average()
306 info.loadavg[0] = (float) v[0];
307 info.loadavg[1] = (float) v[1];
308 info.loadavg[2] = (float) v[2];
311 double get_acpi_temperature(int fd)
315 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
316 (void) fprintf(stderr,
317 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
321 return KELVTOC(temp);
324 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
328 if (GETSYSCTL("hw.acpi.battery.time", battime))
329 (void) fprintf(stderr,
330 "Cannot read sysctl \"hw.acpi.battery.time\"\n");
333 snprintf(buf, n, "Discharging, remaining %d:%2.2d",
334 battime / 60, battime % 60);
336 snprintf(buf, n, "Battery is charging");
340 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
346 int open_acpi_temperature(const char *name)
351 char *get_acpi_ac_adapter(void)
354 char *acstate = (char *) malloc(100);
356 if (GETSYSCTL("hw.acpi.acline", state)) {
357 (void) fprintf(stderr,
358 "Cannot read sysctl \"hw.acpi.acline\"\n");
364 strcpy(acstate, "Running on AC Power");
366 strcpy(acstate, "Running on battery");
376 char *get_adt746x_cpu()
381 char *get_adt746x_fan()
386 /* rdtsc() and get_freq_dynamic() copied from linux.c */
388 #if defined(__i386) || defined(__x86_64)
389 __inline__ unsigned long long int rdtsc()
391 unsigned long long int x;
392 __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
397 float get_freq_dynamic()
399 #if defined(__i386) || defined(__x86_64)
401 struct timeval tvstart, tvstop;
402 unsigned long long cycles[2]; /* gotta be 64 bit */
403 unsigned int microseconds; /* total time taken */
405 memset(&tz, 0, sizeof(tz));
407 /* get this function in cached memory */
408 gettimeofday(&tvstart, &tz);
410 gettimeofday(&tvstart, &tz);
412 /* we don't trust that this is any specific length of time */
415 gettimeofday(&tvstop, &tz);
416 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
417 (tvstop.tv_usec - tvstart.tv_usec);
419 return (cycles[1] - cycles[0]) / microseconds;
427 /* First, try to obtain CPU frequency via dev.cpu.0.freq sysctl
428 * (cpufreq(4)). If failed, do i386 magic. */
431 if (GETSYSCTL("dev.cpu.0.freq", freq) == 0)
442 void update_wifi_stats()