diff --git a/configure.ac.in b/configure.ac.in index 03c28c77..2e2f9ed4 100644 --- a/configure.ac.in +++ b/configure.ac.in @@ -62,10 +62,10 @@ case $uname in WANT_DEVSTAT=yes WANT_OPTRESET=yes ;; -# NetBSD*) -# WANT_KVM=yes -# WANT_OSSLIB=yes -# ;; + NetBSD*) + WANT_KVM=yes + WANT_OSSLIB=yes + ;; OpenBSD*) WANT_KVM=yes @@ -87,7 +87,7 @@ esac AM_CONDITIONAL(BUILD_LINUX, test x$uname = xLinux) #AM_CONDITIONAL(BUILD_SOLARIS, test x$uname = xSunOS) 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) BUILD_DATE=$(LANG=en_US LC_ALL=en_US LOCALE=en_US date) @@ -784,7 +784,7 @@ dnl dnl Some headers 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]) AC_CHECK_HEADERS([sys/mount.h], [], [], [#ifdef HAVE_SYS_PARAM_H diff --git a/src/Makefile.am b/src/Makefile.am index 3582af03..03ba9cd0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -119,9 +119,9 @@ endif if BUILD_FREEBSD optional_sources += $(freebsd) endif -#if BUILD_NETBSD -#optional_sources += $(netbsd) -#endif +if BUILD_NETBSD +optional_sources += $(netbsd) +endif if BUILD_OPENBSD optional_sources += $(openbsd) endif diff --git a/src/conky.c b/src/conky.c index 30c7d104..88006fcf 100644 --- a/src/conky.c +++ b/src/conky.c @@ -65,7 +65,7 @@ #include #include #ifdef NCURSES -#include +#include #endif #ifdef XOAP #include @@ -117,8 +117,11 @@ #include "freebsd.h" #elif defined(__OpenBSD__) #include "openbsd.h" +#elif defined(__NetBSD__) +#include "netbsd.h" #endif + #if defined(__FreeBSD_kernel__) #include #endif @@ -784,7 +787,7 @@ void generate_text_internal(char *p, int p_max_size, OBJ(read_tcp) { print_read_tcp(obj, p, p_max_size); } -#ifndef __OpenBSD__ +#if !defined(__OpenBSD__) && !defined(__NetBSD__) OBJ(acpitemp) { 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) { static int ok = 1; if (ok) { -#ifndef __OpenBSD__ +#if !defined(__OpenBSD__) && !defined(__NetBSD__) ok = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.i); #else @@ -846,7 +849,7 @@ void generate_text_internal(char *p, int p_max_size, #endif /* __linux__ */ -#ifndef __OpenBSD__ +#if !defined(__OpenBSD__) && !defined(__NetBSD__) OBJ(acpifan) { get_acpi_fan(p, p_max_size); } @@ -3493,6 +3496,9 @@ static void main_loop(void) next_update_time = get_time(); info.looped = 0; 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) { char buf[max_user_text]; @@ -3503,6 +3509,7 @@ static void main_loop(void) update_interval = update_interval_old; } } +#endif info.looped++; #ifdef SIGNAL_BLOCKING @@ -5680,6 +5687,10 @@ void initialisation(int argc, char **argv) { } pthread_mutex_init(&kvm_proc_mutex, NULL); #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) { int c = getopt_long(argc, argv, getopt_string, longopts, NULL); @@ -5960,9 +5971,11 @@ int main(int argc, char **argv) curl_global_cleanup(); #endif -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) kvm_close(kd); +#ifndef __NetBSD__ pthread_mutex_destroy(&kvm_proc_mutex); +#endif #endif return 0; diff --git a/src/core.c b/src/core.c index 5c0e1f38..a8d1395c 100644 --- a/src/core.c +++ b/src/core.c @@ -65,7 +65,7 @@ #include "top.h" #ifdef NCURSES -#include +#include #endif /* check for OS and include appropriate headers */ @@ -75,6 +75,8 @@ #include "freebsd.h" #elif defined(__OpenBSD__) #include "openbsd.h" +#elif defined(__NetBSD__) +#include "netbsd.h" #endif #include @@ -237,7 +239,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #endif /* __linux__ */ -#ifndef __OpenBSD__ +#if !defined(__NetBSD__) && !defined(__OpenBSD__) END OBJ(acpifan, 0) END OBJ(battery, 0) 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) { root->prev = obj->prev; switch (obj->type) { -#ifndef __OpenBSD__ +#if !defined(__OpenBSD__) && !defined(__NetBSD__) case OBJ_acpitemp: close(data.i); break; @@ -1645,7 +1647,7 @@ void free_text_objects(struct text_object *root, int internal) #endif /* HAVE_LUA */ case OBJ_pre_exec: break; -#ifndef __OpenBSD__ +#if !defined(__OpenBSD__) && !defined(__NetBSD__) case OBJ_battery: free(data.s); break; diff --git a/src/fs.c b/src/fs.c index 65f36f26..db7aa3df 100644 --- a/src/fs.c +++ b/src/fs.c @@ -44,6 +44,11 @@ #include #endif +/* NetBSD, Solaris */ +#ifdef HAVE_SYS_STATVFS_H +#include +#endif + /* freebsd && netbsd */ #ifdef HAVE_SYS_PARAM_H #include @@ -52,7 +57,7 @@ #include #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 #endif @@ -118,6 +123,7 @@ struct fs_stat *prepare_fs_stat(const char *s) static void update_fs_stat(struct fs_stat *fs) { +#ifdef HAVE_SYS_STATFS_H struct statfs64 s; 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) ? */ fs->avail = (long long)s.f_bavail * 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); } else { 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) { -#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; + if (statfs(path, &s) == 0) { +#else + struct statvfs s; + + if (statvfs(path, &s) == 0) { +#endif strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE); } else { NORM_ERR("statfs '%s': %s", path, strerror(errno)); diff --git a/src/netbsd.c b/src/netbsd.c index 6fae5edc..49a39439 100644 --- a/src/netbsd.c +++ b/src/netbsd.c @@ -30,239 +30,210 @@ #include "netbsd.h" #include "net_stat.h" +#include "top.h" +#include +#include +#include -static kvm_t *kd = NULL; -int kd_init = 0, nkd_init = 0; u_int32_t sensvalue; char errbuf[_POSIX2_LINE_MAX]; +static short cpu_setup = 0; -static int init_kvm(void) -{ - if (kd_init) { - return 0; - } +inline void proc_find_top(struct process **cpu, struct process **mem); - kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); - if (kd == NULL) { - 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 +prepare_update(void) { } -void update_uptime() +int +update_uptime(void) { - int mib[2] = { CTL_KERN, KERN_BOOTTIME }; - struct timeval boottime; - time_t now; - int size = sizeof(boottime); + int mib[2] = { CTL_KERN, KERN_BOOTTIME }; + struct timeval boottime; + time_t now; + size_t size; - if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) - && (boottime.tv_sec != 0)) { + size = sizeof(boottime); + + if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) { + warn("sysctl kern.boottime failed"); + info.uptime = 0; + } else { time(&now); info.uptime = now - boottime.tv_sec; - } else { - warn("could not get uptime"); - info.uptime = 0; } -} -int check_mount(char *s) -{ - /* stub */ return 0; } -void update_meminfo() +/* checks is mp is a mounted mountpoint */ +int +check_mount(char *mp) { - int mib[] = { CTL_VM, VM_UVMEXP2 }; - int total_pages, inactive_pages, free_pages; - int swap_avail, swap_free; - const int pagesize = getpagesize(); - struct uvmexp_sysctl uvmexp; - size_t size = sizeof(uvmexp); + int nbmount, i; + struct statvfs *mntbuf; - if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) { - warn("could not get memory info"); - return; - } + nbmount = getmntinfo(&mntbuf, MNT_NOWAIT); - total_pages = uvmexp.npages; - free_pages = uvmexp.free; - inactive_pages = uvmexp.inactive; - - 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; + for (i = 0; i < nbmount; i++) { + if (strcmp(mntbuf[i].f_mntonname, mp) == 0) { + return 1; } } - if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead, - sizeof(ifhead)) < 0) { - warn("cannot kvm_read"); - return; + return 0; +} + +/* 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 */ delta = current_update_time - last_update_time; if (delta <= 0.0001) { - return; + return 0; } - for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first; - ifnet.if_list.tqe_next && i < 16; - 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; + if (getifaddrs(&ifap) < 0) { + return 0; } + + 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; - - info.procs = 0; - - if (init_kvm() < 0) { - return; - } else { - kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), - &n_processes); + if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) { + warn("sysctl KERN_PROC_ALL failed"); + return 0; } - 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; - int i, cnt = 0; + int n_processes, i, cnt = 0; + struct kinfo_proc2 *p; info.run_procs = 0; - if (init_kvm() < 0) { - return; - } else { - p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), - &n_processes); - for (i = 0; i < n_processes; i++) { - if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL - || p[i].p_stat == LSONPROC) { - cnt++; - } - } - } + p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), + &n_processes); + + for (i = 0; i < n_processes; i++) + if (p[i].p_stat == LSRUN || + p[i].p_stat == LSIDL || + p[i].p_stat == LSONPROC) + cnt++; info.run_procs = cnt; + + return 0; } struct cpu_load_struct { @@ -275,13 +246,18 @@ struct cpu_load_struct fresh = { long cpu_used, oldtotal, oldused; -void update_cpu_usage() +int update_cpu_usage(void) { long used, total; static u_int64_t cp_time[CPUSTATES]; 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) { 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]; if ((total - oldtotal) != 0) { - info.cpu_usage = ((double) (used - oldused)) / - (double) (total - oldtotal); + info.cpu_usage[0] = ((double) (used - oldused)) / + (double) (total - oldtotal); } else { - info.cpu_usage = 0; + info.cpu_usage[0] = 0; } oldused = used; oldtotal = total; + + return 0; } -void update_load_average() +int update_load_average(void) { double v[3]; @@ -316,6 +294,8 @@ void update_load_average() info.loadavg[0] = (float) v[0]; info.loadavg[1] = (float) v[1]; info.loadavg[2] = (float) v[2]; + + return 0; } double get_acpi_temperature(int fd) @@ -364,3 +344,155 @@ int get_entropy_poolsize(unsigned int *val) { 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); +} diff --git a/src/netbsd.h b/src/netbsd.h index 908bf55e..edae8682 100644 --- a/src/netbsd.h +++ b/src/netbsd.h @@ -12,6 +12,8 @@ #include #include +#include + #include #include #include @@ -33,4 +35,6 @@ int get_entropy_avail(unsigned int *); int get_entropy_poolsize(unsigned int *); +kvm_t *kd; + #endif /*NETBSD_H_*/