OpenBSDProcessList.c 7.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
htop - OpenBSDProcessList.c
(C) 2014 Hisham H. Muhammad
(C) 2015 Michael McConville
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "ProcessList.h"
#include "OpenBSDProcessList.h"
#include "OpenBSDProcess.h"

#include <unistd.h>
#include <stdlib.h>
Michael McConville's avatar
Michael McConville committed
15
#include <err.h>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <fcntl.h>
#include <string.h>
#include <sys/resource.h>

/*{

#include <kvm.h>

typedef struct CPUData_ {
   unsigned long long int totalTime;
   unsigned long long int totalPeriod;
} CPUData;

typedef struct OpenBSDProcessList_ {
   ProcessList super;
   kvm_t* kd;

   CPUData* cpus;

} OpenBSDProcessList;

}*/

45
46
47
48
#ifndef CLAMP
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
#endif

49
50
51
52
53
54
static int pageSizeKb;
static long fscale;

ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
   int mib[] = { CTL_HW, HW_NCPU };
   int fmib[] = { CTL_KERN, KERN_FSCALE };
55
   int i, e;
56
57
   OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList));
   ProcessList* pl = (ProcessList*) opl;
58
   size_t size = sizeof(pl->cpuCount);
59

60
   ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId);
61
62
63
64
   e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0);
   if (e == -1 || pl->cpuCount < 1) {
      pl->cpuCount = 1;
   }
65
   opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData));
66
67
68

   size = sizeof(fscale);
   if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0)
69
      err(1, "fscale sysctl call failed");
70
71

   for (i = 0; i < pl->cpuCount; i++) {
72
73
      opl->cpus[i].totalTime = 1;
      opl->cpus[i].totalPeriod = 1;
74
   }
75

76
77
78
   pageSizeKb = PAGE_SIZE_KB;

   // XXX: last arg should eventually be an errbuf
79
80
   opl->kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
   assert(opl->kd);
81
82
83
84
85

   return pl;
}

void ProcessList_delete(ProcessList* this) {
86
87
   const OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
   if (opl->kd) kvm_close(opl->kd);
88

89
   free(opl->cpus);
90

91
92
93
94
95
96
97
98
99
100
   ProcessList_done(this);
   free(this);
}

static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) {
   static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
   struct uvmexp uvmexp;
   size_t size = sizeof(uvmexp);

   if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
101
      err(1, "uvmexp sysctl call failed");
102
103
104
105
106
107
   }

   //kb_pagesize = uvmexp.pagesize / 1024;
   pl->usedMem = uvmexp.active * pageSizeKb;
   pl->totalMem = uvmexp.npages * pageSizeKb;

108
   /*
109
   const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl;
110

111
112
113
114
115
116
117
118
   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 *= pageSizeKb;
   pl->freeMem = pl->totalMem - pl->usedMem;
   sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0);
   pl->cachedMem *= pageSizeKb;
119

120
   struct kvm_swap swap[16];
121
   int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0);
122
123
124
125
126
127
128
129
   pl->totalSwap = 0;
   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;
130

131
132
133
134
135
136
   pl->sharedMem = 0;  // currently unused
   pl->buffersMem = 0; // not exposed to userspace
   */
}

char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) {
Michael McConville's avatar
Michael McConville committed
137
138
139
   char *s, *buf, **arg;
   size_t cpsz, len = 0, n;
   int i;
140

141
142
143
144
   /*
    * We attempt to fall back to just the command name (argv[0]) if we
    * fail to construct the full command at any point.
    */
Michael McConville's avatar
Michael McConville committed
145
146
   arg = kvm_getargv(kd, kproc, 500);
   if (arg == NULL) {
147
148
      if ((s = strdup(kproc->p_comm)) == NULL) {
         err(1, NULL);
149
150
      }
      return s;
Michael McConville's avatar
Michael McConville committed
151
152
153
154
   }
   for (i = 0; arg[i] != NULL; i++) {
      len += strlen(arg[i]) + 1;
   }
155
156
157
158
159
160
   if ((buf = s = malloc(len)) == NULL) {
      if ((s = strdup(kproc->p_comm)) == NULL) {
         err(1, NULL);
      }
      return s;
   }
Michael McConville's avatar
Michael McConville committed
161
162
163
164
   for (i = 0; arg[i] != NULL; i++) {
      n = strlcpy(buf, arg[i], (s + len) - buf);
      buf += n;
      if (i == 0) {
165
         *basenameEnd = n;
Michael McConville's avatar
Michael McConville committed
166
167
      }
      *buf = ' ';
168
      buf++;
Michael McConville's avatar
Michael McConville committed
169
170
171
   }
   *(buf - 1) = '\0';
   return s;
172
173
174
175
176
177
}

/*
 * Taken from OpenBSD's ps(1).
 */
double getpcpu(const struct kinfo_proc *kp) {
178
179
   if (fscale == 0)
      return (0.0);
180

181
#define   fxtofl(fixpt)   ((double)(fixpt) / fscale)
182

183
   return (100.0 * fxtofl(kp->p_pctcpu));
184
185
186
}

void ProcessList_goThroughEntries(ProcessList* this) {
187
   OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
188
189
190
191
192
193
194
195
196
   Settings* settings = this->settings;
   bool hideKernelThreads = settings->hideKernelThreads;
   bool hideUserlandThreads = settings->hideUserlandThreads;
   struct kinfo_proc* kproc;
   bool preExisting;
   Process* proc;
   OpenBSDProcess* fp;
   int count = 0;
   int i;
197

198
   OpenBSDProcessList_scanMemoryInfo(this);
199

200
   // use KERN_PROC_KTHREAD to also include kernel threads
201
   struct kinfo_proc* kprocs = kvm_getprocs(opl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
202
   //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count);
203

204
205
   for (i = 0; i < count; i++) {
      kproc = &kprocs[i];
206

207
208
209
210
211
      preExisting = false;
      proc = ProcessList_getProcess(this, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new);
      fp = (OpenBSDProcess*) proc;

      proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc))
212
                  || (hideUserlandThreads && Process_isUserlandThread(proc)));
213

214
215
216
217
218
219
220
221
222
223
224
      if (!preExisting) {
         proc->ppid = kproc->p_ppid;
         proc->tpgid = kproc->p_tpgid;
         proc->tgid = kproc->p_pid;
         proc->session = kproc->p_sid;
         proc->tty_nr = kproc->p_tdev;
         proc->pgrp = kproc->p__pgid;
         proc->st_uid = kproc->p_uid;
         proc->starttime_ctime = kproc->p_ustart_sec;
         proc->user = UsersTable_getRef(this->usersTable, proc->st_uid);
         ProcessList_add((ProcessList*)this, proc);
225
         proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset);
226
227
228
      } else {
         if (settings->updateProcessNames) {
            free(proc->comm);
229
            proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset);
230
231
232
233
234
         }
      }

      proc->m_size = kproc->p_vm_dsize;
      proc->m_resident = kproc->p_vm_rssize;
235
      proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0;
236
      proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->cpuCount*100.0);
237
238
      //proc->nlwp = kproc->p_numthreads;
      //proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10);
239
240
241
      proc->nice = kproc->p_nice - 20;
      proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000);
      proc->time *= 100;
242
243
244
      proc->priority = kproc->p_priority - PZERO;

      switch (kproc->p_stat) {
245
246
247
248
249
250
251
252
         case SIDL:    proc->state = 'I'; break;
         case SRUN:    proc->state = 'R'; break;
         case SSLEEP:  proc->state = 'S'; break;
         case SSTOP:   proc->state = 'T'; break;
         case SZOMB:   proc->state = 'Z'; break;
         case SDEAD:   proc->state = 'D'; break;
         case SONPROC: proc->state = 'P'; break;
         default:      proc->state = '?';
253
254
255
256
257
      }

      if (Process_isKernelThread(proc)) {
         this->kernelThreads++;
      }
258

259
      this->totalTasks++;
260
261
      // SRUN ('R') means runnable, not running
      if (proc->state == 'P') {
262
         this->runningTasks++;
263
      }
264
265
266
      proc->updated = true;
   }
}