FreeBSDProcessList.c 2.96 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
#include <kvm.h>

21
22
23
24
25
typedef struct CPUData_ {
   unsigned long long int totalTime;
   unsigned long long int totalPeriod;
} CPUData;

Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
typedef struct FreeBSDProcessList_ {
   ProcessList super;
   kvm_t* kd;
29
30
31

   CPUData* cpus;

Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
} FreeBSDProcessList;

Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
36
static int MIB_vm_stats_vm_v_wire_count[4];
Hisham Muhammad's avatar
Hisham Muhammad committed
37
static int MIB_vm_stats_vm_v_cache_count[4];
Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
static int MIB_hw_physmem[2];

Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
static int pageSizeKb;

42
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
   FreeBSDProcessList* fpl = calloc(1, sizeof(FreeBSDProcessList));
   ProcessList* pl = (ProcessList*) fpl;
45
   ProcessList_init(pl, usersTable, pidWhiteList, userId);
Hisham Muhammad's avatar
Hisham Muhammad committed
46
47
48
49
50

   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
51
   pl->cpuCount = MAX(cpus, 1);
52
   fpl->cpus = realloc(fpl->cpus, cpus * sizeof(CPUData));
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54

   for (int i = 0; i < cpus; i++) {
55
56
      fpl->cpus[i].totalTime = 1;
      fpl->cpus[i].totalPeriod = 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
57
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
58
   
Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
61
62
63
64
65
66
67
68
69
   size_t len;
   len = 4; sysctlnametomib("vm.stats.vm.v_wire_count",  MIB_vm_stats_vm_v_wire_count, &len);
   len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len);
   len = 2; sysctlnametomib("hw.physmem",                MIB_hw_physmem, &len);
   pageSizeKb = PAGE_SIZE_KB;   
   
   fpl->kd = kvm_open(NULL, "/dev/null", NULL, 0, NULL);
   assert(fpl->kd);

   return pl;
}
Hisham Muhammad's avatar
Hisham Muhammad committed
70

Hisham Muhammad's avatar
Hisham Muhammad committed
71
72
73
74
75
76
void ProcessList_delete(ProcessList* this) {
   const FreeBSDProcessList* fpl = (FreeBSDProcessList*) this;
   if (fpl->kd) kvm_close(fpl->kd);
  
   ProcessList_done(this);
   free(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
77
78
79
80
81
82
83
84
85
}

static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) {
   const FreeBSDProcessList* fpl = (FreeBSDProcessList*) pl;
   
   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);
Hisham Muhammad's avatar
Hisham Muhammad committed
86
   pl->usedMem *= pageSizeKb;
Hisham Muhammad's avatar
Hisham Muhammad committed
87
   pl->freeMem = pl->totalMem - pl->usedMem;
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
   sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0);
   pl->cachedMem *= pageSizeKb;
Hisham Muhammad's avatar
Hisham Muhammad committed
90
   
Hisham Muhammad's avatar
Hisham Muhammad committed
91
92
   struct kvm_swap swap[16];
   int nswap = kvm_getswapinfo(fpl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
93
   pl->totalSwap = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
94
95
96
97
98
99
100
101
102
103
   pl->usedSwap = 0;
   for (int i = 0; i < nswap; i++) {
      pl->totalSwap += swap[i].ksw_total;
      pl->usedSwap += swap[i].ksw_used;
   }
   pl->totalSwap *= pageSizeKb;
   pl->usedSwap *= pageSizeKb;
   
   pl->sharedMem = 0;  // currently unused
   pl->buffersMem = 0; // not exposed to userspace
Hisham Muhammad's avatar
Hisham Muhammad committed
104
105
106
107
}

void ProcessList_scan(ProcessList* this) {
   (void) this;
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110

   FreeBSDProcessList_scanMemoryInfo(this);

Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
   // stub!
}