summaryrefslogtreecommitdiffstats
path: root/dragonflybsd
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:00:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:00:29 +0000
commit52e25213825024b8bb446eb26b03bedc9d5c2103 (patch)
treeda70bf44b2423f6f8e9a070c063fed79d190b489 /dragonflybsd
parentInitial commit. (diff)
downloadhtop-52e25213825024b8bb446eb26b03bedc9d5c2103.tar.xz
htop-52e25213825024b8bb446eb26b03bedc9d5c2103.zip
Adding upstream version 3.2.2.upstream/3.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dragonflybsd')
-rw-r--r--dragonflybsd/DragonFlyBSDProcess.c110
-rw-r--r--dragonflybsd/DragonFlyBSDProcess.h32
-rw-r--r--dragonflybsd/DragonFlyBSDProcessList.c615
-rw-r--r--dragonflybsd/DragonFlyBSDProcessList.h64
-rw-r--r--dragonflybsd/Platform.c262
-rw-r--r--dragonflybsd/Platform.h120
-rw-r--r--dragonflybsd/ProcessField.h19
7 files changed, 1222 insertions, 0 deletions
diff --git a/dragonflybsd/DragonFlyBSDProcess.c b/dragonflybsd/DragonFlyBSDProcess.c
new file mode 100644
index 0000000..7ff9245
--- /dev/null
+++ b/dragonflybsd/DragonFlyBSDProcess.c
@@ -0,0 +1,110 @@
+/*
+htop - dragonflybsd/DragonFlyBSDProcess.c
+(C) 2015 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "dragonflybsd/DragonFlyBSDProcess.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#include "CRT.h"
+
+#include "dragonflybsd/Platform.h"
+
+
+const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
+ [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
+ [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, },
+ [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
+ [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping (<20s), I Idle, Q Queued for Run, R running, D disk, Z zombie, T traced, W paging, B Blocked, A AskedPage, C Core, J Jailed)", .flags = 0, },
+ [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, },
+ [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, },
+ [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, },
+ [TTY] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, },
+ [TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, },
+ [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
+ [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
+ [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
+ [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
+ [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
+ [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
+ [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
+ [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
+ [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, .defaultSortDesc = true, },
+ [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
+ [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = " CPU%", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, .defaultSortDesc = true, .autoWidth = true, },
+ [PERCENT_NORM_CPU] = { .name = "PERCENT_NORM_CPU", .title = "NCPU%", .description = "Normalized percentage of the CPU time the process used in the last sampling (normalized by cpu count)", .flags = 0, .defaultSortDesc = true, .autoWidth = true, },
+ [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, .defaultSortDesc = true, },
+ [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
+ [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
+ [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
+ [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
+ [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
+ [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
+ [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
+ [JID] = { .name = "JID", .title = "JID", .description = "Jail prison ID", .flags = 0, .pidColumn = true, },
+ [JAIL] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = 0, },
+};
+
+Process* DragonFlyBSDProcess_new(const Settings* settings) {
+ DragonFlyBSDProcess* this = xCalloc(1, sizeof(DragonFlyBSDProcess));
+ Object_setClass(this, Class(DragonFlyBSDProcess));
+ Process_init(&this->super, settings);
+ return &this->super;
+}
+
+void Process_delete(Object* cast) {
+ DragonFlyBSDProcess* this = (DragonFlyBSDProcess*) cast;
+ Process_done((Process*)cast);
+ free(this->jname);
+ free(this);
+}
+
+static void DragonFlyBSDProcess_writeField(const Process* this, RichString* str, ProcessField field) {
+ const DragonFlyBSDProcess* fp = (const DragonFlyBSDProcess*) this;
+ char buffer[256]; buffer[255] = '\0';
+ int attr = CRT_colors[DEFAULT_COLOR];
+ size_t n = sizeof(buffer) - 1;
+ switch (field) {
+ // add Platform-specific fields here
+ case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, Process_isKernelThread(this) ? -1 : this->pid); break;
+ case JID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, fp->jid); break;
+ case JAIL: Process_printLeftAlignedField(str, attr, fp->jname, 11); return;
+ default:
+ Process_writeField(this, str, field);
+ return;
+ }
+ RichString_appendWide(str, attr, buffer);
+}
+
+static int DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
+ const DragonFlyBSDProcess* p1 = (const DragonFlyBSDProcess*)v1;
+ const DragonFlyBSDProcess* p2 = (const DragonFlyBSDProcess*)v2;
+
+ switch (key) {
+ // add Platform-specific fields here
+ case JID:
+ return SPACESHIP_NUMBER(p1->jid, p2->jid);
+ case JAIL:
+ return SPACESHIP_NULLSTR(p1->jname, p2->jname);
+ default:
+ return Process_compareByKey_Base(v1, v2, key);
+ }
+}
+
+const ProcessClass DragonFlyBSDProcess_class = {
+ .super = {
+ .extends = Class(Process),
+ .display = Process_display,
+ .delete = Process_delete,
+ .compare = Process_compare
+ },
+ .writeField = DragonFlyBSDProcess_writeField,
+ .compareByKey = DragonFlyBSDProcess_compareByKey
+};
diff --git a/dragonflybsd/DragonFlyBSDProcess.h b/dragonflybsd/DragonFlyBSDProcess.h
new file mode 100644
index 0000000..e0a77ef
--- /dev/null
+++ b/dragonflybsd/DragonFlyBSDProcess.h
@@ -0,0 +1,32 @@
+#ifndef HEADER_DragonFlyBSDProcess
+#define HEADER_DragonFlyBSDProcess
+/*
+htop - dragonflybsd/DragonFlyBSDProcess.h
+(C) 2015 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include <stdbool.h>
+
+#include "Object.h"
+#include "Process.h"
+#include "Settings.h"
+
+
+typedef struct DragonFlyBSDProcess_ {
+ Process super;
+ int jid;
+ char* jname;
+} DragonFlyBSDProcess;
+
+extern const ProcessClass DragonFlyBSDProcess_class;
+
+extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
+
+Process* DragonFlyBSDProcess_new(const Settings* settings);
+
+void Process_delete(Object* cast);
+
+#endif
diff --git a/dragonflybsd/DragonFlyBSDProcessList.c b/dragonflybsd/DragonFlyBSDProcessList.c
new file mode 100644
index 0000000..f46d6ce
--- /dev/null
+++ b/dragonflybsd/DragonFlyBSDProcessList.c
@@ -0,0 +1,615 @@
+/*
+htop - DragonFlyBSDProcessList.c
+(C) 2014 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "dragonflybsd/DragonFlyBSDProcessList.h"
+
+#include <fcntl.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/param.h>
+
+#include "CRT.h"
+#include "Macros.h"
+
+#include "dragonflybsd/DragonFlyBSDProcess.h"
+
+
+static int MIB_hw_physmem[2];
+static int MIB_vm_stats_vm_v_page_count[4];
+static int pageSize;
+static int pageSizeKb;
+
+static int MIB_vm_stats_vm_v_wire_count[4];
+static int MIB_vm_stats_vm_v_active_count[4];
+static int MIB_vm_stats_vm_v_cache_count[4];
+static int MIB_vm_stats_vm_v_inactive_count[4];
+static int MIB_vm_stats_vm_v_free_count[4];
+
+static int MIB_vfs_bufspace[2];
+
+static int MIB_kern_cp_time[2];
+static int MIB_kern_cp_times[2];
+static int kernelFScale;
+
+ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId) {
+ size_t len;
+ char errbuf[_POSIX2_LINE_MAX];
+ DragonFlyBSDProcessList* dfpl = xCalloc(1, sizeof(DragonFlyBSDProcessList));
+ ProcessList* pl = (ProcessList*) dfpl;
+ ProcessList_init(pl, Class(DragonFlyBSDProcess), usersTable, dynamicMeters, dynamicColumns, pidMatchList, userId);
+
+ // physical memory in system: hw.physmem
+ // physical page size: hw.pagesize
+ // usable pagesize : vm.stats.vm.v_page_size
+ len = 2; sysctlnametomib("hw.physmem", MIB_hw_physmem, &len);
+
+ len = sizeof(pageSize);
+ if (sysctlbyname("vm.stats.vm.v_page_size", &pageSize, &len, NULL, 0) == -1)
+ CRT_fatalError("Cannot get pagesize by sysctl");
+ pageSizeKb = pageSize / ONE_K;
+
+ // usable page count vm.stats.vm.v_page_count
+ // actually usable memory : vm.stats.vm.v_page_count * vm.stats.vm.v_page_size
+ len = 4; sysctlnametomib("vm.stats.vm.v_page_count", MIB_vm_stats_vm_v_page_count, &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_active_count", MIB_vm_stats_vm_v_active_count, &len);
+ len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len);
+ len = 4; sysctlnametomib("vm.stats.vm.v_inactive_count", MIB_vm_stats_vm_v_inactive_count, &len);
+ len = 4; sysctlnametomib("vm.stats.vm.v_free_count", MIB_vm_stats_vm_v_free_count, &len);
+
+ len = 2; sysctlnametomib("vfs.bufspace", MIB_vfs_bufspace, &len);
+
+ int cpus = 1;
+ len = sizeof(cpus);
+ if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) != 0) {
+ cpus = 1;
+ }
+
+ size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
+ len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
+ dfpl->cp_time_o = xCalloc(CPUSTATES, sizeof(unsigned long));
+ dfpl->cp_time_n = xCalloc(CPUSTATES, sizeof(unsigned long));
+ len = sizeof_cp_time_array;
+
+ // fetch initial single (or average) CPU clicks from kernel
+ sysctl(MIB_kern_cp_time, 2, dfpl->cp_time_o, &len, NULL, 0);
+
+ // on smp box, fetch rest of initial CPU's clicks
+ if (cpus > 1) {
+ len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
+ dfpl->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
+ dfpl->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
+ len = cpus * sizeof_cp_time_array;
+ sysctl(MIB_kern_cp_times, 2, dfpl->cp_times_o, &len, NULL, 0);
+ }
+
+ pl->existingCPUs = MAXIMUM(cpus, 1);
+ // TODO: support offline CPUs and hot swapping
+ pl->activeCPUs = pl->existingCPUs;
+
+ if (cpus == 1 ) {
+ dfpl->cpus = xRealloc(dfpl->cpus, sizeof(CPUData));
+ } else {
+ // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well)
+ dfpl->cpus = xRealloc(dfpl->cpus, (pl->existingCPUs + 1) * sizeof(CPUData));
+ }
+
+ len = sizeof(kernelFScale);
+ if (sysctlbyname("kern.fscale", &kernelFScale, &len, NULL, 0) == -1) {
+ //sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
+ kernelFScale = 2048;
+ }
+
+ dfpl->kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
+ if (dfpl->kd == NULL) {
+ CRT_fatalError("kvm_openfiles() failed");
+ }
+
+ return pl;
+}
+
+void ProcessList_delete(ProcessList* this) {
+ const DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) this;
+ if (dfpl->kd) {
+ kvm_close(dfpl->kd);
+ }
+
+ if (dfpl->jails) {
+ Hashtable_delete(dfpl->jails);
+ }
+ free(dfpl->cp_time_o);
+ free(dfpl->cp_time_n);
+ free(dfpl->cp_times_o);
+ free(dfpl->cp_times_n);
+ free(dfpl->cpus);
+
+ ProcessList_done(this);
+ free(this);
+}
+
+static inline void DragonFlyBSDProcessList_scanCPUTime(ProcessList* pl) {
+ const DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) pl;
+
+ unsigned int cpus = pl->existingCPUs; // actual CPU count
+ unsigned int maxcpu = cpus; // max iteration (in case we have average + smp)
+ int cp_times_offset;
+
+ assert(cpus > 0);
+
+ size_t sizeof_cp_time_array;
+
+ unsigned long* cp_time_n; // old clicks state
+ unsigned long* cp_time_o; // current clicks state
+
+ unsigned long cp_time_d[CPUSTATES];
+ double cp_time_p[CPUSTATES];
+
+ // get averages or single CPU clicks
+ sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
+ sysctl(MIB_kern_cp_time, 2, dfpl->cp_time_n, &sizeof_cp_time_array, NULL, 0);
+
+ // get rest of CPUs
+ if (cpus > 1) {
+ // on smp systems DragonFlyBSD kernel concats all CPU states into one long array in
+ // kern.cp_times sysctl OID
+ // we store averages in dfpl->cpus[0], and actual cores after that
+ maxcpu = cpus + 1;
+ sizeof_cp_time_array = cpus * sizeof(unsigned long) * CPUSTATES;
+ sysctl(MIB_kern_cp_times, 2, dfpl->cp_times_n, &sizeof_cp_time_array, NULL, 0);
+ }
+
+ for (unsigned int i = 0; i < maxcpu; i++) {
+ if (cpus == 1) {
+ // single CPU box
+ cp_time_n = dfpl->cp_time_n;
+ cp_time_o = dfpl->cp_time_o;
+ } else {
+ if (i == 0 ) {
+ // average
+ cp_time_n = dfpl->cp_time_n;
+ cp_time_o = dfpl->cp_time_o;
+ } else {
+ // specific smp cores
+ cp_times_offset = i - 1;
+ cp_time_n = dfpl->cp_times_n + (cp_times_offset * CPUSTATES);
+ cp_time_o = dfpl->cp_times_o + (cp_times_offset * CPUSTATES);
+ }
+ }
+
+ // diff old vs new
+ unsigned long long total_o = 0;
+ unsigned long long total_n = 0;
+ unsigned long long total_d = 0;
+ for (int s = 0; s < CPUSTATES; s++) {
+ cp_time_d[s] = cp_time_n[s] - cp_time_o[s];
+ total_o += cp_time_o[s];
+ total_n += cp_time_n[s];
+ }
+
+ // totals
+ total_d = total_n - total_o;
+ if (total_d < 1 ) {
+ total_d = 1;
+ }
+
+ // save current state as old and calc percentages
+ for (int s = 0; s < CPUSTATES; ++s) {
+ cp_time_o[s] = cp_time_n[s];
+ cp_time_p[s] = ((double)cp_time_d[s]) / ((double)total_d) * 100;
+ }
+
+ CPUData* cpuData = &(dfpl->cpus[i]);
+ cpuData->userPercent = cp_time_p[CP_USER];
+ cpuData->nicePercent = cp_time_p[CP_NICE];
+ cpuData->systemPercent = cp_time_p[CP_SYS];
+ cpuData->irqPercent = cp_time_p[CP_INTR];
+ cpuData->systemAllPercent = cp_time_p[CP_SYS] + cp_time_p[CP_INTR];
+ // this one is not really used, but we store it anyway
+ cpuData->idlePercent = cp_time_p[CP_IDLE];
+ }
+}
+
+static inline void DragonFlyBSDProcessList_scanMemoryInfo(ProcessList* pl) {
+ DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) pl;
+
+ // @etosan:
+ // memory counter relationships seem to be these:
+ // total = active + wired + inactive + cache + free
+ // htop_used (unavail to anybody) = active + wired
+ // htop_cache (for cache meter) = buffers + cache
+ // user_free (avail to procs) = buffers + inactive + cache + free
+ size_t len = sizeof(pl->totalMem);
+
+ //disabled for now, as it is always smaller than phycal amount of memory...
+ //...to avoid "where is my memory?" questions
+ //sysctl(MIB_vm_stats_vm_v_page_count, 4, &(pl->totalMem), &len, NULL, 0);
+ //pl->totalMem *= pageSizeKb;
+ sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0);
+ pl->totalMem /= 1024;
+
+ sysctl(MIB_vm_stats_vm_v_active_count, 4, &(dfpl->memActive), &len, NULL, 0);
+ dfpl->memActive *= pageSizeKb;
+
+ sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(dfpl->memWire), &len, NULL, 0);
+ dfpl->memWire *= pageSizeKb;
+
+ sysctl(MIB_vfs_bufspace, 2, &(pl->buffersMem), &len, NULL, 0);
+ pl->buffersMem /= 1024;
+
+ sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0);
+ pl->cachedMem *= pageSizeKb;
+ pl->usedMem = dfpl->memActive + dfpl->memWire;
+
+ struct kvm_swap swap[16];
+ int nswap = kvm_getswapinfo(dfpl->kd, swap, ARRAYSIZE(swap), 0);
+ 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;
+}
+
+//static void DragonFlyBSDProcessList_updateExe(const struct kinfo_proc* kproc, Process* proc) {
+// const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, kproc->kp_pid };
+// char buffer[2048];
+// size_t size = sizeof(buffer);
+// if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
+// Process_updateExe(proc, NULL);
+// return;
+// }
+//
+// /* Kernel threads return an empty buffer */
+// if (buffer[0] == '\0') {
+// Process_updateExe(proc, NULL);
+// return;
+// }
+//
+// Process_updateExe(proc, buffer);
+//}
+
+static void DragonFlyBSDProcessList_updateExe(const struct kinfo_proc* kproc, Process* proc) {
+ if (Process_isKernelThread(proc))
+ return;
+
+ char path[32];
+ xSnprintf(path, sizeof(path), "/proc/%d/file", kproc->kp_pid);
+
+ char target[PATH_MAX];
+ ssize_t ret = readlink(path, target, sizeof(target) - 1);
+ if (ret <= 0)
+ return;
+
+ target[ret] = '\0';
+ Process_updateExe(proc, target);
+}
+
+static void DragonFlyBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
+ const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, kproc->kp_pid };
+ char buffer[2048];
+ size_t size = sizeof(buffer);
+ if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
+ free(proc->procCwd);
+ proc->procCwd = NULL;
+ return;
+ }
+
+ /* Kernel threads return an empty buffer */
+ if (buffer[0] == '\0') {
+ free(proc->procCwd);
+ proc->procCwd = NULL;
+ return;
+ }
+
+ free_and_xStrdup(&proc->procCwd, buffer);
+}
+
+static void DragonFlyBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
+ Process_updateComm(proc, kproc->kp_comm);
+
+ char** argv = kvm_getargv(kd, kproc, 0);
+ if (!argv || !argv[0]) {
+ Process_updateCmdline(proc, kproc->kp_comm, 0, strlen(kproc->kp_comm));
+ return;
+ }
+
+ size_t len = 0;
+ for (int i = 0; argv[i]; i++) {
+ len += strlen(argv[i]) + 1;
+ }
+
+ char* cmdline = xMalloc(len);
+
+ char* at = cmdline;
+ int end = 0;
+ for (int i = 0; argv[i]; i++) {
+ at = stpcpy(at, argv[i]);
+ if (end == 0) {
+ end = at - cmdline;
+ }
+ *at++ = ' ';
+ }
+ at--;
+ *at = '\0';
+
+ Process_updateCmdline(proc, cmdline, 0, end);
+
+ free(cmdline);
+}
+
+static inline void DragonFlyBSDProcessList_scanJails(DragonFlyBSDProcessList* dfpl) {
+ size_t len;
+ char* jls; /* Jail list */
+ char* curpos;
+ char* nextpos;
+
+ if (sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1) {
+ CRT_fatalError("initial sysctlbyname / jail.list failed");
+ }
+
+retry:
+ if (len == 0)
+ return;
+
+ jls = xMalloc(len);
+
+ if (sysctlbyname("jail.list", jls, &len, NULL, 0) == -1) {
+ if (errno == ENOMEM) {
+ free(jls);
+ goto retry;
+ }
+ CRT_fatalError("sysctlbyname / jail.list failed");
+ }
+
+ if (dfpl->jails) {
+ Hashtable_delete(dfpl->jails);
+ }
+
+ dfpl->jails = Hashtable_new(20, true);
+ curpos = jls;
+ while (curpos) {
+ int jailid;
+ char* str_hostname;
+
+ nextpos = strchr(curpos, '\n');
+ if (nextpos) {
+ *nextpos++ = 0;
+ }
+
+ jailid = atoi(strtok(curpos, " "));
+ str_hostname = strtok(NULL, " ");
+
+ char* jname = (char*) (Hashtable_get(dfpl->jails, jailid));
+ if (jname == NULL) {
+ jname = xStrdup(str_hostname);
+ Hashtable_put(dfpl->jails, jailid, jname);
+ }
+
+ curpos = nextpos;
+ }
+
+ free(jls);
+}
+
+static char* DragonFlyBSDProcessList_readJailName(DragonFlyBSDProcessList* dfpl, int jailid) {
+ char* hostname;
+ char* jname;
+
+ if (jailid != 0 && dfpl->jails && (hostname = (char*)Hashtable_get(dfpl->jails, jailid))) {
+ jname = xStrdup(hostname);
+ } else {
+ jname = xStrdup("-");
+ }
+
+ return jname;
+}
+
+void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
+ DragonFlyBSDProcessList* dfpl = (DragonFlyBSDProcessList*) super;
+ const Settings* settings = super->settings;
+ bool hideKernelThreads = settings->hideKernelThreads;
+ bool hideUserlandThreads = settings->hideUserlandThreads;
+
+ DragonFlyBSDProcessList_scanMemoryInfo(super);
+ DragonFlyBSDProcessList_scanCPUTime(super);
+ DragonFlyBSDProcessList_scanJails(dfpl);
+
+ // in pause mode only gather global data for meters (CPU/memory/...)
+ if (pauseProcessUpdate) {
+ return;
+ }
+
+ int count = 0;
+
+ const struct kinfo_proc* kprocs = kvm_getprocs(dfpl->kd, KERN_PROC_ALL | (!hideUserlandThreads ? KERN_PROC_FLAG_LWP : 0), 0, &count);
+
+ for (int i = 0; i < count; i++) {
+ const struct kinfo_proc* kproc = &kprocs[i];
+ bool preExisting = false;
+ bool ATTR_UNUSED isIdleProcess = false;
+
+ // note: dragonflybsd kernel processes all have the same pid, so we misuse the kernel thread address to give them a unique identifier
+ Process* proc = ProcessList_getProcess(super, kproc->kp_ktaddr ? (pid_t)kproc->kp_ktaddr : kproc->kp_pid, &preExisting, DragonFlyBSDProcess_new);
+ DragonFlyBSDProcess* dfp = (DragonFlyBSDProcess*) proc;
+
+ if (!preExisting) {
+ dfp->jid = kproc->kp_jailid;
+ if (kproc->kp_ktaddr && kproc->kp_flags & P_SYSTEM) {
+ // dfb kernel threads all have the same pid, so we misuse the kernel thread address to give them a unique identifier
+ proc->pid = (pid_t)kproc->kp_ktaddr;
+ proc->isKernelThread = true;
+ } else {
+ proc->pid = kproc->kp_pid; // process ID
+ proc->isKernelThread = false;
+ }
+ proc->isUserlandThread = kproc->kp_nthreads > 1;
+ proc->ppid = kproc->kp_ppid; // parent process id
+ proc->tpgid = kproc->kp_tpgid; // tty process group id
+ //proc->tgid = kproc->kp_lwp.kl_tid; // thread group id
+ proc->tgid = kproc->kp_pid; // thread group id
+ proc->pgrp = kproc->kp_pgid; // process group id
+ proc->session = kproc->kp_sid;
+ proc->st_uid = kproc->kp_uid; // user ID
+ proc->processor = kproc->kp_lwp.kl_origcpu;
+ proc->starttime_ctime = kproc->kp_start.tv_sec;
+ Process_fillStarttimeBuffer(proc);
+ proc->user = UsersTable_getRef(super->usersTable, proc->st_uid);
+
+ proc->tty_nr = kproc->kp_tdev; // control terminal device number
+ const char* name = (kproc->kp_tdev != NODEV) ? devname(kproc->kp_tdev, S_IFCHR) : NULL;
+ if (!name) {
+ free(proc->tty_name);
+ proc->tty_name = NULL;
+ } else {
+ free_and_xStrdup(&proc->tty_name, name);
+ }
+
+ DragonFlyBSDProcessList_updateExe(kproc, proc);
+ DragonFlyBSDProcessList_updateProcessName(dfpl->kd, kproc, proc);
+
+ if (settings->ss->flags & PROCESS_FLAG_CWD) {
+ DragonFlyBSDProcessList_updateCwd(kproc, proc);
+ }
+
+ ProcessList_add(super, proc);
+
+ dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
+ } else {
+ proc->processor = kproc->kp_lwp.kl_cpuid;
+ if (dfp->jid != kproc->kp_jailid) { // process can enter jail anytime
+ dfp->jid = kproc->kp_jailid;
+ free(dfp->jname);
+ dfp->jname = DragonFlyBSDProcessList_readJailName(dfpl, kproc->kp_jailid);
+ }
+ // if there are reapers in the system, process can get reparented anytime
+ proc->ppid = kproc->kp_ppid;
+ if (proc->st_uid != kproc->kp_uid) { // some processes change users (eg. to lower privs)
+ proc->st_uid = kproc->kp_uid;
+ proc->user = UsersTable_getRef(super->usersTable, proc->st_uid);
+ }
+ if (settings->updateProcessNames) {
+ DragonFlyBSDProcessList_updateProcessName(dfpl->kd, kproc, proc);
+ }
+ }
+
+ proc->m_virt = kproc->kp_vm_map_size / ONE_K;
+ proc->m_resident = kproc->kp_vm_rssize * pageSizeKb;
+ proc->nlwp = kproc->kp_nthreads; // number of lwp thread
+ proc->time = (kproc->kp_lwp.kl_uticks + kproc->kp_lwp.kl_sticks + kproc->kp_lwp.kl_iticks) / 10000;
+
+ proc->percent_cpu = 100.0 * ((double)kproc->kp_lwp.kl_pctcpu / (double)kernelFScale);
+ proc->percent_mem = 100.0 * proc->m_resident / (double)(super->totalMem);
+ Process_updateCPUFieldWidths(proc->percent_cpu);
+
+ if (proc->percent_cpu > 0.1) {
+ // system idle process should own all CPU time left regardless of CPU count
+ if (String_eq("idle", kproc->kp_comm)) {
+ isIdleProcess = true;
+ }
+ }
+
+ if (kproc->kp_lwp.kl_pid != -1)
+ proc->priority = kproc->kp_lwp.kl_prio;
+ else
+ proc->priority = -kproc->kp_lwp.kl_tdprio;
+
+ switch (kproc->kp_lwp.kl_rtprio.type) {
+ case RTP_PRIO_REALTIME:
+ proc->nice = PRIO_MIN - 1 - RTP_PRIO_MAX + kproc->kp_lwp.kl_rtprio.prio;
+ break;
+ case RTP_PRIO_IDLE:
+ proc->nice = PRIO_MAX + 1 + kproc->kp_lwp.kl_rtprio.prio;
+ break;
+ case RTP_PRIO_THREAD:
+ proc->nice = PRIO_MIN - 1 - RTP_PRIO_MAX - kproc->kp_lwp.kl_rtprio.prio;
+ break;
+ default:
+ proc->nice = kproc->kp_nice;
+ break;
+ }
+
+ // would be nice if we could store multiple states in proc->state (as enum) and have writeField render them
+ /* Taken from: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/c163a4d7ee9c6857ee4e04a3a2cbb50c3de29da1/sys/sys/proc_common.h */
+ switch (kproc->kp_stat) {
+ case SIDL: proc->state = IDLE; isIdleProcess = true; break;
+ case SACTIVE:
+ switch (kproc->kp_lwp.kl_stat) {
+ case LSSLEEP:
+ if (kproc->kp_lwp.kl_flags & LWP_SINTR) // interruptible wait short/long
+ if (kproc->kp_lwp.kl_slptime >= MAXSLP) {
+ proc->state = IDLE;
+ isIdleProcess = true;
+ } else {
+ proc->state = SLEEPING;
+ }
+ else if (kproc->kp_lwp.kl_tdflags & TDF_SINTR) // interruptible lwkt wait
+ proc->state = SLEEPING;
+ else if (kproc->kp_paddr) // uninterruptible wait
+ proc->state = UNINTERRUPTIBLE_WAIT;
+ else // uninterruptible lwkt wait
+ proc->state = UNINTERRUPTIBLE_WAIT;
+ break;
+ case LSRUN:
+ if (kproc->kp_lwp.kl_stat == LSRUN) {
+ if (!(kproc->kp_lwp.kl_tdflags & (TDF_RUNNING | TDF_RUNQ)))
+ proc->state = QUEUED;
+ else
+ proc->state = RUNNING;
+ }
+ break;
+ case LSSTOP:
+ proc->state = STOPPED;
+ break;
+ default:
+ proc->state = PAGING;
+ break;
+ }
+ break;
+ case SSTOP: proc->state = STOPPED; break;
+ case SZOMB: proc->state = ZOMBIE; break;
+ case SCORE: proc->state = BLOCKED; break;
+ default: proc->state = UNKNOWN;
+ }
+
+ if (kproc->kp_flags & P_SWAPPEDOUT)
+ proc->state = SLEEPING;
+ if (kproc->kp_flags & P_TRACED)
+ proc->state = TRACED;
+ if (kproc->kp_flags & P_JAILED)
+ proc->state = TRACED;
+
+ if (Process_isKernelThread(proc))
+ super->kernelThreads++;
+
+ super->totalTasks++;
+
+ if (proc->state == RUNNING)
+ super->runningTasks++;
+
+ proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
+ proc->updated = true;
+ }
+}
+
+bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) {
+ assert(id < super->existingCPUs);
+
+ // TODO: support offline CPUs and hot swapping
+ (void) super; (void) id;
+
+ return true;
+}
diff --git a/dragonflybsd/DragonFlyBSDProcessList.h b/dragonflybsd/DragonFlyBSDProcessList.h
new file mode 100644
index 0000000..c1bf2d1
--- /dev/null
+++ b/dragonflybsd/DragonFlyBSDProcessList.h
@@ -0,0 +1,64 @@
+#ifndef HEADER_DragonFlyBSDProcessList
+#define HEADER_DragonFlyBSDProcessList
+/*
+htop - DragonFlyBSDProcessList.h
+(C) 2014 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include <sys/types.h> // required for kvm.h
+#include <kvm.h>
+#include <osreldate.h>
+#include <stdbool.h>
+#include <sys/jail.h>
+#include <sys/param.h>
+#include <sys/resource.h>
+#include <sys/uio.h>
+
+#include "Hashtable.h"
+#include "ProcessList.h"
+#include "UsersTable.h"
+
+#include "dragonflybsd/DragonFlyBSDProcess.h"
+
+
+typedef struct CPUData_ {
+ double userPercent;
+ double nicePercent;
+ double systemPercent;
+ double irqPercent;
+ double idlePercent;
+ double systemAllPercent;
+} CPUData;
+
+typedef struct DragonFlyBSDProcessList_ {
+ ProcessList super;
+ kvm_t* kd;
+
+ unsigned long long int memWire;
+ unsigned long long int memActive;
+ unsigned long long int memInactive;
+ unsigned long long int memFree;
+
+ CPUData* cpus;
+
+ unsigned long* cp_time_o;
+ unsigned long* cp_time_n;
+
+ unsigned long* cp_times_o;
+ unsigned long* cp_times_n;
+
+ Hashtable* jails;
+} DragonFlyBSDProcessList;
+
+ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId);
+
+void ProcessList_delete(ProcessList* this);
+
+void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate);
+
+bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id);
+
+#endif
diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c
new file mode 100644
index 0000000..8a684d8
--- /dev/null
+++ b/dragonflybsd/Platform.c
@@ -0,0 +1,262 @@
+/*
+htop - dragonflybsd/Platform.c
+(C) 2014 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "dragonflybsd/Platform.h"
+
+#include <math.h>
+#include <time.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <vm/vm_param.h>
+
+#include "ClockMeter.h"
+#include "CPUMeter.h"
+#include "DateMeter.h"
+#include "DateTimeMeter.h"
+#include "HostnameMeter.h"
+#include "LoadAverageMeter.h"
+#include "MemoryMeter.h"
+#include "MemorySwapMeter.h"
+#include "ProcessList.h"
+#include "SwapMeter.h"
+#include "SysArchMeter.h"
+#include "TasksMeter.h"
+#include "UptimeMeter.h"
+#include "dragonflybsd/DragonFlyBSDProcess.h"
+#include "dragonflybsd/DragonFlyBSDProcessList.h"
+
+const ScreenDefaults Platform_defaultScreens[] = {
+ {
+ .name = "Main",
+ .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
+ .sortKey = "PERCENT_CPU",
+ },
+};
+
+const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
+
+const SignalItem Platform_signals[] = {
+ { .name = " 0 Cancel", .number = 0 },
+ { .name = " 1 SIGHUP", .number = 1 },
+ { .name = " 2 SIGINT", .number = 2 },
+ { .name = " 3 SIGQUIT", .number = 3 },
+ { .name = " 4 SIGILL", .number = 4 },
+ { .name = " 5 SIGTRAP", .number = 5 },
+ { .name = " 6 SIGABRT", .number = 6 },
+ { .name = " 7 SIGEMT", .number = 7 },
+ { .name = " 8 SIGFPE", .number = 8 },
+ { .name = " 9 SIGKILL", .number = 9 },
+ { .name = "10 SIGBUS", .number = 10 },
+ { .name = "11 SIGSEGV", .number = 11 },
+ { .name = "12 SIGSYS", .number = 12 },
+ { .name = "13 SIGPIPE", .number = 13 },
+ { .name = "14 SIGALRM", .number = 14 },
+ { .name = "15 SIGTERM", .number = 15 },
+ { .name = "16 SIGURG", .number = 16 },
+ { .name = "17 SIGSTOP", .number = 17 },
+ { .name = "18 SIGTSTP", .number = 18 },
+ { .name = "19 SIGCONT", .number = 19 },
+ { .name = "20 SIGCHLD", .number = 20 },
+ { .name = "21 SIGTTIN", .number = 21 },
+ { .name = "22 SIGTTOU", .number = 22 },
+ { .name = "23 SIGIO", .number = 23 },
+ { .name = "24 SIGXCPU", .number = 24 },
+ { .name = "25 SIGXFSZ", .number = 25 },
+ { .name = "26 SIGVTALRM", .number = 26 },
+ { .name = "27 SIGPROF", .number = 27 },
+ { .name = "28 SIGWINCH", .number = 28 },
+ { .name = "29 SIGINFO", .number = 29 },
+ { .name = "30 SIGUSR1", .number = 30 },
+ { .name = "31 SIGUSR2", .number = 31 },
+ { .name = "32 SIGTHR", .number = 32 },
+ { .name = "33 SIGLIBRT", .number = 33 },
+};
+
+const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
+
+const MeterClass* const Platform_meterTypes[] = {
+ &CPUMeter_class,
+ &ClockMeter_class,
+ &DateMeter_class,
+ &DateTimeMeter_class,
+ &LoadAverageMeter_class,
+ &LoadMeter_class,
+ &MemoryMeter_class,
+ &MemorySwapMeter_class,
+ &SwapMeter_class,
+ &TasksMeter_class,
+ &UptimeMeter_class,
+ &BatteryMeter_class,
+ &HostnameMeter_class,
+ &SysArchMeter_class,
+ &AllCPUsMeter_class,
+ &AllCPUs2Meter_class,
+ &AllCPUs4Meter_class,
+ &AllCPUs8Meter_class,
+ &LeftCPUsMeter_class,
+ &RightCPUsMeter_class,
+ &LeftCPUs2Meter_class,
+ &RightCPUs2Meter_class,
+ &LeftCPUs4Meter_class,
+ &RightCPUs4Meter_class,
+ &LeftCPUs8Meter_class,
+ &RightCPUs8Meter_class,
+ &BlankMeter_class,
+ NULL
+};
+
+bool Platform_init(void) {
+ /* no platform-specific setup needed */
+ return true;
+}
+
+void Platform_done(void) {
+ /* no platform-specific cleanup needed */
+}
+
+void Platform_setBindings(Htop_Action* keys) {
+ /* no platform-specific key bindings */
+ (void) keys;
+}
+
+int Platform_getUptime(void) {
+ struct timeval bootTime, currTime;
+ int mib[2] = { CTL_KERN, KERN_BOOTTIME };
+ size_t size = sizeof(bootTime);
+
+ int err = sysctl(mib, 2, &bootTime, &size, NULL, 0);
+ if (err) {
+ return -1;
+ }
+ gettimeofday(&currTime, NULL);
+
+ return (int) difftime(currTime.tv_sec, bootTime.tv_sec);
+}
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
+ struct loadavg loadAverage;
+ int mib[2] = { CTL_VM, VM_LOADAVG };
+ size_t size = sizeof(loadAverage);
+
+ int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0);
+ if (err) {
+ *one = 0;
+ *five = 0;
+ *fifteen = 0;
+ return;
+ }
+ *one = (double) loadAverage.ldavg[0] / loadAverage.fscale;
+ *five = (double) loadAverage.ldavg[1] / loadAverage.fscale;
+ *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale;
+}
+
+int Platform_getMaxPid(void) {
+ int maxPid;
+ size_t size = sizeof(maxPid);
+ int err = sysctlbyname("kern.pid_max", &maxPid, &size, NULL, 0);
+ if (err) {
+ return 999999;
+ }
+ return maxPid;
+}
+
+double Platform_setCPUValues(Meter* this, unsigned int cpu) {
+ const DragonFlyBSDProcessList* fpl = (const DragonFlyBSDProcessList*) this->pl;
+ unsigned int cpus = this->pl->activeCPUs;
+ const CPUData* cpuData;
+
+ if (cpus == 1) {
+ // single CPU box has everything in fpl->cpus[0]
+ cpuData = &(fpl->cpus[0]);
+ } else {
+ cpuData = &(fpl->cpus[cpu]);
+ }
+
+ double percent;
+ double* v = this->values;
+
+ v[CPU_METER_NICE] = cpuData->nicePercent;
+ v[CPU_METER_NORMAL] = cpuData->userPercent;
+ if (this->pl->settings->detailedCPUTime) {
+ v[CPU_METER_KERNEL] = cpuData->systemPercent;
+ v[CPU_METER_IRQ] = cpuData->irqPercent;
+ this->curItems = 4;
+ percent = v[0] + v[1] + v[2] + v[3];
+ } else {
+ v[2] = cpuData->systemAllPercent;
+ this->curItems = 3;
+ percent = v[0] + v[1] + v[2];
+ }
+
+ percent = isnan(percent) ? 0.0 : CLAMP(percent, 0.0, 100.0);
+
+ v[CPU_METER_FREQUENCY] = NAN;
+ v[CPU_METER_TEMPERATURE] = NAN;
+
+ return percent;
+}
+
+void Platform_setMemoryValues(Meter* this) {
+ // TODO
+ const ProcessList* pl = this->pl;
+
+ this->total = pl->totalMem;
+ this->values[MEMORY_METER_USED] = pl->usedMem;
+ this->values[MEMORY_METER_BUFFERS] = pl->buffersMem;
+ // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
+ this->values[MEMORY_METER_CACHE] = pl->cachedMem;
+ // this->values[MEMORY_METER_AVAILABLE] = "available memory"
+}
+
+void Platform_setSwapValues(Meter* this) {
+ const ProcessList* pl = this->pl;
+ this->total = pl->totalSwap;
+ this->values[SWAP_METER_USED] = pl->usedSwap;
+ this->values[SWAP_METER_CACHE] = NAN;
+}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ // TODO
+ (void)pid; // prevent unused warning
+ return NULL;
+}
+
+FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
+ (void)pid;
+ return NULL;
+}
+
+bool Platform_getDiskIO(DiskIOData* data) {
+ // TODO
+ (void)data;
+ return false;
+}
+
+bool Platform_getNetworkIO(NetworkIOData* data) {
+ // TODO
+ (void)data;
+ return false;
+}
+
+void Platform_getBattery(double* percent, ACPresence* isOnAC) {
+ int life;
+ size_t life_len = sizeof(life);
+ if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
+ *percent = NAN;
+ else
+ *percent = life;
+
+ int acline;
+ size_t acline_len = sizeof(acline);
+ if (sysctlbyname("hw.acpi.acline", &acline, &acline_len, NULL, 0) == -1)
+ *isOnAC = AC_ERROR;
+ else
+ *isOnAC = acline == 0 ? AC_ABSENT : AC_PRESENT;
+}
diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h
new file mode 100644
index 0000000..cf69356
--- /dev/null
+++ b/dragonflybsd/Platform.h
@@ -0,0 +1,120 @@
+#ifndef HEADER_Platform
+#define HEADER_Platform
+/*
+htop - dragonflybsd/Platform.h
+(C) 2014 Hisham H. Muhammad
+(C) 2017 Diederik de Groot
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "Action.h"
+#include "BatteryMeter.h"
+#include "DiskIOMeter.h"
+#include "Hashtable.h"
+#include "Macros.h"
+#include "Meter.h"
+#include "NetworkIOMeter.h"
+#include "Process.h"
+#include "ProcessLocksScreen.h"
+#include "SignalsPanel.h"
+#include "CommandLine.h"
+#include "generic/gettime.h"
+#include "generic/hostname.h"
+#include "generic/uname.h"
+
+
+extern const ScreenDefaults Platform_defaultScreens[];
+
+extern const unsigned int Platform_numberOfDefaultScreens;
+
+extern const SignalItem Platform_signals[];
+
+extern const unsigned int Platform_numberOfSignals;
+
+extern const MeterClass* const Platform_meterTypes[];
+
+bool Platform_init(void);
+
+void Platform_done(void);
+
+void Platform_setBindings(Htop_Action* keys);
+
+int Platform_getUptime(void);
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen);
+
+int Platform_getMaxPid(void);
+
+double Platform_setCPUValues(Meter* this, unsigned int cpu);
+
+void Platform_setMemoryValues(Meter* this);
+
+void Platform_setSwapValues(Meter* this);
+
+char* Platform_getProcessEnv(pid_t pid);
+
+FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
+
+bool Platform_getDiskIO(DiskIOData* data);
+
+bool Platform_getNetworkIO(NetworkIOData* data);
+
+void Platform_getBattery(double* percent, ACPresence* isOnAC);
+
+static inline void Platform_getHostname(char* buffer, size_t size) {
+ Generic_hostname(buffer, size);
+}
+
+static inline void Platform_getRelease(char** string) {
+ *string = Generic_uname();
+}
+
+#define PLATFORM_LONG_OPTIONS
+
+static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
+
+static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
+ return STATUS_ERROR_EXIT;
+}
+
+static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {
+ Generic_gettime_realtime(tv, msec);
+}
+
+static inline void Platform_gettime_monotonic(uint64_t* msec) {
+ Generic_gettime_monotonic(msec);
+}
+
+static inline Hashtable* Platform_dynamicMeters(void) {
+ return NULL;
+}
+
+static inline void Platform_dynamicMetersDone(ATTR_UNUSED Hashtable* table) { }
+
+static inline void Platform_dynamicMeterInit(ATTR_UNUSED Meter* meter) { }
+
+static inline void Platform_dynamicMeterUpdateValues(ATTR_UNUSED Meter* meter) { }
+
+static inline void Platform_dynamicMeterDisplay(ATTR_UNUSED const Meter* meter, ATTR_UNUSED RichString* out) { }
+
+static inline Hashtable* Platform_dynamicColumns(void) {
+ return NULL;
+}
+
+static inline void Platform_dynamicColumnsDone(ATTR_UNUSED Hashtable* table) { }
+
+static inline const char* Platform_dynamicColumnInit(ATTR_UNUSED unsigned int key) {
+ return NULL;
+}
+
+static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) {
+ return false;
+}
+
+#endif
diff --git a/dragonflybsd/ProcessField.h b/dragonflybsd/ProcessField.h
new file mode 100644
index 0000000..1409675
--- /dev/null
+++ b/dragonflybsd/ProcessField.h
@@ -0,0 +1,19 @@
+#ifndef HEADER_DragonFlyBSDProcessField
+#define HEADER_DragonFlyBSDProcessField
+/*
+htop - dragonflybsd/ProcessField.h
+(C) 2020 htop dev team
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+
+#define PLATFORM_PROCESS_FIELDS \
+ JID = 100, \
+ JAIL = 101, \
+ \
+ DUMMY_BUMP_FIELD = CWD, \
+ // End of list
+
+
+#endif /* HEADER_DragonFlyBSDProcessField */