2008-02-20 20:30:45 +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
|
|
|
|
*
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 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
|
|
|
|
2009-05-14 22:39:54 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-03-16 13:33:15 +00:00
|
|
|
#include <sys/dkstat.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>
|
|
|
|
#include <net/if_mib.h>
|
2006-10-18 16:02:28 +00:00
|
|
|
#include <net/if_media.h>
|
|
|
|
#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
|
|
|
|
2006-10-18 16:02:28 +00:00
|
|
|
#include <dev/wi/if_wavelan_ieee.h>
|
2009-03-28 19:53:02 +00:00
|
|
|
#include <dev/acpica/acpiio.h>
|
2006-10-18 16:02:28 +00:00
|
|
|
|
2005-08-24 05:33:10 +00:00
|
|
|
#include "conky.h"
|
2009-03-28 19:53:02 +00:00
|
|
|
#include "freebsd.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "top.h"
|
|
|
|
#include "diskio.h"
|
2005-08-24 05:33:10 +00:00
|
|
|
|
2008-02-20 20:30:45 +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
|
2006-03-20 11:44:29 +00:00
|
|
|
#define FREEBSD_DEBUG
|
2006-03-18 16:04:36 +00:00
|
|
|
#endif
|
|
|
|
|
2005-08-29 19:39:35 +00:00
|
|
|
inline void proc_find_top(struct process **cpu, struct process **mem);
|
|
|
|
|
2005-08-27 12:59:46 +00:00
|
|
|
static short cpu_setup = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
static int getsysctl(char *name, void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
size_t nlen = len;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return -1;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 19:53:02 +00:00
|
|
|
if (nlen != len && errno == ENOMEM) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return -1;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ifmibdata *data = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2008-03-20 20:56:13 +00:00
|
|
|
static int swapmode(unsigned long *retavail, unsigned long *retfree)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
int n;
|
2008-03-20 20:56:13 +00:00
|
|
|
unsigned long pagesize = getpagesize();
|
2005-07-20 00:30:40 +00:00
|
|
|
struct kvm_swap swapary[1];
|
|
|
|
|
|
|
|
*retavail = 0;
|
|
|
|
*retfree = 0;
|
|
|
|
|
2008-03-20 20:56:13 +00:00
|
|
|
#define CONVERT(v) ((quad_t)(v) * (pagesize / 1024))
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
n = kvm_getswapinfo(kd, swapary, 1, 0);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (n < 0 || swapary[0].ksw_total == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
*retavail = CONVERT(swapary[0].ksw_total);
|
|
|
|
*retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
|
|
|
|
|
|
|
|
n = (int) ((double) swapary[0].ksw_used * 100.0 /
|
2006-03-20 11:44:29 +00:00
|
|
|
(double) swapary[0].ksw_total);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return n;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void prepare_update()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_uptime()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
int mib[2] = { CTL_KERN, KERN_BOOTTIME };
|
|
|
|
struct timeval boottime;
|
|
|
|
time_t now;
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t size = sizeof(boottime);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
|
|
|
|
&& (boottime.tv_sec != 0)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
time(&now);
|
|
|
|
info.uptime = now - boottime.tv_sec;
|
|
|
|
} else {
|
2006-03-20 11:44:29 +00:00
|
|
|
fprintf(stderr, "Could not get uptime\n");
|
2005-07-20 00:30:40 +00:00
|
|
|
info.uptime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
int check_mount(char *s)
|
|
|
|
{
|
|
|
|
struct statfs *mntbuf;
|
|
|
|
int i, mntsize;
|
|
|
|
|
|
|
|
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
|
2008-02-20 20:30:45 +00:00
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
|
|
|
if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
|
2007-08-05 04:47:21 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_meminfo()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-07-06 09:25:28 +00:00
|
|
|
u_int total_pages, inactive_pages, free_pages;
|
2008-03-20 20:56:13 +00:00
|
|
|
unsigned long swap_avail, swap_free;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
int pagesize = getpagesize();
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
|
2008-06-15 07:08:52 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
|
2008-06-15 07:08:52 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
|
2008-06-15 07:08:52 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-20 20:56:13 +00:00
|
|
|
info.memmax = total_pages * (pagesize >> 10);
|
|
|
|
info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
|
2008-06-21 20:37:58 +00:00
|
|
|
info.memeasyfree = info.memfree = info.memmax - info.mem;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
if ((swapmode(&swap_avail, &swap_free)) >= 0) {
|
|
|
|
info.swapmax = swap_avail;
|
|
|
|
info.swap = (swap_avail - swap_free);
|
|
|
|
} else {
|
|
|
|
info.swapmax = 0;
|
|
|
|
info.swap = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_net_stats()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
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;
|
2009-06-06 15:41:51 +00:00
|
|
|
if (delta <= 0.0001 && total_updates != 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (getifaddrs(&ifap) < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
ns = get_net_stat((const char *) ifa->ifa_name);
|
|
|
|
|
|
|
|
if (ifa->ifa_flags & IFF_UP) {
|
2006-03-25 15:30:59 +00:00
|
|
|
struct ifaddrs *iftmp;
|
|
|
|
|
|
|
|
ns->up = 1;
|
2005-07-20 00:30:40 +00:00
|
|
|
last_recv = ns->recv;
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK) {
|
2005-07-20 00:30:40 +00:00
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (iftmp = ifa->ifa_next;
|
|
|
|
iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
|
|
|
|
iftmp = iftmp->ifa_next) {
|
|
|
|
if (iftmp->ifa_addr->sa_family == AF_INET) {
|
2006-03-25 15:30:59 +00:00
|
|
|
memcpy(&(ns->addr), iftmp->ifa_addr,
|
|
|
|
iftmp->ifa_addr->sa_len);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-25 15:30:59 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
ifd = (struct if_data *) ifa->ifa_data;
|
|
|
|
r = ifd->ifi_ibytes;
|
|
|
|
t = ifd->ifi_obytes;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (r < ns->last_read_recv) {
|
|
|
|
ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
ns->recv += (r - ns->last_read_recv);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ns->last_read_recv = r;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (t < ns->last_read_trans) {
|
|
|
|
ns->trans += ((long long) 4294967295U -
|
2006-03-20 11:44:29 +00:00
|
|
|
ns->last_read_trans) + t;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
ns->trans += (t - ns->last_read_trans);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ns->last_read_trans = t;
|
|
|
|
|
|
|
|
/* calculate speeds */
|
|
|
|
ns->recv_speed = (ns->recv - last_recv) / delta;
|
|
|
|
ns->trans_speed = (ns->trans - last_trans) / delta;
|
2006-03-25 15:30:59 +00:00
|
|
|
} else {
|
|
|
|
ns->up = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_total_processes()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
int n_processes;
|
|
|
|
|
2006-03-22 19:27:31 +00:00
|
|
|
kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
info.procs = n_processes;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_running_processes()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
2006-03-22 19:27:31 +00:00
|
|
|
p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2007-12-31 00:00:35 +00:00
|
|
|
#if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
|
2008-02-20 20:30:45 +00:00
|
|
|
if (p[i].kp_proc.p_stat == SRUN) {
|
2005-07-20 00:30:40 +00:00
|
|
|
#else
|
2008-02-20 20:30:45 +00:00
|
|
|
if (p[i].ki_stat == SRUN) {
|
2005-07-20 00:30:40 +00:00
|
|
|
#endif
|
2006-03-22 19:27:31 +00:00
|
|
|
cnt++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-03-22 19:27:31 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
info.run_procs = cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cpu_load_struct {
|
|
|
|
unsigned long load[5];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
|
|
|
|
long cpu_used, oldtotal, oldused;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_cpu_count()
|
2005-08-27 12:59:46 +00:00
|
|
|
{
|
2006-03-18 16:04:36 +00:00
|
|
|
/* int cpu_count = 0; */
|
2005-08-27 12:59:46 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
|
|
|
|
* It's possible to get a CPU count, but as we fulfill only
|
|
|
|
* info.cpu_usage[0], it's better to report there's only one CPU.
|
|
|
|
* It should fix some bugs (e.g. cpugraph) */
|
2006-03-12 23:26:12 +00:00
|
|
|
#if 0
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
|
2005-08-27 12:59:46 +00:00
|
|
|
info.cpu_count = cpu_count;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-03-12 23:26:12 +00:00
|
|
|
#endif
|
|
|
|
info.cpu_count = 1;
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
info.cpu_usage = malloc(info.cpu_count * sizeof(float));
|
|
|
|
if (info.cpu_usage == NULL) {
|
2005-08-27 12:59:46 +00:00
|
|
|
CRIT_ERR("malloc");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-08-27 12:59:46 +00:00
|
|
|
}
|
|
|
|
|
2005-08-29 19:39:35 +00:00
|
|
|
/* XXX: SMP support */
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_cpu_usage()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
long used, total;
|
|
|
|
long cp_time[CPUSTATES];
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t len = sizeof(cp_time);
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2006-11-08 03:06:42 +00:00
|
|
|
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
|
2006-11-12 06:11:47 +00:00
|
|
|
if ((cpu_setup == 0) || (!info.cpu_usage)) {
|
2005-08-27 12:59:46 +00:00
|
|
|
get_cpu_count();
|
|
|
|
cpu_setup = 1;
|
|
|
|
}
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
fprintf(stderr, "Cannot get kern.cp_time");
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fresh.load[0] = cp_time[CP_USER];
|
|
|
|
fresh.load[1] = cp_time[CP_NICE];
|
|
|
|
fresh.load[2] = cp_time[CP_SYS];
|
|
|
|
fresh.load[3] = cp_time[CP_IDLE];
|
|
|
|
fresh.load[4] = cp_time[CP_IDLE];
|
|
|
|
|
|
|
|
used = fresh.load[0] + fresh.load[1] + fresh.load[2];
|
2008-02-20 20:30:45 +00:00
|
|
|
total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
if ((total - oldtotal) != 0) {
|
2006-03-20 11:44:29 +00:00
|
|
|
info.cpu_usage[0] = ((double) (used - oldused)) /
|
|
|
|
(double) (total - oldtotal);
|
2005-07-20 00:30:40 +00:00
|
|
|
} else {
|
2005-08-27 12:59:46 +00:00
|
|
|
info.cpu_usage[0] = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oldused = used;
|
|
|
|
oldtotal = total;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_load_average()
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
double v[3];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
getloadavg(v, 3);
|
|
|
|
|
2008-02-20 10:01:23 +00:00
|
|
|
info.loadavg[0] = (double) v[0];
|
|
|
|
info.loadavg[1] = (double) v[1];
|
|
|
|
info.loadavg[2] = (double) v[2];
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
double get_acpi_temperature(int fd)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
|
2006-03-20 11:44:29 +00:00
|
|
|
fprintf(stderr,
|
2008-02-20 20:30:45 +00:00
|
|
|
"Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
|
|
|
|
return 0.0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return KELVTOC(temp);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 19:53:02 +00:00
|
|
|
static void get_battery_stats(int *battime, int *batcapacity, int *batstate, int *ac) {
|
|
|
|
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
|
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
if (ac && GETSYSCTL("hw.acpi.acline", *ac)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
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
|
|
|
|
2009-03-28 19:53:02 +00:00
|
|
|
void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
|
|
|
|
{
|
|
|
|
int battime, batcapacity, batstate, ac;
|
|
|
|
|
|
|
|
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");
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2006-12-12 23:21:14 +00:00
|
|
|
switch (item) {
|
2008-02-20 20:30:45 +00:00
|
|
|
case BATTERY_TIME:
|
2009-03-28 19:53:02 +00:00
|
|
|
if (batstate == 1 && battime != -1)
|
|
|
|
snprintf(buf, n, "%d:%2.2d", battime / 60, battime % 60);
|
2008-02-20 20:30:45 +00:00
|
|
|
break;
|
2009-03-28 19:53:02 +00:00
|
|
|
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);
|
2008-02-20 20:30:45 +00:00
|
|
|
break;
|
2009-03-28 19:53:02 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown requested battery stat %d\n", item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2009-05-14 22:39:54 +00:00
|
|
|
fprintf(stderr, "Wrong battery unit %s requested\n", bat ? bat : "");
|
2009-03-28 19:53:02 +00:00
|
|
|
return -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2009-03-28 19:53:02 +00:00
|
|
|
return batnum;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_battery_perct(const char *bat)
|
2007-05-05 16:46:12 +00:00
|
|
|
{
|
2009-03-28 19:53:02 +00:00
|
|
|
union acpi_battery_ioctl_arg battio;
|
2009-05-14 22:39:54 +00:00
|
|
|
int batnum, acpifd;
|
2009-03-28 19:53:02 +00:00
|
|
|
int designcap, lastfulcap, batperct;
|
|
|
|
|
|
|
|
if ((battio.unit = batnum = check_bat(bat)) < 0)
|
|
|
|
return 0;
|
|
|
|
if ((acpifd = open("/dev/acpi", O_RDONLY)) < 0) {
|
|
|
|
fprintf(stderr, "Can't open ACPI device\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) {
|
|
|
|
fprintf(stderr, "Unable to get info for battery unit %d\n", batnum);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(acpifd);
|
|
|
|
designcap = battio.bif.dcap;
|
|
|
|
lastfulcap = battio.bif.lfcap;
|
|
|
|
batperct = (designcap > 0 && lastfulcap > 0) ?
|
|
|
|
(int) (((float) lastfulcap / designcap) * 100) : 0;
|
|
|
|
return batperct > 100 ? 100 : batperct;
|
2007-05-05 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int get_battery_perct_bar(const char *bar)
|
2007-05-05 16:46:12 +00:00
|
|
|
{
|
2009-03-28 19:53:02 +00:00
|
|
|
int batperct = get_battery_perct(bar);
|
|
|
|
return (int)(batperct * 2.56 - 1);
|
2007-05-05 16:46:12 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int open_acpi_temperature(const char *name)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2009-03-28 19:53:02 +00:00
|
|
|
/* Not applicable for FreeBSD. */
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
int state;
|
2005-11-12 21:42:00 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-12 21:42:00 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
if (GETSYSCTL("hw.acpi.acline", state)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
|
2005-11-12 21:42:00 +00:00
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +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
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2005-11-13 04:04:00 +00:00
|
|
|
/* not implemented */
|
2008-02-20 20:30:45 +00:00
|
|
|
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 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-28 04:48:27 +00:00
|
|
|
{
|
2005-11-13 04:04:00 +00:00
|
|
|
/* not implemented */
|
2008-02-20 20:30:45 +00:00
|
|
|
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 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-28 04:48:27 +00:00
|
|
|
{
|
2005-11-13 04:04:00 +00:00
|
|
|
/* not implemented */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (p_client_buffer && client_buffer_size > 0) {
|
|
|
|
memset(p_client_buffer, 0, client_buffer_size);
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 11:10:52 +00:00
|
|
|
/* rdtsc() and get_freq_dynamic() copied from linux.c */
|
|
|
|
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2008-02-20 20:30:45 +00:00
|
|
|
__inline__ unsigned long long int rdtsc()
|
2005-08-26 11:10:52 +00:00
|
|
|
{
|
2006-03-20 11:44:29 +00:00
|
|
|
unsigned long long int x;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
__asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
|
2008-02-20 20:30:45 +00:00
|
|
|
return x;
|
2005-08-26 11:10:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
|
2008-05-03 12:30:05 +00:00
|
|
|
const char *p_format, int divisor)
|
2005-08-26 02:16:35 +00:00
|
|
|
{
|
2005-08-26 11:10:52 +00:00
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2006-03-20 11:44:29 +00:00
|
|
|
struct timezone tz;
|
|
|
|
struct timeval tvstart, tvstop;
|
|
|
|
unsigned long long cycles[2]; /* gotta be 64 bit */
|
|
|
|
unsigned int microseconds; /* total time taken */
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(&tz, 0, sizeof(tz));
|
2006-03-20 11:44:29 +00:00
|
|
|
|
|
|
|
/* get this function in cached memory */
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
cycles[0] = rdtsc();
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
|
|
|
|
/* we don't trust that this is any specific length of time */
|
|
|
|
usleep(100);
|
|
|
|
cycles[1] = rdtsc();
|
|
|
|
gettimeofday(&tvstop, &tz);
|
|
|
|
microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
|
|
|
|
(tvstop.tv_usec - tvstart.tv_usec);
|
|
|
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
2008-02-20 20:30:45 +00:00
|
|
|
(float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
|
2005-08-26 11:10:52 +00:00
|
|
|
#else
|
2007-04-15 04:58:41 +00:00
|
|
|
get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
|
2005-08-26 11:10:52 +00:00
|
|
|
#endif
|
2005-08-26 02:16:35 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* void */
|
2008-05-03 12:30:05 +00:00
|
|
|
char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
|
2008-02-20 20:30:45 +00:00
|
|
|
int divisor, unsigned int cpu)
|
2005-07-28 04:48:27 +00:00
|
|
|
{
|
2005-08-24 05:33:10 +00:00
|
|
|
int freq;
|
2006-06-07 10:56:37 +00:00
|
|
|
char *freq_sysctl;
|
2005-11-13 03:33:26 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
freq_sysctl = (char *) calloc(16, sizeof(char));
|
|
|
|
if (freq_sysctl == NULL) {
|
2006-06-07 10:56:37 +00:00
|
|
|
exit(-1);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-06-07 10:56:37 +00:00
|
|
|
|
2006-12-03 18:10:21 +00:00
|
|
|
snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0 || !p_format
|
|
|
|
|| divisor <= 0) {
|
2006-10-18 16:02:28 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL(freq_sysctl, freq) == 0) {
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
(float) freq / divisor);
|
|
|
|
} else {
|
2006-03-21 11:32:59 +00:00
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-06-07 10:56:37 +00:00
|
|
|
|
|
|
|
free(freq_sysctl);
|
2006-08-01 21:24:15 +00:00
|
|
|
return 1;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2005-08-23 06:10:25 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_top()
|
2005-08-23 06:10:25 +00:00
|
|
|
{
|
2005-08-29 19:39:35 +00:00
|
|
|
proc_find_top(info.cpu, info.memu);
|
2005-08-23 06:10:25 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 16:42:48 +00:00
|
|
|
#if 0
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_wifi_stats()
|
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) {
|
|
|
|
ns = get_net_stat((const char *) ifa->ifa_name);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_diskio()
|
2005-08-26 09:54:57 +00:00
|
|
|
{
|
2009-05-14 22:52:18 +00:00
|
|
|
int devs_count, num_selected, num_selections, dn;
|
2006-03-20 11:44:29 +00:00
|
|
|
struct device_selection *dev_select = NULL;
|
|
|
|
long select_generation;
|
2008-02-20 20:30:45 +00:00
|
|
|
static struct statinfo statinfo_cur;
|
2009-05-14 22:52:18 +00:00
|
|
|
char device_name[text_buffer_size];
|
2009-03-28 19:53:02 +00:00
|
|
|
struct diskio_stat *cur;
|
2009-05-14 22:52:18 +00:00
|
|
|
unsigned int reads, writes;
|
|
|
|
unsigned int total_reads = 0, total_writes = 0;
|
|
|
|
|
2005-08-27 11:56:22 +00:00
|
|
|
|
2009-05-14 22:52:18 +00:00
|
|
|
memset(&statinfo_cur, 0, sizeof(statinfo_cur));
|
2009-03-28 19:53:02 +00:00
|
|
|
statinfo_cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
|
|
|
|
stats.current = stats.current_read = stats.current_write = 0;
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2009-05-14 22:52:18 +00:00
|
|
|
if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
|
|
|
|
free(statinfo_cur.dinfo);
|
2005-08-27 11:56:22 +00:00
|
|
|
return;
|
2009-05-14 22:52:18 +00:00
|
|
|
}
|
2005-08-26 09:54:57 +00:00
|
|
|
|
2005-08-27 11:56:22 +00:00
|
|
|
devs_count = statinfo_cur.dinfo->numdevs;
|
|
|
|
if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
|
|
|
|
&select_generation, statinfo_cur.dinfo->generation,
|
2008-02-20 20:30:45 +00:00
|
|
|
statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
|
|
|
|
DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
|
2009-05-14 22:52:18 +00:00
|
|
|
for (dn = 0; dn < devs_count; dn++) {
|
2005-08-27 11:56:22 +00:00
|
|
|
int di;
|
2008-02-20 20:30:45 +00:00
|
|
|
struct devstat *dev;
|
2005-08-27 11:56:22 +00:00
|
|
|
|
|
|
|
di = dev_select[dn].position;
|
2006-03-20 11:44:29 +00:00
|
|
|
dev = &statinfo_cur.dinfo->devices[di];
|
2009-05-14 22:52:18 +00:00
|
|
|
snprintf(device_name, text_buffer_size, "%s%d",
|
|
|
|
dev_select[dn].device_name, dev_select[dn].unit_number);
|
2005-08-27 11:56:22 +00:00
|
|
|
|
2009-05-14 22:52:18 +00:00
|
|
|
total_reads += (reads = dev->bytes[DEVSTAT_READ] / 512);
|
|
|
|
total_writes += (writes = dev->bytes[DEVSTAT_WRITE] / 512);
|
2009-03-28 19:53:02 +00:00
|
|
|
for (cur = stats.next; cur; cur = cur->next) {
|
2009-05-14 22:52:18 +00:00
|
|
|
if (cur->dev && !strcmp(device_name, cur->dev)) {
|
|
|
|
update_diskio_values(cur, reads, writes);
|
|
|
|
break;
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-27 11:56:22 +00:00
|
|
|
}
|
2009-05-14 22:52:18 +00:00
|
|
|
update_diskio_values(&stats, total_reads, total_writes);
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2005-08-27 11:56:22 +00:00
|
|
|
free(dev_select);
|
|
|
|
}
|
|
|
|
|
2005-08-30 17:32:58 +00:00
|
|
|
free(statinfo_cur.dinfo);
|
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
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int comparecpu(const void *a, const void *b)
|
2005-08-29 19:39:35 +00:00
|
|
|
{
|
2008-02-24 16:35:18 +00:00
|
|
|
if (((struct process *)a)->amount > ((struct process *)b)->amount) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return -1;
|
2008-02-24 16:35:18 +00:00
|
|
|
} else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-29 19:39:35 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int comparemem(const void *a, const void *b)
|
2005-08-29 19:39:35 +00:00
|
|
|
{
|
2008-02-24 16:35:18 +00:00
|
|
|
if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return -1;
|
2008-02-24 16:35:18 +00:00
|
|
|
} else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-29 19:39:35 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
inline void proc_find_top(struct process **cpu, struct process **mem)
|
2005-08-29 19:39:35 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, j = 0;
|
|
|
|
struct process *processes;
|
2006-03-18 16:04:36 +00:00
|
|
|
|
2006-03-22 19:27:31 +00:00
|
|
|
int total_pages;
|
|
|
|
|
|
|
|
/* we get total pages count again to be sure it is up to date */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
|
|
|
|
CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
|
|
|
|
}
|
2006-03-22 19:27:31 +00:00
|
|
|
|
|
|
|
p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
|
2008-02-20 20:30:45 +00:00
|
|
|
processes = malloc(n_processes * sizeof(struct process));
|
2006-03-22 19:27:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
|
2006-03-22 19:27:31 +00:00
|
|
|
processes[j].pid = p[i].ki_pid;
|
2008-04-02 18:44:49 +00:00
|
|
|
processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
|
|
|
|
processes[j].totalmem = (float) (p[i].ki_rssize /
|
|
|
|
(float) total_pages) * 100.0;
|
2008-03-30 07:04:25 +00:00
|
|
|
processes[j].vsize = p[i].ki_size;
|
|
|
|
processes[j].rss = (p[i].ki_rssize * getpagesize());
|
2006-03-22 19:27:31 +00:00
|
|
|
j++;
|
2005-08-29 19:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
qsort(processes, j - 1, sizeof(struct process), comparemem);
|
2007-08-05 04:47:21 +00:00
|
|
|
for (i = 0; i < 10 && i < n_processes; i++) {
|
2006-03-22 19:27:31 +00:00
|
|
|
struct process *tmp, *ttmp;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
tmp = malloc(sizeof(struct process));
|
2006-03-22 19:27:31 +00:00
|
|
|
tmp->pid = processes[i].pid;
|
|
|
|
tmp->amount = processes[i].amount;
|
|
|
|
tmp->totalmem = processes[i].totalmem;
|
2008-04-02 18:44:49 +00:00
|
|
|
tmp->name = strndup(processes[i].name, text_buffer_size);
|
2008-03-30 07:04:25 +00:00
|
|
|
tmp->rss = processes[i].rss;
|
|
|
|
tmp->vsize = processes[i].vsize;
|
2005-08-29 19:39:35 +00:00
|
|
|
|
2006-03-22 19:27:31 +00:00
|
|
|
ttmp = mem[i];
|
2006-03-25 14:28:40 +00:00
|
|
|
mem[i] = tmp;
|
2006-03-22 19:27:31 +00:00
|
|
|
if (ttmp != NULL) {
|
|
|
|
free(ttmp->name);
|
|
|
|
free(ttmp);
|
2006-03-18 16:04:36 +00:00
|
|
|
}
|
2006-03-22 19:27:31 +00:00
|
|
|
}
|
2006-03-18 16:04:36 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
qsort(processes, j - 1, sizeof(struct process), comparecpu);
|
2007-08-05 04:47:21 +00:00
|
|
|
for (i = 0; i < 10 && i < n_processes; i++) {
|
2006-03-22 19:27:31 +00:00
|
|
|
struct process *tmp, *ttmp;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
tmp = malloc(sizeof(struct process));
|
2006-03-22 19:27:31 +00:00
|
|
|
tmp->pid = processes[i].pid;
|
|
|
|
tmp->amount = processes[i].amount;
|
|
|
|
tmp->totalmem = processes[i].totalmem;
|
2008-04-02 18:44:49 +00:00
|
|
|
tmp->name = strndup(processes[i].name, text_buffer_size);
|
2008-03-30 07:04:25 +00:00
|
|
|
tmp->rss = processes[i].rss;
|
|
|
|
tmp->vsize = processes[i].vsize;
|
2006-03-22 19:27:31 +00:00
|
|
|
|
|
|
|
ttmp = cpu[i];
|
2006-03-25 14:28:40 +00:00
|
|
|
cpu[i] = tmp;
|
2006-03-22 19:27:31 +00:00
|
|
|
if (ttmp != NULL) {
|
|
|
|
free(ttmp->name);
|
|
|
|
free(ttmp);
|
2006-03-18 16:04:36 +00:00
|
|
|
}
|
2006-03-22 19:27:31 +00:00
|
|
|
}
|
2006-03-20 11:44:29 +00:00
|
|
|
|
2006-03-18 16:04:36 +00:00
|
|
|
#if defined(FREEBSD_DEBUG)
|
2006-03-22 19:27:31 +00:00
|
|
|
printf("=====\nmem\n");
|
|
|
|
for (i = 0; i < 10; i++) {
|
2008-03-30 07:04:25 +00:00
|
|
|
printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
|
|
|
|
mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
|
2006-03-22 19:27:31 +00:00
|
|
|
}
|
2006-03-18 16:04:36 +00:00
|
|
|
#endif
|
2006-03-21 11:32:59 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
free(processes[i].name);
|
|
|
|
}
|
2006-03-22 19:27:31 +00:00
|
|
|
free(processes);
|
2005-08-29 19:39:35 +00:00
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
#if defined(i386) || defined(__i386__)
|
|
|
|
#define APMDEV "/dev/apm"
|
|
|
|
#define APM_UNKNOWN 255
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int apm_getinfo(int fd, apm_info_t aip)
|
2005-08-30 04:19:16 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_adapter()
|
2005-08-30 04:19:16 +00:00
|
|
|
{
|
2006-03-20 11:44:29 +00:00
|
|
|
int fd;
|
|
|
|
struct apm_info info;
|
2007-08-22 05:11:06 +00:00
|
|
|
char *out;
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2007-05-06 09:32:21 +00:00
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
fd = open(APMDEV, O_RDONLY);
|
2007-05-06 09:32:21 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
2005-08-30 04:19:16 +00:00
|
|
|
close(fd);
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
switch (info.ai_acline) {
|
|
|
|
case 0:
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "off-line", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2006-03-20 11:44:29 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2007-05-06 09:32:21 +00:00
|
|
|
if (info.ai_batt_stat == 3) {
|
|
|
|
strncpy(out, "charging", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
} else {
|
|
|
|
strncpy(out, "on-line", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
}
|
2006-03-20 11:44:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "unknown", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2006-03-20 11:44:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_battery_life()
|
2006-03-20 11:44:29 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
u_int batt_life;
|
|
|
|
struct apm_info info;
|
|
|
|
char *out;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2006-03-20 11:44:29 +00:00
|
|
|
|
|
|
|
fd = open(APMDEV, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2005-08-30 04:19:16 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
2005-08-30 04:19:16 +00:00
|
|
|
close(fd);
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
batt_life = info.ai_batt_life;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (batt_life == APM_UNKNOWN) {
|
2005-08-30 04:19:16 +00:00
|
|
|
strncpy(out, "unknown", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else if (batt_life <= 100) {
|
2006-03-20 11:44:29 +00:00
|
|
|
snprintf(out, 16, "%d%%", batt_life);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
|
|
|
} else {
|
2005-08-30 04:19:16 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_battery_time()
|
2005-08-30 04:19:16 +00:00
|
|
|
{
|
2006-03-20 11:44:29 +00:00
|
|
|
int fd;
|
|
|
|
int batt_time;
|
|
|
|
int h, m, s;
|
|
|
|
struct apm_info info;
|
|
|
|
char *out;
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
fd = open(APMDEV, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2005-08-30 04:19:16 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
2005-08-30 04:19:16 +00:00
|
|
|
close(fd);
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2006-03-20 11:44:29 +00:00
|
|
|
batt_time = info.ai_batt_time;
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (batt_time == -1) {
|
2005-08-30 04:19:16 +00:00
|
|
|
strncpy(out, "unknown", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2006-03-20 11:44:29 +00:00
|
|
|
h = batt_time;
|
|
|
|
s = h % 60;
|
|
|
|
h /= 60;
|
|
|
|
m = h % 60;
|
|
|
|
h /= 60;
|
|
|
|
snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
|
|
|
|
}
|
2005-08-30 04:19:16 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2005-08-30 04:19:16 +00:00
|
|
|
}
|
2005-11-17 05:08:08 +00:00
|
|
|
|
2005-12-08 13:20:29 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-28 19:53:02 +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("discharging", buffer, 11)) {
|
|
|
|
buffer[0] = 'D';
|
|
|
|
memmove(buffer + 1, buffer + 11, n - 11);
|
|
|
|
} else if (0 == strncmp("absent/on AC", buffer, 12)) {
|
|
|
|
buffer[0] = 'A';
|
|
|
|
memmove(buffer + 1, buffer + 12, n - 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_entropy(void)
|
2006-11-30 20:46:34 +00:00
|
|
|
{
|
2009-03-28 19:53:02 +00:00
|
|
|
/* Not applicable for FreeBSD as it uses the yarrow prng. */
|
2006-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
|
2005-11-17 05:08:08 +00:00
|
|
|
/* empty stub so conky links */
|
2008-02-20 20:30:45 +00:00
|
|
|
void free_all_processes(void)
|
2005-11-17 05:08:08 +00:00
|
|
|
{
|
|
|
|
}
|