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");
114 void update_meminfo()
116 int mib[] = { CTL_VM, VM_UVMEXP2 };
117 int total_pages, inactive_pages, free_pages;
118 int swap_avail, swap_free;
119 const int pagesize = getpagesize();
120 struct uvmexp_sysctl uvmexp;
121 size_t size = sizeof(uvmexp);
123 info.memmax = info.mem = 0;
124 info.swapmax = info.swap = 0;
127 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
128 warn("could not get memory info");
132 total_pages = uvmexp.npages;
133 free_pages = uvmexp.free;
134 inactive_pages = uvmexp.inactive;
136 info.memmax = (total_pages * pagesize) >> 10;
138 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
140 if (swapmode(&swap_avail, &swap_free) >= 0) {
141 info.swapmax = swap_avail;
142 info.swap = (swap_avail - swap_free);
146 void update_net_stats()
151 struct ifnet_head ifhead; /* interfaces are in a tail queue */
153 static struct nlist namelist[] = {
160 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
162 (void) warnx("cannot kvm_openfiles: %s", errbuf);
165 ("maybe you need to setgid kmem this program?");
167 } else if (kvm_nlist(nkd, namelist) != 0) {
168 (void) warn("cannot kvm_nlist");
174 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
175 sizeof(ifhead)) < 0) {
176 (void) warn("cannot kvm_read");
181 delta = current_update_time - last_update_time;
185 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
186 ifnet.if_list.tqe_next && i < 16;
187 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
190 long long last_recv, last_trans;
192 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
194 ns = get_net_stat(ifnet.if_xname);
196 last_recv = ns->recv;
197 last_trans = ns->trans;
199 if (ifnet.if_ibytes < ns->last_read_recv)
201 ((long long) 4294967295U -
202 ns->last_read_recv) + ifnet.if_ibytes;
204 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
206 ns->last_read_recv = ifnet.if_ibytes;
208 if (ifnet.if_obytes < ns->last_read_trans)
210 ((long long) 4294967295U -
211 ns->last_read_trans) + ifnet.if_obytes;
214 (ifnet.if_obytes - ns->last_read_trans);
216 ns->last_read_trans = ifnet.if_obytes;
218 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
219 ns->last_read_recv = ifnet.if_ibytes;
220 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
221 ns->last_read_trans = ifnet.if_obytes;
223 ns->recv_speed = (ns->recv - last_recv) / delta;
224 ns->trans_speed = (ns->trans - last_trans) / delta;
228 void update_total_processes()
230 /* It's easier to use kvm here than sysctl */
239 kvm_getproc2(kd, KERN_PROC_ALL, 0,
240 sizeof(struct kinfo_proc2), &n_processes);
242 info.procs = n_processes;
245 void update_running_processes()
247 struct kinfo_proc2 *p;
256 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
257 sizeof(struct kinfo_proc2), &n_processes);
258 for (i = 0; i < n_processes; i++)
259 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
260 p[i].p_stat == LSONPROC)
264 info.run_procs = cnt;
267 struct cpu_load_struct {
268 unsigned long load[5];
271 struct cpu_load_struct fresh = {
275 long cpu_used, oldtotal, oldused;
277 void update_cpu_usage()
280 static u_int64_t cp_time[CPUSTATES];
281 size_t len = sizeof(cp_time);
285 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
286 (void) warn("cannot get kern.cp_time");
289 fresh.load[0] = cp_time[CP_USER];
290 fresh.load[1] = cp_time[CP_NICE];
291 fresh.load[2] = cp_time[CP_SYS];
292 fresh.load[3] = cp_time[CP_IDLE];
293 fresh.load[4] = cp_time[CP_IDLE];
295 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
297 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
299 if ((total - oldtotal) != 0)
301 ((double) (used - oldused)) / (double) (total -
311 double get_i2c_info(int *fd, int div, char *devtype)
316 void update_load_average()
321 info.loadavg[0] = (float) v[0];
322 info.loadavg[1] = (float) v[1];
323 info.loadavg[2] = (float) v[2];
326 double get_acpi_temperature(int fd)
331 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
336 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
342 int open_acpi_temperature(const char *name)
347 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
349 if ( !p_client_buffer || client_buffer_size <= 0 )
352 /* not implemented */
353 memset(p_client_buffer,0,client_buffer_size);
358 /*char *get_acpi_fan()*/
359 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
361 if ( !p_client_buffer || client_buffer_size <= 0 )
364 /* not implemented */
365 memset(p_client_buffer,0,client_buffer_size);
370 void update_entropy (void)