htop/freebsd/FreeBSDProcessList.c

82 lines
2.0 KiB
C
Raw Normal View History

2014-11-27 18:27:34 +00:00
/*
2014-11-27 19:44:55 +00:00
htop - FreeBSDProcessList.c
2014-11-27 18:27:34 +00:00
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "ProcessList.h"
2014-11-27 19:44:55 +00:00
#include "FreeBSDProcessList.h"
2014-11-27 18:27:34 +00:00
2014-11-27 19:44:55 +00:00
#include <unistd.h>
2014-11-27 18:27:34 +00:00
#include <stdlib.h>
2014-11-27 19:44:55 +00:00
#include <sys/types.h>
2014-11-27 18:27:34 +00:00
#include <sys/sysctl.h>
2014-11-27 19:44:55 +00:00
#include <fcntl.h>
2014-11-27 18:27:34 +00:00
/*{
2014-11-27 19:44:55 +00:00
#include <kvm.h>
typedef struct FreeBSDProcessList_ {
ProcessList super;
kvm_t* kd;
} FreeBSDProcessList;
2014-11-27 18:27:34 +00:00
}*/
2014-11-27 19:44:55 +00:00
static int MIB_vm_stats_vm_v_wire_count[4];
static int MIB_hw_physmem[2];
2014-11-27 18:27:34 +00:00
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
2014-11-27 19:44:55 +00:00
FreeBSDProcessList* this = calloc(1, sizeof(FreeBSDProcessList));
ProcessList* pl = (ProcessList*) this;
ProcessList_init((ProcessList*)this, usersTable, pidWhiteList);
2014-11-27 18:27:34 +00:00
int cpus = 1;
size_t sizeof_cpus = sizeof(cpus);
int err = sysctlbyname("hw.ncpu", &cpus, &sizeof_cpus, NULL, 0);
if (err) cpus = 1;
2014-11-27 19:44:55 +00:00
pl->cpuCount = MAX(cpus, 1);
pl->cpus = realloc(pl->cpus, cpus * sizeof(CPUData));
2014-11-27 18:27:34 +00:00
for (int i = 0; i < cpus; i++) {
2014-11-27 19:44:55 +00:00
pl->cpus[i].totalTime = 1;
pl->cpus[i].totalPeriod = 1;
2014-11-27 18:27:34 +00:00
}
2014-11-27 19:44:55 +00:00
size_t len = 4;
sysctlnametomib("vm.stats.vm.v_wire_count", MIB_vm_stats_vm_v_wire_count, &len);
len = 2;
sysctlnametomib("hw.physmem", MIB_hw_physmem, &len);
2014-11-27 18:27:34 +00:00
2014-11-27 19:44:55 +00:00
return (ProcessList*) this;
}
static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) {
const FreeBSDProcessList* fpl = (FreeBSDProcessList*) pl;
unsigned long long int swapFree = 0;
size_t len = sizeof(pl->totalMem);
sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0);
pl->totalMem /= 1024;
sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(pl->usedMem), &len, NULL, 0);
pl->usedMem *= PAGE_SIZE / 1024;
pl->freeMem = pl->totalMem - pl->usedMem;
pl->sharedMem = 0;
pl->buffersMem = 0;
pl->cachedMem = 0;
pl->totalSwap = 0;
swapFree = 0;
pl->usedSwap = pl->totalSwap - swapFree;
2014-11-27 18:27:34 +00:00
}
void ProcessList_scan(ProcessList* this) {
(void) this;
2014-11-27 19:44:55 +00:00
FreeBSDProcessList_scanMemoryInfo(this);
2014-11-27 18:27:34 +00:00
// stub!
}