NetBSD port by Thomas Runge.

This commit is contained in:
gfk committed 2003-08-12 02:35:01 +00:00
1 parent d5556d1388
commit c828ddc45b
9 files changed
+346 -25

No files matched your search

+134 -15
View File
@@ -26,6 +26,17 @@
# include <sys/procfs.h>
#endif
#endif
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <uvm/uvm_extern.h>
#include <errno.h>
#include <kvm.h>
static int pageshift;
#define pagetok(size) ((size) << pageshift)
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
static void netbsd_get_procs(LinkedList *procs);
#endif
struct meminfo {
@@ -40,16 +51,7 @@ static void
get_mem_info (struct meminfo *result)
{
#ifndef SOLARIS
reread (meminfo_fd, "get_meminfo:");
result[0].total = getentry ("MemTotal:", buffer);
result[0].free = getentry ("MemFree:", buffer);
result[0].shared = getentry ("MemShared:", buffer);
result[0].buffers = getentry ("Buffers:", buffer);
result[0].cache = getentry ("Cached:", buffer);
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#else
#ifdef SOLARIS
#define MAXSTRSIZE 80
swaptbl_t *s=NULL;
int i, n, num;
@@ -105,13 +107,65 @@ again:
result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024;
result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024;
}
#else
#ifdef __NetBSD__
size_t size;
int mib[2];
struct uvmexp_sysctl uvmexp;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2;
size = sizeof(uvmexp);
if(sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl vm.uvmexp2 failed: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
/* memory */
result[0].total = pagetok(uvmexp.npages);
result[0].free = pagetok(uvmexp.free);
/* not really */
result[0].shared = pagetok(uvmexp.wired);
result[0].buffers = pagetok(uvmexp.execpages);
result[0].cache = pagetok(uvmexp.filepages);
/* swap */
result[1].total = uvmexp.pagesize * uvmexp.swpages;
result[1].free = uvmexp.pagesize * uvmexp.swpginuse;
result[1].free = result[1].total - result[1].free;
#else
reread (meminfo_fd, "get_meminfo:");
result[0].total = getentry ("MemTotal:", buffer);
result[0].free = getentry ("MemFree:", buffer);
result[0].shared = getentry ("MemShared:", buffer);
result[0].buffers = getentry ("Buffers:", buffer);
result[0].cache = getentry ("Cached:", buffer);
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#endif
#endif
}
int
mem_init ()
{
#ifndef SOLARIS
#ifdef SOLARIS
#else
#ifdef __NetBSD__
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
#else
if (!meminfo_fd) {
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
if (meminfo_fd < 0) {
@@ -119,7 +173,7 @@ mem_init ()
exit (1);
}
}
#endif
#endif
return 0;
}
@@ -127,12 +181,16 @@ mem_init ()
int
mem_close ()
{
#ifndef SOLARIS
#ifdef SOLARIS
#else
#ifdef __NetBSD__
#else
if (meminfo_fd)
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
meminfo_fd = 0;
#endif
#endif
return 0;
}
@@ -330,6 +388,7 @@ sort_procs (void *a, void *b)
int
mem_top_screen (int rep, int display)
{
#ifndef __NetBSD__
// Much of this code was ripped from "gmemusage"
char buf[128];
@@ -344,8 +403,9 @@ mem_top_screen (int rep, int display)
*NameLine = "Name:", *VmSizeLine = "VmSize:", *VmRSSLine = "VmRSS", *VmDataLine = "VmData", *VmStkLine = "VmStk", *VmExeLine = "VmExe";
const int
NameLineLen = strlen (NameLine), VmSizeLineLen = strlen (VmSizeLine), VmDataLineLen = strlen (VmDataLine), VmStkLineLen = strlen (VmStkLine), VmExeLineLen = strlen (VmExeLine), VmRSSLineLen = strlen (VmRSSLine);
int threshold = 400, unique;
#endif /* __NetBSD__ */
int i;
proc_mem_info *p;
LinkedList *procs;
@@ -377,6 +437,9 @@ mem_top_screen (int rep, int display)
return -1;
}
#ifdef __NetBSD__
netbsd_get_procs(procs);
#else
if ((proc = opendir ("/proc")) == NULL) {
fprintf (stderr, "mem_top_screen: unable to open /proc");
perror ("");
@@ -476,6 +539,8 @@ mem_top_screen (int rep, int display)
}
closedir (proc);
#endif /* __NetBSD__ */
// Now, print some info...
LL_Rewind (procs);
LL_Sort (procs, sort_procs);
@@ -501,7 +566,9 @@ mem_top_screen (int rep, int display)
LL_Next (procs);
}
#ifndef __NetBSD__
end: // Ack! I hate using labels!
#endif
// Now clean it all up...
LL_Rewind (procs);
do {
@@ -515,4 +582,56 @@ mem_top_screen (int rep, int display)
return 0;
} // End mem_top_screen()
}
#ifdef __NetBSD__
static void netbsd_get_procs(LinkedList *procs)
{
kvm_t *kvmd = NULL;
struct kinfo_proc2 *kprocs = NULL;
struct kinfo_proc2 *pp;
int nproc, i;
proc_mem_info *p;
if((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL
)
{
fprintf(stderr, "kvm_openfiles\n");
exit(EXIT_FAILURE);
}
kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if(kprocs == NULL)
{
fprintf(stderr, "kvm_getproc2\n");
exit(EXIT_FAILURE);
}
for(pp = kprocs, i = 0; i < nproc; pp++, i++)
{
/*
printf("proc: %s (%ld) size: %ld K\n", pp->p_comm,
(long) (pp->p_pid),
(long) (PROCSIZE(pp) << pageshift));
*/
p = malloc(sizeof(proc_mem_info));
if(!p)
{
fprintf(stderr, "mem_top_screen: Error allocating process entry\n");
kvm_close(kvmd);
exit(EXIT_FAILURE);
}
strncpy(p->name, pp->p_comm, 15);
p->name[15] = '\0';
p->totl = (PROCSIZE(pp) << pageshift);
p->number = pp->p_pid;
/* TODO: Check for errors here? */
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
}
#endif /* __NetBSD__ */