FreeBSDProcessList.c 2.02 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - FreeBSDProcessList.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
4
5
6
7
8
(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"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
#include "FreeBSDProcessList.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10

Hisham Muhammad's avatar
Hisham Muhammad committed
11
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
12
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
13
#include <sys/types.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <sys/sysctl.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
#include <fcntl.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
18

/*{

Hisham Muhammad's avatar
Hisham Muhammad committed
19
20
21
22
23
24
25
#include <kvm.h>

typedef struct FreeBSDProcessList_ {
   ProcessList super;
   kvm_t* kd;
} FreeBSDProcessList;

Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
28
29
30
static int MIB_vm_stats_vm_v_wire_count[4];
static int MIB_hw_physmem[2];

Hisham Muhammad's avatar
Hisham Muhammad committed
31
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
34
   FreeBSDProcessList* this = calloc(1, sizeof(FreeBSDProcessList));
   ProcessList* pl = (ProcessList*) this;
   ProcessList_init((ProcessList*)this, usersTable, pidWhiteList);
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37
38
39

   int cpus = 1;
   size_t sizeof_cpus = sizeof(cpus);
   int err = sysctlbyname("hw.ncpu", &cpus, &sizeof_cpus, NULL, 0);
   if (err) cpus = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
   pl->cpuCount = MAX(cpus, 1);
   pl->cpus = realloc(pl->cpus, cpus * sizeof(CPUData));
Hisham Muhammad's avatar
Hisham Muhammad committed
42
43

   for (int i = 0; i < cpus; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
44
45
      pl->cpus[i].totalTime = 1;
      pl->cpus[i].totalPeriod = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
46
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49
50
51
   
   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);
Hisham Muhammad's avatar
Hisham Muhammad committed
52

Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   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;
Hisham Muhammad's avatar
Hisham Muhammad committed
73
74
75
76
}

void ProcessList_scan(ProcessList* this) {
   (void) this;
Hisham Muhammad's avatar
Hisham Muhammad committed
77
78
79

   FreeBSDProcessList_scanMemoryInfo(this);

Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
   // stub!
}