1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
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/>.
30 static kvm_t *kd = NULL;
31 int kd_init = 0, nkd_init = 0;
33 char errbuf[_POSIX2_LINE_MAX];
35 static int init_kvm(void)
41 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
43 warnx("cannot kvm_openfiles: %s", errbuf);
50 static int swapmode(int *retavail, int *retfree)
58 n = swapctl(SWAP_NSWAP, 0, 0);
61 warn("could not get swap information");
65 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
68 warn("memory allocation failed");
72 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
73 warn("could not get swap stats");
77 *retavail += (int) dbtob(sep[n - 1].se_nblks);
78 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
80 *retavail = (int) (*retavail / 1024);
81 *retfree = (int) (*retfree / 1024);
92 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
93 struct timeval boottime;
95 int size = sizeof(boottime);
97 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
98 && (boottime.tv_sec != 0)) {
100 info.uptime = now - boottime.tv_sec;
102 warn("could not get uptime");
107 int check_mount(char *s)
113 void update_meminfo()
115 int mib[] = { CTL_VM, VM_UVMEXP2 };
116 int total_pages, inactive_pages, free_pages;
117 int swap_avail, swap_free;
118 const int pagesize = getpagesize();
119 struct uvmexp_sysctl uvmexp;
120 size_t size = sizeof(uvmexp);
122 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
123 warn("could not get memory info");
127 total_pages = uvmexp.npages;
128 free_pages = uvmexp.free;
129 inactive_pages = uvmexp.inactive;
131 info.memmax = (total_pages * pagesize) >> 10;
132 info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
133 info.memeasyfree = info.memfree = info.memmax - info.mem;
135 if (swapmode(&swap_avail, &swap_free) >= 0) {
136 info.swapmax = swap_avail;
137 info.swap = (swap_avail - swap_free);
141 void update_net_stats()
146 struct ifnet_head ifhead; /* interfaces are in a tail queue */
148 static struct nlist namelist[] = {
155 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
157 warnx("cannot kvm_openfiles: %s", errbuf);
158 warnx("maybe you need to setgid kmem this program?");
160 } else if (kvm_nlist(nkd, namelist) != 0) {
161 warn("cannot kvm_nlist");
168 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
169 sizeof(ifhead)) < 0) {
170 warn("cannot kvm_read");
175 delta = current_update_time - last_update_time;
176 if (delta <= 0.0001 && total_updates != 0) {
180 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
181 ifnet.if_list.tqe_next && i < 16;
182 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
185 long long last_recv, last_trans;
187 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
188 ns = get_net_stat(ifnet.if_xname);
190 last_recv = ns->recv;
191 last_trans = ns->trans;
193 if (ifnet.if_ibytes < ns->last_read_recv) {
194 ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
197 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
200 ns->last_read_recv = ifnet.if_ibytes;
202 if (ifnet.if_obytes < ns->last_read_trans) {
203 ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
206 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
209 ns->last_read_trans = ifnet.if_obytes;
211 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
212 ns->last_read_recv = ifnet.if_ibytes;
213 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
214 ns->last_read_trans = ifnet.if_obytes;
216 ns->recv_speed = (ns->recv - last_recv) / delta;
217 ns->trans_speed = (ns->trans - last_trans) / delta;
221 void update_total_processes()
223 /* It's easier to use kvm here than sysctl */
229 if (init_kvm() < 0) {
232 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
236 info.procs = n_processes;
239 void update_running_processes()
241 struct kinfo_proc2 *p;
247 if (init_kvm() < 0) {
250 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
252 for (i = 0; i < n_processes; i++) {
253 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
254 || p[i].p_stat == LSONPROC) {
260 info.run_procs = cnt;
263 struct cpu_load_struct {
264 unsigned long load[5];
267 struct cpu_load_struct fresh = {
271 long cpu_used, oldtotal, oldused;
273 void update_cpu_usage()
276 static u_int64_t cp_time[CPUSTATES];
277 size_t len = sizeof(cp_time);
281 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
282 warn("cannot get kern.cp_time");
285 fresh.load[0] = cp_time[CP_USER];
286 fresh.load[1] = cp_time[CP_NICE];
287 fresh.load[2] = cp_time[CP_SYS];
288 fresh.load[3] = cp_time[CP_IDLE];
289 fresh.load[4] = cp_time[CP_IDLE];
291 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
292 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
294 if ((total - oldtotal) != 0) {
295 info.cpu_usage = ((double) (used - oldused)) /
296 (double) (total - oldtotal);
305 void update_load_average()
311 info.loadavg[0] = (float) v[0];
312 info.loadavg[1] = (float) v[1];
313 info.loadavg[2] = (float) v[2];
316 double get_acpi_temperature(int fd)
321 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
325 int open_acpi_temperature(const char *name)
330 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
332 if (!p_client_buffer || client_buffer_size <= 0) {
336 /* not implemented */
337 memset(p_client_buffer, 0, client_buffer_size);
340 /* char *get_acpi_fan() */
341 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
343 if (!p_client_buffer || client_buffer_size <= 0) {
347 /* not implemented */
348 memset(p_client_buffer, 0, client_buffer_size);
351 void update_entropy(void)