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-2009 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/>.
33 static kvm_t *kd = NULL;
34 int kd_init = 0, nkd_init = 0;
36 char errbuf[_POSIX2_LINE_MAX];
38 static int init_kvm(void)
44 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
46 warnx("cannot kvm_openfiles: %s", errbuf);
53 static int swapmode(int *retavail, int *retfree)
61 n = swapctl(SWAP_NSWAP, 0, 0);
64 warn("could not get swap information");
68 sep = (struct swapent *) malloc(n * (sizeof(*sep)));
71 warn("memory allocation failed");
75 if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
76 warn("could not get swap stats");
80 *retavail += (int) dbtob(sep[n - 1].se_nblks);
81 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
83 *retavail = (int) (*retavail / 1024);
84 *retfree = (int) (*retfree / 1024);
95 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
96 struct timeval boottime;
98 int size = sizeof(boottime);
100 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
101 && (boottime.tv_sec != 0)) {
103 info.uptime = now - boottime.tv_sec;
105 warn("could not get uptime");
110 int check_mount(char *s)
116 void update_meminfo()
118 int mib[] = { CTL_VM, VM_UVMEXP2 };
119 int total_pages, inactive_pages, free_pages;
120 int swap_avail, swap_free;
121 const int pagesize = getpagesize();
122 struct uvmexp_sysctl uvmexp;
123 size_t size = sizeof(uvmexp);
125 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
126 warn("could not get memory info");
130 total_pages = uvmexp.npages;
131 free_pages = uvmexp.free;
132 inactive_pages = uvmexp.inactive;
134 info.memmax = (total_pages * pagesize) >> 10;
135 info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
136 info.memeasyfree = info.memfree = info.memmax - info.mem;
138 if (swapmode(&swap_avail, &swap_free) >= 0) {
139 info.swapmax = swap_avail;
140 info.swap = (swap_avail - swap_free);
141 info.swapfree = swap_free;
145 void update_net_stats()
150 struct ifnet_head ifhead; /* interfaces are in a tail queue */
152 static struct nlist namelist[] = {
159 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
161 warnx("cannot kvm_openfiles: %s", errbuf);
162 warnx("maybe you need to setgid kmem this program?");
164 } else if (kvm_nlist(nkd, namelist) != 0) {
165 warn("cannot kvm_nlist");
172 if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
173 sizeof(ifhead)) < 0) {
174 warn("cannot kvm_read");
179 delta = current_update_time - last_update_time;
180 if (delta <= 0.0001) {
184 for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
185 ifnet.if_list.tqe_next && i < 16;
186 ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
189 long long last_recv, last_trans;
191 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
192 ns = get_net_stat(ifnet.if_xname, NULL, NULL);
194 last_recv = ns->recv;
195 last_trans = ns->trans;
197 if (ifnet.if_ibytes < ns->last_read_recv) {
198 ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
201 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
204 ns->last_read_recv = ifnet.if_ibytes;
206 if (ifnet.if_obytes < ns->last_read_trans) {
207 ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
210 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
213 ns->last_read_trans = ifnet.if_obytes;
215 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
216 ns->last_read_recv = ifnet.if_ibytes;
217 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
218 ns->last_read_trans = ifnet.if_obytes;
220 ns->recv_speed = (ns->recv - last_recv) / delta;
221 ns->trans_speed = (ns->trans - last_trans) / delta;
225 void update_total_processes()
227 /* It's easier to use kvm here than sysctl */
233 if (init_kvm() < 0) {
236 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
240 info.procs = n_processes;
243 void update_running_processes()
245 struct kinfo_proc2 *p;
251 if (init_kvm() < 0) {
254 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
256 for (i = 0; i < n_processes; i++) {
257 if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
258 || 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 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];
296 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
298 if ((total - oldtotal) != 0) {
299 info.cpu_usage = ((double) (used - oldused)) /
300 (double) (total - oldtotal);
309 void update_load_average()
315 info.loadavg[0] = (float) v[0];
316 info.loadavg[1] = (float) v[1];
317 info.loadavg[2] = (float) v[2];
320 double get_acpi_temperature(int fd)
325 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
329 int open_acpi_temperature(const char *name)
334 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
336 if (!p_client_buffer || client_buffer_size <= 0) {
340 /* not implemented */
341 memset(p_client_buffer, 0, client_buffer_size);
344 /* char *get_acpi_fan() */
345 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
347 if (!p_client_buffer || client_buffer_size <= 0) {
351 /* not implemented */
352 memset(p_client_buffer, 0, client_buffer_size);
355 void update_entropy(void)