1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-02-10 07:59:03 +00:00

Enable NetBSD support.

Based on patches from:

http://pkgsrc-wip.cvs.sourceforge.net/viewvc/pkgsrc-wip/wip/conky/patches/
This commit is contained in:
Brenden Matthews 2012-05-07 10:55:03 -07:00
parent 5c3a6581d1
commit e1390cb3e6
7 changed files with 386 additions and 214 deletions

View File

@ -62,10 +62,10 @@ case $uname in
WANT_DEVSTAT=yes WANT_DEVSTAT=yes
WANT_OPTRESET=yes WANT_OPTRESET=yes
;; ;;
# NetBSD*) NetBSD*)
# WANT_KVM=yes WANT_KVM=yes
# WANT_OSSLIB=yes WANT_OSSLIB=yes
# ;; ;;
OpenBSD*) OpenBSD*)
WANT_KVM=yes WANT_KVM=yes
@ -87,7 +87,7 @@ esac
AM_CONDITIONAL(BUILD_LINUX, test x$uname = xLinux) AM_CONDITIONAL(BUILD_LINUX, test x$uname = xLinux)
#AM_CONDITIONAL(BUILD_SOLARIS, test x$uname = xSunOS) #AM_CONDITIONAL(BUILD_SOLARIS, test x$uname = xSunOS)
AM_CONDITIONAL(BUILD_FREEBSD, test x$uname = xFreeBSD -o x$uname = xGNU/kFreeBSD) AM_CONDITIONAL(BUILD_FREEBSD, test x$uname = xFreeBSD -o x$uname = xGNU/kFreeBSD)
#AM_CONDITIONAL(BUILD_NETBSD, test x$uname = xNetBSD) AM_CONDITIONAL(BUILD_NETBSD, test x$uname = xNetBSD)
AM_CONDITIONAL(BUILD_OPENBSD, test x$uname = xOpenBSD) AM_CONDITIONAL(BUILD_OPENBSD, test x$uname = xOpenBSD)
BUILD_DATE=$(LANG=en_US LC_ALL=en_US LOCALE=en_US date) BUILD_DATE=$(LANG=en_US LC_ALL=en_US LOCALE=en_US date)
@ -784,7 +784,7 @@ dnl
dnl Some headers dnl Some headers
dnl dnl
AC_CHECK_HEADERS([signal.h unistd.h sys/utsname.h sys/stat.h linux/soundcard.h alsa/asoundlib.h dirent.h mcheck.h \ AC_CHECK_HEADERS([signal.h unistd.h sys/utsname.h sys/stat.h linux/soundcard.h alsa/asoundlib.h dirent.h mcheck.h sys/statvfs.h \
sys/statfs.h sys/param.h pthread.h semaphore.h assert.h errno.h time.h]) sys/statfs.h sys/param.h pthread.h semaphore.h assert.h errno.h time.h])
AC_CHECK_HEADERS([sys/mount.h], [], [], AC_CHECK_HEADERS([sys/mount.h], [], [],
[#ifdef HAVE_SYS_PARAM_H [#ifdef HAVE_SYS_PARAM_H

View File

@ -119,9 +119,9 @@ endif
if BUILD_FREEBSD if BUILD_FREEBSD
optional_sources += $(freebsd) optional_sources += $(freebsd)
endif endif
#if BUILD_NETBSD if BUILD_NETBSD
#optional_sources += $(netbsd) optional_sources += $(netbsd)
#endif endif
if BUILD_OPENBSD if BUILD_OPENBSD
optional_sources += $(openbsd) optional_sources += $(openbsd)
endif endif

View File

@ -65,7 +65,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
#ifdef NCURSES #ifdef NCURSES
#include <ncurses.h> #include <curses.h>
#endif #endif
#ifdef XOAP #ifdef XOAP
#include <libxml/parser.h> #include <libxml/parser.h>
@ -117,8 +117,11 @@
#include "freebsd.h" #include "freebsd.h"
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#include "openbsd.h" #include "openbsd.h"
#elif defined(__NetBSD__)
#include "netbsd.h"
#endif #endif
#if defined(__FreeBSD_kernel__) #if defined(__FreeBSD_kernel__)
#include <bsd/bsd.h> #include <bsd/bsd.h>
#endif #endif
@ -784,7 +787,7 @@ void generate_text_internal(char *p, int p_max_size,
OBJ(read_tcp) { OBJ(read_tcp) {
print_read_tcp(obj, p, p_max_size); print_read_tcp(obj, p, p_max_size);
} }
#ifndef __OpenBSD__ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
OBJ(acpitemp) { OBJ(acpitemp) {
temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS); temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
} }
@ -799,7 +802,7 @@ void generate_text_internal(char *p, int p_max_size,
OBJ(freq_g) { OBJ(freq_g) {
static int ok = 1; static int ok = 1;
if (ok) { if (ok) {
#ifndef __OpenBSD__ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
ok = get_freq(p, p_max_size, "%'.2f", 1000, ok = get_freq(p, p_max_size, "%'.2f", 1000,
obj->data.i); obj->data.i);
#else #else
@ -846,7 +849,7 @@ void generate_text_internal(char *p, int p_max_size,
#endif /* __linux__ */ #endif /* __linux__ */
#ifndef __OpenBSD__ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
OBJ(acpifan) { OBJ(acpifan) {
get_acpi_fan(p, p_max_size); get_acpi_fan(p, p_max_size);
} }
@ -3493,6 +3496,9 @@ static void main_loop(void)
next_update_time = get_time(); next_update_time = get_time();
info.looped = 0; info.looped = 0;
while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) { while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
#ifdef __NetBSD__
update_interval = update_interval_old;
#else
if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) { if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
char buf[max_user_text]; char buf[max_user_text];
@ -3503,6 +3509,7 @@ static void main_loop(void)
update_interval = update_interval_old; update_interval = update_interval_old;
} }
} }
#endif
info.looped++; info.looped++;
#ifdef SIGNAL_BLOCKING #ifdef SIGNAL_BLOCKING
@ -5680,6 +5687,10 @@ void initialisation(int argc, char **argv) {
} }
pthread_mutex_init(&kvm_proc_mutex, NULL); pthread_mutex_init(&kvm_proc_mutex, NULL);
#endif #endif
#if defined(__NetBSD__)
if ((kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL)) == NULL)
CRIT_ERR(NULL, NULL, "cannot read kvm");
#endif
while (1) { while (1) {
int c = getopt_long(argc, argv, getopt_string, longopts, NULL); int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
@ -5960,9 +5971,11 @@ int main(int argc, char **argv)
curl_global_cleanup(); curl_global_cleanup();
#endif #endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
kvm_close(kd); kvm_close(kd);
#ifndef __NetBSD__
pthread_mutex_destroy(&kvm_proc_mutex); pthread_mutex_destroy(&kvm_proc_mutex);
#endif
#endif #endif
return 0; return 0;

View File

@ -65,7 +65,7 @@
#include "top.h" #include "top.h"
#ifdef NCURSES #ifdef NCURSES
#include <ncurses.h> #include <curses.h>
#endif #endif
/* check for OS and include appropriate headers */ /* check for OS and include appropriate headers */
@ -75,6 +75,8 @@
#include "freebsd.h" #include "freebsd.h"
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#include "openbsd.h" #include "openbsd.h"
#elif defined(__NetBSD__)
#include "netbsd.h"
#endif #endif
#include <string.h> #include <string.h>
@ -237,7 +239,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#endif /* __linux__ */ #endif /* __linux__ */
#ifndef __OpenBSD__ #if !defined(__NetBSD__) && !defined(__OpenBSD__)
END OBJ(acpifan, 0) END OBJ(acpifan, 0)
END OBJ(battery, 0) END OBJ(battery, 0)
char bat[64]; char bat[64];
@ -1371,7 +1373,7 @@ void free_text_objects(struct text_object *root, int internal)
for (obj = root->prev; obj; obj = root->prev) { for (obj = root->prev; obj; obj = root->prev) {
root->prev = obj->prev; root->prev = obj->prev;
switch (obj->type) { switch (obj->type) {
#ifndef __OpenBSD__ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
case OBJ_acpitemp: case OBJ_acpitemp:
close(data.i); close(data.i);
break; break;
@ -1645,7 +1647,7 @@ void free_text_objects(struct text_object *root, int internal)
#endif /* HAVE_LUA */ #endif /* HAVE_LUA */
case OBJ_pre_exec: case OBJ_pre_exec:
break; break;
#ifndef __OpenBSD__ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
case OBJ_battery: case OBJ_battery:
free(data.s); free(data.s);
break; break;

View File

@ -44,6 +44,11 @@
#include <sys/statfs.h> #include <sys/statfs.h>
#endif #endif
/* NetBSD, Solaris */
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
/* freebsd && netbsd */ /* freebsd && netbsd */
#ifdef HAVE_SYS_PARAM_H #ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> #include <sys/param.h>
@ -52,7 +57,7 @@
#include <sys/mount.h> #include <sys/mount.h>
#endif #endif
#if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__) #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
#include <mntent.h> #include <mntent.h>
#endif #endif
@ -118,6 +123,7 @@ struct fs_stat *prepare_fs_stat(const char *s)
static void update_fs_stat(struct fs_stat *fs) static void update_fs_stat(struct fs_stat *fs)
{ {
#ifdef HAVE_SYS_STATFS_H
struct statfs64 s; struct statfs64 s;
if (statfs64(fs->path, &s) == 0) { if (statfs64(fs->path, &s) == 0) {
@ -125,6 +131,14 @@ static void update_fs_stat(struct fs_stat *fs)
/* bfree (root) or bavail (non-roots) ? */ /* bfree (root) or bavail (non-roots) ? */
fs->avail = (long long)s.f_bavail * s.f_bsize; fs->avail = (long long)s.f_bavail * s.f_bsize;
fs->free = (long long)s.f_bfree * s.f_bsize; fs->free = (long long)s.f_bfree * s.f_bsize;
#else
struct statvfs s;
if (statvfs(fs->path, &s) == 0) {
fs->free = (long long)s.f_bfree * s.f_frsize;
fs->size = (long long)s.f_blocks * s.f_frsize;
fs->avail = (long long)s.f_bavail * s.f_frsize;
#endif
get_fs_type(fs->path, fs->type); get_fs_type(fs->path, fs->type);
} else { } else {
NORM_ERR("statfs64 '%s': %s", fs->path, strerror(errno)); NORM_ERR("statfs64 '%s': %s", fs->path, strerror(errno));
@ -138,10 +152,17 @@ static void update_fs_stat(struct fs_stat *fs)
void get_fs_type(const char *path, char *result) void get_fs_type(const char *path, char *result)
{ {
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__) #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
#ifdef HAVE_SYS_STATFS_H
struct statfs s; struct statfs s;
if (statfs(path, &s) == 0) { if (statfs(path, &s) == 0) {
#else
struct statvfs s;
if (statvfs(path, &s) == 0) {
#endif
strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE); strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
} else { } else {
NORM_ERR("statfs '%s': %s", path, strerror(errno)); NORM_ERR("statfs '%s': %s", path, strerror(errno));

View File

@ -30,239 +30,210 @@
#include "netbsd.h" #include "netbsd.h"
#include "net_stat.h" #include "net_stat.h"
#include "top.h"
#include <sys/types.h>
#include <sys/statvfs.h>
#include <ifaddrs.h>
static kvm_t *kd = NULL;
int kd_init = 0, nkd_init = 0;
u_int32_t sensvalue; u_int32_t sensvalue;
char errbuf[_POSIX2_LINE_MAX]; char errbuf[_POSIX2_LINE_MAX];
static short cpu_setup = 0;
static int init_kvm(void) inline void proc_find_top(struct process **cpu, struct process **mem);
{
if (kd_init) {
return 0;
}
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); void
if (kd == NULL) { prepare_update(void)
warnx("cannot kvm_openfiles: %s", errbuf);
return -1;
}
kd_init = 1;
return 0;
}
static int swapmode(int *retavail, int *retfree)
{
int n;
struct swapent *sep;
*retavail = 0;
*retfree = 0;
n = swapctl(SWAP_NSWAP, 0, 0);
if (n < 1) {
warn("could not get swap information");
return 0;
}
sep = (struct swapent *) malloc(n * (sizeof(*sep)));
if (sep == NULL) {
warn("memory allocation failed");
return 0;
}
if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
warn("could not get swap stats");
return 0;
}
for (; n > 0; n--) {
*retavail += (int) dbtob(sep[n - 1].se_nblks);
*retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
}
*retavail = (int) (*retavail / 1024);
*retfree = (int) (*retfree / 1024);
return 1;
}
void prepare_update()
{ {
} }
void update_uptime() int
update_uptime(void)
{ {
int mib[2] = { CTL_KERN, KERN_BOOTTIME }; int mib[2] = { CTL_KERN, KERN_BOOTTIME };
struct timeval boottime; struct timeval boottime;
time_t now; time_t now;
int size = sizeof(boottime); size_t size;
if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) size = sizeof(boottime);
&& (boottime.tv_sec != 0)) {
if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
warn("sysctl kern.boottime failed");
info.uptime = 0;
} else {
time(&now); time(&now);
info.uptime = now - boottime.tv_sec; info.uptime = now - boottime.tv_sec;
} else {
warn("could not get uptime");
info.uptime = 0;
} }
}
int check_mount(char *s)
{
/* stub */
return 0; return 0;
} }
void update_meminfo() /* checks is mp is a mounted mountpoint */
int
check_mount(char *mp)
{ {
int mib[] = { CTL_VM, VM_UVMEXP2 }; int nbmount, i;
int total_pages, inactive_pages, free_pages; struct statvfs *mntbuf;
int swap_avail, swap_free;
const int pagesize = getpagesize();
struct uvmexp_sysctl uvmexp;
size_t size = sizeof(uvmexp);
if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) { nbmount = getmntinfo(&mntbuf, MNT_NOWAIT);
warn("could not get memory info");
return;
}
total_pages = uvmexp.npages; for (i = 0; i < nbmount; i++) {
free_pages = uvmexp.free; if (strcmp(mntbuf[i].f_mntonname, mp) == 0) {
inactive_pages = uvmexp.inactive; return 1;
info.memmax = (total_pages * pagesize) >> 10;
info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
info.memeasyfree = info.memfree = info.memmax - info.mem;
if (swapmode(&swap_avail, &swap_free) >= 0) {
info.swapmax = swap_avail;
info.swap = (swap_avail - swap_free);
info.swapfree = swap_free;
}
}
void update_net_stats()
{
int i;
double delta;
struct ifnet ifnet;
struct ifnet_head ifhead; /* interfaces are in a tail queue */
u_long ifnetaddr;
static struct nlist namelist[] = {
{ "_ifnet" },
{ NULL }
};
static kvm_t *nkd;
if (!nkd_init) {
nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
if (nkd == NULL) {
warnx("cannot kvm_openfiles: %s", errbuf);
warnx("maybe you need to setgid kmem this program?");
return;
} else if (kvm_nlist(nkd, namelist) != 0) {
warn("cannot kvm_nlist");
return;
} else {
nkd_init = 1;
} }
} }
if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead, return 0;
sizeof(ifhead)) < 0) { }
warn("cannot kvm_read");
return; /* mostly from vmstat.c */
int
update_meminfo(void)
{
int mib[] = { CTL_VM, VM_UVMEXP2 };
struct uvmexp_sysctl uvmexp;
size_t ssize;
ssize = sizeof(uvmexp);
memset(&uvmexp, 0, ssize);
info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = 0;
info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
info.bufmem = 0;
if (sysctl(mib, 2, &uvmexp, &ssize, NULL, 0) < 0) {
warn("sysctl vm.uvmexp2 failed");
return 0;
} }
info.memmax = uvmexp.npages * uvmexp.pagesize / 1024;
info.memfree = uvmexp.inactive * uvmexp.pagesize / 1024;
info.swapmax = uvmexp.swpages * uvmexp.pagesize / 1024;
info.swapfree = (uvmexp.swpages - uvmexp.swpginuse) * \
uvmexp.pagesize / 1024;
info.buffers = uvmexp.filepages * uvmexp.pagesize / 1024;
info.cached = uvmexp.execpages * uvmexp.pagesize / 1024;
info.mem = info.memmax - info.memfree;
info.memeasyfree = info.memfree;
info.bufmem = info.cached + info.buffers;
info.swap = info.swapmax - info.swapfree;
return 0;
}
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 */ /* get delta */
delta = current_update_time - last_update_time; delta = current_update_time - last_update_time;
if (delta <= 0.0001) { if (delta <= 0.0001) {
return; return 0;
} }
for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first; if (getifaddrs(&ifap) < 0) {
ifnet.if_list.tqe_next && i < 16; return 0;
ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
struct net_stat *ns;
long long last_recv, last_trans;
kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
ns = get_net_stat(ifnet.if_xname, NULL, NULL);
ns->up = 1;
last_recv = ns->recv;
last_trans = ns->trans;
if (ifnet.if_ibytes < ns->last_read_recv) {
ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
ifnet.if_ibytes;
} else {
ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
}
ns->last_read_recv = ifnet.if_ibytes;
if (ifnet.if_obytes < ns->last_read_trans) {
ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
ifnet.if_obytes;
} else {
ns->trans += (ifnet.if_obytes - ns->last_read_trans);
}
ns->last_read_trans = ifnet.if_obytes;
ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
ns->last_read_recv = ifnet.if_ibytes;
ns->trans += (ifnet.if_obytes - ns->last_read_trans);
ns->last_read_trans = ifnet.if_obytes;
ns->recv_speed = (ns->recv - last_recv) / delta;
ns->trans_speed = (ns->trans - last_trans) / delta;
} }
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
if (ifa->ifa_flags & IFF_UP) {
struct ifaddrs *iftmp;
ns->up = 1;
last_recv = ns->recv;
last_trans = ns->trans;
if (ifa->ifa_addr->sa_family != AF_LINK) {
continue;
}
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) {
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;
} }
void update_total_processes() int
update_total_processes(void)
{ {
/* It's easier to use kvm here than sysctl */ int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
size_t size;
int n_processes; if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
warn("sysctl KERN_PROC_ALL failed");
info.procs = 0; return 0;
if (init_kvm() < 0) {
return;
} else {
kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
&n_processes);
} }
info.procs = n_processes; info.procs = (size / sizeof (struct kinfo_proc));
return 0;
} }
void update_running_processes() int
update_running_processes()
{ {
struct kinfo_proc2 *p; int n_processes, i, cnt = 0;
int n_processes; struct kinfo_proc2 *p;
int i, cnt = 0;
info.run_procs = 0; info.run_procs = 0;
if (init_kvm() < 0) { p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
return; &n_processes);
} else {
p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), for (i = 0; i < n_processes; i++)
&n_processes); if (p[i].p_stat == LSRUN ||
for (i = 0; i < n_processes; i++) { p[i].p_stat == LSIDL ||
if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL p[i].p_stat == LSONPROC)
|| p[i].p_stat == LSONPROC) { cnt++;
cnt++;
}
}
}
info.run_procs = cnt; info.run_procs = cnt;
return 0;
} }
struct cpu_load_struct { struct cpu_load_struct {
@ -275,13 +246,18 @@ struct cpu_load_struct fresh = {
long cpu_used, oldtotal, oldused; long cpu_used, oldtotal, oldused;
void update_cpu_usage() int update_cpu_usage(void)
{ {
long used, total; long used, total;
static u_int64_t cp_time[CPUSTATES]; static u_int64_t cp_time[CPUSTATES];
size_t len = sizeof(cp_time); size_t len = sizeof(cp_time);
info.cpu_usage = 0; if ((cpu_setup == 0) || (!info.cpu_usage)) {
get_cpu_count();
cpu_setup = 1;
}
info.cpu_usage[0] = 0;
if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) { if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
warn("cannot get kern.cp_time"); warn("cannot get kern.cp_time");
@ -297,17 +273,19 @@ void update_cpu_usage()
total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
if ((total - oldtotal) != 0) { if ((total - oldtotal) != 0) {
info.cpu_usage = ((double) (used - oldused)) / info.cpu_usage[0] = ((double) (used - oldused)) /
(double) (total - oldtotal); (double) (total - oldtotal);
} else { } else {
info.cpu_usage = 0; info.cpu_usage[0] = 0;
} }
oldused = used; oldused = used;
oldtotal = total; oldtotal = total;
return 0;
} }
void update_load_average() int update_load_average(void)
{ {
double v[3]; double v[3];
@ -316,6 +294,8 @@ void update_load_average()
info.loadavg[0] = (float) v[0]; info.loadavg[0] = (float) v[0];
info.loadavg[1] = (float) v[1]; info.loadavg[1] = (float) v[1];
info.loadavg[2] = (float) v[2]; info.loadavg[2] = (float) v[2];
return 0;
} }
double get_acpi_temperature(int fd) double get_acpi_temperature(int fd)
@ -364,3 +344,155 @@ int get_entropy_poolsize(unsigned int *val)
{ {
return 1; return 1;
} }
/* void */
char get_freq(char *p_client_buffer, size_t client_buffer_size,
const char *p_format, int divisor, unsigned int cpu)
{
int freq = cpu;
if (!p_client_buffer || client_buffer_size <= 0 || !p_format
|| divisor <= 0) {
return 0;
}
size_t size = sizeof(freq);
if (sysctlbyname("machdep.est.frequency.current",
NULL, &size, NULL, 0) == 0) {
sysctlbyname("machdep.est.frequency.current", &freq, &size, NULL, 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);
}
return 1;
}
void get_cpu_count()
{
int cpu_count = 1; /* default to 1 cpu */
info.cpu_count = cpu_count;
info.cpu_usage = malloc(info.cpu_count * sizeof(float));
if (info.cpu_usage == NULL)
warn("malloc");
}
void update_diskio()
{
return; /* XXX: implement? hifi: not sure how */
}
int update_top()
{
proc_find_top(info.cpu, info.memu);
return 0;
}
int comparecpu(const void *a, const void *b)
{
if (((struct process *) a)->amount > ((struct process *) b)->amount) {
return -1;
}
if (((struct process *) a)->amount < ((struct process *) b)->amount) {
return 1;
}
return 0;
}
int comparemem(const void *a, const void *b)
{
if (((struct process *) a)->rss > ((struct process *) b)->rss) {
return -1;
}
if (((struct process *) a)->rss < ((struct process *) b)->rss) {
return 1;
}
return 0;
}
inline void proc_find_top(struct process **cpu, struct process **mem)
{
struct kinfo_proc2 *p;
int n_processes;
int i, j = 0;
struct process *processes;
int mib[2];
u_int total_pages;
int64_t usermem;
int pagesize = getpagesize();
/* we get total pages count again to be sure it is up to date */
mib[0] = CTL_HW;
mib[1] = HW_USERMEM64;
size_t size = sizeof(usermem);
if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
err(EXIT_FAILURE, "error reading usermem");
}
/* translate bytes into page count */
total_pages = usermem / pagesize;
int max_size = sizeof(struct kinfo_proc2);
p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
processes = malloc(n_processes * sizeof(struct process));
for (i = 0; i < n_processes; i++) {
if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
processes[j].pid = p[i].p_pid;
processes[j].name = strndup(p[i].p_comm, text_buffer_size);
processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
processes[j].rss = p[i].p_vm_rssize * pagesize;
processes[j].vsize = p[i].p_vm_vsize;
j++;
}
}
qsort(processes, j - 1, sizeof(struct process), comparemem);
for (i = 0; i < 10; i++) {
struct process *tmp, *ttmp;
tmp = malloc(sizeof(struct process));
memcpy(tmp, &processes[i], sizeof(struct process));
tmp->name = strndup(processes[i].name, text_buffer_size);
ttmp = mem[i];
mem[i] = tmp;
if (ttmp != NULL) {
free(ttmp->name);
free(ttmp);
}
}
qsort(processes, j - 1, sizeof(struct process), comparecpu);
for (i = 0; i < 10; i++) {
struct process *tmp, *ttmp;
tmp = malloc(sizeof(struct process));
memcpy(tmp, &processes[i], sizeof(struct process));
tmp->name = strndup(processes[i].name, text_buffer_size);
ttmp = cpu[i];
cpu[i] = tmp;
if (ttmp != NULL) {
free(ttmp->name);
free(ttmp);
}
}
for (i = 0; i < j; i++) {
free(processes[i].name);
}
free(processes);
}

View File

@ -12,6 +12,8 @@
#include <kvm.h> #include <kvm.h>
#include <nlist.h> #include <nlist.h>
#include <pthread.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/types.h> #include <sys/types.h>
@ -33,4 +35,6 @@
int get_entropy_avail(unsigned int *); int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *); int get_entropy_poolsize(unsigned int *);
kvm_t *kd;
#endif /*NETBSD_H_*/ #endif /*NETBSD_H_*/