Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void Machine_init(Machine* this, UsersTable* usersTable, uid_t userId) {

this->htopUserId = getuid();

this->containerized = false;

// discover fixed column width limits
Row_setPidColumnWidth(Platform_getMaxPid());

Expand Down
2 changes: 2 additions & 0 deletions Machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ typedef struct Machine_ {
unsigned int activeCPUs;
unsigned int existingCPUs;

bool containerized; /* whether htop is running inside a container (Linux only; always false elsewhere) */

UsersTable* usersTable;
uid_t htopUserId;
uid_t maxUserId; /* recently observed */
Expand Down
9 changes: 9 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ linux_platform_headers = \
generic/gettime.h \
generic/hostname.h \
generic/uname.h \
linux/CGroupMem.h \
linux/CGroupUtils.h \
linux/Compat.h \
linux/ContainerMeter.h \
linux/GPU.h \
linux/HugePageMeter.h \
linux/IOPriority.h \
Expand All @@ -216,8 +218,10 @@ linux_platform_sources = \
generic/gettime.c \
generic/hostname.c \
generic/uname.c \
linux/CGroupMem.c \
linux/CGroupUtils.c \
linux/Compat.c \
linux/ContainerMeter.c \
linux/GPU.c \
linux/HugePageMeter.c \
linux/IOPriorityPanel.c \
Expand Down Expand Up @@ -527,6 +531,11 @@ coverage:
cppcheck:
cppcheck -q -v . --enable=all -DHAVE_OPENVZ

check_PROGRAMS = linux/CGroupMemTest
TESTS = $(check_PROGRAMS)
linux_CGroupMemTest_SOURCES = linux/CGroupMemTest.c linux/CGroupMem.c linux/CGroupMem.h XUtils.c linux/Compat.c
linux_CGroupMemTest_CPPFLAGS = $(AM_CPPFLAGS) -D_DEFAULT_SOURCE
Comment on lines +534 to +537

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Guard the new unit test behind HTOP_LINUX.

check_PROGRAMS and TESTS are unconditional here, so make check on non-Linux targets will still try to build linux/CGroupMemTest and link Linux-only sources. Wrap this block in if HTOP_LINUX ... endif so the new test only participates in Linux builds.

Suggested fix
+if HTOP_LINUX
 check_PROGRAMS = linux/CGroupMemTest
 TESTS = $(check_PROGRAMS)
 linux_CGroupMemTest_SOURCES = linux/CGroupMemTest.c linux/CGroupMem.c linux/CGroupMem.h XUtils.c linux/Compat.c
 linux_CGroupMemTest_CPPFLAGS = $(AM_CPPFLAGS) -D_DEFAULT_SOURCE
+endif

Based on the PR objective, this test target is Linux-specific.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
check_PROGRAMS = linux/CGroupMemTest
TESTS = $(check_PROGRAMS)
linux_CGroupMemTest_SOURCES = linux/CGroupMemTest.c linux/CGroupMem.c linux/CGroupMem.h XUtils.c linux/Compat.c
linux_CGroupMemTest_CPPFLAGS = $(AM_CPPFLAGS) -D_DEFAULT_SOURCE
if HTOP_LINUX
check_PROGRAMS = linux/CGroupMemTest
TESTS = $(check_PROGRAMS)
linux_CGroupMemTest_SOURCES = linux/CGroupMemTest.c linux/CGroupMem.c linux/CGroupMem.h XUtils.c linux/Compat.c
linux_CGroupMemTest_CPPFLAGS = $(AM_CPPFLAGS) -D_DEFAULT_SOURCE
endif


Comment on lines +534 to +538

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No unit tests …

dist-hook: $(top_distdir)/configure
@if test "x$$FORCE_MAKE_DIST" != x || \
grep 'pkg_m4_included' '$(top_distdir)/configure' >/dev/null; then :; \
Expand Down
12 changes: 8 additions & 4 deletions MemoryMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ in the source distribution for its full text.
#include "RichString.h"


static const int MemoryMeter_attributes[] = {
const int MemoryMeter_attributes[] = {
MEMORY_1,
MEMORY_2,
MEMORY_3,
Expand All @@ -30,7 +30,7 @@ static const int MemoryMeter_attributes[] = {
MEMORY_6
};

static void MemoryMeter_updateValues(Meter* this) {
void MemoryMeter_updateValuesWith(Meter* this, void (*setValues)(Meter*)) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;
Expand All @@ -42,7 +42,7 @@ static void MemoryMeter_updateValues(Meter* this) {
this->values[memoryClassIdx] = NAN;
}

Platform_setMemoryValues(this);
setValues(this);
this->curItems = (uint8_t) Platform_numberOfMemoryClasses;

/* compute the used memory */
Expand Down Expand Up @@ -70,7 +70,11 @@ static void MemoryMeter_updateValues(Meter* this) {
Meter_humanUnit(buffer, this->total, size);
}

static void MemoryMeter_display(const Object* cast, RichString* out) {
static void MemoryMeter_updateValues(Meter* this) {
MemoryMeter_updateValuesWith(this, Platform_setMemoryValues);
}

void MemoryMeter_display(const Object* cast, RichString* out) {
char buffer[50];
const Meter* this = (const Meter*)cast;
const Settings* settings = this->host->settings;
Expand Down
9 changes: 9 additions & 0 deletions MemoryMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ in the source distribution for its full text.
*/

#include "Meter.h"
#include "Object.h"
#include "RichString.h"


typedef struct MemoryClass_s {
const char *label; // e.g. "wired", "shared", "compressed" - platform-specific memory classe names
Expand All @@ -19,4 +22,10 @@ typedef struct MemoryClass_s {

extern const MeterClass MemoryMeter_class;

extern const int MemoryMeter_attributes[];

void MemoryMeter_display(const Object* cast, RichString* out);

void MemoryMeter_updateValuesWith(Meter* this, void (*setValues)(Meter*));

#endif
9 changes: 7 additions & 2 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ static void Settings_defaultMeters(Settings* this, const Machine* host) {
this->hColumns[0].names[0] = xStrdup("AllCPUs");
this->hColumns[0].modes[0] = BAR_METERMODE;
}
this->hColumns[0].names[1] = xStrdup("Memory");
// When containerized, default to the cgroup-aware meters so the limits
// imposed on the container are visible. The "Container*" meters fall back
// to the host figures when no cgroup limit is in effect.
// host->containerized is always false off Linux, so the literal meter
// names below are only ever used on a platform that registers them.
this->hColumns[0].names[1] = xStrdup(host->containerized ? "ContainerMemory" : "Memory");
this->hColumns[0].modes[1] = BAR_METERMODE;
this->hColumns[0].names[2] = xStrdup("Swap");
this->hColumns[0].names[2] = xStrdup(host->containerized ? "ContainerSwap" : "Swap");
this->hColumns[0].modes[2] = BAR_METERMODE;
this->hColumns[1].names[r] = xStrdup("Tasks");
this->hColumns[1].modes[r++] = TEXT_METERMODE;
Expand Down
12 changes: 8 additions & 4 deletions SwapMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ in the source distribution for its full text.
#include "RichString.h"


static const int SwapMeter_attributes[] = {
const int SwapMeter_attributes[] = {
SWAP,
SWAP_CACHE,
SWAP_FRONTSWAP,
};

static void SwapMeter_updateValues(Meter* this) {
void SwapMeter_updateValuesWith(Meter* this, void (*setValues)(Meter*)) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;

this->values[SWAP_METER_CACHE] = NAN; /* 'cached' not present on all platforms */
this->values[SWAP_METER_FRONTSWAP] = NAN; /* 'frontswap' not present on all platforms */
Platform_setSwapValues(this);
setValues(this);

written = Meter_humanUnit(buffer, this->values[SWAP_METER_USED], size);
METER_BUFFER_CHECK(buffer, size, written);
Expand All @@ -42,7 +42,11 @@ static void SwapMeter_updateValues(Meter* this) {
Meter_humanUnit(buffer, this->total, size);
}

static void SwapMeter_display(const Object* cast, RichString* out) {
static void SwapMeter_updateValues(Meter* this) {
SwapMeter_updateValuesWith(this, Platform_setSwapValues);
}

void SwapMeter_display(const Object* cast, RichString* out) {
char buffer[50];
const Meter* this = (const Meter*)cast;
RichString_writeAscii(out, CRT_colors[METER_TEXT], ":");
Expand Down
9 changes: 9 additions & 0 deletions SwapMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ in the source distribution for its full text.
*/

#include "Meter.h"
#include "Object.h"
#include "RichString.h"


typedef enum {
SWAP_METER_USED = 0,
Expand All @@ -18,4 +21,10 @@ typedef enum {

extern const MeterClass SwapMeter_class;

extern const int SwapMeter_attributes[];

void SwapMeter_display(const Object* cast, RichString* out);

void SwapMeter_updateValuesWith(Meter* this, void (*setValues)(Meter*));

#endif
Loading