17 #include <sys/param.h>
18 #include <sys/sysctl.h>
19 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <sys/sched.h>
24 #include <sys/envsys.h>
28 #include <uvm/uvm_extern.h>
30 #include <machine/param.h>
35 static kvm_t *kd = NULL;
36 int kd_init = 0, nkd_init = 0;
38 char errbuf[_POSIX2_LINE_MAX];
40 static int init_kvm(void)
45 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
47 (void) warnx("cannot kvm_openfiles: %s", errbuf);
54 static int swapmode(int *retavail, int *retfree)
62 n = swapctl(SWAP_NSWAP, 0, 0);
65 (void) warn("could not get swap information");
69 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
72 (void) warn("memory allocation failed");
76 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
77 (void) warn("could not get swap stats");
81 *retavail += (int) dbtob(sep[n - 1].se_nblks);
83 (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
85 *retavail = (int) (*retavail / 1024);
86 *retfree = (int) (*retfree / 1024);
98 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
99 struct timeval boottime;
101 int size = sizeof(boottime);
103 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
104 && (boottime.tv_sec != 0)) {
106 info.uptime = now - boottime.tv_sec;
108 (void) warn("could not get uptime");
113 int check_mount(char *s)
119 void update_meminfo()
121 int mib[] = { CTL_VM, VM_UVMEXP2 };
122 int total_pages, inactive_pages, free_pages;
123 int swap_avail, swap_free;
124 const int pagesize = getpagesize();
125 struct uvmexp_sysctl uvmexp;
126 size_t size = sizeof(uvmexp);
128 info.memmax = info.mem = 0;
129 info.swapmax = info.swap = 0;
132 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
133 warn("could not get memory info");
137 total_pages = uvmexp.npages;
138 free_pages = uvmexp.free;
139 inactive_pages = uvmexp.inactive;
141 info.memmax = (total_pages * pagesize) >> 10;
143 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
145 if (swapmode(&swap_avail, &swap_free) >= 0) {
146 info.swapmax = swap_avail;
147 info.swap = (swap_avail - swap_free);
151 void update_net_stats()
156 struct ifnet_head ifhead; /* interfaces are in a tail queue */
158 static struct nlist namelist[] = {
165 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
167 (void) warnx("cannot kvm_openfiles: %s", errbuf);
170 ("maybe you need to setgid kmem this program?");
172 } else if (kvm_nlist(nkd, namelist) != 0) {
173 (void) warn("cannot kvm_nlist");
179 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
180 sizeof(ifhead)) < 0) {
181 (void) warn("cannot kvm_read");
186 delta = current_update_time - last_update_time;
190 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
191 ifnet.if_list.tqe_next && i < 16;
192 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
195 long long last_recv, last_trans;
197 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
199 ns = get_net_stat(ifnet.if_xname);
201 last_recv = ns->recv;
202 last_trans = ns->trans;
204 if (ifnet.if_ibytes < ns->last_read_recv)
206 ((long long) 4294967295U -
207 ns->last_read_recv) + ifnet.if_ibytes;
209 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
211 ns->last_read_recv = ifnet.if_ibytes;
213 if (ifnet.if_obytes < ns->last_read_trans)
215 ((long long) 4294967295U -
216 ns->last_read_trans) + ifnet.if_obytes;
219 (ifnet.if_obytes - ns->last_read_trans);
221 ns->last_read_trans = ifnet.if_obytes;
223 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
224 ns->last_read_recv = ifnet.if_ibytes;
225 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
226 ns->last_read_trans = ifnet.if_obytes;
228 ns->recv_speed = (ns->recv - last_recv) / delta;
229 ns->trans_speed = (ns->trans - last_trans) / delta;
233 void update_total_processes()
235 /* It's easier to use kvm here than sysctl */
244 kvm_getproc2(kd, KERN_PROC_ALL, 0,
245 sizeof(struct kinfo_proc2), &n_processes);
247 info.procs = n_processes;
250 void update_running_processes()
252 struct kinfo_proc2 *p;
261 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
262 sizeof(struct kinfo_proc2), &n_processes);
263 for (i = 0; i < n_processes; i++)
264 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
265 p[i].p_stat == LSONPROC)
269 info.run_procs = cnt;
272 struct cpu_load_struct {
273 unsigned long load[5];
276 struct cpu_load_struct fresh = {
280 long cpu_used, oldtotal, oldused;
282 void update_cpu_usage()
285 static u_int64_t cp_time[CPUSTATES];
286 size_t len = sizeof(cp_time);
290 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
291 (void) warn("cannot get kern.cp_time");
294 fresh.load[0] = cp_time[CP_USER];
295 fresh.load[1] = cp_time[CP_NICE];
296 fresh.load[2] = cp_time[CP_SYS];
297 fresh.load[3] = cp_time[CP_IDLE];
298 fresh.load[4] = cp_time[CP_IDLE];
300 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
302 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
304 if ((total - oldtotal) != 0)
306 ((double) (used - oldused)) / (double) (total -
316 double get_i2c_info(int *fd, int div, char *devtype)
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)
336 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
341 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
347 int open_acpi_temperature(const char *name)
352 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
354 if ( !p_client_buffer || client_buffer_size <= 0 )
357 /* not implemented */
358 memset(p_client_buffer,0,client_buffer_size);
363 /*char *get_acpi_fan()*/
364 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
366 if ( !p_client_buffer || client_buffer_size <= 0 )
369 /* not implemented */
370 memset(p_client_buffer,0,client_buffer_size);
375 void update_entropy (void)