2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2005-08-24 05:33:10 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2018-05-12 16:03:00 +00:00
|
|
|
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2005-08-24 05:33:10 +00:00
|
|
|
|
2012-07-05 09:27:45 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-03-16 13:33:15 +00:00
|
|
|
#include <sys/dkstat.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <sys/ioctl.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resource.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
#include <sys/socket.h>
|
2009-03-28 19:53:02 +00:00
|
|
|
#include <sys/stat.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <sys/sysctl.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <sys/user.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <net/if.h>
|
2006-10-18 16:02:28 +00:00
|
|
|
#include <net/if_media.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <net/if_mib.h>
|
2006-10-18 16:02:28 +00:00
|
|
|
#include <net/if_var.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
|
2005-08-27 11:56:22 +00:00
|
|
|
#include <devstat.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-03-28 19:53:02 +00:00
|
|
|
#include <dev/acpica/acpiio.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <dev/wi/if_wavelan_ieee.h>
|
2006-10-18 16:02:28 +00:00
|
|
|
|
2010-12-09 18:22:34 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2005-08-24 05:33:10 +00:00
|
|
|
#include "conky.h"
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "diskio.h"
|
2009-03-28 19:53:02 +00:00
|
|
|
#include "freebsd.h"
|
|
|
|
#include "logging.h"
|
2009-10-17 14:43:12 +00:00
|
|
|
#include "net_stat.h"
|
2009-11-16 22:30:53 +00:00
|
|
|
#include "text_object.h"
|
2009-03-28 19:53:02 +00:00
|
|
|
#include "top.h"
|
2005-08-24 05:33:10 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
|
|
|
|
#define KELVTOC(x) ((x - 2732) / 10.0)
|
|
|
|
#define MAXSHOWDEVS 16
|
2005-08-27 11:56:22 +00:00
|
|
|
|
2006-03-18 16:04:36 +00:00
|
|
|
#if 0
|
2018-05-12 16:03:00 +00:00
|
|
|
#define FREEBSD_DEBUG
|
2006-03-18 16:04:36 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-14 21:58:02 +00:00
|
|
|
kvm_t *kd;
|
2010-12-09 18:22:34 +00:00
|
|
|
std::mutex kvm_proc_mutex;
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
__attribute__((gnu_inline)) inline void proc_find_top(struct process **cpu,
|
|
|
|
struct process **mem,
|
|
|
|
struct process **time);
|
2005-08-29 19:39:35 +00:00
|
|
|
|
2005-08-27 12:59:46 +00:00
|
|
|
static short cpu_setup = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static int getsysctl(const char *name, void *ptr, size_t len) {
|
|
|
|
size_t nlen = len;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2018-05-13 13:58:03 +00:00
|
|
|
if (sysctlbyname(name, ptr, &nlen, nullptr, 0) == -1) { return -1; }
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-13 13:58:03 +00:00
|
|
|
if (nlen != len && errno == ENOMEM) { return -1; }
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
struct ifmibdata *data = nullptr;
|
2005-07-20 00:30:40 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static int swapmode(unsigned long *retavail, unsigned long *retfree) {
|
|
|
|
int n;
|
|
|
|
unsigned long pagesize = getpagesize();
|
|
|
|
struct kvm_swap swapary[1];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
*retavail = 0;
|
|
|
|
*retfree = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
#define CONVERT(v) ((quad_t)(v) * (pagesize / 1024))
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
n = kvm_getswapinfo(kd, swapary, 1, 0);
|
2018-05-13 13:58:03 +00:00
|
|
|
if (n < 0 || swapary[0].ksw_total == 0) { return 0; }
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
*retavail = CONVERT(swapary[0].ksw_total);
|
|
|
|
*retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
n = (int)((double)swapary[0].ksw_used * 100.0 / (double)swapary[0].ksw_total);
|
2010-05-23 17:03:01 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return n;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void prepare_update(void) {}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_uptime(void) {
|
|
|
|
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
|
|
|
|
struct timeval boottime;
|
|
|
|
time_t now;
|
|
|
|
size_t size = sizeof(boottime);
|
2009-11-16 22:30:53 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((sysctl(mib, 2, &boottime, &size, nullptr, 0) != -1) &&
|
2018-05-12 16:03:00 +00:00
|
|
|
(boottime.tv_sec != 0)) {
|
|
|
|
time(&now);
|
|
|
|
info.uptime = now - boottime.tv_sec;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Could not get uptime\n");
|
|
|
|
info.uptime = 0;
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int check_mount(struct text_object *obj) {
|
|
|
|
struct statfs *mntbuf;
|
|
|
|
int i, mntsize;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!obj->data.s) return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
|
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
2018-05-13 13:58:03 +00:00
|
|
|
if (strcmp(mntbuf[i].f_mntonname, obj->data.s) == 0) { return 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2010-05-23 17:03:01 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_meminfo(void) {
|
|
|
|
u_int total_pages, inactive_pages, free_pages;
|
|
|
|
unsigned long swap_avail, swap_free;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int pagesize = getpagesize();
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
|
|
|
|
}
|
2006-03-25 15:30:59 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
info.memmax = total_pages * (pagesize >> 10);
|
|
|
|
info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
|
|
|
|
info.memwithbuffers = info.mem;
|
|
|
|
info.memeasyfree = info.memfree = info.memmax - info.mem;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if ((swapmode(&swap_avail, &swap_free)) >= 0) {
|
|
|
|
info.swapmax = swap_avail;
|
|
|
|
info.swap = (swap_avail - swap_free);
|
|
|
|
info.swapfree = swap_free;
|
|
|
|
} else {
|
|
|
|
info.swapmax = 0;
|
|
|
|
info.swap = 0;
|
|
|
|
info.swapfree = 0;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_net_stats(void) {
|
|
|
|
struct net_stat *ns;
|
|
|
|
double delta;
|
|
|
|
long long r, t, last_recv, last_trans;
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct if_data *ifd;
|
|
|
|
|
|
|
|
/* get delta */
|
|
|
|
delta = current_update_time - last_update_time;
|
2018-05-13 13:58:03 +00:00
|
|
|
if (delta <= 0.0001) { return 0; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-13 13:58:03 +00:00
|
|
|
if (getifaddrs(&ifap) < 0) { return 0; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
2018-05-12 23:26:31 +00:00
|
|
|
ns = get_net_stat((const char *)ifa->ifa_name, nullptr, NULL);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
if (ifa->ifa_flags & IFF_UP) {
|
|
|
|
struct ifaddrs *iftmp;
|
|
|
|
|
|
|
|
ns->up = 1;
|
|
|
|
last_recv = ns->recv;
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
2018-05-13 13:58:03 +00:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK) { continue; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
for (iftmp = ifa->ifa_next;
|
2018-05-12 23:26:31 +00:00
|
|
|
iftmp != nullptr && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
|
2018-05-12 16:03:00 +00:00
|
|
|
iftmp = iftmp->ifa_next) {
|
|
|
|
if (iftmp->ifa_addr->sa_family == AF_INET) {
|
|
|
|
memcpy(&(ns->addr), iftmp->ifa_addr, iftmp->ifa_addr->sa_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ifd = (struct if_data *)ifa->ifa_data;
|
|
|
|
r = ifd->ifi_ibytes;
|
|
|
|
t = ifd->ifi_obytes;
|
|
|
|
|
|
|
|
if (r < ns->last_read_recv) {
|
|
|
|
ns->recv += ((long long)4294967295U - ns->last_read_recv) + r;
|
|
|
|
} else {
|
|
|
|
ns->recv += (r - ns->last_read_recv);
|
|
|
|
}
|
|
|
|
|
|
|
|
ns->last_read_recv = r;
|
|
|
|
|
|
|
|
if (t < ns->last_read_trans) {
|
|
|
|
ns->trans += ((long long)4294967295U - ns->last_read_trans) + t;
|
|
|
|
} else {
|
|
|
|
ns->trans += (t - ns->last_read_trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
ns->last_read_trans = t;
|
|
|
|
|
|
|
|
/* calculate speeds */
|
|
|
|
ns->recv_speed = (ns->recv - last_recv) / delta;
|
|
|
|
ns->trans_speed = (ns->trans - last_trans) / delta;
|
|
|
|
} else {
|
|
|
|
ns->up = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_total_processes(void) {
|
|
|
|
int n_processes;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
std::lock_guard<std::mutex> guard(kvm_proc_mutex);
|
|
|
|
kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
info.procs = n_processes;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_running_processes(void) {
|
|
|
|
struct kinfo_proc *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(kvm_proc_mutex);
|
|
|
|
p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2018-05-13 13:58:03 +00:00
|
|
|
if (p[i].ki_stat == SRUN) { cnt++; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.run_procs = cnt;
|
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_cpu_count(void) {
|
|
|
|
int cpu_count = 0;
|
|
|
|
size_t cpu_count_len = sizeof(cpu_count);
|
|
|
|
|
|
|
|
if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
|
|
|
|
info.cpu_count = cpu_count;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Cannot get hw.ncpu\n");
|
|
|
|
info.cpu_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.cpu_usage = (float *)malloc((info.cpu_count + 1) * sizeof(float));
|
2018-05-13 13:58:03 +00:00
|
|
|
if (info.cpu_usage == nullptr) { CRIT_ERR(nullptr, NULL, "malloc"); }
|
2005-08-27 12:59:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 18:53:19 +00:00
|
|
|
struct cpu_info {
|
2018-05-12 16:03:00 +00:00
|
|
|
long oldtotal;
|
|
|
|
long oldused;
|
2010-01-16 18:53:19 +00:00
|
|
|
};
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_cpu_usage(void) {
|
|
|
|
int i, j = 0;
|
|
|
|
long used, total;
|
2018-05-12 23:26:31 +00:00
|
|
|
long *cp_time = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
size_t cp_len;
|
2018-05-12 23:26:31 +00:00
|
|
|
static struct cpu_info *cpu = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
unsigned int malloc_cpu_size = 0;
|
|
|
|
extern void *global_cpu;
|
|
|
|
|
|
|
|
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
|
|
|
|
if ((cpu_setup == 0) || (!info.cpu_usage)) {
|
|
|
|
get_cpu_count();
|
|
|
|
cpu_setup = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!global_cpu) {
|
|
|
|
malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
|
|
|
|
cpu = (cpu_info *)malloc(malloc_cpu_size);
|
|
|
|
memset(cpu, 0, malloc_cpu_size);
|
|
|
|
global_cpu = cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cpu[0] is overall stats, get it from separate sysctl */
|
|
|
|
cp_len = CPUSTATES * sizeof(long);
|
|
|
|
cp_time = (long int *)malloc(cp_len);
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (sysctlbyname("kern.cp_time", cp_time, &cp_len, nullptr, 0) < 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
fprintf(stderr, "Cannot get kern.cp_time\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
for (j = 0; j < CPUSTATES; j++) total += cp_time[j];
|
|
|
|
|
|
|
|
used = total - cp_time[CP_IDLE];
|
|
|
|
|
|
|
|
if ((total - cpu[0].oldtotal) != 0) {
|
|
|
|
info.cpu_usage[0] =
|
|
|
|
((double)(used - cpu[0].oldused)) / (double)(total - cpu[0].oldtotal);
|
|
|
|
} else {
|
|
|
|
info.cpu_usage[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu[0].oldused = used;
|
|
|
|
cpu[0].oldtotal = total;
|
|
|
|
|
|
|
|
free(cp_time);
|
|
|
|
|
|
|
|
/* per-core stats */
|
|
|
|
cp_len = CPUSTATES * sizeof(long) * info.cpu_count;
|
|
|
|
cp_time = (long int *)malloc(cp_len);
|
|
|
|
|
|
|
|
/* on e.g. i386 SMP we may have more values than actual cpus; this will just
|
|
|
|
* drop extra values */
|
2018-05-12 23:26:31 +00:00
|
|
|
if (sysctlbyname("kern.cp_times", cp_time, &cp_len, nullptr, 0) < 0 &&
|
2018-05-12 16:03:00 +00:00
|
|
|
errno != ENOMEM) {
|
|
|
|
fprintf(stderr, "Cannot get kern.cp_times\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < info.cpu_count; i++) {
|
|
|
|
total = 0;
|
|
|
|
for (j = 0; j < CPUSTATES; j++) total += cp_time[i * CPUSTATES + j];
|
|
|
|
|
|
|
|
used = total - cp_time[i * CPUSTATES + CP_IDLE];
|
|
|
|
|
|
|
|
if ((total - cpu[i + 1].oldtotal) != 0) {
|
|
|
|
info.cpu_usage[i + 1] = ((double)(used - cpu[i + 1].oldused)) /
|
|
|
|
(double)(total - cpu[i + 1].oldtotal);
|
|
|
|
} else {
|
|
|
|
info.cpu_usage[i + 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu[i + 1].oldused = used;
|
|
|
|
cpu[i + 1].oldtotal = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cp_time);
|
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:54:01 +00:00
|
|
|
void free_cpu(struct text_object *) { /* no-op */ }
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_load_average(void) {
|
|
|
|
double v[3];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
getloadavg(v, 3);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
info.loadavg[0] = (double)v[0];
|
|
|
|
info.loadavg[1] = (double)v[1];
|
|
|
|
info.loadavg[2] = (double)v[2];
|
2010-05-23 17:03:01 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double get_acpi_temperature(int fd) {
|
|
|
|
int temp;
|
|
|
|
(void)fd;
|
|
|
|
|
|
|
|
if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp) == 0) {
|
|
|
|
return KELVTOC(temp);
|
|
|
|
} else if (GETSYSCTL("dev.cpu.0.temperature", temp) == 0) {
|
|
|
|
return KELVTOC(temp);
|
|
|
|
} else if (GETSYSCTL("dev.amdtemp.0.core0.sensor0", temp) == 0) {
|
|
|
|
return KELVTOC(temp);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Cannot get temperature from sysctl\n");
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0.0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void get_battery_stats(int *battime, int *batcapacity, int *batstate,
|
|
|
|
int *ac) {
|
|
|
|
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
|
|
|
|
}
|
|
|
|
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
|
|
|
|
}
|
|
|
|
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
|
|
|
|
}
|
|
|
|
if (ac && GETSYSCTL("hw.acpi.acline", *ac)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
}
|
2006-03-26 01:47:55 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item) {
|
|
|
|
int battime, batcapacity, batstate, ac;
|
|
|
|
(void)bat;
|
|
|
|
|
|
|
|
get_battery_stats(&battime, &batcapacity, &batstate, &ac);
|
|
|
|
|
|
|
|
if (batstate != 1 && batstate != 2 && batstate != 0 && batstate != 7)
|
|
|
|
fprintf(stderr, "Unknown battery state %d!\n", batstate);
|
|
|
|
else if (batstate != 1 && ac == 0)
|
|
|
|
fprintf(stderr, "Battery charging while not on AC!\n");
|
|
|
|
else if (batstate == 1 && ac == 1)
|
|
|
|
fprintf(stderr, "Battery discharing while on AC!\n");
|
|
|
|
|
|
|
|
switch (item) {
|
|
|
|
case BATTERY_TIME:
|
|
|
|
if (batstate == 1 && battime != -1)
|
|
|
|
snprintf(buf, n, "%d:%2.2d", battime / 60, battime % 60);
|
|
|
|
break;
|
|
|
|
case BATTERY_STATUS:
|
|
|
|
if (batstate == 1) // Discharging
|
|
|
|
snprintf(buf, n, "remaining (%d%%)", batcapacity);
|
|
|
|
else
|
|
|
|
snprintf(buf, n,
|
|
|
|
batstate == 2
|
|
|
|
? "charging (%d%%)"
|
|
|
|
: (batstate == 7 ? "absent/on AC" : "charged (%d%%)"),
|
|
|
|
batcapacity);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown requested battery stat %d\n", item);
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static int check_bat(const char *bat) {
|
|
|
|
int batnum, numbatts;
|
|
|
|
char *endptr;
|
|
|
|
if (GETSYSCTL("hw.acpi.battery.units", numbatts)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.units\"\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (numbatts <= 0) {
|
|
|
|
fprintf(stderr, "No battery unit detected\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!bat || (batnum = strtol(bat, &endptr, 10)) < 0 || bat == endptr ||
|
|
|
|
batnum > numbatts) {
|
|
|
|
fprintf(stderr, "Wrong battery unit %s requested\n", bat ? bat : "");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return batnum;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int get_battery_perct(const char *bat) {
|
|
|
|
int batcapacity;
|
2009-03-28 19:53:02 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
get_battery_stats(nullptr, &batcapacity, NULL, NULL);
|
2018-05-12 16:03:00 +00:00
|
|
|
return batcapacity;
|
2007-05-05 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double get_battery_perct_bar(struct text_object *obj) {
|
|
|
|
int batperct = get_battery_perct(obj->data.s);
|
|
|
|
return batperct;
|
2007-05-05 16:46:12 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int open_acpi_temperature(const char *name) {
|
|
|
|
(void)name;
|
|
|
|
/* Not applicable for FreeBSD. */
|
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size,
|
|
|
|
const char *adapter) {
|
|
|
|
int state;
|
2005-11-12 21:42:00 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
(void)adapter; // only linux uses this
|
2010-01-24 14:34:03 +00:00
|
|
|
|
2018-05-13 13:58:03 +00:00
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) { return; }
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (GETSYSCTL("hw.acpi.acline", state)) {
|
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (state) {
|
|
|
|
strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
|
|
|
|
} else {
|
|
|
|
strncpy(p_client_buffer, "Running on battery", client_buffer_size);
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) {
|
|
|
|
/* not implemented */
|
|
|
|
if (p_client_buffer && client_buffer_size > 0) {
|
|
|
|
memset(p_client_buffer, 0, client_buffer_size);
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* void */
|
2018-05-12 16:03:00 +00:00
|
|
|
char get_freq(char *p_client_buffer, size_t client_buffer_size,
|
|
|
|
const char *p_format, int divisor, unsigned int cpu) {
|
|
|
|
int freq;
|
|
|
|
char *freq_sysctl;
|
|
|
|
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0 || !p_format ||
|
|
|
|
divisor <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
freq_sysctl = (char *)calloc(16, sizeof(char));
|
2018-05-13 13:58:03 +00:00
|
|
|
if (freq_sysctl == nullptr) { exit(-1); }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
|
|
|
|
|
|
|
|
if (GETSYSCTL(freq_sysctl, freq) == 0) {
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
(float)freq / divisor);
|
|
|
|
} else {
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(freq_sysctl);
|
|
|
|
return 1;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2005-08-23 06:10:25 +00:00
|
|
|
|
2007-08-06 16:42:48 +00:00
|
|
|
#if 0
|
2009-06-21 21:44:48 +00:00
|
|
|
void update_wifi_stats(void)
|
2005-08-23 06:10:25 +00:00
|
|
|
{
|
2006-10-18 16:02:28 +00:00
|
|
|
struct ifreq ifr; /* interface stats */
|
|
|
|
struct wi_req wireq;
|
2008-02-20 20:30:45 +00:00
|
|
|
struct net_stat *ns;
|
2006-10-18 16:02:28 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
int s;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Get iface table */
|
|
|
|
if (getifaddrs(&ifap) < 0) {
|
2006-10-18 16:02:28 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-10-18 16:02:28 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
2018-05-12 23:26:31 +00:00
|
|
|
ns = get_net_stat((const char *) ifa->ifa_name, nullptr, NULL);
|
2006-10-18 16:02:28 +00:00
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
|
|
|
|
/* Get media type */
|
|
|
|
bzero(&ifmr, sizeof(ifmr));
|
2006-10-19 18:52:14 +00:00
|
|
|
strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
|
2008-04-10 22:45:45 +00:00
|
|
|
close(s);
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We can monitor only wireless interfaces
|
|
|
|
* which are not in hostap mode */
|
|
|
|
if ((ifmr.ifm_active & IFM_IEEE80211)
|
|
|
|
&& !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
|
2006-10-18 16:02:28 +00:00
|
|
|
/* Get wi status */
|
|
|
|
bzero(&ifr, sizeof(ifr));
|
2006-10-19 18:52:14 +00:00
|
|
|
strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
|
2008-02-20 20:30:45 +00:00
|
|
|
wireq.wi_type = WI_RID_COMMS_QUALITY;
|
|
|
|
wireq.wi_len = WI_MAX_DATALEN;
|
|
|
|
ifr.ifr_data = (void *) &wireq;
|
2006-10-18 16:02:28 +00:00
|
|
|
|
|
|
|
if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
|
|
|
|
perror("ioctl (getting wi status)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* wi_val[0] = quality
|
2006-10-18 16:02:28 +00:00
|
|
|
* wi_val[1] = signal
|
2008-02-20 20:30:45 +00:00
|
|
|
* wi_val[2] = noise */
|
2006-10-18 16:02:28 +00:00
|
|
|
ns->linkstatus = (int) wireq.wi_val[1];
|
|
|
|
}
|
2006-11-12 14:17:31 +00:00
|
|
|
cleanup:
|
2006-10-18 16:02:28 +00:00
|
|
|
close(s);
|
|
|
|
}
|
2005-08-23 06:10:25 +00:00
|
|
|
}
|
2007-08-06 16:42:48 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int update_diskio(void) {
|
|
|
|
int devs_count, num_selected, num_selections, dn;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct device_selection *dev_select = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
long select_generation;
|
|
|
|
static struct statinfo statinfo_cur;
|
|
|
|
char device_name[text_buffer_size.get(*state)];
|
|
|
|
struct diskio_stat *cur;
|
|
|
|
unsigned int reads, writes;
|
|
|
|
unsigned int total_reads = 0, total_writes = 0;
|
|
|
|
|
|
|
|
memset(&statinfo_cur, 0, sizeof(statinfo_cur));
|
|
|
|
statinfo_cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
|
|
|
|
stats.current = stats.current_read = stats.current_write = 0;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (devstat_getdevs(nullptr, &statinfo_cur) < 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
free(statinfo_cur.dinfo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
devs_count = statinfo_cur.dinfo->numdevs;
|
|
|
|
if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
|
|
|
|
&select_generation, statinfo_cur.dinfo->generation,
|
2018-05-13 13:58:03 +00:00
|
|
|
statinfo_cur.dinfo->devices, devs_count, nullptr, 0,
|
|
|
|
NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
for (dn = 0; dn < devs_count; dn++) {
|
|
|
|
int di;
|
|
|
|
struct devstat *dev;
|
|
|
|
|
|
|
|
di = dev_select[dn].position;
|
|
|
|
dev = &statinfo_cur.dinfo->devices[di];
|
|
|
|
snprintf(device_name, text_buffer_size.get(*state), "%s%d",
|
|
|
|
dev_select[dn].device_name, dev_select[dn].unit_number);
|
|
|
|
|
|
|
|
total_reads += (reads = dev->bytes[DEVSTAT_READ] / 512);
|
|
|
|
total_writes += (writes = dev->bytes[DEVSTAT_WRITE] / 512);
|
|
|
|
for (cur = stats.next; cur; cur = cur->next) {
|
|
|
|
if (cur->dev && !strcmp(device_name, cur->dev)) {
|
|
|
|
update_diskio_values(cur, reads, writes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_diskio_values(&stats, total_reads, total_writes);
|
|
|
|
|
|
|
|
free(dev_select);
|
|
|
|
}
|
|
|
|
|
2018-08-07 15:54:01 +00:00
|
|
|
if (statinfo_cur.dinfo->mem_ptr) { free(statinfo_cur.dinfo->mem_ptr); }
|
2018-05-12 16:03:00 +00:00
|
|
|
free(statinfo_cur.dinfo);
|
|
|
|
return 0;
|
2005-08-27 11:56:22 +00:00
|
|
|
}
|
2005-08-29 19:39:35 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* While topless is obviously better, top is also not bad. */
|
2005-08-29 19:39:35 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_top_info(void) {
|
|
|
|
struct kinfo_proc *p;
|
|
|
|
struct process *proc;
|
|
|
|
int n_processes;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(kvm_proc_mutex);
|
|
|
|
p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
|
|
|
|
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
proc = get_process(p[i].ki_pid);
|
|
|
|
|
|
|
|
proc->time_stamp = g_time;
|
|
|
|
proc->name = strndup(p[i].ki_comm, text_buffer_size.get(*state));
|
|
|
|
proc->basename = strndup(p[i].ki_comm, text_buffer_size.get(*state));
|
|
|
|
proc->amount = 100.0 * p[i].ki_pctcpu / FSCALE;
|
|
|
|
proc->vsize = p[i].ki_size;
|
|
|
|
proc->rss = (p[i].ki_rssize * getpagesize());
|
|
|
|
/* ki_runtime is in microseconds, total_cpu_time in centiseconds.
|
|
|
|
* Therefore we divide by 10000. */
|
|
|
|
proc->total_cpu_time = p[i].ki_runtime / 10000;
|
|
|
|
}
|
|
|
|
}
|
2005-08-29 19:39:35 +00:00
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void get_battery_short_status(char *buffer, unsigned int n, const char *bat) {
|
|
|
|
get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
|
|
|
|
if (0 == strncmp("charging", buffer, 8)) {
|
|
|
|
buffer[0] = 'C';
|
|
|
|
memmove(buffer + 1, buffer + 8, n - 8);
|
|
|
|
} else if (0 == strncmp("remaining", buffer, 9)) {
|
|
|
|
buffer[0] = 'D';
|
|
|
|
memmove(buffer + 1, buffer + 9, n - 9);
|
|
|
|
} else if (0 == strncmp("charged", buffer, 7)) {
|
|
|
|
buffer[0] = 'F';
|
|
|
|
memmove(buffer + 1, buffer + 7, n - 7);
|
|
|
|
} else if (0 == strncmp("absent/on AC", buffer, 12)) {
|
|
|
|
buffer[0] = 'N';
|
|
|
|
memmove(buffer + 1, buffer + 12, n - 12);
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int get_entropy_avail(unsigned int *val) {
|
|
|
|
/* Not applicable for FreeBSD as it uses the yarrow prng. */
|
|
|
|
(void)val;
|
|
|
|
return 1;
|
2009-11-12 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int get_entropy_poolsize(unsigned int *val) {
|
|
|
|
/* Not applicable for FreeBSD as it uses the yarrow prng. */
|
|
|
|
(void)val;
|
|
|
|
return 1;
|
2009-08-07 07:24:24 +00:00
|
|
|
}
|