aboutsummaryrefslogtreecommitdiffstats
path: root/openbsd/Platform.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2020-08-27 07:48:10 +0200
committerDaniel Lange <DLange@git.local>2020-08-27 07:48:10 +0200
commitf3147ea2d1598914c2db53e8cfb34c8ff81e2ff4 (patch)
tree3ee82b2af2ab3d38b6e4b07f3994516aac72f742 /openbsd/Platform.c
parentdf568a576f7b44ac5a2b9b7222c7f39d9932f626 (diff)
downloaddebian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.tar.gz
debian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.tar.bz2
debian_htop-f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4.zip
New upstream version 3.0.0upstream/3.0.0
Diffstat (limited to 'openbsd/Platform.c')
-rw-r--r--openbsd/Platform.c157
1 files changed, 74 insertions, 83 deletions
diff --git a/openbsd/Platform.c b/openbsd/Platform.c
index 01b6c47..0f5279e 100644
--- a/openbsd/Platform.c
+++ b/openbsd/Platform.c
@@ -20,9 +20,8 @@ in the source distribution for its full text.
#include "OpenBSDProcess.h"
#include "OpenBSDProcessList.h"
-#include <sys/sched.h>
-#include <uvm/uvmexp.h>
#include <sys/param.h>
+#include <sys/sysctl.h>
#include <sys/swap.h>
#include <unistd.h>
@@ -30,10 +29,13 @@ in the source distribution for its full text.
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
-#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <math.h>
/*{
#include "Action.h"
@@ -44,54 +46,6 @@ extern ProcessFieldData Process_fields[];
}*/
-#define MAXCPU 256
-// XXX: probably should be a struct member
-static int64_t old_v[MAXCPU][5];
-
-/*
- * Copyright (c) 1984, 1989, William LeFebvre, Rice University
- * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
- *
- * Taken directly from OpenBSD's top(1).
- *
- * percentages(cnt, out, new, old, diffs) - calculate percentage change
- * between array "old" and "new", putting the percentages in "out".
- * "cnt" is size of each array and "diffs" is used for scratch space.
- * The array "old" is updated on each call.
- * The routine assumes modulo arithmetic. This function is especially
- * useful on BSD machines for calculating cpu state percentages.
- */
-static int percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs) {
- int64_t change, total_change, *dp, half_total;
- int i;
-
- /* initialization */
- total_change = 0;
- dp = diffs;
-
- /* calculate changes for each state and the overall change */
- for (i = 0; i < cnt; i++) {
- if ((change = *new - *old) < 0) {
- /* this only happens when the counter wraps */
- change = INT64_MAX - *old + *new;
- }
- total_change += (*dp++ = change);
- *old++ = *new++;
- }
-
- /* avoid divide by zero potential */
- if (total_change == 0)
- total_change = 1;
-
- /* calculate percentages based on overall change, rounding up */
- half_total = total_change / 2l;
- for (i = 0; i < cnt; i++)
- *out++ = ((*diffs++ * 1000 + half_total) / total_change);
-
- /* return the total in case the caller wants to use it */
- return (total_change);
-}
-
ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
int Platform_numberOfFields = LAST_PROCESSFIELD;
@@ -197,43 +151,38 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
int Platform_getMaxPid() {
// this is hard-coded in sys/sys/proc.h - no sysctl exists
- return 32766;
+ return 99999;
}
double Platform_setCPUValues(Meter* this, int cpu) {
- int i;
- double perc;
-
- OpenBSDProcessList* pl = (OpenBSDProcessList*) this->pl;
- CPUData* cpuData = &(pl->cpus[cpu]);
- int64_t new_v[CPUSTATES], diff_v[CPUSTATES], scratch_v[CPUSTATES];
+ const OpenBSDProcessList* pl = (OpenBSDProcessList*) this->pl;
+ const CPUData* cpuData = &(pl->cpus[cpu]);
+ double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod;
+ double totalPercent;
double *v = this->values;
- size_t size = sizeof(double) * CPUSTATES;
- int mib[] = { CTL_KERN, KERN_CPTIME2, cpu-1 };
- if (sysctl(mib, 3, new_v, &size, NULL, 0) == -1) {
- return 0.;
- }
-
- // XXX: why?
- cpuData->totalPeriod = 1;
-
- percentages(CPUSTATES, diff_v, new_v,
- (int64_t *)old_v[cpu-1], scratch_v);
- for (i = 0; i < CPUSTATES; i++) {
- old_v[cpu-1][i] = new_v[i];
- v[i] = diff_v[i] / 10.;
- }
-
- Meter_setItems(this, 4);
-
- perc = v[0] + v[1] + v[2] + v[3];
-
- if (perc <= 100. && perc >= 0.) {
- return perc;
+ v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0;
+ v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0;
+ if (this->pl->settings->detailedCPUTime) {
+ v[CPU_METER_KERNEL] = cpuData->sysPeriod / total * 100.0;
+ v[CPU_METER_IRQ] = cpuData->intrPeriod / total * 100.0;
+ v[CPU_METER_SOFTIRQ] = 0.0;
+ v[CPU_METER_STEAL] = 0.0;
+ v[CPU_METER_GUEST] = 0.0;
+ v[CPU_METER_IOWAIT] = 0.0;
+ v[CPU_METER_FREQUENCY] = -1;
+ Meter_setItems(this, 8);
+ totalPercent = v[0]+v[1]+v[2]+v[3];
} else {
- return 0.;
+ v[2] = cpuData->sysAllPeriod / total * 100.0;
+ v[3] = 0.0; // No steal nor guest on OpenBSD
+ totalPercent = v[0]+v[1]+v[2];
+ Meter_setItems(this, 4);
}
+
+ totalPercent = CLAMP(totalPercent, 0.0, 100.0);
+ if (isnan(totalPercent)) totalPercent = 0.0;
+ return totalPercent;
}
void Platform_setMemoryValues(Meter* this) {
@@ -294,6 +243,48 @@ void Platform_setTasksValues(Meter* this) {
}
char* Platform_getProcessEnv(pid_t pid) {
- // TODO
- return NULL;
+ char errbuf[_POSIX2_LINE_MAX];
+ char *env;
+ char **ptr;
+ int count;
+ kvm_t *kt;
+ struct kinfo_proc *kproc;
+ size_t capacity = 4096, size = 0;
+
+ if ((kt = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL)
+ return NULL;
+
+ if ((kproc = kvm_getprocs(kt, KERN_PROC_PID, pid,
+ sizeof(struct kinfo_proc), &count)) == NULL) {\
+ (void) kvm_close(kt);
+ return NULL;
+ }
+
+ if ((ptr = kvm_getenvv(kt, kproc, 0)) == NULL) {
+ (void) kvm_close(kt);
+ return NULL;
+ }
+
+ env = xMalloc(capacity);
+ for (char **p = ptr; *p; p++) {
+ size_t len = strlen(*p) + 1;
+
+ if (size + len > capacity) {
+ capacity *= 2;
+ env = xRealloc(env, capacity);
+ }
+
+ strlcpy(env + size, *p, len);
+ size += len;
+ }
+
+ if (size < 2 || env[size - 1] || env[size - 2]) {
+ if (size + 2 < capacity)
+ env = xRealloc(env, capacity + 2);
+ env[size] = 0;
+ env[size+1] = 0;
+ }
+
+ (void) kvm_close(kt);
+ return env;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy