1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 static kvm_t *kd = NULL;
35 int kd_init = 0, nkd_init = 0;
37 char errbuf[_POSIX2_LINE_MAX];
39 static int init_kvm(void)
45 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
47 warnx("cannot kvm_openfiles: %s", errbuf);
54 static int swapmode(int *retavail, int *retfree)
62 n = swapctl(SWAP_NSWAP, 0, 0);
65 warn("could not get swap information");
69 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
72 warn("memory allocation failed");
76 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
77 warn("could not get swap stats");
81 *retavail += (int) dbtob(sep[n - 1].se_nblks);
82 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
84 *retavail = (int) (*retavail / 1024);
85 *retfree = (int) (*retfree / 1024);
96 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
97 struct timeval boottime;
99 int size = sizeof(boottime);
101 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
102 && (boottime.tv_sec != 0)) {
104 info.uptime = now - boottime.tv_sec;
106 warn("could not get uptime");
111 int check_mount(char *s)
117 void update_meminfo()
119 int mib[] = { CTL_VM, VM_UVMEXP2 };
120 int total_pages, inactive_pages, free_pages;
121 int swap_avail, swap_free;
122 const int pagesize = getpagesize();
123 struct uvmexp_sysctl uvmexp;
124 size_t size = sizeof(uvmexp);
126 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
127 warn("could not get memory info");
131 total_pages = uvmexp.npages;
132 free_pages = uvmexp.free;
133 inactive_pages = uvmexp.inactive;
135 info.memmax = (total_pages * pagesize) >> 10;
136 info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
137 info.memeasyfree = info.memfree = info.memmax - info.mem;
139 if (swapmode(&swap_avail, &swap_free) >= 0) {
140 info.swapmax = swap_avail;
141 info.swap = (swap_avail - swap_free);
142 info.swapfree = 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 warnx("cannot kvm_openfiles: %s", errbuf);
163 warnx("maybe you need to setgid kmem this program?");
165 } else if (kvm_nlist(nkd, namelist) != 0) {
166 warn("cannot kvm_nlist");
173 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
174 sizeof(ifhead)) < 0) {
175 warn("cannot kvm_read");
180 delta = current_update_time - last_update_time;
181 if (delta <= 0.0001) {
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 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
193 ns = get_net_stat(ifnet.if_xname, NULL, NULL);
195 last_recv = ns->recv;
196 last_trans = ns->trans;
198 if (ifnet.if_ibytes < ns->last_read_recv) {
199 ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
202 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
205 ns->last_read_recv = ifnet.if_ibytes;
207 if (ifnet.if_obytes < ns->last_read_trans) {
208 ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
211 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
214 ns->last_read_trans = ifnet.if_obytes;
216 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
217 ns->last_read_recv = ifnet.if_ibytes;
218 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
219 ns->last_read_trans = ifnet.if_obytes;
221 ns->recv_speed = (ns->recv - last_recv) / delta;
222 ns->trans_speed = (ns->trans - last_trans) / delta;
226 void update_total_processes()
228 /* It's easier to use kvm here than sysctl */
234 if (init_kvm() < 0) {
237 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
241 info.procs = n_processes;
244 void update_running_processes()
246 struct kinfo_proc2 *p;
252 if (init_kvm() < 0) {
255 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
257 for (i = 0; i < n_processes; i++) {
258 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
259 || 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 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];
297 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
299 if ((total - oldtotal) != 0) {
300 info.cpu_usage = ((double) (used - oldused)) /
301 (double) (total - oldtotal);
310 void update_load_average()
316 info.loadavg[0] = (float) v[0];
317 info.loadavg[1] = (float) v[1];
318 info.loadavg[2] = (float) v[2];
321 double get_acpi_temperature(int fd)
326 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
330 int open_acpi_temperature(const char *name)
335 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
337 (void) adapter; // only linux uses this
339 if (!p_client_buffer || client_buffer_size <= 0) {
343 /* not implemented */
344 memset(p_client_buffer, 0, client_buffer_size);
347 /* char *get_acpi_fan() */
348 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
350 if (!p_client_buffer || client_buffer_size <= 0) {
354 /* not implemented */
355 memset(p_client_buffer, 0, client_buffer_size);
358 int get_entropy_avail(unsigned int *val)
363 int get_entropy_poolsize(unsigned int *val)