2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <sys/sched.h>
50 #include <sys/envsys.h>
54 #include <uvm/uvm_extern.h>
56 #include <machine/param.h>
61 static kvm_t *kd = NULL;
62 int kd_init = 0, nkd_init = 0;
64 char errbuf[_POSIX2_LINE_MAX];
66 static int init_kvm(void)
71 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
73 (void) warnx("cannot kvm_openfiles: %s", errbuf);
80 static int swapmode(int *retavail, int *retfree)
88 n = swapctl(SWAP_NSWAP, 0, 0);
91 (void) warn("could not get swap information");
95 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
98 (void) warn("memory allocation failed");
102 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
103 (void) warn("could not get swap stats");
107 *retavail += (int) dbtob(sep[n - 1].se_nblks);
109 (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
111 *retavail = (int) (*retavail / 1024);
112 *retfree = (int) (*retfree / 1024);
118 void prepare_update()
124 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
125 struct timeval boottime;
127 int size = sizeof(boottime);
129 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
130 && (boottime.tv_sec != 0)) {
132 info.uptime = now - boottime.tv_sec;
134 (void) warn("could not get uptime");
139 int check_mount(char *s)
145 void update_meminfo()
147 int mib[] = { CTL_VM, VM_UVMEXP2 };
148 int total_pages, inactive_pages, free_pages;
149 int swap_avail, swap_free;
150 const int pagesize = getpagesize();
151 struct uvmexp_sysctl uvmexp;
152 size_t size = sizeof(uvmexp);
154 info.memmax = info.mem = 0;
155 info.swapmax = info.swap = 0;
158 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
159 warn("could not get memory info");
163 total_pages = uvmexp.npages;
164 free_pages = uvmexp.free;
165 inactive_pages = uvmexp.inactive;
167 info.memmax = (total_pages * pagesize) >> 10;
169 ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
171 if (swapmode(&swap_avail, &swap_free) >= 0) {
172 info.swapmax = swap_avail;
173 info.swap = (swap_avail - swap_free);
177 void update_net_stats()
182 struct ifnet_head ifhead; /* interfaces are in a tail queue */
184 static struct nlist namelist[] = {
191 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
193 (void) warnx("cannot kvm_openfiles: %s", errbuf);
196 ("maybe you need to setgid kmem this program?");
198 } else if (kvm_nlist(nkd, namelist) != 0) {
199 (void) warn("cannot kvm_nlist");
205 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
206 sizeof(ifhead)) < 0) {
207 (void) warn("cannot kvm_read");
212 delta = current_update_time - last_update_time;
216 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
217 ifnet.if_list.tqe_next && i < 16;
218 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
221 long long last_recv, last_trans;
223 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
225 ns = get_net_stat(ifnet.if_xname);
227 last_recv = ns->recv;
228 last_trans = ns->trans;
230 if (ifnet.if_ibytes < ns->last_read_recv)
232 ((long long) 4294967295U -
233 ns->last_read_recv) + ifnet.if_ibytes;
235 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
237 ns->last_read_recv = ifnet.if_ibytes;
239 if (ifnet.if_obytes < ns->last_read_trans)
241 ((long long) 4294967295U -
242 ns->last_read_trans) + ifnet.if_obytes;
245 (ifnet.if_obytes - ns->last_read_trans);
247 ns->last_read_trans = ifnet.if_obytes;
249 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
250 ns->last_read_recv = ifnet.if_ibytes;
251 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
252 ns->last_read_trans = ifnet.if_obytes;
254 ns->recv_speed = (ns->recv - last_recv) / delta;
255 ns->trans_speed = (ns->trans - last_trans) / delta;
259 void update_total_processes()
261 /* It's easier to use kvm here than sysctl */
270 kvm_getproc2(kd, KERN_PROC_ALL, 0,
271 sizeof(struct kinfo_proc2), &n_processes);
273 info.procs = n_processes;
276 void update_running_processes()
278 struct kinfo_proc2 *p;
287 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
288 sizeof(struct kinfo_proc2), &n_processes);
289 for (i = 0; i < n_processes; i++)
290 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
291 p[i].p_stat == LSONPROC)
295 info.run_procs = cnt;
298 struct cpu_load_struct {
299 unsigned long load[5];
302 struct cpu_load_struct fresh = {
306 long cpu_used, oldtotal, oldused;
308 void update_cpu_usage()
311 static u_int64_t cp_time[CPUSTATES];
312 size_t len = sizeof(cp_time);
316 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
317 (void) warn("cannot get kern.cp_time");
320 fresh.load[0] = cp_time[CP_USER];
321 fresh.load[1] = cp_time[CP_NICE];
322 fresh.load[2] = cp_time[CP_SYS];
323 fresh.load[3] = cp_time[CP_IDLE];
324 fresh.load[4] = cp_time[CP_IDLE];
326 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
328 fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
330 if ((total - oldtotal) != 0)
332 ((double) (used - oldused)) / (double) (total -
342 double get_i2c_info(int *fd, int div, char *devtype)
347 void update_load_average()
352 info.loadavg[0] = (float) v[0];
353 info.loadavg[1] = (float) v[1];
354 info.loadavg[2] = (float) v[2];
357 double get_acpi_temperature(int fd)
362 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
367 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
373 int open_acpi_temperature(const char *name)
378 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
380 if ( !p_client_buffer || client_buffer_size <= 0 )
383 /* not implemented */
384 memset(p_client_buffer,0,client_buffer_size);
389 /*char *get_acpi_fan()*/
390 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
392 if ( !p_client_buffer || client_buffer_size <= 0 )
395 /* not implemented */
396 memset(p_client_buffer,0,client_buffer_size);
401 void update_entropy (void)