diff options
Diffstat (limited to 'linux')
-rw-r--r-- | linux/CGroupUtils.c | 341 | ||||
-rw-r--r-- | linux/CGroupUtils.h | 16 | ||||
-rw-r--r-- | linux/HugePageMeter.c | 106 | ||||
-rw-r--r-- | linux/HugePageMeter.h | 15 | ||||
-rw-r--r-- | linux/IOPriority.h | 36 | ||||
-rw-r--r-- | linux/IOPriorityPanel.c | 57 | ||||
-rw-r--r-- | linux/IOPriorityPanel.h | 17 | ||||
-rw-r--r-- | linux/LibSensors.c | 269 | ||||
-rw-r--r-- | linux/LibSensors.h | 13 | ||||
-rw-r--r-- | linux/LinuxProcess.c | 412 | ||||
-rw-r--r-- | linux/LinuxProcess.h | 135 | ||||
-rw-r--r-- | linux/LinuxProcessList.c | 2302 | ||||
-rw-r--r-- | linux/LinuxProcessList.h | 126 | ||||
-rw-r--r-- | linux/Platform.c | 1033 | ||||
-rw-r--r-- | linux/Platform.h | 141 | ||||
-rw-r--r-- | linux/PressureStallMeter.c | 149 | ||||
-rw-r--r-- | linux/PressureStallMeter.h | 26 | ||||
-rw-r--r-- | linux/ProcessField.h | 52 | ||||
-rw-r--r-- | linux/SELinuxMeter.c | 93 | ||||
-rw-r--r-- | linux/SELinuxMeter.h | 15 | ||||
-rw-r--r-- | linux/SystemdMeter.c | 388 | ||||
-rw-r--r-- | linux/SystemdMeter.h | 16 | ||||
-rw-r--r-- | linux/ZramMeter.c | 73 | ||||
-rw-r--r-- | linux/ZramMeter.h | 9 | ||||
-rw-r--r-- | linux/ZramStats.h | 10 |
25 files changed, 5850 insertions, 0 deletions
diff --git a/linux/CGroupUtils.c b/linux/CGroupUtils.c new file mode 100644 index 0000000..c11c460 --- /dev/null +++ b/linux/CGroupUtils.c @@ -0,0 +1,341 @@ +/* +htop - CGroupUtils.h +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/CGroupUtils.h" + +#include "XUtils.h" + + +typedef struct StrBuf_state { + char* buf; + size_t size; + size_t pos; +} StrBuf_state; + +typedef bool (*StrBuf_putc_t)(StrBuf_state* p, char c); + +static bool StrBuf_putc_count(StrBuf_state* p, ATTR_UNUSED char c) { + p->pos++; + return true; +} + +static bool StrBuf_putc_write(StrBuf_state* p, char c) { + if (p->pos >= p->size) + return false; + + p->buf[p->pos] = c; + p->pos++; + return true; +} + +static bool StrBuf_putsn(StrBuf_state* p, StrBuf_putc_t w, const char* s, size_t count) { + for (; count; count--) + if (!w(p, *s++)) + return false; + + return true; +} + +static bool StrBuf_putsz(StrBuf_state* p, StrBuf_putc_t w, const char* s) { + while (*s) + if (!w(p, *s++)) + return false; + + return true; +} + +static bool Label_checkEqual(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen == strlen(expected) && String_startsWith(labelStart, expected); +} + +static bool Label_checkPrefix(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen > strlen(expected) && String_startsWith(labelStart, expected); +} + +static bool Label_checkSuffix(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen > strlen(expected) && String_startsWith(labelStart + labelLen - strlen(expected), expected); +} + +static bool CGroup_filterName_internal(const char* cgroup, StrBuf_state* s, StrBuf_putc_t w) { + const char* str_slice_suffix = ".slice"; + const char* str_system_slice = "system.slice"; + const char* str_user_slice = "user.slice"; + const char* str_machine_slice = "machine.slice"; + const char* str_user_slice_prefix = "/user-"; + const char* str_system_slice_prefix = "/system-"; + + const char* str_lxc_monitor_legacy = "lxc.monitor"; + const char* str_lxc_payload_legacy = "lxc.payload"; + const char* str_lxc_monitor_prefix = "lxc.monitor."; + const char* str_lxc_payload_prefix = "lxc.payload."; + + const char* str_nspawn_scope_prefix = "machine-"; + const char* str_nspawn_monitor_label = "/supervisor"; + const char* str_nspawn_payload_label = "/payload"; + + const char* str_snap_scope_prefix = "snap."; + + const char* str_service_suffix = ".service"; + const char* str_scope_suffix = ".scope"; + + while (*cgroup) { + if ('/' == *cgroup) { + while ('/' == *cgroup) + cgroup++; + + if (!w(s, '/')) + return false; + + continue; + } + + const char* labelStart = cgroup; + const char* nextSlash = strchrnul(labelStart, '/'); + const size_t labelLen = nextSlash - labelStart; + + if (Label_checkEqual(labelStart, labelLen, str_system_slice)) { + cgroup = nextSlash; + + if (!StrBuf_putsz(s, w, "[S]")) + return false; + + if (String_startsWith(cgroup, str_system_slice_prefix)) { + cgroup = strchrnul(cgroup + 1, '/'); + continue; + } + + continue; + } + + if (Label_checkEqual(labelStart, labelLen, str_machine_slice)) { + cgroup = nextSlash; + + if (!StrBuf_putsz(s, w, "[M]")) + return false; + + continue; + } + + if (Label_checkEqual(labelStart, labelLen, str_user_slice)) { + cgroup = nextSlash; + + if (!StrBuf_putsz(s, w, "[U]")) + return false; + + if (!String_startsWith(cgroup, str_user_slice_prefix)) + continue; + + const char* userSliceSlash = strchrnul(cgroup + strlen(str_user_slice_prefix), '/'); + const char* sliceSpec = userSliceSlash - strlen(str_slice_suffix); + + if (!String_startsWith(sliceSpec, str_slice_suffix)) + continue; + + const size_t sliceNameLen = sliceSpec - (cgroup + strlen(str_user_slice_prefix)); + + s->pos--; + if (!w(s, ':')) + return false; + + if (!StrBuf_putsn(s, w, cgroup + strlen(str_user_slice_prefix), sliceNameLen)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = userSliceSlash; + + continue; + } + + if (Label_checkSuffix(labelStart, labelLen, str_slice_suffix)) { + const size_t sliceNameLen = labelLen - strlen(str_slice_suffix); + + if (!w(s, '[')) + return false; + + if (!StrBuf_putsn(s, w, cgroup, sliceNameLen)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = nextSlash; + + continue; + } + + if (Label_checkPrefix(labelStart, labelLen, str_lxc_payload_prefix)) { + const size_t cgroupNameLen = labelLen - strlen(str_lxc_payload_prefix); + + if (!StrBuf_putsz(s, w, "[lxc:")) + return false; + + if (!StrBuf_putsn(s, w, cgroup + strlen(str_lxc_payload_prefix), cgroupNameLen)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = nextSlash; + + continue; + } + + if (Label_checkPrefix(labelStart, labelLen, str_lxc_monitor_prefix)) { + const size_t cgroupNameLen = labelLen - strlen(str_lxc_monitor_prefix); + + if (!StrBuf_putsz(s, w, "[LXC:")) + return false; + + if (!StrBuf_putsn(s, w, cgroup + strlen(str_lxc_monitor_prefix), cgroupNameLen)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = nextSlash; + + continue; + } + + // LXC legacy cgroup naming + if (Label_checkEqual(labelStart, labelLen, str_lxc_monitor_legacy) || + Label_checkEqual(labelStart, labelLen, str_lxc_payload_legacy)) { + bool isMonitor = Label_checkEqual(labelStart, labelLen, str_lxc_monitor_legacy); + + labelStart = nextSlash; + while (*labelStart == '/') + labelStart++; + + nextSlash = strchrnul(labelStart, '/'); + if (nextSlash - labelStart > 0) { + if (!StrBuf_putsz(s, w, isMonitor ? "[LXC:" : "[lxc:")) + return false; + + if (!StrBuf_putsn(s, w, labelStart, nextSlash - labelStart)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = nextSlash; + continue; + } + + labelStart = cgroup; + nextSlash = labelStart + labelLen; + } + + if (Label_checkSuffix(labelStart, labelLen, str_service_suffix)) { + const size_t serviceNameLen = labelLen - strlen(str_service_suffix); + + if (String_startsWith(cgroup, "user@")) { + cgroup = nextSlash; + + while (*cgroup == '/') + cgroup++; + + continue; + } + + if (!StrBuf_putsn(s, w, cgroup, serviceNameLen)) + return false; + + cgroup = nextSlash; + + continue; + } + + if (Label_checkSuffix(labelStart, labelLen, str_scope_suffix)) { + const size_t scopeNameLen = labelLen - strlen(str_scope_suffix); + + if (Label_checkPrefix(labelStart, scopeNameLen, str_nspawn_scope_prefix)) { + const size_t machineScopeNameLen = scopeNameLen - strlen(str_nspawn_scope_prefix); + + const bool is_monitor = String_startsWith(nextSlash, str_nspawn_monitor_label); + + if (!StrBuf_putsz(s, w, is_monitor ? "[SNC:" : "[snc:")) + return false; + + if (!StrBuf_putsn(s, w, cgroup + strlen(str_nspawn_scope_prefix), machineScopeNameLen)) + return false; + + if (!w(s, ']')) + return false; + + cgroup = nextSlash; + if (String_startsWith(nextSlash, str_nspawn_monitor_label)) + cgroup += strlen(str_nspawn_monitor_label); + else if (String_startsWith(nextSlash, str_nspawn_payload_label)) + cgroup += strlen(str_nspawn_payload_label); + + continue; + } else if (Label_checkPrefix(labelStart, scopeNameLen, str_snap_scope_prefix)) { + const char* nextDot = strchrnul(labelStart + strlen(str_snap_scope_prefix), '.'); + + if (!StrBuf_putsz(s, w, "!snap:")) + return false; + + if (nextDot >= labelStart + scopeNameLen) { + nextDot = labelStart + scopeNameLen; + } + + if (!StrBuf_putsn(s, w, labelStart + strlen(str_snap_scope_prefix), nextDot - (labelStart + strlen(str_snap_scope_prefix)))) + return false; + + cgroup = nextSlash; + + continue; + } + + if (!w(s, '!')) + return false; + + if (!StrBuf_putsn(s, w, cgroup, scopeNameLen)) + return false; + + cgroup = nextSlash; + + continue; + } + + // Default behavior: Copy the full label + cgroup = labelStart; + + if (!StrBuf_putsn(s, w, cgroup, labelLen)) + return false; + + cgroup = nextSlash; + } + + return true; +} + +char* CGroup_filterName(const char* cgroup) { + StrBuf_state s = { + .buf = NULL, + .size = 0, + .pos = 0, + }; + + if (!CGroup_filterName_internal(cgroup, &s, StrBuf_putc_count)) { + return NULL; + } + + s.buf = xCalloc(s.pos + 1, sizeof(char)); + s.size = s.pos; + s.pos = 0; + + if (!CGroup_filterName_internal(cgroup, &s, StrBuf_putc_write)) { + free(s.buf); + return NULL; + } + + s.buf[s.size] = '\0'; + return s.buf; +} diff --git a/linux/CGroupUtils.h b/linux/CGroupUtils.h new file mode 100644 index 0000000..ff13437 --- /dev/null +++ b/linux/CGroupUtils.h @@ -0,0 +1,16 @@ +#ifndef HEADER_CGroupUtils +#define HEADER_CGroupUtils +/* +htop - CGroupUtils.h +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include <stdbool.h> +#include <stddef.h> + + +char* CGroup_filterName(const char* cgroup); + +#endif /* HEADER_CGroupUtils */ diff --git a/linux/HugePageMeter.c b/linux/HugePageMeter.c new file mode 100644 index 0000000..1efde2f --- /dev/null +++ b/linux/HugePageMeter.c @@ -0,0 +1,106 @@ +/* +htop - HugePageMeter.c +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/HugePageMeter.h" + +#include <assert.h> +#include <math.h> +#include <stddef.h> + +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "ProcessList.h" +#include "RichString.h" +#include "linux/LinuxProcessList.h" + + +static const char* HugePageMeter_active_labels[4] = { NULL, NULL, NULL, NULL }; + +static const int HugePageMeter_attributes[] = { + HUGEPAGE_1, + HUGEPAGE_2, + HUGEPAGE_3, + HUGEPAGE_4, +}; + +static const char* const HugePageMeter_labels[] = { + " 64K:", " 128K:", " 256K:", " 512K:", + " 1M:", " 2M:", " 4M:", " 8M:", " 16M:", " 32M:", " 64M:", " 128M:", " 256M:", " 512M:", + " 1G:", " 2G:", " 4G:", " 8G:", " 16G:", " 32G:", " 64G:", " 128G:", " 256G:", " 512G:", +}; + +static void HugePageMeter_updateValues(Meter* this) { + assert(ARRAYSIZE(HugePageMeter_labels) == HTOP_HUGEPAGE_COUNT); + + char* buffer = this->txtBuffer; + size_t size = sizeof(this->txtBuffer); + int written; + memory_t usedTotal = 0; + unsigned nextUsed = 0; + + const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl; + this->total = lpl->totalHugePageMem; + this->values[0] = 0; + HugePageMeter_active_labels[0] = " used:"; + for (unsigned i = 1; i < ARRAYSIZE(HugePageMeter_active_labels); i++) { + this->values[i] = NAN; + HugePageMeter_active_labels[i] = NULL; + } + for (unsigned i = 0; i < HTOP_HUGEPAGE_COUNT; i++) { + memory_t value = lpl->usedHugePageMem[i]; + if (value != MEMORY_MAX) { + this->values[nextUsed] = value; + usedTotal += value; + HugePageMeter_active_labels[nextUsed] = HugePageMeter_labels[i]; + if (++nextUsed == ARRAYSIZE(HugePageMeter_active_labels)) { + break; + } + } + } + + written = Meter_humanUnit(buffer, usedTotal, size); + METER_BUFFER_CHECK(buffer, size, written); + + METER_BUFFER_APPEND_CHR(buffer, size, '/'); + + Meter_humanUnit(buffer, this->total, size); +} + +static void HugePageMeter_display(const Object* cast, RichString* out) { + char buffer[50]; + const Meter* this = (const Meter*)cast; + + RichString_writeAscii(out, CRT_colors[METER_TEXT], ":"); + Meter_humanUnit(buffer, this->total, sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); + + for (unsigned i = 0; i < ARRAYSIZE(HugePageMeter_active_labels); i++) { + if (isnan(this->values[i])) { + break; + } + RichString_appendAscii(out, CRT_colors[METER_TEXT], HugePageMeter_active_labels[i]); + Meter_humanUnit(buffer, this->values[i], sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[HUGEPAGE_1 + i], buffer); + } +} + +const MeterClass HugePageMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = HugePageMeter_display, + }, + .updateValues = HugePageMeter_updateValues, + .defaultMode = BAR_METERMODE, + .maxItems = ARRAYSIZE(HugePageMeter_active_labels), + .total = 100.0, + .attributes = HugePageMeter_attributes, + .name = "HugePages", + .uiName = "HugePages", + .caption = "HP" +}; diff --git a/linux/HugePageMeter.h b/linux/HugePageMeter.h new file mode 100644 index 0000000..d74a19e --- /dev/null +++ b/linux/HugePageMeter.h @@ -0,0 +1,15 @@ +#ifndef HEADER_HugePageMeter +#define HEADER_HugePageMeter +/* +htop - HugePageMeter.h +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Meter.h" + + +extern const MeterClass HugePageMeter_class; + +#endif /* HEADER_HugePageMeter */ diff --git a/linux/IOPriority.h b/linux/IOPriority.h new file mode 100644 index 0000000..78bc470 --- /dev/null +++ b/linux/IOPriority.h @@ -0,0 +1,36 @@ +#ifndef HEADER_IOPriority +#define HEADER_IOPriority +/* +htop - IOPriority.h +(C) 2004-2012 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. + +Based on ionice, +Copyright (C) 2005 Jens Axboe <jens@axboe.dk> +Released under the terms of the GNU General Public License version 2 +*/ + +enum { + IOPRIO_CLASS_NONE, + IOPRIO_CLASS_RT, + IOPRIO_CLASS_BE, + IOPRIO_CLASS_IDLE, +}; + +#define IOPRIO_WHO_PROCESS 1 + +#define IOPRIO_CLASS_SHIFT (13) +#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1) + +#define IOPriority_class(ioprio_) ((int) ((ioprio_) >> IOPRIO_CLASS_SHIFT) ) +#define IOPriority_data(ioprio_) ((int) ((ioprio_) & IOPRIO_PRIO_MASK) ) + +typedef int IOPriority; + +#define IOPriority_tuple(class_, data_) (((class_) << IOPRIO_CLASS_SHIFT) | (data_)) + +#define IOPriority_None IOPriority_tuple(IOPRIO_CLASS_NONE, 0) +#define IOPriority_Idle IOPriority_tuple(IOPRIO_CLASS_IDLE, 7) + +#endif diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c new file mode 100644 index 0000000..3e91bc4 --- /dev/null +++ b/linux/IOPriorityPanel.c @@ -0,0 +1,57 @@ +/* +htop - IOPriorityPanel.c +(C) 2004-2012 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/IOPriorityPanel.h" + +#include <stdbool.h> +#include <stddef.h> + +#include "FunctionBar.h" +#include "ListItem.h" +#include "Object.h" +#include "XUtils.h" +#include "IOPriority.h" + + +Panel* IOPriorityPanel_new(IOPriority currPrio) { + Panel* this = Panel_new(1, 1, 1, 1, Class(ListItem), true, FunctionBar_newEnterEsc("Set ", "Cancel ")); + + Panel_setHeader(this, "IO Priority:"); + Panel_add(this, (Object*) ListItem_new("None (based on nice)", IOPriority_None)); + if (currPrio == IOPriority_None) { + Panel_setSelected(this, 0); + } + static const struct { + int klass; + const char* name; + } classes[] = { + { .klass = IOPRIO_CLASS_RT, .name = "Realtime" }, + { .klass = IOPRIO_CLASS_BE, .name = "Best-effort" }, + { .klass = 0, .name = NULL } + }; + for (int c = 0; classes[c].name; c++) { + for (int i = 0; i < 8; i++) { + char name[50]; + xSnprintf(name, sizeof(name), "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : "")); + IOPriority ioprio = IOPriority_tuple(classes[c].klass, i); + Panel_add(this, (Object*) ListItem_new(name, ioprio)); + if (currPrio == ioprio) { + Panel_setSelected(this, Panel_size(this) - 1); + } + } + } + Panel_add(this, (Object*) ListItem_new("Idle", IOPriority_Idle)); + if (currPrio == IOPriority_Idle) { + Panel_setSelected(this, Panel_size(this) - 1); + } + return this; +} + +IOPriority IOPriorityPanel_getIOPriority(Panel* this) { + const ListItem* selected = (ListItem*) Panel_getSelected(this); + return selected ? selected->key : IOPriority_None; +} diff --git a/linux/IOPriorityPanel.h b/linux/IOPriorityPanel.h new file mode 100644 index 0000000..cb5b338 --- /dev/null +++ b/linux/IOPriorityPanel.h @@ -0,0 +1,17 @@ +#ifndef HEADER_IOPriorityPanel +#define HEADER_IOPriorityPanel +/* +htop - IOPriorityPanel.h +(C) 2004-2012 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Panel.h" +#include "linux/IOPriority.h" + +Panel* IOPriorityPanel_new(IOPriority currPrio); + +IOPriority IOPriorityPanel_getIOPriority(Panel* this); + +#endif diff --git a/linux/LibSensors.c b/linux/LibSensors.c new file mode 100644 index 0000000..9a27fe5 --- /dev/null +++ b/linux/LibSensors.c @@ -0,0 +1,269 @@ +#include "linux/LibSensors.h" + +#include "config.h" + +#ifdef HAVE_SENSORS_SENSORS_H + +#include <assert.h> +#include <dlfcn.h> +#include <errno.h> +#include <limits.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sensors/sensors.h> + +#include "Macros.h" +#include "XUtils.h" +#include "linux/LinuxProcessList.h" + + +#ifdef BUILD_STATIC + +#define sym_sensors_init sensors_init +#define sym_sensors_cleanup sensors_cleanup +#define sym_sensors_get_detected_chips sensors_get_detected_chips +#define sym_sensors_get_features sensors_get_features +#define sym_sensors_get_subfeature sensors_get_subfeature +#define sym_sensors_get_value sensors_get_value + +#else + +static int (*sym_sensors_init)(FILE*); +static void (*sym_sensors_cleanup)(void); +static const sensors_chip_name* (*sym_sensors_get_detected_chips)(const sensors_chip_name*, int*); +static const sensors_feature* (*sym_sensors_get_features)(const sensors_chip_name*, int*); +static const sensors_subfeature* (*sym_sensors_get_subfeature)(const sensors_chip_name*, const sensors_feature*, sensors_subfeature_type); +static int (*sym_sensors_get_value)(const sensors_chip_name*, int, double*); + +static void* dlopenHandle = NULL; + +#endif /* BUILD_STATIC */ + +int LibSensors_init(void) { +#ifdef BUILD_STATIC + + return sym_sensors_init(NULL); + +#else + + if (!dlopenHandle) { + /* Find the unversioned libsensors.so (symlink) and prefer that, but Debian has .so.5 and Fedora .so.4 without + matching symlinks (unless people install the -dev packages) */ + dlopenHandle = dlopen("libsensors.so", RTLD_LAZY); + if (!dlopenHandle) + dlopenHandle = dlopen("libsensors.so.5", RTLD_LAZY); + if (!dlopenHandle) + dlopenHandle = dlopen("libsensors.so.4", RTLD_LAZY); + if (!dlopenHandle) + goto dlfailure; + + /* Clear any errors */ + dlerror(); + + #define resolve(symbolname) do { \ + *(void **)(&sym_##symbolname) = dlsym(dlopenHandle, #symbolname); \ + if (!sym_##symbolname || dlerror() != NULL) \ + goto dlfailure; \ + } while(0) + + resolve(sensors_init); + resolve(sensors_cleanup); + resolve(sensors_get_detected_chips); + resolve(sensors_get_features); + resolve(sensors_get_subfeature); + resolve(sensors_get_value); + + #undef resolve + } + + return sym_sensors_init(NULL); + + +dlfailure: + if (dlopenHandle) { + dlclose(dlopenHandle); + dlopenHandle = NULL; + } + return -1; + +#endif /* BUILD_STATIC */ +} + +void LibSensors_cleanup(void) { +#ifdef BUILD_STATIC + + sym_sensors_cleanup(); + +#else + + if (dlopenHandle) { + sym_sensors_cleanup(); + + dlclose(dlopenHandle); + dlopenHandle = NULL; + } + +#endif /* BUILD_STATIC */ +} + +int LibSensors_reload(void) { +#ifndef BUILD_STATIC + if (!dlopenHandle) { + errno = ENOTSUP; + return -1; + } +#endif /* !BUILD_STATIC */ + + sym_sensors_cleanup(); + return sym_sensors_init(NULL); +} + +static int tempDriverPriority(const sensors_chip_name* chip) { + static const struct TempDriverDefs { + const char* prefix; + int priority; + } tempDrivers[] = { + { "coretemp", 0 }, + { "via_cputemp", 0 }, + { "cpu_thermal", 0 }, + { "k10temp", 0 }, + { "zenpower", 0 }, + /* Low priority drivers */ + { "acpitz", 1 }, + }; + + for (size_t i = 0; i < ARRAYSIZE(tempDrivers); i++) + if (String_eq(chip->prefix, tempDrivers[i].prefix)) + return tempDrivers[i].priority; + + return -1; +} + +void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs) { + assert(existingCPUs > 0 && existingCPUs < 16384); + double data[existingCPUs + 1]; + for (size_t i = 0; i < existingCPUs + 1; i++) + data[i] = NAN; + +#ifndef BUILD_STATIC + if (!dlopenHandle) + goto out; +#endif /* !BUILD_STATIC */ + + unsigned int coreTempCount = 0; + int topPriority = 99; + + int n = 0; + for (const sensors_chip_name* chip = sym_sensors_get_detected_chips(NULL, &n); chip; chip = sym_sensors_get_detected_chips(NULL, &n)) { + const int priority = tempDriverPriority(chip); + if (priority < 0) + continue; + + if (priority > topPriority) + continue; + + if (priority < topPriority) { + /* Clear data from lower priority sensor */ + for (size_t i = 0; i < existingCPUs + 1; i++) + data[i] = NAN; + } + + topPriority = priority; + + int m = 0; + for (const sensors_feature* feature = sym_sensors_get_features(chip, &m); feature; feature = sym_sensors_get_features(chip, &m)) { + if (feature->type != SENSORS_FEATURE_TEMP) + continue; + + if (!feature->name || !String_startsWith(feature->name, "temp")) + continue; + + unsigned long int tempID = strtoul(feature->name + strlen("temp"), NULL, 10); + if (tempID == 0 || tempID == ULONG_MAX) + continue; + + /* Feature name IDs start at 1, adjust to start at 0 to match data indices */ + tempID--; + + if (tempID > existingCPUs) + continue; + + const sensors_subfeature* subFeature = sym_sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT); + if (!subFeature) + continue; + + double temp; + int r = sym_sensors_get_value(chip, subFeature->number, &temp); + if (r != 0) + continue; + + /* If already set, e.g. Ryzen reporting platform temperature for each die, use the bigger one */ + if (isnan(data[tempID])) { + data[tempID] = temp; + if (tempID > 0) + coreTempCount++; + } else { + data[tempID] = MAXIMUM(data[tempID], temp); + } + } + } + + /* Adjust data for chips not providing a platform temperature */ + if (coreTempCount + 1 == activeCPUs || coreTempCount + 1 == activeCPUs / 2) { + memmove(&data[1], &data[0], existingCPUs * sizeof(*data)); + data[0] = NAN; + coreTempCount++; + + /* Check for further adjustments */ + } + + /* Only package temperature - copy to all cores */ + if (coreTempCount == 0 && !isnan(data[0])) { + for (unsigned int i = 1; i <= existingCPUs; i++) + data[i] = data[0]; + + /* No further adjustments */ + goto out; + } + + /* No package temperature - set to max core temperature */ + if (isnan(data[0]) && coreTempCount != 0) { + double maxTemp = NAN; + for (unsigned int i = 1; i <= existingCPUs; i++) { + if (isnan(data[i])) + continue; + + maxTemp = MAXIMUM(maxTemp, data[i]); + } + + data[0] = maxTemp; + + /* Check for further adjustments */ + } + + /* Only temperature for core 0, maybe Ryzen - copy to all other cores */ + if (coreTempCount == 1 && !isnan(data[1])) { + for (unsigned int i = 2; i <= existingCPUs; i++) + data[i] = data[1]; + + /* No further adjustments */ + goto out; + } + + /* Half the temperatures, probably HT/SMT - copy to second half */ + const unsigned int delta = activeCPUs / 2; + if (coreTempCount == delta) { + memcpy(&data[delta + 1], &data[1], delta * sizeof(*data)); + + /* No further adjustments */ + goto out; + } + +out: + for (unsigned int i = 0; i <= existingCPUs; i++) + cpus[i].temperature = data[i]; +} + +#endif /* HAVE_SENSORS_SENSORS_H */ diff --git a/linux/LibSensors.h b/linux/LibSensors.h new file mode 100644 index 0000000..aa89979 --- /dev/null +++ b/linux/LibSensors.h @@ -0,0 +1,13 @@ +#ifndef HEADER_LibSensors +#define HEADER_LibSensors + +#include "linux/LinuxProcessList.h" + + +int LibSensors_init(void); +void LibSensors_cleanup(void); +int LibSensors_reload(void); + +void LibSensors_getCPUTemperatures(CPUData* cpus, unsigned int existingCPUs, unsigned int activeCPUs); + +#endif /* HEADER_LibSensors */ diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c new file mode 100644 index 0000000..381b7cf --- /dev/null +++ b/linux/LinuxProcess.c @@ -0,0 +1,412 @@ +/* +htop - LinuxProcess.c +(C) 2014 Hisham H. Muhammad +(C) 2020 Red Hat, Inc. All Rights Reserved. +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/LinuxProcess.h" + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <syscall.h> +#include <unistd.h> + +#include "CRT.h" +#include "Macros.h" +#include "Process.h" +#include "ProvideCurses.h" +#include "RichString.h" +#include "XUtils.h" +#include "linux/IOPriority.h" + + +/* semi-global */ +int pageSize; +int pageSizeKB; + +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, R running, D disk, Z zombie, T traced, W paging, I idle)", .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, }, + [CMINFLT] = { .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .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, }, + [CMAJFLT] = { .name = "CMAJFLT", .title = " CMAJFLT ", .description = "Children processes' major faults", .flags = 0, .defaultSortDesc = true, }, + [UTIME] = { .name = "UTIME", .title = " UTIME+ ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, .defaultSortDesc = true, }, + [STIME] = { .name = "STIME", .title = " STIME+ ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, .defaultSortDesc = true, }, + [CUTIME] = { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, .defaultSortDesc = true, }, + [CSTIME] = { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .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, }, + [M_SHARE] = { .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, .defaultSortDesc = true, }, + [M_TRS] = { .name = "M_TRS", .title = " CODE ", .description = "Size of the .text segment of the process (CODE)", .flags = 0, .defaultSortDesc = true, }, + [M_DRS] = { .name = "M_DRS", .title = " DATA ", .description = "Size of the .data segment plus stack usage of the process (DATA)", .flags = 0, .defaultSortDesc = true, }, + [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process (calculated from memory maps)", .flags = PROCESS_FLAG_LINUX_LRS_FIX, .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, .defaultSortDesc = true, }, + [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, }, +#ifdef HAVE_OPENVZ + [CTID] = { .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, }, + [VPID] = { .name = "VPID", .title = "VPID", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, .pidColumn = true, }, +#endif +#ifdef HAVE_VSERVER + [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, }, +#endif + [RCHAR] = { .name = "RCHAR", .title = "RCHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [WCHAR] = { .name = "WCHAR", .title = "WCHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [SYSCR] = { .name = "SYSCR", .title = " READ_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [SYSCW] = { .name = "SYSCW", .title = " WRITE_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [RBYTES] = { .name = "RBYTES", .title = " IO_R ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [WBYTES] = { .name = "WBYTES", .title = " IO_W ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [CNCLWB] = { .name = "CNCLWB", .title = " IO_C ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [IO_READ_RATE] = { .name = "IO_READ_RATE", .title = " DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [IO_WRITE_RATE] = { .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [IO_RATE] = { .name = "IO_RATE", .title = " DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, .defaultSortDesc = true, }, + [CGROUP] = { .name = "CGROUP", .title = "CGROUP (raw)", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, .autoWidth = true, }, + [CCGROUP] = { .name = "CCGROUP", .title = "CGROUP (compressed)", .description = "Which cgroup the process is in (condensed to essentials)", .flags = PROCESS_FLAG_LINUX_CGROUP, .autoWidth = true, }, + [OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, .defaultSortDesc = true, }, + [IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, }, +#ifdef HAVE_DELAYACCT + [PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD% ", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, }, + [PERCENT_IO_DELAY] = { .name = "PERCENT_IO_DELAY", .title = " IOD% ", .description = "Block I/O delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, }, + [PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWPD% ", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, }, +#endif + [M_PSS] = { .name = "M_PSS", .title = " PSS ", .description = "proportional set size, same as M_RESIDENT but each page is divided by the number of processes sharing it", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, }, + [M_SWAP] = { .name = "M_SWAP", .title = " SWAP ", .description = "Size of the process's swapped pages", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, }, + [M_PSSWP] = { .name = "M_PSSWP", .title = " PSSWP ", .description = "shows proportional swap share of this mapping, unlike \"Swap\", this does not take into account swapped out page of underlying shmem objects", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, }, + [CTXT] = { .name = "CTXT", .title = " CTXT ", .description = "Context switches (incremental sum of voluntary_ctxt_switches and nonvoluntary_ctxt_switches)", .flags = PROCESS_FLAG_LINUX_CTXT, .defaultSortDesc = true, }, + [SECATTR] = { .name = "SECATTR", .title = "Security Attribute", .description = "Security attribute of the process (e.g. SELinux or AppArmor)", .flags = PROCESS_FLAG_LINUX_SECATTR, .autoWidth = true, }, + [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process from /proc/[pid]/comm", .flags = 0, }, + [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process from /proc/[pid]/exe", .flags = 0, }, + [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, }, + [AUTOGROUP_ID] = { .name = "AUTOGROUP_ID", .title = "AGRP", .description = "The autogroup identifier of the process", .flags = PROCESS_FLAG_LINUX_AUTOGROUP, }, + [AUTOGROUP_NICE] = { .name = "AUTOGROUP_NICE", .title = " ANI", .description = "Nice value (the higher the value, the more other processes take priority) associated with the process autogroup", .flags = PROCESS_FLAG_LINUX_AUTOGROUP, }, +}; + +Process* LinuxProcess_new(const Settings* settings) { + LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess)); + Object_setClass(this, Class(LinuxProcess)); + Process_init(&this->super, settings); + return &this->super; +} + +void Process_delete(Object* cast) { + LinuxProcess* this = (LinuxProcess*) cast; + Process_done((Process*)cast); + free(this->cgroup_short); + free(this->cgroup); +#ifdef HAVE_OPENVZ + free(this->ctid); +#endif + free(this->secattr); + free(this); +} + +/* +[1] Note that before kernel 2.6.26 a process that has not asked for +an io priority formally uses "none" as scheduling class, but the +io scheduler will treat such processes as if it were in the best +effort class. The priority within the best effort class will be +dynamically derived from the cpu nice level of the process: +io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page +*/ +static int LinuxProcess_effectiveIOPriority(const LinuxProcess* this) { + if (IOPriority_class(this->ioPriority) == IOPRIO_CLASS_NONE) { + return IOPriority_tuple(IOPRIO_CLASS_BE, (this->super.nice + 20) / 5); + } + + return this->ioPriority; +} + +#ifdef __ANDROID__ +#define SYS_ioprio_get __NR_ioprio_get +#define SYS_ioprio_set __NR_ioprio_set +#endif + +IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) { + IOPriority ioprio = 0; +// Other OSes masquerading as Linux (NetBSD?) don't have this syscall +#ifdef SYS_ioprio_get + ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->super.pid); +#endif + this->ioPriority = ioprio; + return ioprio; +} + +bool LinuxProcess_setIOPriority(Process* this, Arg ioprio) { +// Other OSes masquerading as Linux (NetBSD?) don't have this syscall +#ifdef SYS_ioprio_set + syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio.i); +#endif + return (LinuxProcess_updateIOPriority((LinuxProcess*)this) == ioprio.i); +} + +bool LinuxProcess_isAutogroupEnabled(void) { + char buf[16]; + if (xReadfile(PROCDIR "/sys/kernel/sched_autogroup_enabled", buf, sizeof(buf)) < 0) + return false; + return buf[0] == '1'; +} + +bool LinuxProcess_changeAutogroupPriorityBy(Process* this, Arg delta) { + char buffer[256]; + xSnprintf(buffer, sizeof(buffer), PROCDIR "/%d/autogroup", this->pid); + + FILE* file = fopen(buffer, "r+"); + if (!file) + return false; + + long int identity; + int nice; + int ok = fscanf(file, "/autogroup-%ld nice %d", &identity, &nice); + bool success; + if (ok == 2) { + rewind(file); + xSnprintf(buffer, sizeof(buffer), "%d", nice + delta.i); + success = fputs(buffer, file) > 0; + } else { + success = false; + } + + fclose(file); + return success; +} + +static void LinuxProcess_writeField(const Process* this, RichString* str, ProcessField field) { + const LinuxProcess* lp = (const LinuxProcess*) this; + bool coloring = this->settings->highlightMegabytes; + char buffer[256]; buffer[255] = '\0'; + int attr = CRT_colors[DEFAULT_COLOR]; + size_t n = sizeof(buffer) - 1; + switch (field) { + case CMINFLT: Process_printCount(str, lp->cminflt, coloring); return; + case CMAJFLT: Process_printCount(str, lp->cmajflt, coloring); return; + case M_DRS: Process_printBytes(str, lp->m_drs * pageSize, coloring); return; + case M_LRS: + if (lp->m_lrs) { + Process_printBytes(str, lp->m_lrs * pageSize, coloring); + return; + } + + attr = CRT_colors[PROCESS_SHADOW]; + xSnprintf(buffer, n, " N/A "); + break; + case M_TRS: Process_printBytes(str, lp->m_trs * pageSize, coloring); return; + case M_SHARE: Process_printBytes(str, lp->m_share * pageSize, coloring); return; + case M_PSS: Process_printKBytes(str, lp->m_pss, coloring); return; + case M_SWAP: Process_printKBytes(str, lp->m_swap, coloring); return; + case M_PSSWP: Process_printKBytes(str, lp->m_psswp, coloring); return; + case UTIME: Process_printTime(str, lp->utime, coloring); return; + case STIME: Process_printTime(str, lp->stime, coloring); return; + case CUTIME: Process_printTime(str, lp->cutime, coloring); return; + case CSTIME: Process_printTime(str, lp->cstime, coloring); return; + case RCHAR: Process_printBytes(str, lp->io_rchar, coloring); return; + case WCHAR: Process_printBytes(str, lp->io_wchar, coloring); return; + case SYSCR: Process_printCount(str, lp->io_syscr, coloring); return; + case SYSCW: Process_printCount(str, lp->io_syscw, coloring); return; + case RBYTES: Process_printBytes(str, lp->io_read_bytes, coloring); return; + case WBYTES: Process_printBytes(str, lp->io_write_bytes, coloring); return; + case CNCLWB: Process_printBytes(str, lp->io_cancelled_write_bytes, coloring); return; + case IO_READ_RATE: Process_printRate(str, lp->io_rate_read_bps, coloring); return; + case IO_WRITE_RATE: Process_printRate(str, lp->io_rate_write_bps, coloring); return; + case IO_RATE: { + double totalRate; + if (!isnan(lp->io_rate_read_bps) && !isnan(lp->io_rate_write_bps)) + totalRate = lp->io_rate_read_bps + lp->io_rate_write_bps; + else if (!isnan(lp->io_rate_read_bps)) + totalRate = lp->io_rate_read_bps; + else if (!isnan(lp->io_rate_write_bps)) + totalRate = lp->io_rate_write_bps; + else + totalRate = NAN; + Process_printRate(str, totalRate, coloring); + return; + } + #ifdef HAVE_OPENVZ + case CTID: xSnprintf(buffer, n, "%-8s ", lp->ctid ? lp->ctid : ""); break; + case VPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, lp->vpid); break; + #endif + #ifdef HAVE_VSERVER + case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break; + #endif + case CGROUP: xSnprintf(buffer, n, "%-*.*s ", Process_fieldWidths[CGROUP], Process_fieldWidths[CGROUP], lp->cgroup ? lp->cgroup : "N/A"); break; + case CCGROUP: xSnprintf(buffer, n, "%-*.*s ", Process_fieldWidths[CCGROUP], Process_fieldWidths[CCGROUP], lp->cgroup_short ? lp->cgroup_short : (lp->cgroup ? lp->cgroup : "N/A")); break; + case OOM: xSnprintf(buffer, n, "%4u ", lp->oom); break; + case IO_PRIORITY: { + int klass = IOPriority_class(lp->ioPriority); + if (klass == IOPRIO_CLASS_NONE) { + // see note [1] above + xSnprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5); + } else if (klass == IOPRIO_CLASS_BE) { + xSnprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority)); + } else if (klass == IOPRIO_CLASS_RT) { + attr = CRT_colors[PROCESS_HIGH_PRIORITY]; + xSnprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority)); + } else if (klass == IOPRIO_CLASS_IDLE) { + attr = CRT_colors[PROCESS_LOW_PRIORITY]; + xSnprintf(buffer, n, "id "); + } else { + xSnprintf(buffer, n, "?? "); + } + break; + } + #ifdef HAVE_DELAYACCT + case PERCENT_CPU_DELAY: Process_printPercentage(lp->cpu_delay_percent, buffer, n, 5, &attr); break; + case PERCENT_IO_DELAY: Process_printPercentage(lp->blkio_delay_percent, buffer, n, 5, &attr); break; + case PERCENT_SWAP_DELAY: Process_printPercentage(lp->swapin_delay_percent, buffer, n, 5, &attr); break; + #endif + case CTXT: + if (lp->ctxt_diff > 1000) { + attr |= A_BOLD; + } + xSnprintf(buffer, n, "%5lu ", lp->ctxt_diff); + break; + case SECATTR: snprintf(buffer, n, "%-*.*s ", Process_fieldWidths[SECATTR], Process_fieldWidths[SECATTR], lp->secattr ? lp->secattr : "N/A"); break; + case AUTOGROUP_ID: + if (lp->autogroup_id != -1) { + xSnprintf(buffer, n, "%4ld ", lp->autogroup_id); + } else { + attr = CRT_colors[PROCESS_SHADOW]; + xSnprintf(buffer, n, " N/A "); + } + break; + case AUTOGROUP_NICE: + if (lp->autogroup_id != -1) { + xSnprintf(buffer, n, "%3d ", lp->autogroup_nice); + attr = lp->autogroup_nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY] + : lp->autogroup_nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY] + : CRT_colors[PROCESS_SHADOW]; + } else { + attr = CRT_colors[PROCESS_SHADOW]; + xSnprintf(buffer, n, "N/A "); + } + break; + default: + Process_writeField(this, str, field); + return; + } + RichString_appendAscii(str, attr, buffer); +} + +static double adjustNaN(double num) { + if (isnan(num)) + return -0.0005; + + return num; +} + +static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) { + const LinuxProcess* p1 = (const LinuxProcess*)v1; + const LinuxProcess* p2 = (const LinuxProcess*)v2; + + switch (key) { + case M_DRS: + return SPACESHIP_NUMBER(p1->m_drs, p2->m_drs); + case M_LRS: + return SPACESHIP_NUMBER(p1->m_lrs, p2->m_lrs); + case M_TRS: + return SPACESHIP_NUMBER(p1->m_trs, p2->m_trs); + case M_SHARE: + return SPACESHIP_NUMBER(p1->m_share, p2->m_share); + case M_PSS: + return SPACESHIP_NUMBER(p1->m_pss, p2->m_pss); + case M_SWAP: + return SPACESHIP_NUMBER(p1->m_swap, p2->m_swap); + case M_PSSWP: + return SPACESHIP_NUMBER(p1->m_psswp, p2->m_psswp); + case UTIME: + return SPACESHIP_NUMBER(p1->utime, p2->utime); + case CUTIME: + return SPACESHIP_NUMBER(p1->cutime, p2->cutime); + case STIME: + return SPACESHIP_NUMBER(p1->stime, p2->stime); + case CSTIME: + return SPACESHIP_NUMBER(p1->cstime, p2->cstime); + case RCHAR: + return SPACESHIP_NUMBER(p1->io_rchar, p2->io_rchar); + case WCHAR: + return SPACESHIP_NUMBER(p1->io_wchar, p2->io_wchar); + case SYSCR: + return SPACESHIP_NUMBER(p1->io_syscr, p2->io_syscr); + case SYSCW: + return SPACESHIP_NUMBER(p1->io_syscw, p2->io_syscw); + case RBYTES: + return SPACESHIP_NUMBER(p1->io_read_bytes, p2->io_read_bytes); + case WBYTES: + return SPACESHIP_NUMBER(p1->io_write_bytes, p2->io_write_bytes); + case CNCLWB: + return SPACESHIP_NUMBER(p1->io_cancelled_write_bytes, p2->io_cancelled_write_bytes); + case IO_READ_RATE: + return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_read_bps), adjustNaN(p2->io_rate_read_bps)); + case IO_WRITE_RATE: + return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_write_bps), adjustNaN(p2->io_rate_write_bps)); + case IO_RATE: + return SPACESHIP_NUMBER(adjustNaN(p1->io_rate_read_bps) + adjustNaN(p1->io_rate_write_bps), adjustNaN(p2->io_rate_read_bps) + adjustNaN(p2->io_rate_write_bps)); + #ifdef HAVE_OPENVZ + case CTID: + return SPACESHIP_NULLSTR(p1->ctid, p2->ctid); + case VPID: + return SPACESHIP_NUMBER(p1->vpid, p2->vpid); + #endif + #ifdef HAVE_VSERVER + case VXID: + return SPACESHIP_NUMBER(p1->vxid, p2->vxid); + #endif + case CGROUP: + return SPACESHIP_NULLSTR(p1->cgroup, p2->cgroup); + case CCGROUP: + return SPACESHIP_NULLSTR(p1->cgroup_short, p2->cgroup_short); + case OOM: + return SPACESHIP_NUMBER(p1->oom, p2->oom); + #ifdef HAVE_DELAYACCT + case PERCENT_CPU_DELAY: + return SPACESHIP_NUMBER(p1->cpu_delay_percent, p2->cpu_delay_percent); + case PERCENT_IO_DELAY: + return SPACESHIP_NUMBER(p1->blkio_delay_percent, p2->blkio_delay_percent); + case PERCENT_SWAP_DELAY: + return SPACESHIP_NUMBER(p1->swapin_delay_percent, p2->swapin_delay_percent); + #endif + case IO_PRIORITY: + return SPACESHIP_NUMBER(LinuxProcess_effectiveIOPriority(p1), LinuxProcess_effectiveIOPriority(p2)); + case CTXT: + return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff); + case SECATTR: + return SPACESHIP_NULLSTR(p1->secattr, p2->secattr); + case AUTOGROUP_ID: + return SPACESHIP_NUMBER(p1->autogroup_id, p2->autogroup_id); + case AUTOGROUP_NICE: + return SPACESHIP_NUMBER(p1->autogroup_nice, p2->autogroup_nice); + default: + return Process_compareByKey_Base(v1, v2, key); + } +} + +const ProcessClass LinuxProcess_class = { + .super = { + .extends = Class(Process), + .display = Process_display, + .delete = Process_delete, + .compare = Process_compare + }, + .writeField = LinuxProcess_writeField, + .compareByKey = LinuxProcess_compareByKey +}; diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h new file mode 100644 index 0000000..3e5d380 --- /dev/null +++ b/linux/LinuxProcess.h @@ -0,0 +1,135 @@ +#ifndef HEADER_LinuxProcess +#define HEADER_LinuxProcess +/* +htop - LinuxProcess.h +(C) 2014 Hisham H. Muhammad +(C) 2020 Red Hat, Inc. All Rights Reserved. +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include <stdbool.h> +#include <sys/types.h> + +#include "linux/IOPriority.h" +#include "Object.h" +#include "Process.h" +#include "Settings.h" + + +#define PROCESS_FLAG_LINUX_IOPRIO 0x00000100 +#define PROCESS_FLAG_LINUX_OPENVZ 0x00000200 +#define PROCESS_FLAG_LINUX_VSERVER 0x00000400 +#define PROCESS_FLAG_LINUX_CGROUP 0x00000800 +#define PROCESS_FLAG_LINUX_OOM 0x00001000 +#define PROCESS_FLAG_LINUX_SMAPS 0x00002000 +#define PROCESS_FLAG_LINUX_CTXT 0x00004000 +#define PROCESS_FLAG_LINUX_SECATTR 0x00008000 +#define PROCESS_FLAG_LINUX_LRS_FIX 0x00010000 +#define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000 +#define PROCESS_FLAG_LINUX_AUTOGROUP 0x00080000 + +typedef struct LinuxProcess_ { + Process super; + IOPriority ioPriority; + unsigned long int cminflt; + unsigned long int cmajflt; + unsigned long long int utime; + unsigned long long int stime; + unsigned long long int cutime; + unsigned long long int cstime; + long m_share; + long m_pss; + long m_swap; + long m_psswp; + long m_trs; + long m_drs; + long m_lrs; + + /* Process flags */ + unsigned long int flags; + + /* Data read (in bytes) */ + unsigned long long io_rchar; + + /* Data written (in bytes) */ + unsigned long long io_wchar; + + /* Number of read(2) syscalls */ + unsigned long long io_syscr; + + /* Number of write(2) syscalls */ + unsigned long long io_syscw; + + /* Storage data read (in bytes) */ + unsigned long long io_read_bytes; + + /* Storage data written (in bytes) */ + unsigned long long io_write_bytes; + + /* Storage data cancelled (in bytes) */ + unsigned long long io_cancelled_write_bytes; + + /* Point in time of last io scan (in milliseconds elapsed since the Epoch) */ + unsigned long long io_last_scan_time_ms; + + /* Storage data read (in bytes per second) */ + double io_rate_read_bps; + + /* Storage data written (in bytes per second) */ + double io_rate_write_bps; + + #ifdef HAVE_OPENVZ + char* ctid; + pid_t vpid; + #endif + #ifdef HAVE_VSERVER + unsigned int vxid; + #endif + char* cgroup; + char* cgroup_short; + unsigned int oom; + #ifdef HAVE_DELAYACCT + unsigned long long int delay_read_time; + unsigned long long cpu_delay_total; + unsigned long long blkio_delay_total; + unsigned long long swapin_delay_total; + float cpu_delay_percent; + float blkio_delay_percent; + float swapin_delay_percent; + #endif + unsigned long ctxt_total; + unsigned long ctxt_diff; + char* secattr; + unsigned long long int last_mlrs_calctime; + + /* Autogroup scheduling (CFS) information */ + long int autogroup_id; + int autogroup_nice; +} LinuxProcess; + +extern int pageSize; + +extern int pageSizeKB; + +extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; + +extern const ProcessClass LinuxProcess_class; + +Process* LinuxProcess_new(const Settings* settings); + +void Process_delete(Object* cast); + +IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this); + +bool LinuxProcess_setIOPriority(Process* this, Arg ioprio); + +bool LinuxProcess_isAutogroupEnabled(void); + +bool LinuxProcess_changeAutogroupPriorityBy(Process* this, Arg delta); + +bool Process_isThread(const Process* this); + +#endif diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c new file mode 100644 index 0000000..eca9459 --- /dev/null +++ b/linux/LinuxProcessList.c @@ -0,0 +1,2302 @@ +/* +htop - LinuxProcessList.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "linux/LinuxProcessList.h" + +#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <math.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <time.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> + +#ifdef HAVE_DELAYACCT +#include <linux/netlink.h> +#include <linux/taskstats.h> +#include <netlink/attr.h> +#include <netlink/handlers.h> +#include <netlink/msg.h> +#include <netlink/netlink.h> +#include <netlink/socket.h> +#include <netlink/genl/genl.h> +#include <netlink/genl/ctrl.h> +#endif + +#include "Compat.h" +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "Process.h" +#include "Settings.h" +#include "XUtils.h" +#include "linux/CGroupUtils.h" +#include "linux/LinuxProcess.h" +#include "linux/Platform.h" // needed for GNU/hurd to get PATH_MAX // IWYU pragma: keep + +#if defined(MAJOR_IN_MKDEV) +#include <sys/mkdev.h> +#elif defined(MAJOR_IN_SYSMACROS) +#include <sys/sysmacros.h> +#endif + +#ifdef HAVE_SENSORS_SENSORS_H +#include "LibSensors.h" +#endif + +#ifndef O_PATH +#define O_PATH 010000000 // declare for ancient glibc versions +#endif + +/* Not exposed yet. Defined at include/linux/sched.h */ +#ifndef PF_KTHREAD +#define PF_KTHREAD 0x00200000 +#endif + +static long long btime = -1; + +static long jiffy; + +static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* mode) { + assert(String_eq(mode, "r")); /* only currently supported mode */ + + int fd = Compat_openat(openatArg, pathname, O_RDONLY); + if (fd < 0) + return NULL; + + FILE* stream = fdopen(fd, mode); + if (!stream) + close(fd); + + return stream; +} + +static int sortTtyDrivers(const void* va, const void* vb) { + const TtyDriver* a = (const TtyDriver*) va; + const TtyDriver* b = (const TtyDriver*) vb; + + int r = SPACESHIP_NUMBER(a->major, b->major); + if (r) + return r; + + return SPACESHIP_NUMBER(a->minorFrom, b->minorFrom); +} + +static void LinuxProcessList_initTtyDrivers(LinuxProcessList* this) { + TtyDriver* ttyDrivers; + + char buf[16384]; + ssize_t r = xReadfile(PROCTTYDRIVERSFILE, buf, sizeof(buf)); + if (r < 0) + return; + + int numDrivers = 0; + int allocd = 10; + ttyDrivers = xMallocArray(allocd, sizeof(TtyDriver)); + char* at = buf; + while (*at != '\0') { + at = strchr(at, ' '); // skip first token + while (*at == ' ') at++; // skip spaces + const char* token = at; // mark beginning of path + at = strchr(at, ' '); // find end of path + *at = '\0'; at++; // clear and skip + ttyDrivers[numDrivers].path = xStrdup(token); // save + while (*at == ' ') at++; // skip spaces + token = at; // mark beginning of major + at = strchr(at, ' '); // find end of major + *at = '\0'; at++; // clear and skip + ttyDrivers[numDrivers].major = atoi(token); // save + while (*at == ' ') at++; // skip spaces + token = at; // mark beginning of minorFrom + while (*at >= '0' && *at <= '9') at++; //find end of minorFrom + if (*at == '-') { // if has range + *at = '\0'; at++; // clear and skip + ttyDrivers[numDrivers].minorFrom = atoi(token); // save + token = at; // mark beginning of minorTo + at = strchr(at, ' '); // find end of minorTo + *at = '\0'; at++; // clear and skip + ttyDrivers[numDrivers].minorTo = atoi(token); // save + } else { // no range + *at = '\0'; at++; // clear and skip + ttyDrivers[numDrivers].minorFrom = atoi(token); // save + ttyDrivers[numDrivers].minorTo = atoi(token); // save + } + at = strchr(at, '\n'); // go to end of line + at++; // skip + numDrivers++; + if (numDrivers == allocd) { + allocd += 10; + ttyDrivers = xReallocArray(ttyDrivers, allocd, sizeof(TtyDriver)); + } + } + numDrivers++; + ttyDrivers = xRealloc(ttyDrivers, sizeof(TtyDriver) * numDrivers); + ttyDrivers[numDrivers - 1].path = NULL; + qsort(ttyDrivers, numDrivers - 1, sizeof(TtyDriver), sortTtyDrivers); + this->ttyDrivers = ttyDrivers; +} + +#ifdef HAVE_DELAYACCT + +static void LinuxProcessList_initNetlinkSocket(LinuxProcessList* this) { + this->netlink_socket = nl_socket_alloc(); + if (this->netlink_socket == NULL) { + return; + } + if (nl_connect(this->netlink_socket, NETLINK_GENERIC) < 0) { + return; + } + this->netlink_family = genl_ctrl_resolve(this->netlink_socket, TASKSTATS_GENL_NAME); +} + +#endif + +static unsigned int scanAvailableCPUsFromCPUinfo(LinuxProcessList* this) { + FILE* file = fopen(PROCCPUINFOFILE, "r"); + if (file == NULL) + return this->super.existingCPUs; + + unsigned int availableCPUs = 0; + + while (!feof(file)) { + char buffer[PROC_LINE_LENGTH]; + + if (fgets(buffer, PROC_LINE_LENGTH, file) == NULL) + break; + + if (String_startsWith(buffer, "processor")) + availableCPUs++; + } + + fclose(file); + + return availableCPUs ? availableCPUs : 1; +} + +static void LinuxProcessList_updateCPUcount(ProcessList* super) { + /* Similar to get_nprocs_conf(3) / _SC_NPROCESSORS_CONF + * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/getsysstats.c;hb=HEAD + */ + + LinuxProcessList* this = (LinuxProcessList*) super; + unsigned int existing = 0, active = 0; + + // Initialize the cpuData array before anything else. + if (!this->cpuData) { + this->cpuData = xCalloc(2, sizeof(CPUData)); + this->cpuData[0].online = true; /* average is always "online" */ + this->cpuData[1].online = true; + super->activeCPUs = 1; + super->existingCPUs = 1; + } + + DIR* dir = opendir("/sys/devices/system/cpu"); + if (!dir) + return; + + unsigned int currExisting = super->existingCPUs; + + const struct dirent* entry; + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) + continue; + + if (!String_startsWith(entry->d_name, "cpu")) + continue; + + char* endp; + unsigned long int id = strtoul(entry->d_name + 3, &endp, 10); + if (id == ULONG_MAX || endp == entry->d_name + 3 || *endp != '\0') + continue; + +#ifdef HAVE_OPENAT + int cpuDirFd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW); + if (cpuDirFd < 0) + continue; +#else + char cpuDirFd[4096]; + xSnprintf(cpuDirFd, sizeof(cpuDirFd), "/sys/devices/system/cpu/%s", entry->d_name); +#endif + + existing++; + + /* readdir() iterates with no specific order */ + unsigned int max = MAXIMUM(existing, id + 1); + if (max > currExisting) { + this->cpuData = xReallocArrayZero(this->cpuData, currExisting ? (currExisting + 1) : 0, max + /* aggregate */ 1, sizeof(CPUData)); + this->cpuData[0].online = true; /* average is always "online" */ + currExisting = max; + } + + char buffer[8]; + ssize_t res = xReadfileat(cpuDirFd, "online", buffer, sizeof(buffer)); + /* If the file "online" does not exist or on failure count as active */ + if (res < 1 || buffer[0] != '0') { + active++; + this->cpuData[id + 1].online = true; + } else { + this->cpuData[id + 1].online = false; + } + + Compat_openatArgClose(cpuDirFd); + } + + closedir(dir); + + // return if no CPU is found + if (existing < 1) + return; + + if (Running_containerized) { + /* LXC munges /proc/cpuinfo but not the /sys/devices/system/cpu/ files, + * so limit the visible CPUs to what the guest has been configured to see: */ + currExisting = active = scanAvailableCPUsFromCPUinfo(this); + } + +#ifdef HAVE_SENSORS_SENSORS_H + /* When started with offline CPUs, libsensors does not monitor those, + * even when they become online. */ + if (super->existingCPUs != 0 && (active > super->activeCPUs || currExisting > super->existingCPUs)) + LibSensors_reload(); +#endif + + super->activeCPUs = active; + assert(Running_containerized || (existing == currExisting)); + super->existingCPUs = currExisting; +} + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId) { + LinuxProcessList* this = xCalloc(1, sizeof(LinuxProcessList)); + ProcessList* pl = &(this->super); + + ProcessList_init(pl, Class(LinuxProcess), usersTable, dynamicMeters, dynamicColumns, pidMatchList, userId); + LinuxProcessList_initTtyDrivers(this); + + // Initialize page size + pageSize = sysconf(_SC_PAGESIZE); + if (pageSize == -1) + CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)"); + pageSizeKB = pageSize / ONE_K; + + // Initialize clock ticks + jiffy = sysconf(_SC_CLK_TCK); + if (jiffy == -1) + CRT_fatalError("Cannot get clock ticks by sysconf(_SC_CLK_TCK)"); + + // Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+) + this->haveSmapsRollup = (access(PROCDIR "/self/smaps_rollup", R_OK) == 0); + + // Read btime (the kernel boot time, as number of seconds since the epoch) + FILE* statfile = fopen(PROCSTATFILE, "r"); + if (statfile == NULL) + CRT_fatalError("Cannot open " PROCSTATFILE); + while (true) { + char buffer[PROC_LINE_LENGTH + 1]; + if (fgets(buffer, sizeof(buffer), statfile) == NULL) + break; + if (String_startsWith(buffer, "btime ") == false) + continue; + if (sscanf(buffer, "btime %lld\n", &btime) == 1) + break; + CRT_fatalError("Failed to parse btime from " PROCSTATFILE); + } + + fclose(statfile); + + if (btime == -1) + CRT_fatalError("No btime in " PROCSTATFILE); + + // Initialize CPU count + LinuxProcessList_updateCPUcount(pl); + + return pl; +} + +void ProcessList_delete(ProcessList* pl) { + LinuxProcessList* this = (LinuxProcessList*) pl; + ProcessList_done(pl); + free(this->cpuData); + if (this->ttyDrivers) { + for (int i = 0; this->ttyDrivers[i].path; i++) { + free(this->ttyDrivers[i].path); + } + free(this->ttyDrivers); + } + #ifdef HAVE_DELAYACCT + if (this->netlink_socket) { + nl_close(this->netlink_socket); + nl_socket_free(this->netlink_socket); + } + #endif + free(this); +} + +static inline unsigned long long LinuxProcessList_adjustTime(unsigned long long t) { + return t * 100 / jiffy; +} + +/* Taken from: https://github.com/torvalds/linux/blob/64570fbc14f8d7cb3fe3995f20e26bc25ce4b2cc/fs/proc/array.c#L120 */ +static inline ProcessState LinuxProcessList_getProcessState(char state) { + switch (state) { + case 'S': return SLEEPING; + case 'X': return DEFUNCT; + case 'Z': return ZOMBIE; + case 't': return TRACED; + case 'T': return STOPPED; + case 'D': return UNINTERRUPTIBLE_WAIT; + case 'R': return RUNNING; + case 'P': return BLOCKED; + case 'I': return IDLE; + default: return UNKNOWN; + } +} + +static bool LinuxProcessList_readStatFile(Process* process, openat_arg_t procFd, char* command, size_t commLen) { + LinuxProcess* lp = (LinuxProcess*) process; + + char buf[MAX_READ + 1]; + ssize_t r = xReadfileat(procFd, "stat", buf, sizeof(buf)); + if (r < 0) + return false; + + /* (1) pid - %d */ + assert(process->pid == atoi(buf)); + char* location = strchr(buf, ' '); + if (!location) + return false; + + /* (2) comm - (%s) */ + location += 2; + char* end = strrchr(location, ')'); + if (!end) + return false; + + String_safeStrncpy(command, location, MINIMUM((size_t)(end - location + 1), commLen)); + + location = end + 2; + + /* (3) state - %c */ + process->state = LinuxProcessList_getProcessState(location[0]); + location += 2; + + /* (4) ppid - %d */ + process->ppid = strtol(location, &location, 10); + location += 1; + + /* (5) pgrp - %d */ + process->pgrp = strtol(location, &location, 10); + location += 1; + + /* (6) session - %d */ + process->session = strtol(location, &location, 10); + location += 1; + + /* (7) tty_nr - %d */ + process->tty_nr = strtoul(location, &location, 10); + location += 1; + + /* (8) tpgid - %d */ + process->tpgid = strtol(location, &location, 10); + location += 1; + + /* (9) flags - %u */ + lp->flags = strtoul(location, &location, 10); + location += 1; + + /* (10) minflt - %lu */ + process->minflt = strtoull(location, &location, 10); + location += 1; + + /* (11) cminflt - %lu */ + lp->cminflt = strtoull(location, &location, 10); + location += 1; + + /* (12) majflt - %lu */ + process->majflt = strtoull(location, &location, 10); + location += 1; + + /* (13) cmajflt - %lu */ + lp->cmajflt = strtoull(location, &location, 10); + location += 1; + + /* (14) utime - %lu */ + lp->utime = LinuxProcessList_adjustTime(strtoull(location, &location, 10)); + location += 1; + + /* (15) stime - %lu */ + lp->stime = LinuxProcessList_adjustTime(strtoull(location, &location, 10)); + location += 1; + + /* (16) cutime - %ld */ + lp->cutime = LinuxProcessList_adjustTime(strtoull(location, &location, 10)); + location += 1; + + /* (17) cstime - %ld */ + lp->cstime = LinuxProcessList_adjustTime(strtoull(location, &location, 10)); + location += 1; + + /* (18) priority - %ld */ + process->priority = strtol(location, &location, 10); + location += 1; + + /* (19) nice - %ld */ + process->nice = strtol(location, &location, 10); + location += 1; + + /* (20) num_threads - %ld */ + process->nlwp = strtol(location, &location, 10); + location += 1; + + /* Skip (21) itrealvalue - %ld */ + location = strchr(location, ' ') + 1; + + /* (22) starttime - %llu */ + if (process->starttime_ctime == 0) { + process->starttime_ctime = btime + LinuxProcessList_adjustTime(strtoll(location, &location, 10)) / 100; + } else { + location = strchr(location, ' '); + } + location += 1; + + /* Skip (23) - (38) */ + for (int i = 0; i < 16; i++) { + location = strchr(location, ' ') + 1; + } + + assert(location != NULL); + + /* (39) processor - %d */ + process->processor = strtol(location, &location, 10); + + /* Ignore further fields */ + + process->time = lp->utime + lp->stime; + + return true; +} + + +static bool LinuxProcessList_updateUser(ProcessList* processList, Process* process, openat_arg_t procFd) { + struct stat sstat; +#ifdef HAVE_OPENAT + int statok = fstat(procFd, &sstat); +#else + int statok = stat(procFd, &sstat); +#endif + if (statok == -1) + return false; + + if (process->st_uid != sstat.st_uid) { + process->st_uid = sstat.st_uid; + process->user = UsersTable_getRef(processList->usersTable, sstat.st_uid); + } + + return true; +} + +static void LinuxProcessList_readIoFile(LinuxProcess* process, openat_arg_t procFd, unsigned long long realtimeMs) { + char buffer[1024]; + ssize_t r = xReadfileat(procFd, "io", buffer, sizeof(buffer)); + if (r < 0) { + process->io_rate_read_bps = NAN; + process->io_rate_write_bps = NAN; + process->io_rchar = ULLONG_MAX; + process->io_wchar = ULLONG_MAX; + process->io_syscr = ULLONG_MAX; + process->io_syscw = ULLONG_MAX; + process->io_read_bytes = ULLONG_MAX; + process->io_write_bytes = ULLONG_MAX; + process->io_cancelled_write_bytes = ULLONG_MAX; + process->io_last_scan_time_ms = realtimeMs; + return; + } + + unsigned long long last_read = process->io_read_bytes; + unsigned long long last_write = process->io_write_bytes; + unsigned long long time_delta = realtimeMs > process->io_last_scan_time_ms ? realtimeMs - process->io_last_scan_time_ms : 0; + + char* buf = buffer; + const char* line; + while ((line = strsep(&buf, "\n")) != NULL) { + switch (line[0]) { + case 'r': + if (line[1] == 'c' && String_startsWith(line + 2, "har: ")) { + process->io_rchar = strtoull(line + 7, NULL, 10); + } else if (String_startsWith(line + 1, "ead_bytes: ")) { + process->io_read_bytes = strtoull(line + 12, NULL, 10); + process->io_rate_read_bps = time_delta ? (process->io_read_bytes - last_read) * /*ms to s*/1000. / time_delta : NAN; + } + break; + case 'w': + if (line[1] == 'c' && String_startsWith(line + 2, "har: ")) { + process->io_wchar = strtoull(line + 7, NULL, 10); + } else if (String_startsWith(line + 1, "rite_bytes: ")) { + process->io_write_bytes = strtoull(line + 13, NULL, 10); + process->io_rate_write_bps = time_delta ? (process->io_write_bytes - last_write) * /*ms to s*/1000. / time_delta : NAN; + } + break; + case 's': + if (line[4] == 'r' && String_startsWith(line + 1, "yscr: ")) { + process->io_syscr = strtoull(line + 7, NULL, 10); + } else if (String_startsWith(line + 1, "yscw: ")) { + process->io_syscw = strtoull(line + 7, NULL, 10); + } + break; + case 'c': + if (String_startsWith(line + 1, "ancelled_write_bytes: ")) { + process->io_cancelled_write_bytes = strtoull(line + 23, NULL, 10); + } + } + } + + process->io_last_scan_time_ms = realtimeMs; +} + +typedef struct LibraryData_ { + uint64_t size; + bool exec; +} LibraryData; + +static inline uint64_t fast_strtoull_dec(char** str, int maxlen) { + register uint64_t result = 0; + + if (!maxlen) + --maxlen; + + while (maxlen-- && **str >= '0' && **str <= '9') { + result *= 10; + result += **str - '0'; + (*str)++; + } + + return result; +} + +static inline uint64_t fast_strtoull_hex(char** str, int maxlen) { + register uint64_t result = 0; + register int nibble, letter; + const long valid_mask = 0x03FF007E; + + if (!maxlen) + --maxlen; + + while (maxlen--) { + nibble = (unsigned char)**str; + if (!(valid_mask & (1 << (nibble & 0x1F)))) + break; + if ((nibble < '0') || (nibble & ~0x20) > 'F') + break; + letter = (nibble & 0x40) ? 'A' - '9' - 1 : 0; + nibble &=~0x20; // to upper + nibble ^= 0x10; // switch letters and digits + nibble -= letter; + nibble &= 0x0f; + result <<= 4; + result += (uint64_t)nibble; + (*str)++; + } + + return result; +} + +static void LinuxProcessList_calcLibSize_helper(ATTR_UNUSED ht_key_t key, void* value, void* data) { + if (!data) + return; + + if (!value) + return; + + const LibraryData* v = (const LibraryData*)value; + uint64_t* d = (uint64_t*)data; + if (!v->exec) + return; + + *d += v->size; +} + +static void LinuxProcessList_readMaps(LinuxProcess* process, openat_arg_t procFd, bool calcSize, bool checkDeletedLib) { + Process* proc = (Process*)process; + + proc->usesDeletedLib = false; + + FILE* mapsfile = fopenat(procFd, "maps", "r"); + if (!mapsfile) + return; + + Hashtable* ht = NULL; + if (calcSize) + ht = Hashtable_new(64, true); + + char buffer[1024]; + while (fgets(buffer, sizeof(buffer), mapsfile)) { + uint64_t map_start; + uint64_t map_end; + bool map_execute; + unsigned int map_devmaj; + unsigned int map_devmin; + uint64_t map_inode; + + // Short circuit test: Look for a slash + if (!strchr(buffer, '/')) + continue; + + // Parse format: "%Lx-%Lx %4s %x %2x:%2x %Ld" + char* readptr = buffer; + + map_start = fast_strtoull_hex(&readptr, 16); + if ('-' != *readptr++) + continue; + + map_end = fast_strtoull_hex(&readptr, 16); + if (' ' != *readptr++) + continue; + + map_execute = (readptr[2] == 'x'); + readptr += 4; + if (' ' != *readptr++) + continue; + + while (*readptr > ' ') + readptr++; // Skip parsing this hex value + if (' ' != *readptr++) + continue; + + map_devmaj = fast_strtoull_hex(&readptr, 4); + if (':' != *readptr++) + continue; + + map_devmin = fast_strtoull_hex(&readptr, 4); + if (' ' != *readptr++) + continue; + + //Minor shortcut: Once we know there's no file for this region, we skip + if (!map_devmaj && !map_devmin) + continue; + + map_inode = fast_strtoull_dec(&readptr, 20); + if (!map_inode) + continue; + + if (calcSize) { + LibraryData* libdata = Hashtable_get(ht, map_inode); + if (!libdata) { + libdata = xCalloc(1, sizeof(LibraryData)); + Hashtable_put(ht, map_inode, libdata); + } + + libdata->size += map_end - map_start; + libdata->exec |= map_execute; + } + + if (checkDeletedLib && map_execute && !proc->usesDeletedLib) { + while (*readptr == ' ') + readptr++; + + if (*readptr != '/') + continue; + + if (String_startsWith(readptr, "/memfd:")) + continue; + + /* Virtualbox maps /dev/zero for memory allocation. That results in + * false positive, so ignore. */ + if (String_eq(readptr, "/dev/zero (deleted)\n")) + continue; + + if (strstr(readptr, " (deleted)\n")) { + proc->usesDeletedLib = true; + if (!calcSize) + break; + } + } + } + + fclose(mapsfile); + + if (calcSize) { + uint64_t total_size = 0; + Hashtable_foreach(ht, LinuxProcessList_calcLibSize_helper, &total_size); + + Hashtable_delete(ht); + + process->m_lrs = total_size / pageSize; + } +} + +static bool LinuxProcessList_readStatmFile(LinuxProcess* process, openat_arg_t procFd) { + FILE* statmfile = fopenat(procFd, "statm", "r"); + if (!statmfile) + return false; + + long int dummy, dummy2; + + int r = fscanf(statmfile, "%ld %ld %ld %ld %ld %ld %ld", + &process->super.m_virt, + &process->super.m_resident, + &process->m_share, + &process->m_trs, + &dummy, /* unused since Linux 2.6; always 0 */ + &process->m_drs, + &dummy2); /* unused since Linux 2.6; always 0 */ + fclose(statmfile); + + if (r == 7) { + process->super.m_virt *= pageSizeKB; + process->super.m_resident *= pageSizeKB; + } + + return r == 7; +} + +static bool LinuxProcessList_checkPidNamespace(Process* process, openat_arg_t procFd) { + FILE* statusfile = fopenat(procFd, "status", "r"); + if (!statusfile) + return false; + + while (true) { + char buffer[PROC_LINE_LENGTH + 1]; + if (fgets(buffer, sizeof(buffer), statusfile) == NULL) + break; + + if (!String_startsWith(buffer, "NSpid:")) + continue; + + char* ptr = buffer; + int pid_ns_count = 0; + while (*ptr && *ptr != '\n' && !isdigit(*ptr)) + ++ptr; + + while (*ptr && *ptr != '\n') { + if (isdigit(*ptr)) + pid_ns_count++; + while (isdigit(*ptr)) + ++ptr; + while (*ptr && *ptr != '\n' && !isdigit(*ptr)) + ++ptr; + } + + if (pid_ns_count > 1) + process->isRunningInContainer = true; + + break; + } + + fclose(statusfile); + return true; +} + +static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, openat_arg_t procFd, bool haveSmapsRollup) { + //http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719 + //kernel will return data in chunks of size PAGE_SIZE or less. + FILE* f = fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r"); + if (!f) + return false; + + process->m_pss = 0; + process->m_swap = 0; + process->m_psswp = 0; + + char buffer[256]; + while (fgets(buffer, sizeof(buffer), f)) { + if (!strchr(buffer, '\n')) { + // Partial line, skip to end of this line + while (fgets(buffer, sizeof(buffer), f)) { + if (strchr(buffer, '\n')) { + break; + } + } + continue; + } + + if (String_startsWith(buffer, "Pss:")) { + process->m_pss += strtol(buffer + 4, NULL, 10); + } else if (String_startsWith(buffer, "Swap:")) { + process->m_swap += strtol(buffer + 5, NULL, 10); + } else if (String_startsWith(buffer, "SwapPss:")) { + process->m_psswp += strtol(buffer + 8, NULL, 10); + } + } + + fclose(f); + return true; +} + +#ifdef HAVE_OPENVZ + +static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t procFd) { + if (access(PROCDIR "/vz", R_OK) != 0) { + free(process->ctid); + process->ctid = NULL; + process->vpid = process->super.pid; + return; + } + + FILE* file = fopenat(procFd, "status", "r"); + if (!file) { + free(process->ctid); + process->ctid = NULL; + process->vpid = process->super.pid; + return; + } + + bool foundEnvID = false; + bool foundVPid = false; + char linebuf[256]; + while (fgets(linebuf, sizeof(linebuf), file) != NULL) { + if (strchr(linebuf, '\n') == NULL) { + // Partial line, skip to end of this line + while (fgets(linebuf, sizeof(linebuf), file) != NULL) { + if (strchr(linebuf, '\n') != NULL) { + break; + } + } + continue; + } + + char* name_value_sep = strchr(linebuf, ':'); + if (name_value_sep == NULL) { + continue; + } + + int field; + if (0 == strncasecmp(linebuf, "envID", name_value_sep - linebuf)) { + field = 1; + } else if (0 == strncasecmp(linebuf, "VPid", name_value_sep - linebuf)) { + field = 2; + } else { + continue; + } + + do { + name_value_sep++; + } while (*name_value_sep != '\0' && *name_value_sep <= 32); + + char* value_end = name_value_sep; + + while (*value_end > 32) { + value_end++; + } + + if (name_value_sep == value_end) { + continue; + } + + *value_end = '\0'; + + switch (field) { + case 1: + foundEnvID = true; + if (!String_eq(name_value_sep, process->ctid ? process->ctid : "")) + free_and_xStrdup(&process->ctid, name_value_sep); + break; + case 2: + foundVPid = true; + process->vpid = strtoul(name_value_sep, NULL, 0); + break; + default: + //Sanity Check: Should never reach here, or the implementation is missing something! + assert(false && "OpenVZ handling: Unimplemented case for field handling reached."); + } + } + + fclose(file); + + if (!foundEnvID) { + free(process->ctid); + process->ctid = NULL; + } + + if (!foundVPid) { + process->vpid = process->super.pid; + } +} + +#endif + +static bool isContainerOrVMSlice(char* cgroup) { + if (String_startsWith(cgroup, "/user") || String_startsWith(cgroup, "/system")) + return false; + + return true; +} + +static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t procFd) { + FILE* file = fopenat(procFd, "cgroup", "r"); + if (!file) { + if (process->cgroup) { + free(process->cgroup); + process->cgroup = NULL; + } + if (process->cgroup_short) { + free(process->cgroup_short); + process->cgroup_short = NULL; + } + return; + } + char output[PROC_LINE_LENGTH + 1]; + output[0] = '\0'; + char* at = output; + int left = PROC_LINE_LENGTH; + while (!feof(file) && left > 0) { + char buffer[PROC_LINE_LENGTH + 1]; + const char* ok = fgets(buffer, PROC_LINE_LENGTH, file); + if (!ok) + break; + + char* group = buffer; + for (size_t i = 0; i < 2; i++) { + group = strchrnul(group, ':'); + if (!*group) + break; + group++; + } + + char* eol = strchrnul(group, '\n'); + *eol = '\0'; + + if (at != output) { + *at = ';'; + at++; + left--; + } + int wrote = snprintf(at, left, "%s", group); + left -= wrote; + } + fclose(file); + + bool changed = !process->cgroup || !String_eq(process->cgroup, output); + + Process_updateFieldWidth(CGROUP, strlen(output)); + free_and_xStrdup(&process->cgroup, output); + + if (!changed) { + if (process->cgroup_short) { + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup_short)); + } else { + //CCGROUP is alias to normal CGROUP if shortening fails + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup)); + } + return; + } + + char* cgroup_short = CGroup_filterName(process->cgroup); + if (cgroup_short) { + Process_updateFieldWidth(CCGROUP, strlen(cgroup_short)); + free_and_xStrdup(&process->cgroup_short, cgroup_short); + free(cgroup_short); + } else { + //CCGROUP is alias to normal CGROUP if shortening fails + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup)); + free(process->cgroup_short); + process->cgroup_short = NULL; + } +} + +#ifdef HAVE_VSERVER + +static void LinuxProcessList_readVServerData(LinuxProcess* process, openat_arg_t procFd) { + FILE* file = fopenat(procFd, "status", "r"); + if (!file) + return; + + char buffer[PROC_LINE_LENGTH + 1]; + process->vxid = 0; + while (fgets(buffer, PROC_LINE_LENGTH, file)) { + if (String_startsWith(buffer, "VxID:")) { + int vxid; + int ok = sscanf(buffer, "VxID:\t%32d", &vxid); + if (ok >= 1) { + process->vxid = vxid; + } + } + #if defined HAVE_ANCIENT_VSERVER + else if (String_startsWith(buffer, "s_context:")) { + int vxid; + int ok = sscanf(buffer, "s_context:\t%32d", &vxid); + if (ok >= 1) { + process->vxid = vxid; + } + } + #endif + } + fclose(file); +} + +#endif + +static void LinuxProcessList_readOomData(LinuxProcess* process, openat_arg_t procFd) { + FILE* file = fopenat(procFd, "oom_score", "r"); + if (!file) + return; + + char buffer[PROC_LINE_LENGTH + 1]; + if (fgets(buffer, PROC_LINE_LENGTH, file)) { + unsigned int oom; + int ok = sscanf(buffer, "%u", &oom); + if (ok >= 1) { + process->oom = oom; + } + } + fclose(file); +} + +static void LinuxProcessList_readAutogroup(LinuxProcess* process, openat_arg_t procFd) { + process->autogroup_id = -1; + + char autogroup[64]; // space for two numeric values and fixed length strings + ssize_t amtRead = xReadfileat(procFd, "autogroup", autogroup, sizeof(autogroup)); + if (amtRead < 0) + return; + + long int identity; + int nice; + int ok = sscanf(autogroup, "/autogroup-%ld nice %d", &identity, &nice); + if (ok == 2) { + process->autogroup_id = identity; + process->autogroup_nice = nice; + } +} + +static void LinuxProcessList_readCtxtData(LinuxProcess* process, openat_arg_t procFd) { + FILE* file = fopenat(procFd, "status", "r"); + if (!file) + return; + + char buffer[PROC_LINE_LENGTH + 1]; + unsigned long ctxt = 0; + while (fgets(buffer, PROC_LINE_LENGTH, file)) { + if (String_startsWith(buffer, "voluntary_ctxt_switches:")) { + unsigned long vctxt; + int ok = sscanf(buffer, "voluntary_ctxt_switches:\t%lu", &vctxt); + if (ok >= 1) { + ctxt += vctxt; + } + } else if (String_startsWith(buffer, "nonvoluntary_ctxt_switches:")) { + unsigned long nvctxt; + int ok = sscanf(buffer, "nonvoluntary_ctxt_switches:\t%lu", &nvctxt); + if (ok >= 1) { + ctxt += nvctxt; + } + } + } + fclose(file); + process->ctxt_diff = (ctxt > process->ctxt_total) ? (ctxt - process->ctxt_total) : 0; + process->ctxt_total = ctxt; +} + +static void LinuxProcessList_readSecattrData(LinuxProcess* process, openat_arg_t procFd) { + FILE* file = fopenat(procFd, "attr/current", "r"); + if (!file) { + free(process->secattr); + process->secattr = NULL; + return; + } + + char buffer[PROC_LINE_LENGTH + 1]; + const char* res = fgets(buffer, sizeof(buffer), file); + fclose(file); + if (!res) { + free(process->secattr); + process->secattr = NULL; + return; + } + char* newline = strchr(buffer, '\n'); + if (newline) { + *newline = '\0'; + } + + Process_updateFieldWidth(SECATTR, strlen(buffer)); + + if (process->secattr && String_eq(process->secattr, buffer)) { + return; + } + free_and_xStrdup(&process->secattr, buffer); +} + +static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd) { + char pathBuffer[PATH_MAX + 1] = {0}; + +#if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT) + ssize_t r = readlinkat(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1); +#else + ssize_t r = Compat_readlink(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1); +#endif + + if (r < 0) { + free(process->super.procCwd); + process->super.procCwd = NULL; + return; + } + + pathBuffer[r] = '\0'; + + if (process->super.procCwd && String_eq(process->super.procCwd, pathBuffer)) + return; + + free_and_xStrdup(&process->super.procCwd, pathBuffer); +} + +#ifdef HAVE_DELAYACCT + +static int handleNetlinkMsg(struct nl_msg* nlmsg, void* linuxProcess) { + struct nlmsghdr* nlhdr; + struct nlattr* nlattrs[TASKSTATS_TYPE_MAX + 1]; + const struct nlattr* nlattr; + struct taskstats stats; + int rem; + LinuxProcess* lp = (LinuxProcess*) linuxProcess; + + nlhdr = nlmsg_hdr(nlmsg); + + if (genlmsg_parse(nlhdr, 0, nlattrs, TASKSTATS_TYPE_MAX, NULL) < 0) { + return NL_SKIP; + } + + if ((nlattr = nlattrs[TASKSTATS_TYPE_AGGR_PID]) || (nlattr = nlattrs[TASKSTATS_TYPE_NULL])) { + memcpy(&stats, nla_data(nla_next(nla_data(nlattr), &rem)), sizeof(stats)); + assert(lp->super.pid == (pid_t)stats.ac_pid); + + unsigned long long int timeDelta = stats.ac_etime * 1000 - lp->delay_read_time; + #define BOUNDS(x) (isnan(x) ? 0.0 : ((x) > 100) ? 100.0 : (x)) + #define DELTAPERC(x,y) BOUNDS((float) ((x) - (y)) / timeDelta * 100) + lp->cpu_delay_percent = DELTAPERC(stats.cpu_delay_total, lp->cpu_delay_total); + lp->blkio_delay_percent = DELTAPERC(stats.blkio_delay_total, lp->blkio_delay_total); + lp->swapin_delay_percent = DELTAPERC(stats.swapin_delay_total, lp->swapin_delay_total); + #undef DELTAPERC + #undef BOUNDS + + lp->swapin_delay_total = stats.swapin_delay_total; + lp->blkio_delay_total = stats.blkio_delay_total; + lp->cpu_delay_total = stats.cpu_delay_total; + lp->delay_read_time = stats.ac_etime * 1000; + } + return NL_OK; +} + +static void LinuxProcessList_readDelayAcctData(LinuxProcessList* this, LinuxProcess* process) { + struct nl_msg* msg; + + if (!this->netlink_socket) { + LinuxProcessList_initNetlinkSocket(this); + if (!this->netlink_socket) { + goto delayacct_failure; + } + } + + if (nl_socket_modify_cb(this->netlink_socket, NL_CB_VALID, NL_CB_CUSTOM, handleNetlinkMsg, process) < 0) { + goto delayacct_failure; + } + + if (! (msg = nlmsg_alloc())) { + goto delayacct_failure; + } + + if (! genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, this->netlink_family, 0, NLM_F_REQUEST, TASKSTATS_CMD_GET, TASKSTATS_VERSION)) { + nlmsg_free(msg); + } + + if (nla_put_u32(msg, TASKSTATS_CMD_ATTR_PID, process->super.pid) < 0) { + nlmsg_free(msg); + } + + if (nl_send_sync(this->netlink_socket, msg) < 0) { + goto delayacct_failure; + } + + if (nl_recvmsgs_default(this->netlink_socket) < 0) { + goto delayacct_failure; + } + + return; + +delayacct_failure: + process->swapin_delay_percent = NAN; + process->blkio_delay_percent = NAN; + process->cpu_delay_percent = NAN; +} + +#endif + +static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t procFd) { + char command[4096 + 1]; // max cmdline length on Linux + ssize_t amtRead = xReadfileat(procFd, "cmdline", command, sizeof(command)); + if (amtRead <= 0) + return false; + + int tokenEnd = 0; + int tokenStart = 0; + int lastChar = 0; + bool argSepNUL = false; + bool argSepSpace = false; + + for (int i = 0; i < amtRead; i++) { + /* newline used as delimiter - when forming the mergedCommand, newline is + * converted to space by Process_makeCommandStr */ + if (command[i] == '\0') { + command[i] = '\n'; + } else { + /* Record some information for the argument parsing heuristic below. */ + if (tokenEnd) + argSepNUL = true; + if (command[i] <= ' ') + argSepSpace = true; + } + + if (command[i] == '\n') { + if (tokenEnd == 0) { + tokenEnd = i; + } + } else { + /* htop considers the next character after the last / that is before + * basenameOffset, as the start of the basename in cmdline - see + * Process_writeCommand */ + if (!tokenEnd && command[i] == '/') { + tokenStart = i + 1; + } + lastChar = i; + } + } + + command[lastChar + 1] = '\0'; + + if (!argSepNUL && argSepSpace) { + /* Argument parsing heuristic. + * + * This heuristic is used for processes that rewrite their command line. + * Normally the command line is split by using NUL bytes between each argument. + * But some programs like chrome flatten this using spaces. + * + * This heuristic tries its best to undo this loss of information. + * To achieve this, we treat every character <= 32 as argument separators + * (i.e. all of ASCII control sequences and space). + * We then search for the basename of the cmdline in the first argument we found that way. + * As path names may contain we try to cross-validate if the path we got that way exists. + */ + + tokenStart = tokenEnd = 0; + + // From initial scan we know there's at least one space. + // Check if that's part of a filename for an existing file. + if (Compat_faccessat(AT_FDCWD, command, F_OK, AT_SYMLINK_NOFOLLOW) != 0) { + // If we reach here the path does not exist. + // Thus begin searching for the part of it that actually is. + + int tokenArg0Start = 0; + + for (int i = 0; i <= lastChar; i++) { + /* Any ASCII control or space used as delimiter */ + char tmpCommandChar = command[i]; + + if (command[i] <= ' ') { + if (!tokenEnd) { + command[i] = '\0'; + + bool found = Compat_faccessat(AT_FDCWD, command, F_OK, AT_SYMLINK_NOFOLLOW) == 0; + + // Restore if this wasn't it + command[i] = found ? '\n' : tmpCommandChar; + + if (found) + tokenEnd = i; + if (!tokenArg0Start) + tokenArg0Start = tokenStart; + } else { + // Split on every further separator, regardless of path correctness + command[i] = '\n'; + } + } else if (!tokenEnd) { + if (command[i] == '/' || (command[i] == '\\' && (!tokenStart || command[tokenStart - 1] == '\\'))) { + tokenStart = i + 1; + } else if (command[i] == ':' && (command[i + 1] != '/' && command[i + 1] != '\\')) { + tokenEnd = i; + } + } + } + + if (!tokenEnd) { + tokenStart = tokenArg0Start; + + // No token delimiter found, forcibly split + for (int i = 0; i <= lastChar; i++) { + if (command[i] <= ' ') { + command[i] = '\n'; + if (!tokenEnd) { + tokenEnd = i; + } + } + } + } + } + + /* Some command lines are hard to parse, like + * file.so [kdeinit5] file local:/run/user/1000/klauncherdqbouY.1.slave-socket local:/run/user/1000/kded5TwsDAx.1.slave-socket + * Reset if start is behind end. + */ + if (tokenStart >= tokenEnd) + tokenStart = tokenEnd = 0; + } + + if (tokenEnd == 0) { + tokenEnd = lastChar + 1; + } + + Process_updateCmdline(process, command, tokenStart, tokenEnd); + + /* /proc/[pid]/comm could change, so should be updated */ + if ((amtRead = xReadfileat(procFd, "comm", command, sizeof(command))) > 0) { + command[amtRead - 1] = '\0'; + Process_updateComm(process, command); + } else { + Process_updateComm(process, NULL); + } + + char filename[MAX_NAME + 1]; + + /* execve could change /proc/[pid]/exe, so procExe should be updated */ +#if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT) + amtRead = readlinkat(procFd, "exe", filename, sizeof(filename) - 1); +#else + amtRead = Compat_readlink(procFd, "exe", filename, sizeof(filename) - 1); +#endif + if (amtRead > 0) { + filename[amtRead] = 0; + if (!process->procExe || + (!process->procExeDeleted && !String_eq(filename, process->procExe)) || + process->procExeDeleted) { + + const char* deletedMarker = " (deleted)"; + const size_t markerLen = strlen(deletedMarker); + const size_t filenameLen = strlen(filename); + + if (filenameLen > markerLen) { + bool oldExeDeleted = process->procExeDeleted; + + process->procExeDeleted = String_eq(filename + filenameLen - markerLen, deletedMarker); + + if (process->procExeDeleted) + filename[filenameLen - markerLen] = '\0'; + + if (oldExeDeleted != process->procExeDeleted) + process->mergedCommand.lastUpdate = 0; + } + + Process_updateExe(process, filename); + } + } else if (process->procExe) { + Process_updateExe(process, NULL); + process->procExeDeleted = false; + } + + return true; +} + +static char* LinuxProcessList_updateTtyDevice(TtyDriver* ttyDrivers, unsigned long int tty_nr) { + unsigned int maj = major(tty_nr); + unsigned int min = minor(tty_nr); + + int i = -1; + for (;;) { + i++; + if ((!ttyDrivers[i].path) || maj < ttyDrivers[i].major) { + break; + } + if (maj > ttyDrivers[i].major) { + continue; + } + if (min < ttyDrivers[i].minorFrom) { + break; + } + if (min > ttyDrivers[i].minorTo) { + continue; + } + unsigned int idx = min - ttyDrivers[i].minorFrom; + struct stat sstat; + char* fullPath; + for (;;) { + xAsprintf(&fullPath, "%s/%d", ttyDrivers[i].path, idx); + int err = stat(fullPath, &sstat); + if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) { + return fullPath; + } + free(fullPath); + + xAsprintf(&fullPath, "%s%d", ttyDrivers[i].path, idx); + err = stat(fullPath, &sstat); + if (err == 0 && major(sstat.st_rdev) == maj && minor(sstat.st_rdev) == min) { + return fullPath; + } + free(fullPath); + + if (idx == min) { + break; + } + + idx = min; + } + int err = stat(ttyDrivers[i].path, &sstat); + if (err == 0 && tty_nr == sstat.st_rdev) { + return xStrdup(ttyDrivers[i].path); + } + } + char* out; + xAsprintf(&out, "/dev/%u:%u", maj, min); + return out; +} + +static bool isOlderThan(const ProcessList* pl, const Process* proc, unsigned int seconds) { + assert(pl->realtimeMs > 0); + + /* Starttime might not yet be parsed */ + if (proc->starttime_ctime <= 0) + return false; + + uint64_t realtime = pl->realtimeMs / 1000; + + if (realtime < (uint64_t)proc->starttime_ctime) + return false; + + return realtime - proc->starttime_ctime > seconds; +} + +static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_t parentFd, const char* dirname, const Process* parent, double period) { + ProcessList* pl = (ProcessList*) this; + const struct dirent* entry; + const Settings* settings = pl->settings; + const ScreenSettings* ss = settings->ss; + +#ifdef HAVE_OPENAT + int dirFd = openat(parentFd, dirname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + if (dirFd < 0) + return false; + DIR* dir = fdopendir(dirFd); +#else + char dirFd[4096]; + xSnprintf(dirFd, sizeof(dirFd), "%s/%s", parentFd, dirname); + DIR* dir = opendir(dirFd); +#endif + if (!dir) { + Compat_openatArgClose(dirFd); + return false; + } + + const unsigned int activeCPUs = pl->activeCPUs; + const bool hideKernelThreads = settings->hideKernelThreads; + const bool hideUserlandThreads = settings->hideUserlandThreads; + const bool hideRunningInContainer = settings->hideRunningInContainer; + while ((entry = readdir(dir)) != NULL) { + const char* name = entry->d_name; + + // Ignore all non-directories + if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) { + continue; + } + + // The RedHat kernel hides threads with a dot. + // I believe this is non-standard. + if (name[0] == '.') { + name++; + } + + // Just skip all non-number directories. + if (name[0] < '0' || name[0] > '9') { + continue; + } + + // filename is a number: process directory + int pid; + { + char* endptr; + unsigned long parsedPid = strtoul(name, &endptr, 10); + if (parsedPid == 0 || parsedPid == ULONG_MAX || *endptr != '\0') + continue; + pid = parsedPid; + } + + // Skip task directory of main thread + if (parent && pid == parent->pid) + continue; + +#ifdef HAVE_OPENAT + int procFd = openat(dirFd, entry->d_name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + if (procFd < 0) + continue; +#else + char procFd[4096]; + xSnprintf(procFd, sizeof(procFd), "%s/%s", dirFd, entry->d_name); +#endif + + bool preExisting; + Process* proc = ProcessList_getProcess(pl, pid, &preExisting, LinuxProcess_new); + LinuxProcess* lp = (LinuxProcess*) proc; + + proc->tgid = parent ? parent->pid : pid; + proc->isUserlandThread = proc->pid != proc->tgid; + + LinuxProcessList_recurseProcTree(this, procFd, "task", proc, period); + + if (ss->flags & PROCESS_FLAG_LINUX_CGROUP || hideRunningInContainer) { + LinuxProcessList_readCGroupFile(lp, procFd); + if (hideRunningInContainer && lp->cgroup && isContainerOrVMSlice(lp->cgroup)) { + if (!LinuxProcessList_checkPidNamespace(proc, procFd)) { + goto errorReadingProcess; + } + } + } + + /* + * These conditions will not trigger on first occurrence, cause we need to + * add the process to the ProcessList and do all one time scans + * (e.g. parsing the cmdline to detect a kernel thread) + * But it will short-circuit subsequent scans. + */ + if (preExisting && hideKernelThreads && Process_isKernelThread(proc)) { + proc->updated = true; + proc->show = false; + pl->kernelThreads++; + pl->totalTasks++; + Compat_openatArgClose(procFd); + continue; + } + if (preExisting && hideUserlandThreads && Process_isUserlandThread(proc)) { + proc->updated = true; + proc->show = false; + pl->userlandThreads++; + pl->totalTasks++; + Compat_openatArgClose(procFd); + continue; + } + if (preExisting && hideRunningInContainer && proc->isRunningInContainer) { + proc->updated = true; + proc->show = false; + Compat_openatArgClose(procFd); + continue; + } + + if (ss->flags & PROCESS_FLAG_IO) + LinuxProcessList_readIoFile(lp, procFd, pl->realtimeMs); + + if (!LinuxProcessList_readStatmFile(lp, procFd)) + goto errorReadingProcess; + + { + bool prev = proc->usesDeletedLib; + + if (!proc->isKernelThread && !proc->isUserlandThread && + ((ss->flags & PROCESS_FLAG_LINUX_LRS_FIX) || (settings->highlightDeletedExe && !proc->procExeDeleted && isOlderThan(pl, proc, 10)))) { + + // Check if we really should recalculate the M_LRS value for this process + uint64_t passedTimeInMs = pl->realtimeMs - lp->last_mlrs_calctime; + + uint64_t recheck = ((uint64_t)rand()) % 2048; + + if (passedTimeInMs > recheck) { + lp->last_mlrs_calctime = pl->realtimeMs; + LinuxProcessList_readMaps(lp, procFd, ss->flags & PROCESS_FLAG_LINUX_LRS_FIX, settings->highlightDeletedExe); + } + } else { + /* Copy from process structure in threads and reset if setting got disabled */ + proc->usesDeletedLib = (proc->isUserlandThread && parent) ? parent->usesDeletedLib : false; + lp->m_lrs = (proc->isUserlandThread && parent) ? ((const LinuxProcess*)parent)->m_lrs : 0; + } + + if (prev != proc->usesDeletedLib) + proc->mergedCommand.lastUpdate = 0; + } + + if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) { + if (!parent) { + // Read smaps file of each process only every second pass to improve performance + static int smaps_flag = 0; + if ((pid & 1) == smaps_flag) { + LinuxProcessList_readSmapsFile(lp, procFd, this->haveSmapsRollup); + } + if (pid == 1) { + smaps_flag = !smaps_flag; + } + } else { + lp->m_pss = ((const LinuxProcess*)parent)->m_pss; + } + } + + char statCommand[MAX_NAME + 1]; + unsigned long long int lasttimes = (lp->utime + lp->stime); + unsigned long int tty_nr = proc->tty_nr; + if (!LinuxProcessList_readStatFile(proc, procFd, statCommand, sizeof(statCommand))) + goto errorReadingProcess; + + if (lp->flags & PF_KTHREAD) { + proc->isKernelThread = true; + } + + if (tty_nr != proc->tty_nr && this->ttyDrivers) { + free(proc->tty_name); + proc->tty_name = LinuxProcessList_updateTtyDevice(this->ttyDrivers, proc->tty_nr); + } + + if (ss->flags & PROCESS_FLAG_LINUX_IOPRIO) { + LinuxProcess_updateIOPriority(lp); + } + + /* period might be 0 after system sleep */ + float percent_cpu = (period < 1E-6) ? 0.0F : ((lp->utime + lp->stime - lasttimes) / period * 100.0); + proc->percent_cpu = CLAMP(percent_cpu, 0.0F, activeCPUs * 100.0F); + proc->percent_mem = proc->m_resident / (double)(pl->totalMem) * 100.0; + Process_updateCPUFieldWidths(proc->percent_cpu); + + if (! LinuxProcessList_updateUser(pl, proc, procFd)) + goto errorReadingProcess; + + if (!preExisting) { + + #ifdef HAVE_OPENVZ + if (ss->flags & PROCESS_FLAG_LINUX_OPENVZ) { + LinuxProcessList_readOpenVZData(lp, procFd); + } + #endif + + #ifdef HAVE_VSERVER + if (ss->flags & PROCESS_FLAG_LINUX_VSERVER) { + LinuxProcessList_readVServerData(lp, procFd); + } + #endif + + if (proc->isKernelThread) { + Process_updateCmdline(proc, NULL, 0, 0); + } else if (!LinuxProcessList_readCmdlineFile(proc, procFd)) { + Process_updateCmdline(proc, statCommand, 0, strlen(statCommand)); + } + + Process_fillStarttimeBuffer(proc); + + ProcessList_add(pl, proc); + } else { + if (settings->updateProcessNames && proc->state != ZOMBIE) { + if (proc->isKernelThread) { + Process_updateCmdline(proc, NULL, 0, 0); + } else if (!LinuxProcessList_readCmdlineFile(proc, procFd)) { + Process_updateCmdline(proc, statCommand, 0, strlen(statCommand)); + } + } + } + + #ifdef HAVE_DELAYACCT + if (ss->flags & PROCESS_FLAG_LINUX_DELAYACCT) { + LinuxProcessList_readDelayAcctData(this, lp); + } + #endif + + if (ss->flags & PROCESS_FLAG_LINUX_OOM) { + LinuxProcessList_readOomData(lp, procFd); + } + + if (ss->flags & PROCESS_FLAG_LINUX_CTXT) { + LinuxProcessList_readCtxtData(lp, procFd); + } + + if (ss->flags & PROCESS_FLAG_LINUX_SECATTR) { + LinuxProcessList_readSecattrData(lp, procFd); + } + + if (ss->flags & PROCESS_FLAG_CWD) { + LinuxProcessList_readCwd(lp, procFd); + } + + if ((ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) { + LinuxProcessList_readAutogroup(lp, procFd); + } + + if (!proc->cmdline && statCommand[0] && + (proc->state == ZOMBIE || Process_isKernelThread(proc) || settings->showThreadNames)) { + Process_updateCmdline(proc, statCommand, 0, strlen(statCommand)); + } + + if (Process_isKernelThread(proc)) { + pl->kernelThreads++; + } else if (Process_isUserlandThread(proc)) { + pl->userlandThreads++; + } + + /* Set at the end when we know if a new entry is a thread */ + proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); + + pl->totalTasks++; + /* runningTasks is set in LinuxProcessList_scanCPUTime() from /proc/stat */ + proc->updated = true; + Compat_openatArgClose(procFd); + continue; + + // Exception handler. + +errorReadingProcess: + { +#ifdef HAVE_OPENAT + if (procFd >= 0) + close(procFd); +#endif + + if (preExisting) { + /* + * The only real reason for coming here (apart from Linux violating the /proc API) + * would be the process going away with its /proc files disappearing (!HAVE_OPENAT). + * However, we want to keep in the process list for now for the "highlight dying" mode. + */ + } else { + /* A really short-lived process that we don't have full info about */ + Process_delete((Object*)proc); + } + } + } + closedir(dir); + return true; +} + +static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { + memory_t availableMem = 0; + memory_t freeMem = 0; + memory_t totalMem = 0; + memory_t buffersMem = 0; + memory_t cachedMem = 0; + memory_t sharedMem = 0; + memory_t swapTotalMem = 0; + memory_t swapCacheMem = 0; + memory_t swapFreeMem = 0; + memory_t sreclaimableMem = 0; + + FILE* file = fopen(PROCMEMINFOFILE, "r"); + if (!file) + CRT_fatalError("Cannot open " PROCMEMINFOFILE); + + char buffer[128]; + while (fgets(buffer, sizeof(buffer), file)) { + + #define tryRead(label, variable) \ + if (String_startsWith(buffer, label)) { \ + memory_t parsed_; \ + if (sscanf(buffer + strlen(label), "%llu kB", &parsed_) == 1) { \ + (variable) = parsed_; \ + } \ + break; \ + } else (void) 0 /* Require a ";" after the macro use. */ + + switch (buffer[0]) { + case 'M': + tryRead("MemAvailable:", availableMem); + tryRead("MemFree:", freeMem); + tryRead("MemTotal:", totalMem); + break; + case 'B': + tryRead("Buffers:", buffersMem); + break; + case 'C': + tryRead("Cached:", cachedMem); + break; + case 'S': + switch (buffer[1]) { + case 'h': + tryRead("Shmem:", sharedMem); + break; + case 'w': + tryRead("SwapTotal:", swapTotalMem); + tryRead("SwapCached:", swapCacheMem); + tryRead("SwapFree:", swapFreeMem); + break; + case 'R': + tryRead("SReclaimable:", sreclaimableMem); + break; + } + break; + } + + #undef tryRead + } + + fclose(file); + + /* + * Compute memory partition like procps(free) + * https://gitlab.com/procps-ng/procps/-/blob/master/proc/sysinfo.c + * + * Adjustments: + * - Shmem in part of Cached (see https://lore.kernel.org/patchwork/patch/648763/), + * do not show twice by subtracting from Cached and do not subtract twice from used. + */ + this->totalMem = totalMem; + this->cachedMem = cachedMem + sreclaimableMem - sharedMem; + this->sharedMem = sharedMem; + const memory_t usedDiff = freeMem + cachedMem + sreclaimableMem + buffersMem; + this->usedMem = (totalMem >= usedDiff) ? totalMem - usedDiff : totalMem - freeMem; + this->buffersMem = buffersMem; + this->availableMem = availableMem != 0 ? MINIMUM(availableMem, totalMem) : freeMem; + this->totalSwap = swapTotalMem; + this->usedSwap = swapTotalMem - swapFreeMem - swapCacheMem; + this->cachedSwap = swapCacheMem; +} + +static void LinuxProcessList_scanHugePages(LinuxProcessList* this) { + this->totalHugePageMem = 0; + for (unsigned i = 0; i < HTOP_HUGEPAGE_COUNT; i++) { + this->usedHugePageMem[i] = MEMORY_MAX; + } + + DIR* dir = opendir("/sys/kernel/mm/hugepages"); + if (!dir) + return; + + const struct dirent* entry; + while ((entry = readdir(dir)) != NULL) { + const char* name = entry->d_name; + + /* Ignore all non-directories */ + if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) + continue; + + if (!String_startsWith(name, "hugepages-")) + continue; + + char* endptr; + unsigned long int hugePageSize = strtoul(name + strlen("hugepages-"), &endptr, 10); + if (!endptr || *endptr != 'k') + continue; + + char content[64]; + char hugePagePath[128]; + ssize_t r; + + xSnprintf(hugePagePath, sizeof(hugePagePath), "/sys/kernel/mm/hugepages/%s/nr_hugepages", name); + r = xReadfile(hugePagePath, content, sizeof(content)); + if (r <= 0) + continue; + + memory_t total = strtoull(content, NULL, 10); + if (total == 0) + continue; + + xSnprintf(hugePagePath, sizeof(hugePagePath), "/sys/kernel/mm/hugepages/%s/free_hugepages", name); + r = xReadfile(hugePagePath, content, sizeof(content)); + if (r <= 0) + continue; + + memory_t free = strtoull(content, NULL, 10); + + int shift = ffsl(hugePageSize) - 1 - (HTOP_HUGEPAGE_BASE_SHIFT - 10); + assert(shift >= 0 && shift < HTOP_HUGEPAGE_COUNT); + + this->totalHugePageMem += total * hugePageSize; + this->usedHugePageMem[shift] = (total - free) * hugePageSize; + } + + closedir(dir); +} + +static inline void LinuxProcessList_scanZramInfo(LinuxProcessList* this) { + memory_t totalZram = 0; + memory_t usedZramComp = 0; + memory_t usedZramOrig = 0; + + char mm_stat[34]; + char disksize[34]; + + unsigned int i = 0; + for (;;) { + xSnprintf(mm_stat, sizeof(mm_stat), "/sys/block/zram%u/mm_stat", i); + xSnprintf(disksize, sizeof(disksize), "/sys/block/zram%u/disksize", i); + i++; + FILE* disksize_file = fopen(disksize, "r"); + FILE* mm_stat_file = fopen(mm_stat, "r"); + if (disksize_file == NULL || mm_stat_file == NULL) { + if (disksize_file) { + fclose(disksize_file); + } + if (mm_stat_file) { + fclose(mm_stat_file); + } + break; + } + memory_t size = 0; + memory_t orig_data_size = 0; + memory_t compr_data_size = 0; + + if (!fscanf(disksize_file, "%llu\n", &size) || + !fscanf(mm_stat_file, " %llu %llu", &orig_data_size, &compr_data_size)) { + fclose(disksize_file); + fclose(mm_stat_file); + break; + } + + totalZram += size; + usedZramComp += compr_data_size; + usedZramOrig += orig_data_size; + + fclose(disksize_file); + fclose(mm_stat_file); + } + + this->zram.totalZram = totalZram / 1024; + this->zram.usedZramComp = usedZramComp / 1024; + this->zram.usedZramOrig = usedZramOrig / 1024; +} + +static inline void LinuxProcessList_scanZfsArcstats(LinuxProcessList* lpl) { + memory_t dbufSize = 0; + memory_t dnodeSize = 0; + memory_t bonusSize = 0; + + FILE* file = fopen(PROCARCSTATSFILE, "r"); + if (file == NULL) { + lpl->zfs.enabled = 0; + return; + } + char buffer[128]; + while (fgets(buffer, 128, file)) { + #define tryRead(label, variable) \ + if (String_startsWith(buffer, label)) { \ + sscanf(buffer + strlen(label), " %*2u %32llu", variable); \ + break; \ + } else (void) 0 /* Require a ";" after the macro use. */ + #define tryReadFlag(label, variable, flag) \ + if (String_startsWith(buffer, label)) { \ + (flag) = sscanf(buffer + strlen(label), " %*2u %32llu", variable); \ + break; \ + } else (void) 0 /* Require a ";" after the macro use. */ + + switch (buffer[0]) { + case 'c': + tryRead("c_min", &lpl->zfs.min); + tryRead("c_max", &lpl->zfs.max); + tryReadFlag("compressed_size", &lpl->zfs.compressed, lpl->zfs.isCompressed); + break; + case 'u': + tryRead("uncompressed_size", &lpl->zfs.uncompressed); + break; + case 's': + tryRead("size", &lpl->zfs.size); + break; + case 'h': + tryRead("hdr_size", &lpl->zfs.header); + break; + case 'd': + tryRead("dbuf_size", &dbufSize); + tryRead("dnode_size", &dnodeSize); + break; + case 'b': + tryRead("bonus_size", &bonusSize); + break; + case 'a': + tryRead("anon_size", &lpl->zfs.anon); + break; + case 'm': + tryRead("mfu_size", &lpl->zfs.MFU); + tryRead("mru_size", &lpl->zfs.MRU); + break; + } + #undef tryRead + #undef tryReadFlag + } + fclose(file); + + lpl->zfs.enabled = (lpl->zfs.size > 0 ? 1 : 0); + lpl->zfs.size /= 1024; + lpl->zfs.min /= 1024; + lpl->zfs.max /= 1024; + lpl->zfs.MFU /= 1024; + lpl->zfs.MRU /= 1024; + lpl->zfs.anon /= 1024; + lpl->zfs.header /= 1024; + lpl->zfs.other = (dbufSize + dnodeSize + bonusSize) / 1024; + if ( lpl->zfs.isCompressed ) { + lpl->zfs.compressed /= 1024; + lpl->zfs.uncompressed /= 1024; + } +} + +static inline double LinuxProcessList_scanCPUTime(ProcessList* super) { + LinuxProcessList* this = (LinuxProcessList*) super; + + LinuxProcessList_updateCPUcount(super); + + FILE* file = fopen(PROCSTATFILE, "r"); + if (!file) + CRT_fatalError("Cannot open " PROCSTATFILE); + + unsigned int existingCPUs = super->existingCPUs; + unsigned int lastAdjCpuId = 0; + + for (unsigned int i = 0; i <= existingCPUs; i++) { + char buffer[PROC_LINE_LENGTH + 1]; + unsigned long long int usertime, nicetime, systemtime, idletime; + unsigned long long int ioWait = 0, irq = 0, softIrq = 0, steal = 0, guest = 0, guestnice = 0; + + const char* ok = fgets(buffer, sizeof(buffer), file); + if (!ok) + break; + + // cpu fields are sorted first + if (!String_startsWith(buffer, "cpu")) + break; + + // Depending on your kernel version, + // 5, 7, 8 or 9 of these fields will be set. + // The rest will remain at zero. + unsigned int adjCpuId; + if (i == 0) { + (void) sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); + adjCpuId = 0; + } else { + unsigned int cpuid; + (void) sscanf(buffer, "cpu%4u %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); + adjCpuId = cpuid + 1; + } + + if (adjCpuId > super->existingCPUs) + break; + + for (unsigned int j = lastAdjCpuId + 1; j < adjCpuId; j++) { + // Skipped an ID, but /proc/stat is ordered => got offline CPU + memset(&(this->cpuData[j]), '\0', sizeof(CPUData)); + } + lastAdjCpuId = adjCpuId; + + // Guest time is already accounted in usertime + usertime -= guest; + nicetime -= guestnice; + // Fields existing on kernels >= 2.6 + // (and RHEL's patched kernel 2.4...) + unsigned long long int idlealltime = idletime + ioWait; + unsigned long long int systemalltime = systemtime + irq + softIrq; + unsigned long long int virtalltime = guest + guestnice; + unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime; + CPUData* cpuData = &(this->cpuData[adjCpuId]); + // Since we do a subtraction (usertime - guest) and cputime64_to_clock_t() + // used in /proc/stat rounds down numbers, it can lead to a case where the + // integer overflow. + cpuData->userPeriod = saturatingSub(usertime, cpuData->userTime); + cpuData->nicePeriod = saturatingSub(nicetime, cpuData->niceTime); + cpuData->systemPeriod = saturatingSub(systemtime, cpuData->systemTime); + cpuData->systemAllPeriod = saturatingSub(systemalltime, cpuData->systemAllTime); + cpuData->idleAllPeriod = saturatingSub(idlealltime, cpuData->idleAllTime); + cpuData->idlePeriod = saturatingSub(idletime, cpuData->idleTime); + cpuData->ioWaitPeriod = saturatingSub(ioWait, cpuData->ioWaitTime); + cpuData->irqPeriod = saturatingSub(irq, cpuData->irqTime); + cpuData->softIrqPeriod = saturatingSub(softIrq, cpuData->softIrqTime); + cpuData->stealPeriod = saturatingSub(steal, cpuData->stealTime); + cpuData->guestPeriod = saturatingSub(virtalltime, cpuData->guestTime); + cpuData->totalPeriod = saturatingSub(totaltime, cpuData->totalTime); + cpuData->userTime = usertime; + cpuData->niceTime = nicetime; + cpuData->systemTime = systemtime; + cpuData->systemAllTime = systemalltime; + cpuData->idleAllTime = idlealltime; + cpuData->idleTime = idletime; + cpuData->ioWaitTime = ioWait; + cpuData->irqTime = irq; + cpuData->softIrqTime = softIrq; + cpuData->stealTime = steal; + cpuData->guestTime = virtalltime; + cpuData->totalTime = totaltime; + } + + double period = (double)this->cpuData[0].totalPeriod / super->activeCPUs; + + char buffer[PROC_LINE_LENGTH + 1]; + while (fgets(buffer, sizeof(buffer), file)) { + if (String_startsWith(buffer, "procs_running")) { + super->runningTasks = strtoul(buffer + strlen("procs_running"), NULL, 10); + break; + } + } + + fclose(file); + + return period; +} + +static int scanCPUFrequencyFromSysCPUFreq(LinuxProcessList* this) { + unsigned int existingCPUs = this->super.existingCPUs; + int numCPUsWithFrequency = 0; + unsigned long totalFrequency = 0; + + /* + * On some AMD and Intel CPUs read()ing scaling_cur_freq is quite slow (> 1ms). This delay + * accumulates for every core. For details see issue#471. + * If the read on CPU 0 takes longer than 500us bail out and fall back to reading the + * frequencies from /proc/cpuinfo. + * Once the condition has been met, bail out early for the next couple of scans. + */ + static int timeout = 0; + + if (timeout > 0) { + timeout--; + return -1; + } + + for (unsigned int i = 0; i < existingCPUs; ++i) { + if (!ProcessList_isCPUonline(&this->super, i)) + continue; + + char pathBuffer[64]; + xSnprintf(pathBuffer, sizeof(pathBuffer), "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq", i); + + struct timespec start; + if (i == 0) + clock_gettime(CLOCK_MONOTONIC, &start); + + FILE* file = fopen(pathBuffer, "r"); + if (!file) + return -errno; + + unsigned long frequency; + if (fscanf(file, "%lu", &frequency) == 1) { + /* convert kHz to MHz */ + frequency = frequency / 1000; + this->cpuData[i + 1].frequency = frequency; + numCPUsWithFrequency++; + totalFrequency += frequency; + } + + fclose(file); + + if (i == 0) { + struct timespec end; + clock_gettime(CLOCK_MONOTONIC, &end); + const time_t timeTakenUs = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000; + if (timeTakenUs > 500) { + timeout = 30; + return -1; + } + } + + } + + if (numCPUsWithFrequency > 0) + this->cpuData[0].frequency = (double)totalFrequency / numCPUsWithFrequency; + + return 0; +} + +static void scanCPUFrequencyFromCPUinfo(LinuxProcessList* this) { + FILE* file = fopen(PROCCPUINFOFILE, "r"); + if (file == NULL) + return; + + unsigned int existingCPUs = this->super.existingCPUs; + int numCPUsWithFrequency = 0; + double totalFrequency = 0; + int cpuid = -1; + + while (!feof(file)) { + double frequency; + char buffer[PROC_LINE_LENGTH]; + + if (fgets(buffer, PROC_LINE_LENGTH, file) == NULL) + break; + + if (sscanf(buffer, "processor : %d", &cpuid) == 1) { + continue; + } else if ( + (sscanf(buffer, "cpu MHz : %lf", &frequency) == 1) || + (sscanf(buffer, "clock : %lfMHz", &frequency) == 1) + ) { + if (cpuid < 0 || (unsigned int)cpuid > (existingCPUs - 1)) { + continue; + } + + CPUData* cpuData = &(this->cpuData[cpuid + 1]); + /* do not override sysfs data */ + if (isnan(cpuData->frequency)) { + cpuData->frequency = frequency; + } + numCPUsWithFrequency++; + totalFrequency += frequency; + } else if (buffer[0] == '\n') { + cpuid = -1; + } + } + fclose(file); + + if (numCPUsWithFrequency > 0) { + this->cpuData[0].frequency = totalFrequency / numCPUsWithFrequency; + } +} + +static void LinuxProcessList_scanCPUFrequency(LinuxProcessList* this) { + unsigned int existingCPUs = this->super.existingCPUs; + + for (unsigned int i = 0; i <= existingCPUs; i++) { + this->cpuData[i].frequency = NAN; + } + + if (scanCPUFrequencyFromSysCPUFreq(this) == 0) { + return; + } + + scanCPUFrequencyFromCPUinfo(this); +} + +void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { + LinuxProcessList* this = (LinuxProcessList*) super; + const Settings* settings = super->settings; + + LinuxProcessList_scanMemoryInfo(super); + LinuxProcessList_scanHugePages(this); + LinuxProcessList_scanZfsArcstats(this); + LinuxProcessList_scanZramInfo(this); + + double period = LinuxProcessList_scanCPUTime(super); + + if (settings->showCPUFrequency) { + LinuxProcessList_scanCPUFrequency(this); + } + + #ifdef HAVE_SENSORS_SENSORS_H + if (settings->showCPUTemperature) + LibSensors_getCPUTemperatures(this->cpuData, this->super.existingCPUs, this->super.activeCPUs); + #endif + + // in pause mode only gather global data for meters (CPU/memory/...) + if (pauseProcessUpdate) { + return; + } + + if (settings->ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) { + // Refer to sched(7) 'autogroup feature' section + // The kernel feature can be enabled/disabled through procfs at + // any time, so check for it at the start of each sample - only + // read from per-process procfs files if it's globally enabled. + this->haveAutogroup = LinuxProcess_isAutogroupEnabled(); + } else { + this->haveAutogroup = false; + } + + /* PROCDIR is an absolute path */ + assert(PROCDIR[0] == '/'); +#ifdef HAVE_OPENAT + openat_arg_t rootFd = AT_FDCWD; +#else + openat_arg_t rootFd = ""; +#endif + + LinuxProcessList_recurseProcTree(this, rootFd, PROCDIR, NULL, period); +} + +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { + assert(id < super->existingCPUs); + + const LinuxProcessList* this = (const LinuxProcessList*) super; + return this->cpuData[id + 1].online; +} diff --git a/linux/LinuxProcessList.h b/linux/LinuxProcessList.h new file mode 100644 index 0000000..6c2f7db --- /dev/null +++ b/linux/LinuxProcessList.h @@ -0,0 +1,126 @@ +#ifndef HEADER_LinuxProcessList +#define HEADER_LinuxProcessList +/* +htop - LinuxProcessList.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" + +#include <stdbool.h> +#include <sys/types.h> + +#include "Hashtable.h" +#include "ProcessList.h" +#include "UsersTable.h" +#include "ZramStats.h" +#include "zfs/ZfsArcStats.h" + +#define HTOP_HUGEPAGE_BASE_SHIFT 16 +#define HTOP_HUGEPAGE_COUNT 24 + +typedef struct CPUData_ { + unsigned long long int totalTime; + unsigned long long int userTime; + unsigned long long int systemTime; + unsigned long long int systemAllTime; + unsigned long long int idleAllTime; + unsigned long long int idleTime; + unsigned long long int niceTime; + unsigned long long int ioWaitTime; + unsigned long long int irqTime; + unsigned long long int softIrqTime; + unsigned long long int stealTime; + unsigned long long int guestTime; + + unsigned long long int totalPeriod; + unsigned long long int userPeriod; + unsigned long long int systemPeriod; + unsigned long long int systemAllPeriod; + unsigned long long int idleAllPeriod; + unsigned long long int idlePeriod; + unsigned long long int nicePeriod; + unsigned long long int ioWaitPeriod; + unsigned long long int irqPeriod; + unsigned long long int softIrqPeriod; + unsigned long long int stealPeriod; + unsigned long long int guestPeriod; + + double frequency; + + #ifdef HAVE_SENSORS_SENSORS_H + double temperature; + #endif + + bool online; +} CPUData; + +typedef struct TtyDriver_ { + char* path; + unsigned int major; + unsigned int minorFrom; + unsigned int minorTo; +} TtyDriver; + +typedef struct LinuxProcessList_ { + ProcessList super; + + CPUData* cpuData; + + TtyDriver* ttyDrivers; + bool haveSmapsRollup; + bool haveAutogroup; + + #ifdef HAVE_DELAYACCT + struct nl_sock* netlink_socket; + int netlink_family; + #endif + + memory_t totalHugePageMem; + memory_t usedHugePageMem[HTOP_HUGEPAGE_COUNT]; + + memory_t availableMem; + + ZfsArcStats zfs; + ZramStats zram; +} LinuxProcessList; + +#ifndef PROCDIR +#define PROCDIR "/proc" +#endif + +#ifndef PROCCPUINFOFILE +#define PROCCPUINFOFILE PROCDIR "/cpuinfo" +#endif + +#ifndef PROCSTATFILE +#define PROCSTATFILE PROCDIR "/stat" +#endif + +#ifndef PROCMEMINFOFILE +#define PROCMEMINFOFILE PROCDIR "/meminfo" +#endif + +#ifndef PROCARCSTATSFILE +#define PROCARCSTATSFILE PROCDIR "/spl/kstat/zfs/arcstats" +#endif + +#ifndef PROCTTYDRIVERSFILE +#define PROCTTYDRIVERSFILE PROCDIR "/tty/drivers" +#endif + +#ifndef PROC_LINE_LENGTH +#define PROC_LINE_LENGTH 4096 +#endif + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId); + +void ProcessList_delete(ProcessList* pl); + +void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); + +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); + +#endif diff --git a/linux/Platform.c b/linux/Platform.c new file mode 100644 index 0000000..64f25c4 --- /dev/null +++ b/linux/Platform.c @@ -0,0 +1,1033 @@ +/* +htop - linux/Platform.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" + +#include "linux/Platform.h" + +#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <math.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include <sys/sysmacros.h> + +#include "BatteryMeter.h" +#include "ClockMeter.h" +#include "Compat.h" +#include "CPUMeter.h" +#include "DateMeter.h" +#include "DateTimeMeter.h" +#include "DiskIOMeter.h" +#include "HostnameMeter.h" +#include "HugePageMeter.h" +#include "LoadAverageMeter.h" +#include "Macros.h" +#include "MainPanel.h" +#include "Meter.h" +#include "MemoryMeter.h" +#include "MemorySwapMeter.h" +#include "NetworkIOMeter.h" +#include "Object.h" +#include "Panel.h" +#include "PressureStallMeter.h" +#include "ProcessList.h" +#include "ProvideCurses.h" +#include "linux/SELinuxMeter.h" +#include "Settings.h" +#include "SwapMeter.h" +#include "SysArchMeter.h" +#include "TasksMeter.h" +#include "UptimeMeter.h" +#include "XUtils.h" +#include "linux/IOPriority.h" +#include "linux/IOPriorityPanel.h" +#include "linux/LinuxProcess.h" +#include "linux/LinuxProcessList.h" +#include "linux/SystemdMeter.h" +#include "linux/ZramMeter.h" +#include "linux/ZramStats.h" +#include "zfs/ZfsArcMeter.h" +#include "zfs/ZfsArcStats.h" +#include "zfs/ZfsCompressedArcMeter.h" + +#ifdef HAVE_LIBCAP +#include <errno.h> +#include <sys/capability.h> +#endif + +#ifdef HAVE_SENSORS_SENSORS_H +#include "LibSensors.h" +#endif + +#ifndef O_PATH +#define O_PATH 010000000 // declare for ancient glibc versions +#endif + + +#ifdef HAVE_LIBCAP +enum CapMode { + CAP_MODE_OFF, + CAP_MODE_BASIC, + CAP_MODE_STRICT +}; +#endif + +bool Running_containerized = false; + +const ScreenDefaults Platform_defaultScreens[] = { + { + .name = "Main", + .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT M_SHARE STATE PERCENT_CPU PERCENT_MEM TIME Command", + .sortKey = "PERCENT_CPU", + }, + { + .name = "I/O", + .columns = "PID USER IO_PRIORITY IO_RATE IO_READ_RATE IO_WRITE_RATE PERCENT_SWAP_DELAY PERCENT_IO_DELAY Command", + .sortKey = "IO_RATE", + }, +}; + +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 = " 6 SIGIOT", .number = 6 }, + { .name = " 7 SIGBUS", .number = 7 }, + { .name = " 8 SIGFPE", .number = 8 }, + { .name = " 9 SIGKILL", .number = 9 }, + { .name = "10 SIGUSR1", .number = 10 }, + { .name = "11 SIGSEGV", .number = 11 }, + { .name = "12 SIGUSR2", .number = 12 }, + { .name = "13 SIGPIPE", .number = 13 }, + { .name = "14 SIGALRM", .number = 14 }, + { .name = "15 SIGTERM", .number = 15 }, + { .name = "16 SIGSTKFLT", .number = 16 }, + { .name = "17 SIGCHLD", .number = 17 }, + { .name = "18 SIGCONT", .number = 18 }, + { .name = "19 SIGSTOP", .number = 19 }, + { .name = "20 SIGTSTP", .number = 20 }, + { .name = "21 SIGTTIN", .number = 21 }, + { .name = "22 SIGTTOU", .number = 22 }, + { .name = "23 SIGURG", .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 SIGIO", .number = 29 }, + { .name = "29 SIGPOLL", .number = 29 }, + { .name = "30 SIGPWR", .number = 30 }, + { .name = "31 SIGSYS", .number = 31 }, +}; + +const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); + +static enum { BAT_PROC, BAT_SYS, BAT_ERR } Platform_Battery_method = BAT_PROC; +static time_t Platform_Battery_cacheTime; +static double Platform_Battery_cachePercent = NAN; +static ACPresence Platform_Battery_cacheIsOnAC; + +#ifdef HAVE_LIBCAP +static enum CapMode Platform_capabilitiesMode = CAP_MODE_BASIC; +#endif + +static Htop_Reaction Platform_actionSetIOPriority(State* st) { + if (Settings_isReadonly()) + return HTOP_OK; + + const LinuxProcess* p = (const LinuxProcess*) Panel_getSelected((Panel*)st->mainPanel); + if (!p) + return HTOP_OK; + + IOPriority ioprio1 = p->ioPriority; + Panel* ioprioPanel = IOPriorityPanel_new(ioprio1); + const void* set = Action_pickFromVector(st, ioprioPanel, 20, true); + if (set) { + IOPriority ioprio2 = IOPriorityPanel_getIOPriority(ioprioPanel); + bool ok = MainPanel_foreachProcess(st->mainPanel, LinuxProcess_setIOPriority, (Arg) { .i = ioprio2 }, NULL); + if (!ok) { + beep(); + } + } + Panel_delete((Object*)ioprioPanel); + return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; +} + +static bool Platform_changeAutogroupPriority(MainPanel* panel, int delta) { + if (LinuxProcess_isAutogroupEnabled() == false) { + beep(); + return false; + } + bool anyTagged; + bool ok = MainPanel_foreachProcess(panel, LinuxProcess_changeAutogroupPriorityBy, (Arg) { .i = delta }, &anyTagged); + if (!ok) + beep(); + return anyTagged; +} + +static Htop_Reaction Platform_actionHigherAutogroupPriority(State* st) { + if (Settings_isReadonly()) + return HTOP_OK; + + bool changed = Platform_changeAutogroupPriority(st->mainPanel, -1); + return changed ? HTOP_REFRESH : HTOP_OK; +} + +static Htop_Reaction Platform_actionLowerAutogroupPriority(State* st) { + if (Settings_isReadonly()) + return HTOP_OK; + + bool changed = Platform_changeAutogroupPriority(st->mainPanel, 1); + return changed ? HTOP_REFRESH : HTOP_OK; +} + +void Platform_setBindings(Htop_Action* keys) { + keys['i'] = Platform_actionSetIOPriority; + keys['{'] = Platform_actionLowerAutogroupPriority; + keys['}'] = Platform_actionHigherAutogroupPriority; + keys[KEY_F(19)] = Platform_actionLowerAutogroupPriority; // Shift-F7 + keys[KEY_F(20)] = Platform_actionHigherAutogroupPriority; // Shift-F8 +} + +const MeterClass* const Platform_meterTypes[] = { + &CPUMeter_class, + &ClockMeter_class, + &DateMeter_class, + &DateTimeMeter_class, + &LoadAverageMeter_class, + &LoadMeter_class, + &MemoryMeter_class, + &SwapMeter_class, + &MemorySwapMeter_class, + &SysArchMeter_class, + &HugePageMeter_class, + &TasksMeter_class, + &UptimeMeter_class, + &BatteryMeter_class, + &HostnameMeter_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, + &PressureStallCPUSomeMeter_class, + &PressureStallIOSomeMeter_class, + &PressureStallIOFullMeter_class, + &PressureStallMemorySomeMeter_class, + &PressureStallMemoryFullMeter_class, + &ZfsArcMeter_class, + &ZfsCompressedArcMeter_class, + &ZramMeter_class, + &DiskIOMeter_class, + &NetworkIOMeter_class, + &SELinuxMeter_class, + &SystemdMeter_class, + NULL +}; + +int Platform_getUptime(void) { + double uptime = 0; + FILE* fd = fopen(PROCDIR "/uptime", "r"); + if (fd) { + int n = fscanf(fd, "%64lf", &uptime); + fclose(fd); + if (n <= 0) { + return 0; + } + } + return floor(uptime); +} + +void Platform_getLoadAverage(double* one, double* five, double* fifteen) { + FILE* fd = fopen(PROCDIR "/loadavg", "r"); + if (!fd) + goto err; + + double scanOne, scanFive, scanFifteen; + int r = fscanf(fd, "%lf %lf %lf", &scanOne, &scanFive, &scanFifteen); + fclose(fd); + if (r != 3) + goto err; + + *one = scanOne; + *five = scanFive; + *fifteen = scanFifteen; + return; + +err: + *one = NAN; + *five = NAN; + *fifteen = NAN; +} + +int Platform_getMaxPid(void) { + FILE* file = fopen(PROCDIR "/sys/kernel/pid_max", "r"); + if (!file) + return -1; + + int maxPid = 4194303; + int match = fscanf(file, "%32d", &maxPid); + (void) match; + fclose(file); + return maxPid; +} + +double Platform_setCPUValues(Meter* this, unsigned int cpu) { + const LinuxProcessList* pl = (const LinuxProcessList*) this->pl; + const CPUData* cpuData = &(pl->cpuData[cpu]); + double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod); + double percent; + double* v = this->values; + + if (!cpuData->online) { + this->curItems = 0; + return NAN; + } + + 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->systemPeriod / total * 100.0; + v[CPU_METER_IRQ] = cpuData->irqPeriod / total * 100.0; + v[CPU_METER_SOFTIRQ] = cpuData->softIrqPeriod / total * 100.0; + v[CPU_METER_STEAL] = cpuData->stealPeriod / total * 100.0; + v[CPU_METER_GUEST] = cpuData->guestPeriod / total * 100.0; + v[CPU_METER_IOWAIT] = cpuData->ioWaitPeriod / total * 100.0; + this->curItems = 8; + if (this->pl->settings->accountGuestInCPUMeter) { + percent = v[0] + v[1] + v[2] + v[3] + v[4] + v[5] + v[6]; + } else { + percent = v[0] + v[1] + v[2] + v[3] + v[4]; + } + } else { + v[2] = cpuData->systemAllPeriod / total * 100.0; + v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0; + this->curItems = 4; + percent = v[0] + v[1] + v[2] + v[3]; + } + percent = CLAMP(percent, 0.0, 100.0); + if (isnan(percent)) { + percent = 0.0; + } + + v[CPU_METER_FREQUENCY] = cpuData->frequency; + +#ifdef HAVE_SENSORS_SENSORS_H + v[CPU_METER_TEMPERATURE] = cpuData->temperature; +#else + v[CPU_METER_TEMPERATURE] = NAN; +#endif + + return percent; +} + +void Platform_setMemoryValues(Meter* this) { + const ProcessList* pl = this->pl; + const LinuxProcessList* lpl = (const LinuxProcessList*) pl; + + this->total = pl->totalMem; + this->values[MEMORY_METER_USED] = pl->usedMem; + this->values[MEMORY_METER_BUFFERS] = pl->buffersMem; + this->values[MEMORY_METER_SHARED] = pl->sharedMem; + this->values[MEMORY_METER_CACHE] = pl->cachedMem; + this->values[MEMORY_METER_AVAILABLE] = pl->availableMem; + + if (lpl->zfs.enabled != 0 && !Running_containerized) { + // ZFS does not shrink below the value of zfs_arc_min. + unsigned long long int shrinkableSize = 0; + if (lpl->zfs.size > lpl->zfs.min) + shrinkableSize = lpl->zfs.size - lpl->zfs.min; + this->values[MEMORY_METER_USED] -= shrinkableSize; + this->values[MEMORY_METER_CACHE] += shrinkableSize; + this->values[MEMORY_METER_AVAILABLE] += shrinkableSize; + } +} + +void Platform_setSwapValues(Meter* this) { + const ProcessList* pl = this->pl; + this->total = pl->totalSwap; + this->values[0] = pl->usedSwap; + this->values[1] = pl->cachedSwap; +} + +void Platform_setZramValues(Meter* this) { + const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl; + this->total = lpl->zram.totalZram; + this->values[0] = lpl->zram.usedZramComp; + this->values[1] = lpl->zram.usedZramOrig; +} + +void Platform_setZfsArcValues(Meter* this) { + const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl; + + ZfsArcMeter_readStats(this, &(lpl->zfs)); +} + +void Platform_setZfsCompressedArcValues(Meter* this) { + const LinuxProcessList* lpl = (const LinuxProcessList*) this->pl; + + ZfsCompressedArcMeter_readStats(this, &(lpl->zfs)); +} + +char* Platform_getProcessEnv(pid_t pid) { + char procname[128]; + xSnprintf(procname, sizeof(procname), PROCDIR "/%d/environ", pid); + FILE* fd = fopen(procname, "r"); + if (!fd) + return NULL; + + char* env = NULL; + + size_t capacity = 0; + size_t size = 0; + ssize_t bytes = 0; + + do { + size += bytes; + capacity += 4096; + env = xRealloc(env, capacity); + } while ((bytes = fread(env + size, 1, capacity - size, fd)) > 0); + + fclose(fd); + + if (bytes < 0) { + free(env); + return NULL; + } + + size += bytes; + + env = xRealloc(env, size + 2); + + env[size] = '\0'; + env[size + 1] = '\0'; + + return env; +} + +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { + FileLocks_ProcessData* pdata = xCalloc(1, sizeof(FileLocks_ProcessData)); + DIR* dirp; + int dfd; + + char path[PATH_MAX]; + xSnprintf(path, sizeof(path), PROCDIR "/%d/fdinfo/", pid); + if (strlen(path) >= (sizeof(path) - 2)) + goto err; + + if (!(dirp = opendir(path))) + goto err; + + if ((dfd = dirfd(dirp)) == -1) { + closedir(dirp); + goto err; + } + + FileLocks_LockData** data_ref = &pdata->locks; + for (struct dirent* de; (de = readdir(dirp)); ) { + if (String_eq(de->d_name, ".") || String_eq(de->d_name, "..")) + continue; + + errno = 0; + char *end = de->d_name; + int file = strtoull(de->d_name, &end, 10); + if (errno || *end) + continue; + + int fd = openat(dfd, de->d_name, O_RDONLY | O_CLOEXEC); + if(fd == -1) + continue; + FILE *f = fdopen(fd, "r"); + if(!f) { + close(fd); + continue; + } + + for (char buffer[1024]; fgets(buffer, sizeof(buffer), f); ) { + if (!strchr(buffer, '\n')) + continue; + + if (strncmp(buffer, "lock:\t", strlen("lock:\t"))) + continue; + + FileLocks_Data data = {.fd = file}; + int _; + unsigned int maj, min; + char lock_end[25], locktype[32], exclusive[32], readwrite[32]; + if (10 != sscanf(buffer + strlen("lock:\t"), "%d: %31s %31s %31s %d %x:%x:%"PRIu64" %"PRIu64" %24s", + &_, locktype, exclusive, readwrite, &_, + &maj, &min, &data.inode, + &data.start, lock_end)) + continue; + + data.locktype = xStrdup(locktype); + data.exclusive = xStrdup(exclusive); + data.readwrite = xStrdup(readwrite); + data.dev = makedev(maj, min); + + if (String_eq(lock_end, "EOF")) + data.end = ULLONG_MAX; + else + data.end = strtoull(lock_end, NULL, 10); + + xSnprintf(path, sizeof(path), PROCDIR "/%d/fd/%s", pid, de->d_name); + char link[PATH_MAX]; + ssize_t link_len; + if (strlen(path) < (sizeof(path) - 2) && (link_len = readlink(path, link, sizeof(link))) != -1) + data.filename = xStrndup(link, link_len); + + *data_ref = xCalloc(1, sizeof(FileLocks_LockData)); + (*data_ref)->data = data; + data_ref = &(*data_ref)->next; + } + + fclose(f); + } + + closedir(dirp); + return pdata; + +err: + pdata->error = true; + return pdata; +} + +void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred) { + *ten = *sixty = *threehundred = 0; + char procname[128]; + xSnprintf(procname, sizeof(procname), PROCDIR "/pressure/%s", file); + FILE* fd = fopen(procname, "r"); + if (!fd) { + *ten = *sixty = *threehundred = NAN; + return; + } + int total = fscanf(fd, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); + if (!some) { + total = fscanf(fd, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred); + } + (void) total; + assert(total == 3); + fclose(fd); +} + +bool Platform_getDiskIO(DiskIOData* data) { + FILE* fd = fopen(PROCDIR "/diskstats", "r"); + if (!fd) + return false; + + char lastTopDisk[32] = { '\0' }; + + unsigned long long int read_sum = 0, write_sum = 0, timeSpend_sum = 0; + char lineBuffer[256]; + while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + char diskname[32]; + unsigned long long int read_tmp, write_tmp, timeSpend_tmp; + if (sscanf(lineBuffer, "%*d %*d %31s %*u %*u %llu %*u %*u %*u %llu %*u %*u %llu", diskname, &read_tmp, &write_tmp, &timeSpend_tmp) == 4) { + if (String_startsWith(diskname, "dm-")) + continue; + + if (String_startsWith(diskname, "zram")) + continue; + + /* only count root disks, e.g. do not count IO from sda and sda1 twice */ + if (lastTopDisk[0] && String_startsWith(diskname, lastTopDisk)) + continue; + + /* This assumes disks are listed directly before any of their partitions */ + String_safeStrncpy(lastTopDisk, diskname, sizeof(lastTopDisk)); + + read_sum += read_tmp; + write_sum += write_tmp; + timeSpend_sum += timeSpend_tmp; + } + } + fclose(fd); + /* multiply with sector size */ + data->totalBytesRead = 512 * read_sum; + data->totalBytesWritten = 512 * write_sum; + data->totalMsTimeSpend = timeSpend_sum; + return true; +} + +bool Platform_getNetworkIO(NetworkIOData* data) { + FILE* fd = fopen(PROCDIR "/net/dev", "r"); + if (!fd) + return false; + + memset(data, 0, sizeof(NetworkIOData)); + char lineBuffer[512]; + while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + char interfaceName[32]; + unsigned long long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted; + if (sscanf(lineBuffer, "%31s %llu %llu %*u %*u %*u %*u %*u %*u %llu %llu", + interfaceName, + &bytesReceived, + &packetsReceived, + &bytesTransmitted, + &packetsTransmitted) != 5) + continue; + + if (String_eq(interfaceName, "lo:")) + continue; + + data->bytesReceived += bytesReceived; + data->packetsReceived += packetsReceived; + data->bytesTransmitted += bytesTransmitted; + data->packetsTransmitted += packetsTransmitted; + } + + fclose(fd); + + return true; +} + +// Linux battery reading by Ian P. Hands (iphands@gmail.com, ihands@redhat.com). + +#define PROC_BATTERY_DIR PROCDIR "/acpi/battery" +#define PROC_POWERSUPPLY_DIR PROCDIR "/acpi/ac_adapter" +#define PROC_POWERSUPPLY_ACSTATE_FILE PROC_POWERSUPPLY_DIR "/AC/state" +#define SYS_POWERSUPPLY_DIR "/sys/class/power_supply" + +// ---------------------------------------- +// READ FROM /proc +// ---------------------------------------- + +static double Platform_Battery_getProcBatInfo(void) { + DIR* batteryDir = opendir(PROC_BATTERY_DIR); + if (!batteryDir) + return NAN; + + uint64_t totalFull = 0; + uint64_t totalRemain = 0; + + struct dirent* dirEntry = NULL; + while ((dirEntry = readdir(batteryDir))) { + const char* entryName = dirEntry->d_name; + if (!String_startsWith(entryName, "BAT")) + continue; + + char filePath[256]; + char bufInfo[1024] = {0}; + xSnprintf(filePath, sizeof(filePath), "%s/%s/info", PROC_BATTERY_DIR, entryName); + ssize_t r = xReadfile(filePath, bufInfo, sizeof(bufInfo)); + if (r < 0) + continue; + + char bufState[1024] = {0}; + xSnprintf(filePath, sizeof(filePath), "%s/%s/state", PROC_BATTERY_DIR, entryName); + r = xReadfile(filePath, bufState, sizeof(bufState)); + if (r < 0) + continue; + + const char* line; + + //Getting total charge for all batteries + char* buf = bufInfo; + while ((line = strsep(&buf, "\n")) != NULL) { + char field[100] = {0}; + int val = 0; + if (2 != sscanf(line, "%99[^:]:%d", field, &val)) + continue; + + if (String_eq(field, "last full capacity")) { + totalFull += val; + break; + } + } + + //Getting remaining charge for all batteries + buf = bufState; + while ((line = strsep(&buf, "\n")) != NULL) { + char field[100] = {0}; + int val = 0; + if (2 != sscanf(line, "%99[^:]:%d", field, &val)) + continue; + + if (String_eq(field, "remaining capacity")) { + totalRemain += val; + break; + } + } + } + + closedir(batteryDir); + + return totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN; +} + +static ACPresence procAcpiCheck(void) { + char buffer[1024] = {0}; + ssize_t r = xReadfile(PROC_POWERSUPPLY_ACSTATE_FILE, buffer, sizeof(buffer)); + if (r < 1) + return AC_ERROR; + + return String_eq(buffer, "on-line") ? AC_PRESENT : AC_ABSENT; +} + +static void Platform_Battery_getProcData(double* percent, ACPresence* isOnAC) { + *isOnAC = procAcpiCheck(); + *percent = AC_ERROR != *isOnAC ? Platform_Battery_getProcBatInfo() : NAN; +} + +// ---------------------------------------- +// READ FROM /sys +// ---------------------------------------- + +static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) { + *percent = NAN; + *isOnAC = AC_ERROR; + + DIR* dir = opendir(SYS_POWERSUPPLY_DIR); + if (!dir) + return; + + uint64_t totalFull = 0; + uint64_t totalRemain = 0; + + const struct dirent* dirEntry; + while ((dirEntry = readdir(dir))) { + const char* entryName = dirEntry->d_name; + +#ifdef HAVE_OPENAT + int entryFd = openat(dirfd(dir), entryName, O_DIRECTORY | O_PATH); + if (entryFd < 0) + continue; +#else + char entryFd[4096]; + xSnprintf(entryFd, sizeof(entryFd), SYS_POWERSUPPLY_DIR "/%s", entryName); +#endif + + enum { AC, BAT } type; + if (String_startsWith(entryName, "BAT")) { + type = BAT; + } else if (String_startsWith(entryName, "AC")) { + type = AC; + } else { + char buffer[32]; + ssize_t ret = xReadfileat(entryFd, "type", buffer, sizeof(buffer)); + if (ret <= 0) + goto next; + + /* drop optional trailing newlines */ + for (char* buf = &buffer[(size_t)ret - 1]; *buf == '\n'; buf--) + *buf = '\0'; + + if (String_eq(buffer, "Battery")) + type = BAT; + else if (String_eq(buffer, "Mains")) + type = AC; + else + goto next; + } + + if (type == BAT) { + char buffer[1024]; + ssize_t r = xReadfileat(entryFd, "uevent", buffer, sizeof(buffer)); + if (r < 0) + goto next; + + bool full = false; + bool now = false; + + double fullCharge = 0; + double capacityLevel = NAN; + const char* line; + + char* buf = buffer; + while ((line = strsep(&buf, "\n")) != NULL) { + char field[100] = {0}; + int val = 0; + if (2 != sscanf(line, "POWER_SUPPLY_%99[^=]=%d", field, &val)) + continue; + + if (String_eq(field, "CAPACITY")) { + capacityLevel = val / 100.0; + continue; + } + + if (String_eq(field, "ENERGY_FULL") || String_eq(field, "CHARGE_FULL")) { + fullCharge = val; + totalFull += fullCharge; + full = true; + if (now) + break; + continue; + } + + if (String_eq(field, "ENERGY_NOW") || String_eq(field, "CHARGE_NOW")) { + totalRemain += val; + now = true; + if (full) + break; + continue; + } + } + + if (!now && full && !isnan(capacityLevel)) + totalRemain += capacityLevel * fullCharge; + + } else if (type == AC) { + if (*isOnAC != AC_ERROR) + goto next; + + char buffer[2]; + ssize_t r = xReadfileat(entryFd, "online", buffer, sizeof(buffer)); + if (r < 1) { + *isOnAC = AC_ERROR; + goto next; + } + + if (buffer[0] == '0') + *isOnAC = AC_ABSENT; + else if (buffer[0] == '1') + *isOnAC = AC_PRESENT; + } + +next: + Compat_openatArgClose(entryFd); + } + + closedir(dir); + + *percent = totalFull > 0 ? ((double) totalRemain * 100.0) / (double) totalFull : NAN; +} + +void Platform_getBattery(double* percent, ACPresence* isOnAC) { + time_t now = time(NULL); + // update battery reading is slow. Update it each 10 seconds only. + if (now < Platform_Battery_cacheTime + 10) { + *percent = Platform_Battery_cachePercent; + *isOnAC = Platform_Battery_cacheIsOnAC; + return; + } + + if (Platform_Battery_method == BAT_PROC) { + Platform_Battery_getProcData(percent, isOnAC); + if (isnan(*percent)) + Platform_Battery_method = BAT_SYS; + } + if (Platform_Battery_method == BAT_SYS) { + Platform_Battery_getSysData(percent, isOnAC); + if (isnan(*percent)) + Platform_Battery_method = BAT_ERR; + } + if (Platform_Battery_method == BAT_ERR) { + *percent = NAN; + *isOnAC = AC_ERROR; + } else { + *percent = CLAMP(*percent, 0.0, 100.0); + } + Platform_Battery_cachePercent = *percent; + Platform_Battery_cacheIsOnAC = *isOnAC; + Platform_Battery_cacheTime = now; +} + +void Platform_longOptionsUsage(const char* name) +{ +#ifdef HAVE_LIBCAP + printf( +" --drop-capabilities[=off|basic|strict] Drop Linux capabilities when running as root\n" +" off - do not drop any capabilities\n" +" basic (default) - drop all capabilities not needed by %s\n" +" strict - drop all capabilities except those needed for\n" +" core functionality\n", name); +#else + (void) name; +#endif +} + +CommandLineStatus Platform_getLongOption(int opt, int argc, char** argv) { +#ifndef HAVE_LIBCAP + (void) argc; + (void) argv; +#endif + + switch (opt) { +#ifdef HAVE_LIBCAP + case 160: { + const char* mode = optarg; + if (!mode && optind < argc && argv[optind] != NULL && + (argv[optind][0] != '\0' && argv[optind][0] != '-')) { + mode = argv[optind++]; + } + + if (!mode || String_eq(mode, "basic")) { + Platform_capabilitiesMode = CAP_MODE_BASIC; + } else if (String_eq(mode, "off")) { + Platform_capabilitiesMode = CAP_MODE_OFF; + } else if (String_eq(mode, "strict")) { + Platform_capabilitiesMode = CAP_MODE_STRICT; + } else { + fprintf(stderr, "Error: invalid capabilities mode \"%s\".\n", mode); + return STATUS_ERROR_EXIT; + } + return STATUS_OK; + } +#endif + + default: + break; + } + return STATUS_ERROR_EXIT; +} + +#ifdef HAVE_LIBCAP +static int dropCapabilities(enum CapMode mode) { + + if (mode == CAP_MODE_OFF) + return 0; + + /* capabilities we keep to operate */ + const cap_value_t keepcapsStrict[] = { + CAP_DAC_READ_SEARCH, + CAP_SYS_PTRACE, + }; + const cap_value_t keepcapsBasic[] = { + CAP_DAC_READ_SEARCH, /* read non world-readable process files of other users, like /proc/[pid]/io */ + CAP_KILL, /* send signals to processes of other users */ + CAP_SYS_NICE, /* lower process nice value / change nice value for arbitrary processes */ + CAP_SYS_PTRACE, /* read /proc/[pid]/exe */ +#ifdef HAVE_DELAYACCT + CAP_NET_ADMIN, /* communicate over netlink socket for delay accounting */ +#endif + }; + const cap_value_t* const keepcaps = (mode == CAP_MODE_BASIC) ? keepcapsBasic : keepcapsStrict; + const size_t ncap = (mode == CAP_MODE_BASIC) ? ARRAYSIZE(keepcapsBasic) : ARRAYSIZE(keepcapsStrict); + + cap_t caps = cap_init(); + if (caps == NULL) { + fprintf(stderr, "Error: can not initialize capabilities: %s\n", strerror(errno)); + return -1; + } + + if (cap_clear(caps) < 0) { + fprintf(stderr, "Error: can not clear capabilities: %s\n", strerror(errno)); + cap_free(caps); + return -1; + } + + cap_t currCaps = cap_get_proc(); + if (currCaps == NULL) { + fprintf(stderr, "Error: can not get current process capabilities: %s\n", strerror(errno)); + cap_free(caps); + return -1; + } + + for (size_t i = 0; i < ncap; i++) { + if (!CAP_IS_SUPPORTED(keepcaps[i])) + continue; + + cap_flag_value_t current; + if (cap_get_flag(currCaps, keepcaps[i], CAP_PERMITTED, ¤t) < 0) { + fprintf(stderr, "Error: can not get current value of capability %d: %s\n", keepcaps[i], strerror(errno)); + cap_free(currCaps); + cap_free(caps); + return -1; + } + + if (current != CAP_SET) + continue; + + if (cap_set_flag(caps, CAP_PERMITTED, 1, &keepcaps[i], CAP_SET) < 0) { + fprintf(stderr, "Error: can not set permitted capability %d: %s\n", keepcaps[i], strerror(errno)); + cap_free(currCaps); + cap_free(caps); + return -1; + } + + if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &keepcaps[i], CAP_SET) < 0) { + fprintf(stderr, "Error: can not set effective capability %d: %s\n", keepcaps[i], strerror(errno)); + cap_free(currCaps); + cap_free(caps); + return -1; + } + } + + if (cap_set_proc(caps) < 0) { + fprintf(stderr, "Error: can not set process capabilities: %s\n", strerror(errno)); + cap_free(currCaps); + cap_free(caps); + return -1; + } + + cap_free(currCaps); + cap_free(caps); + + return 0; +} +#endif + +bool Platform_init(void) { +#ifdef HAVE_LIBCAP + if (dropCapabilities(Platform_capabilitiesMode) < 0) + return false; +#endif + + if (access(PROCDIR, R_OK) != 0) { + fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR); + return false; + } + +#ifdef HAVE_SENSORS_SENSORS_H + LibSensors_init(); +#endif + + char target[PATH_MAX]; + ssize_t ret = readlink(PROCDIR "/self/ns/pid", target, sizeof(target) - 1); + if (ret > 0) { + target[ret] = '\0'; + + if (!String_eq("pid:[4026531836]", target)) { // magic constant PROC_PID_INIT_INO from include/linux/proc_ns.h#L46 + Running_containerized = true; + return true; // early return + } + } + + FILE* fd = fopen(PROCDIR "/1/mounts", "r"); + if (fd) { + char lineBuffer[256]; + while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { + // detect lxc or overlayfs and guess that this means we are running containerized + if (String_startsWith(lineBuffer, "lxcfs /proc") || String_startsWith(lineBuffer, "overlay ")) { + Running_containerized = true; + break; + } + } + fclose(fd); + } // if (fd) + + return true; +} + +void Platform_done(void) { +#ifdef HAVE_SENSORS_SENSORS_H + LibSensors_cleanup(); +#endif +} diff --git a/linux/Platform.h b/linux/Platform.h new file mode 100644 index 0000000..f6ac188 --- /dev/null +++ b/linux/Platform.h @@ -0,0 +1,141 @@ +#ifndef HEADER_Platform +#define HEADER_Platform +/* +htop - linux/Platform.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" + +#include <limits.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <sys/time.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 "RichString.h" +#include "SignalsPanel.h" +#include "CommandLine.h" +#include "generic/gettime.h" +#include "generic/hostname.h" +#include "generic/uname.h" + +/* GNU/Hurd does not have PATH_MAX in limits.h */ +#ifndef PATH_MAX + #define PATH_MAX 4096 +#endif + + +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); + +extern bool Running_containerized; + +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); + +void Platform_setZramValues(Meter* this); + +void Platform_setZfsArcValues(Meter* this); + +void Platform_setZfsCompressedArcValues(Meter* this); + +char* Platform_getProcessEnv(pid_t pid); + +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid); + +void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred); + +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(); +} + +#ifdef HAVE_LIBCAP + #define PLATFORM_LONG_OPTIONS \ + {"drop-capabilities", optional_argument, 0, 160}, +#else + #define PLATFORM_LONG_OPTIONS +#endif + +void Platform_longOptionsUsage(const char* name); + +CommandLineStatus Platform_getLongOption(int opt, int argc, char** argv); + +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/linux/PressureStallMeter.c b/linux/PressureStallMeter.c new file mode 100644 index 0000000..e5089fc --- /dev/null +++ b/linux/PressureStallMeter.c @@ -0,0 +1,149 @@ +/* +htop - PressureStallMeter.c +(C) 2004-2011 Hisham H. Muhammad +(C) 2019 Ran Benita +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/PressureStallMeter.h" + +#include <stdbool.h> +#include <string.h> + +#include "CRT.h" +#include "Meter.h" +#include "Object.h" +#include "Platform.h" +#include "RichString.h" +#include "XUtils.h" + + +static const int PressureStallMeter_attributes[] = { + PRESSURE_STALL_TEN, + PRESSURE_STALL_SIXTY, + PRESSURE_STALL_THREEHUNDRED +}; + +static void PressureStallMeter_updateValues(Meter* this) { + const char* file; + if (strstr(Meter_name(this), "CPU")) { + file = "cpu"; + } else if (strstr(Meter_name(this), "IO")) { + file = "io"; + } else { + file = "memory"; + } + + bool some; + if (strstr(Meter_name(this), "Some")) { + some = true; + } else { + some = false; + } + + Platform_getPressureStall(file, some, &this->values[0], &this->values[1], &this->values[2]); + + /* only print bar for ten (not sixty and threehundred), cause the sum is meaningless */ + this->curItems = 1; + + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s %s %5.2lf%% %5.2lf%% %5.2lf%%", some ? "some" : "full", file, this->values[0], this->values[1], this->values[2]); +} + +static void PressureStallMeter_display(const Object* cast, RichString* out) { + const Meter* this = (const Meter*)cast; + char buffer[20]; + int len; + + len = xSnprintf(buffer, sizeof(buffer), "%5.2lf%% ", this->values[0]); + RichString_appendnAscii(out, CRT_colors[PRESSURE_STALL_TEN], buffer, len); + len = xSnprintf(buffer, sizeof(buffer), "%5.2lf%% ", this->values[1]); + RichString_appendnAscii(out, CRT_colors[PRESSURE_STALL_SIXTY], buffer, len); + len = xSnprintf(buffer, sizeof(buffer), "%5.2lf%% ", this->values[2]); + RichString_appendnAscii(out, CRT_colors[PRESSURE_STALL_THREEHUNDRED], buffer, len); +} + +const MeterClass PressureStallCPUSomeMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = PressureStallMeter_display, + }, + .updateValues = PressureStallMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 3, + .total = 100.0, + .attributes = PressureStallMeter_attributes, + .name = "PressureStallCPUSome", + .uiName = "PSI some CPU", + .caption = "PSI some CPU: ", + .description = "Pressure Stall Information, some cpu" +}; + +const MeterClass PressureStallIOSomeMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = PressureStallMeter_display, + }, + .updateValues = PressureStallMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 3, + .total = 100.0, + .attributes = PressureStallMeter_attributes, + .name = "PressureStallIOSome", + .uiName = "PSI some IO", + .caption = "PSI some IO: ", + .description = "Pressure Stall Information, some io" +}; + +const MeterClass PressureStallIOFullMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = PressureStallMeter_display, + }, + .updateValues = PressureStallMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 3, + .total = 100.0, + .attributes = PressureStallMeter_attributes, + .name = "PressureStallIOFull", + .uiName = "PSI full IO", + .caption = "PSI full IO: ", + .description = "Pressure Stall Information, full io" +}; + +const MeterClass PressureStallMemorySomeMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = PressureStallMeter_display, + }, + .updateValues = PressureStallMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 3, + .total = 100.0, + .attributes = PressureStallMeter_attributes, + .name = "PressureStallMemorySome", + .uiName = "PSI some memory", + .caption = "PSI some memory: ", + .description = "Pressure Stall Information, some memory" +}; + +const MeterClass PressureStallMemoryFullMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = PressureStallMeter_display, + }, + .updateValues = PressureStallMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 3, + .total = 100.0, + .attributes = PressureStallMeter_attributes, + .name = "PressureStallMemoryFull", + .uiName = "PSI full memory", + .caption = "PSI full memory: ", + .description = "Pressure Stall Information, full memory" +}; diff --git a/linux/PressureStallMeter.h b/linux/PressureStallMeter.h new file mode 100644 index 0000000..8acf46b --- /dev/null +++ b/linux/PressureStallMeter.h @@ -0,0 +1,26 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_PressureStallMeter +#define HEADER_PressureStallMeter +/* +htop - PressureStallMeter.h +(C) 2004-2011 Hisham H. Muhammad +(C) 2019 Ran Benita +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Meter.h" + + +extern const MeterClass PressureStallCPUSomeMeter_class; + +extern const MeterClass PressureStallIOSomeMeter_class; + +extern const MeterClass PressureStallIOFullMeter_class; + +extern const MeterClass PressureStallMemorySomeMeter_class; + +extern const MeterClass PressureStallMemoryFullMeter_class; + +#endif diff --git a/linux/ProcessField.h b/linux/ProcessField.h new file mode 100644 index 0000000..17cafa9 --- /dev/null +++ b/linux/ProcessField.h @@ -0,0 +1,52 @@ +#ifndef HEADER_LinuxProcessField +#define HEADER_LinuxProcessField +/* +htop - linux/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 \ + CMINFLT = 11, \ + CMAJFLT = 13, \ + UTIME = 14, \ + STIME = 15, \ + CUTIME = 16, \ + CSTIME = 17, \ + M_SHARE = 41, \ + M_TRS = 42, \ + M_DRS = 43, \ + M_LRS = 44, \ + CTID = 100, \ + VPID = 101, \ + VXID = 102, \ + RCHAR = 103, \ + WCHAR = 104, \ + SYSCR = 105, \ + SYSCW = 106, \ + RBYTES = 107, \ + WBYTES = 108, \ + CNCLWB = 109, \ + IO_READ_RATE = 110, \ + IO_WRITE_RATE = 111, \ + IO_RATE = 112, \ + CGROUP = 113, \ + OOM = 114, \ + IO_PRIORITY = 115, \ + PERCENT_CPU_DELAY = 116, \ + PERCENT_IO_DELAY = 117, \ + PERCENT_SWAP_DELAY = 118, \ + M_PSS = 119, \ + M_SWAP = 120, \ + M_PSSWP = 121, \ + CTXT = 122, \ + SECATTR = 123, \ + AUTOGROUP_ID = 127, \ + AUTOGROUP_NICE = 128, \ + CCGROUP = 129, \ + // End of list + + +#endif /* HEADER_LinuxProcessField */ diff --git a/linux/SELinuxMeter.c b/linux/SELinuxMeter.c new file mode 100644 index 0000000..c35cb68 --- /dev/null +++ b/linux/SELinuxMeter.c @@ -0,0 +1,93 @@ +/* +htop - SELinuxMeter.c +(C) 2020 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/SELinuxMeter.h" + +#include "CRT.h" + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/statfs.h> +#include <sys/statvfs.h> + +#include "Object.h" +#include "XUtils.h" + + +static const int SELinuxMeter_attributes[] = { + METER_TEXT, +}; + +static bool enabled = false; +static bool enforcing = false; + +static bool hasSELinuxMount(void) { + struct statfs sfbuf; + int r = statfs("/sys/fs/selinux", &sfbuf); + if (r != 0) { + return false; + } + + if ((uint32_t)sfbuf.f_type != /* SELINUX_MAGIC */ 0xf97cff8cU) { + return false; + } + + struct statvfs vfsbuf; + r = statvfs("/sys/fs/selinux", &vfsbuf); + if (r != 0 || (vfsbuf.f_flag & ST_RDONLY)) { + return false; + } + + return true; +} + +static bool isSelinuxEnabled(void) { + return hasSELinuxMount() && (0 == access("/etc/selinux/config", F_OK)); +} + +static bool isSelinuxEnforcing(void) { + if (!enabled) { + return false; + } + + char buf[20]; + ssize_t r = xReadfile("/sys/fs/selinux/enforce", buf, sizeof(buf)); + if (r < 0) + return false; + + int enforce = 0; + if (sscanf(buf, "%d", &enforce) != 1) { + return false; + } + + return !!enforce; +} + +static void SELinuxMeter_updateValues(Meter* this) { + enabled = isSelinuxEnabled(); + enforcing = isSelinuxEnforcing(); + + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s%s", enabled ? "enabled" : "disabled", enabled ? (enforcing ? "; mode: enforcing" : "; mode: permissive") : ""); +} + +const MeterClass SELinuxMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + }, + .updateValues = SELinuxMeter_updateValues, + .defaultMode = TEXT_METERMODE, + .maxItems = 0, + .total = 100.0, + .attributes = SELinuxMeter_attributes, + .name = "SELinux", + .uiName = "SELinux", + .description = "SELinux state overview", + .caption = "SELinux: " +}; diff --git a/linux/SELinuxMeter.h b/linux/SELinuxMeter.h new file mode 100644 index 0000000..d8a04db --- /dev/null +++ b/linux/SELinuxMeter.h @@ -0,0 +1,15 @@ +#ifndef HEADER_SELinuxMeter +#define HEADER_SELinuxMeter +/* +htop - SELinuxMeter.h +(C) 2020 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Meter.h" + + +extern const MeterClass SELinuxMeter_class; + +#endif /* HEADER_SELinuxMeter */ diff --git a/linux/SystemdMeter.c b/linux/SystemdMeter.c new file mode 100644 index 0000000..cee3231 --- /dev/null +++ b/linux/SystemdMeter.c @@ -0,0 +1,388 @@ +/* +htop - SystemdMeter.c +(C) 2020 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "linux/SystemdMeter.h" + +#include <dlfcn.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/wait.h> + +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "RichString.h" +#include "Settings.h" +#include "XUtils.h" + +#if defined(BUILD_STATIC) && defined(HAVE_LIBSYSTEMD) +#include <systemd/sd-bus.h> +#endif + + +#ifdef BUILD_STATIC + +#define sym_sd_bus_open_system sd_bus_open_system +#define sym_sd_bus_get_property_string sd_bus_get_property_string +#define sym_sd_bus_get_property_trivial sd_bus_get_property_trivial +#define sym_sd_bus_unref sd_bus_unref + +#else + +typedef void sd_bus; +typedef void sd_bus_error; +static int (*sym_sd_bus_open_system)(sd_bus**); +static int (*sym_sd_bus_get_property_string)(sd_bus*, const char*, const char*, const char*, const char*, sd_bus_error*, char**); +static int (*sym_sd_bus_get_property_trivial)(sd_bus*, const char*, const char*, const char*, const char*, sd_bus_error*, char, void*); +static sd_bus* (*sym_sd_bus_unref)(sd_bus*); +static void* dlopenHandle = NULL; + +#endif /* BUILD_STATIC */ + +#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD) +static sd_bus* bus = NULL; +#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */ + + +#define INVALID_VALUE ((unsigned int)-1) + +static char* systemState = NULL; +static unsigned int nFailedUnits = INVALID_VALUE; +static unsigned int nInstalledJobs = INVALID_VALUE; +static unsigned int nNames = INVALID_VALUE; +static unsigned int nJobs = INVALID_VALUE; + +static void SystemdMeter_done(ATTR_UNUSED Meter* this) { + free(systemState); + systemState = NULL; + +#ifdef BUILD_STATIC +# ifdef HAVE_LIBSYSTEMD + if (bus) { + sym_sd_bus_unref(bus); + } + bus = NULL; +# endif /* HAVE_LIBSYSTEMD */ +#else /* BUILD_STATIC */ + if (bus && dlopenHandle) { + sym_sd_bus_unref(bus); + } + bus = NULL; + + if (dlopenHandle) { + dlclose(dlopenHandle); + dlopenHandle = NULL; + } +#endif /* BUILD_STATIC */ +} + +#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD) +static int updateViaLib(void) { +#ifndef BUILD_STATIC + if (!dlopenHandle) { + dlopenHandle = dlopen("libsystemd.so.0", RTLD_LAZY); + if (!dlopenHandle) + goto dlfailure; + + /* Clear any errors */ + dlerror(); + + #define resolve(symbolname) do { \ + *(void **)(&sym_##symbolname) = dlsym(dlopenHandle, #symbolname); \ + if (!sym_##symbolname || dlerror() != NULL) \ + goto dlfailure; \ + } while(0) + + resolve(sd_bus_open_system); + resolve(sd_bus_get_property_string); + resolve(sd_bus_get_property_trivial); + resolve(sd_bus_unref); + + #undef resolve + } +#endif /* !BUILD_STATIC */ + + int r; + + /* Connect to the system bus */ + if (!bus) { + r = sym_sd_bus_open_system(&bus); + if (r < 0) + goto busfailure; + } + + static const char* const busServiceName = "org.freedesktop.systemd1"; + static const char* const busObjectPath = "/org/freedesktop/systemd1"; + static const char* const busInterfaceName = "org.freedesktop.systemd1.Manager"; + + r = sym_sd_bus_get_property_string(bus, + busServiceName, /* service to contact */ + busObjectPath, /* object path */ + busInterfaceName, /* interface name */ + "SystemState", /* property name */ + NULL, /* object to return error in */ + &systemState); + if (r < 0) + goto busfailure; + + r = sym_sd_bus_get_property_trivial(bus, + busServiceName, /* service to contact */ + busObjectPath, /* object path */ + busInterfaceName, /* interface name */ + "NFailedUnits", /* property name */ + NULL, /* object to return error in */ + 'u', /* property type */ + &nFailedUnits); + if (r < 0) + goto busfailure; + + r = sym_sd_bus_get_property_trivial(bus, + busServiceName, /* service to contact */ + busObjectPath, /* object path */ + busInterfaceName, /* interface name */ + "NInstalledJobs", /* property name */ + NULL, /* object to return error in */ + 'u', /* property type */ + &nInstalledJobs); + if (r < 0) + goto busfailure; + + r = sym_sd_bus_get_property_trivial(bus, + busServiceName, /* service to contact */ + busObjectPath, /* object path */ + busInterfaceName, /* interface name */ + "NNames", /* property name */ + NULL, /* object to return error in */ + 'u', /* property type */ + &nNames); + if (r < 0) + goto busfailure; + + r = sym_sd_bus_get_property_trivial(bus, + busServiceName, /* service to contact */ + busObjectPath, /* object path */ + busInterfaceName, /* interface name */ + "NJobs", /* property name */ + NULL, /* object to return error in */ + 'u', /* property type */ + &nJobs); + if (r < 0) + goto busfailure; + + /* success */ + return 0; + +busfailure: + sym_sd_bus_unref(bus); + bus = NULL; + return -2; + +#ifndef BUILD_STATIC +dlfailure: + if (dlopenHandle) { + dlclose(dlopenHandle); + dlopenHandle = NULL; + } + return -1; +#endif /* !BUILD_STATIC */ +} +#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */ + +static void updateViaExec(void) { + if (Settings_isReadonly()) + return; + + int fdpair[2]; + if (pipe(fdpair) < 0) + return; + + pid_t child = fork(); + if (child < 0) { + close(fdpair[1]); + close(fdpair[0]); + return; + } + + if (child == 0) { + close(fdpair[0]); + dup2(fdpair[1], STDOUT_FILENO); + close(fdpair[1]); + int fdnull = open("/dev/null", O_WRONLY); + if (fdnull < 0) + exit(1); + dup2(fdnull, STDERR_FILENO); + close(fdnull); + // Use of NULL in variadic functions must have a pointer cast. + // The NULL constant is not required by standard to have a pointer type. + execlp( + "systemctl", + "systemctl", + "show", + "--property=SystemState", + "--property=NFailedUnits", + "--property=NNames", + "--property=NJobs", + "--property=NInstalledJobs", + (char *)NULL); + exit(127); + } + close(fdpair[1]); + + int wstatus; + if (waitpid(child, &wstatus, 0) < 0 || !WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) { + close(fdpair[0]); + return; + } + + FILE* commandOutput = fdopen(fdpair[0], "r"); + if (!commandOutput) { + close(fdpair[0]); + return; + } + + char lineBuffer[128]; + while (fgets(lineBuffer, sizeof(lineBuffer), commandOutput)) { + if (String_startsWith(lineBuffer, "SystemState=")) { + char* newline = strchr(lineBuffer + strlen("SystemState="), '\n'); + if (newline) { + *newline = '\0'; + } + free_and_xStrdup(&systemState, lineBuffer + strlen("SystemState=")); + } else if (String_startsWith(lineBuffer, "NFailedUnits=")) { + nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10); + } else if (String_startsWith(lineBuffer, "NNames=")) { + nNames = strtoul(lineBuffer + strlen("NNames="), NULL, 10); + } else if (String_startsWith(lineBuffer, "NJobs=")) { + nJobs = strtoul(lineBuffer + strlen("NJobs="), NULL, 10); + } else if (String_startsWith(lineBuffer, "NInstalledJobs=")) { + nInstalledJobs = strtoul(lineBuffer + strlen("NInstalledJobs="), NULL, 10); + } + } + + fclose(commandOutput); +} + +static void SystemdMeter_updateValues(Meter* this) { + free(systemState); + systemState = NULL; + nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE; + +#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD) + if (updateViaLib() < 0) + updateViaExec(); +#else + updateViaExec(); +#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */ + + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", systemState ? systemState : "???"); +} + +static int zeroDigitColor(unsigned int value) { + switch (value) { + case 0: + return CRT_colors[METER_VALUE]; + case INVALID_VALUE: + return CRT_colors[METER_VALUE_ERROR]; + default: + return CRT_colors[METER_VALUE_NOTICE]; + } +} + +static int valueDigitColor(unsigned int value) { + switch (value) { + case 0: + return CRT_colors[METER_VALUE_NOTICE]; + case INVALID_VALUE: + return CRT_colors[METER_VALUE_ERROR]; + default: + return CRT_colors[METER_VALUE]; + } +} + + +static void SystemdMeter_display(ATTR_UNUSED const Object* cast, RichString* out) { + char buffer[16]; + int len; + int color = METER_VALUE_ERROR; + + if (systemState) { + color = String_eq(systemState, "running") ? METER_VALUE_OK : + String_eq(systemState, "degraded") ? METER_VALUE_ERROR : METER_VALUE_WARN; + } + RichString_writeAscii(out, CRT_colors[color], systemState ? systemState : "N/A"); + + RichString_appendAscii(out, CRT_colors[METER_TEXT], " ("); + + if (nFailedUnits == INVALID_VALUE) { + buffer[0] = '?'; + buffer[1] = '\0'; + len = 1; + } else { + len = xSnprintf(buffer, sizeof(buffer), "%u", nFailedUnits); + } + RichString_appendnAscii(out, zeroDigitColor(nFailedUnits), buffer, len); + + RichString_appendAscii(out, CRT_colors[METER_TEXT], "/"); + + if (nNames == INVALID_VALUE) { + buffer[0] = '?'; + buffer[1] = '\0'; + len = 1; + } else { + len = xSnprintf(buffer, sizeof(buffer), "%u", nNames); + } + RichString_appendnAscii(out, valueDigitColor(nNames), buffer, len); + + RichString_appendAscii(out, CRT_colors[METER_TEXT], " failed) ("); + + if (nJobs == INVALID_VALUE) { + buffer[0] = '?'; + buffer[1] = '\0'; + len = 1; + } else { + len = xSnprintf(buffer, sizeof(buffer), "%u", nJobs); + } + RichString_appendnAscii(out, zeroDigitColor(nJobs), buffer, len); + + RichString_appendAscii(out, CRT_colors[METER_TEXT], "/"); + + if (nInstalledJobs == INVALID_VALUE) { + buffer[0] = '?'; + buffer[1] = '\0'; + len = 1; + } else { + len = xSnprintf(buffer, sizeof(buffer), "%u", nInstalledJobs); + } + RichString_appendnAscii(out, valueDigitColor(nInstalledJobs), buffer, len); + + RichString_appendAscii(out, CRT_colors[METER_TEXT], " jobs)"); +} + +static const int SystemdMeter_attributes[] = { + METER_VALUE +}; + +const MeterClass SystemdMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = SystemdMeter_display + }, + .updateValues = SystemdMeter_updateValues, + .done = SystemdMeter_done, + .defaultMode = TEXT_METERMODE, + .maxItems = 0, + .total = 100.0, + .attributes = SystemdMeter_attributes, + .name = "Systemd", + .uiName = "Systemd state", + .description = "Systemd system state and unit overview", + .caption = "Systemd: ", +}; diff --git a/linux/SystemdMeter.h b/linux/SystemdMeter.h new file mode 100644 index 0000000..a05e087 --- /dev/null +++ b/linux/SystemdMeter.h @@ -0,0 +1,16 @@ +#ifndef HEADER_SystemdMeter +#define HEADER_SystemdMeter + +/* +htop - SystemdMeter.h +(C) 2020 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Meter.h" + + +extern const MeterClass SystemdMeter_class; + +#endif /* HEADER_SystemdMeter */ diff --git a/linux/ZramMeter.c b/linux/ZramMeter.c new file mode 100644 index 0000000..e1e27b7 --- /dev/null +++ b/linux/ZramMeter.c @@ -0,0 +1,73 @@ +#include "linux/ZramMeter.h" + +#include <stddef.h> + +#include "CRT.h" +#include "Meter.h" +#include "Object.h" +#include "Platform.h" +#include "RichString.h" + + +static const int ZramMeter_attributes[] = { + ZRAM +}; + +static void ZramMeter_updateValues(Meter* this) { + char* buffer = this->txtBuffer; + size_t size = sizeof(this->txtBuffer); + int written; + + Platform_setZramValues(this); + + /* on print bar for compressed data size, not uncompressed */ + this->curItems = 1; + + written = Meter_humanUnit(buffer, this->values[0], size); + METER_BUFFER_CHECK(buffer, size, written); + + METER_BUFFER_APPEND_CHR(buffer, size, '('); + + written = Meter_humanUnit(buffer, this->values[1], size); + METER_BUFFER_CHECK(buffer, size, written); + + METER_BUFFER_APPEND_CHR(buffer, size, ')'); + + METER_BUFFER_APPEND_CHR(buffer, size, '/'); + + Meter_humanUnit(buffer, this->total, size); +} + +static void ZramMeter_display(const Object* cast, RichString* out) { + char buffer[50]; + const Meter* this = (const Meter*)cast; + + RichString_writeAscii(out, CRT_colors[METER_TEXT], ":"); + + Meter_humanUnit(buffer, this->total, sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); + + Meter_humanUnit(buffer, this->values[0], sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[METER_TEXT], " used:"); + RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); + + Meter_humanUnit(buffer, this->values[1], sizeof(buffer)); + RichString_appendAscii(out, CRT_colors[METER_TEXT], " uncompressed:"); + RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer); +} + +const MeterClass ZramMeter_class = { + .super = { + .extends = Class(Meter), + .delete = Meter_delete, + .display = ZramMeter_display, + }, + .updateValues = ZramMeter_updateValues, + .defaultMode = BAR_METERMODE, + .maxItems = 2, + .total = 100.0, + .attributes = ZramMeter_attributes, + .name = "Zram", + .uiName = "Zram", + .caption = "zrm" +}; diff --git a/linux/ZramMeter.h b/linux/ZramMeter.h new file mode 100644 index 0000000..ddba1ba --- /dev/null +++ b/linux/ZramMeter.h @@ -0,0 +1,9 @@ +#ifndef HEADER_ZramMeter +#define HEADER_ZramMeter + +#include "Meter.h" + + +extern const MeterClass ZramMeter_class; + +#endif diff --git a/linux/ZramStats.h b/linux/ZramStats.h new file mode 100644 index 0000000..67aadcc --- /dev/null +++ b/linux/ZramStats.h @@ -0,0 +1,10 @@ +#ifndef HEADER_ZramStats +#define HEADER_ZramStats + +typedef struct ZramStats_ { + memory_t totalZram; + memory_t usedZramComp; + memory_t usedZramOrig; +} ZramStats; + +#endif |