17 #include <sys/param.h>
18 #include <sys/sysctl.h>
19 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <sys/sched.h>
25 #include <sys/envsys.h>
29 #include <uvm/uvm_extern.h>
31 #include <machine/param.h>
36 static kvm_t *kd = NULL;
37 int kd_init = 0, nkd_init = 0;
39 char errbuf[_POSIX2_LINE_MAX];
41 static int init_kvm(void)
46 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
48 (void) warnx("cannot kvm_openfiles: %s", errbuf);
55 static int swapmode(int *retavail, int *retfree)
63 n = swapctl(SWAP_NSWAP, 0, 0);
66 (void) warn("could not get swap information");
70 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
73 (void) warn("memory allocation failed");
77 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
78 (void) warn("could not get swap stats");
82 *retavail += (int) dbtob(sep[n - 1].se_nblks);
84 (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
86 *retavail = (int) (*retavail / 1024);
87 *retfree = (int) (*retfree / 1024);
99 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
100 struct timeval boottime;
102 int size = sizeof(boottime);
104 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
105 && (boottime.tv_sec != 0)) {
107 info.uptime = now - boottime.tv_sec;
109 (void) warn("could not get uptime");
115 void update_meminfo()
117 int mib[] = { CTL_VM, VM_UVMEXP2 };
118 int total_pages, inactive_pages, free_pages;
119 int swap_avail, swap_free;
120 const int pagesize = getpagesize();
121 struct uvmexp_sysctl uvmexp;
122 size_t size = sizeof(uvmexp);
124 info.memmax = info.mem = 0;
125 info.swapmax = info.swap = 0;
128 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
129 warn("could not get memory info");
133 total_pages = uvmexp.npages;
134 free_pages = uvmexp.free;
135 inactive_pages = uvmexp.inactive;
137 info.memmax = (total_pages * pagesize) >> 10;
139 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
141 if (swapmode(&swap_avail, &swap_free) >= 0) {
142 info.swapmax = swap_avail;
143 info.swap = (swap_avail - swap_free);
147 void update_net_stats()
152 struct ifnet_head ifhead; /* interfaces are in a tail queue */
154 static struct nlist namelist[] = {
161 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
163 (void) warnx("cannot kvm_openfiles: %s", errbuf);
166 ("maybe you need to setgid kmem this program?");
168 } else if (kvm_nlist(nkd, namelist) != 0) {
169 (void) warn("cannot kvm_nlist");
175 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
176 sizeof(ifhead)) < 0) {
177 (void) warn("cannot kvm_read");
182 delta = current_update_time - last_update_time;
186 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
187 ifnet.if_list.tqe_next && i < 16;
188 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
191 long long last_recv, last_trans;
193 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
195 ns = get_net_stat(ifnet.if_xname);
197 last_recv = ns->recv;
198 last_trans = ns->trans;
200 if (ifnet.if_ibytes < ns->last_read_recv)
202 ((long long) 4294967295U -
203 ns->last_read_recv) + ifnet.if_ibytes;
205 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
207 ns->last_read_recv = ifnet.if_ibytes;
209 if (ifnet.if_obytes < ns->last_read_trans)
211 ((long long) 4294967295U -
212 ns->last_read_trans) + ifnet.if_obytes;
215 (ifnet.if_obytes - ns->last_read_trans);
217 ns->last_read_trans = ifnet.if_obytes;
219 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
220 ns->last_read_recv = ifnet.if_ibytes;
221 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
222 ns->last_read_trans = ifnet.if_obytes;
224 ns->recv_speed = (ns->recv - last_recv) / delta;
225 ns->trans_speed = (ns->trans - last_trans) / delta;
229 void update_total_processes()
231 /* It's easier to use kvm here than sysctl */
240 kvm_getproc2(kd, KERN_PROC_ALL, 0,
241 sizeof(struct kinfo_proc2), &n_processes);
243 info.procs = n_processes;
246 void update_running_processes()
248 struct kinfo_proc2 *p;
257 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
258 sizeof(struct kinfo_proc2), &n_processes);
259 for (i = 0; i < n_processes; i++)
260 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
261 p[i].p_stat == LSONPROC)
265 info.run_procs = cnt;
268 struct cpu_load_struct {
269 unsigned long load[5];
272 struct cpu_load_struct fresh = {
276 long cpu_used, oldtotal, oldused;
278 void update_cpu_usage()
281 static u_int64_t cp_time[CPUSTATES];
282 size_t len = sizeof(cp_time);
286 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
287 (void) warn("cannot get kern.cp_time");
290 fresh.load[0] = cp_time[CP_USER];
291 fresh.load[1] = cp_time[CP_NICE];
292 fresh.load[2] = cp_time[CP_SYS];
293 fresh.load[3] = cp_time[CP_IDLE];
294 fresh.load[4] = cp_time[CP_IDLE];
296 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
298 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
300 if ((total - oldtotal) != 0)
302 ((double) (used - oldused)) / (double) (total -
312 double get_i2c_info(int *fd, int div, char *devtype)
317 void update_load_average()
322 info.loadavg[0] = (float) v[0];
323 info.loadavg[1] = (float) v[1];
324 info.loadavg[2] = (float) v[2];
327 double get_acpi_temperature(int fd)
332 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
337 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
343 int open_acpi_temperature(const char *name)
348 char *get_acpi_ac_adapter(void)