Skip to content

Commit 0a27fef

Browse files
committed
Refactorings and implement PCP and Solaris memory meter support
Extensions to the changes Pierre-Marie made to generalise the memory meter for different platforms: - allow memory meter colors to be reflected in the different color schemes - use a proper color setting for Linux available memory instead of "text" - use sysname metric in PCP to drive correct memory selection and display - add missing PCP metrics for several missing Darwin memory categories - provide Solaris support for setting its own memory categories (noticed previous "buffers" and "cached" on Solaris to be a bit of a guess, and incorrectly ignoring "free" memory) - remove comments relating to platform-specific swap metrics on platforms that do not support those metrics.
1 parent 516e2ce commit 0a27fef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+460
-308
lines changed

Action.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ in the source distribution for its full text.
2929
#include "MainPanel.h"
3030
#include "MemoryMeter.h"
3131
#include "OpenFilesScreen.h"
32+
#include "Platform.h"
3233
#include "Process.h"
3334
#include "ProcessLocksScreen.h"
3435
#include "ProvideCurses.h"

CRT.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
178178
[SWAP_FRONTSWAP] = A_BOLD | ColorPairGrayBlack,
179179
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
180180
[GRAPH_2] = ColorPair(Cyan, Black),
181-
[MEMORY_USED] = ColorPair(Green, Black),
182-
[MEMORY_BUFFERS] = A_BOLD | ColorPair(Blue, Black),
183-
[MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue, Black),
184-
[MEMORY_CACHE] = ColorPair(Yellow, Black),
185-
[MEMORY_SHARED] = ColorPair(Magenta, Black),
186-
[MEMORY_COMPRESSED] = A_BOLD | ColorPairGrayBlack,
181+
[MEMORY_1] = ColorPair(Green, Black),
182+
[MEMORY_2] = A_BOLD | ColorPair(Blue, Black),
183+
[MEMORY_3] = ColorPair(Yellow, Black),
184+
[MEMORY_4] = ColorPair(Magenta, Black),
185+
[MEMORY_5] = A_BOLD | ColorPairGrayBlack,
186+
[MEMORY_6] = ColorPair(Red, Black),
187187
[HUGEPAGE_1] = ColorPair(Green, Black),
188188
[HUGEPAGE_2] = ColorPair(Yellow, Black),
189189
[HUGEPAGE_3] = ColorPair(Red, Black),
@@ -296,12 +296,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
296296
[SWAP_FRONTSWAP] = A_DIM,
297297
[GRAPH_1] = A_BOLD,
298298
[GRAPH_2] = A_NORMAL,
299-
[MEMORY_USED] = A_BOLD,
300-
[MEMORY_BUFFERS] = A_NORMAL,
301-
[MEMORY_BUFFERS_TEXT] = A_NORMAL,
302-
[MEMORY_CACHE] = A_NORMAL,
303-
[MEMORY_SHARED] = A_NORMAL,
304-
[MEMORY_COMPRESSED] = A_DIM,
299+
[MEMORY_1] = A_BOLD,
300+
[MEMORY_2] = A_NORMAL,
301+
[MEMORY_3] = A_NORMAL,
302+
[MEMORY_4] = A_NORMAL,
303+
[MEMORY_5] = A_DIM,
304+
[MEMORY_6] = A_NORMAL,
305305
[HUGEPAGE_1] = A_BOLD,
306306
[HUGEPAGE_2] = A_NORMAL,
307307
[HUGEPAGE_3] = A_REVERSE | A_BOLD,
@@ -414,12 +414,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
414414
[SWAP_FRONTSWAP] = A_BOLD | ColorPair(Black, White),
415415
[GRAPH_1] = A_BOLD | ColorPair(Blue, White),
416416
[GRAPH_2] = ColorPair(Blue, White),
417-
[MEMORY_USED] = ColorPair(Green, White),
418-
[MEMORY_BUFFERS] = ColorPair(Cyan, White),
419-
[MEMORY_BUFFERS_TEXT] = ColorPair(Cyan, White),
420-
[MEMORY_CACHE] = ColorPair(Yellow, White),
421-
[MEMORY_SHARED] = ColorPair(Magenta, White),
422-
[MEMORY_COMPRESSED] = A_BOLD | ColorPair(Black, White),
417+
[MEMORY_1] = ColorPair(Green, White),
418+
[MEMORY_2] = ColorPair(Cyan, White),
419+
[MEMORY_3] = ColorPair(Yellow, White),
420+
[MEMORY_4] = ColorPair(Magenta, White),
421+
[MEMORY_5] = A_BOLD | ColorPair(Black, White),
422+
[MEMORY_6] = ColorPair(Red, White),
423423
[HUGEPAGE_1] = ColorPair(Green, White),
424424
[HUGEPAGE_2] = ColorPair(Yellow, White),
425425
[HUGEPAGE_3] = ColorPair(Red, White),
@@ -532,12 +532,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
532532
[SWAP_FRONTSWAP] = ColorPairGrayBlack,
533533
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Black),
534534
[GRAPH_2] = ColorPair(Cyan, Black),
535-
[MEMORY_USED] = ColorPair(Green, Black),
536-
[MEMORY_BUFFERS] = ColorPair(Cyan, Black),
537-
[MEMORY_BUFFERS_TEXT] = ColorPair(Cyan, Black),
538-
[MEMORY_CACHE] = ColorPair(Yellow, Black),
539-
[MEMORY_SHARED] = ColorPair(Magenta, Black),
540-
[MEMORY_COMPRESSED] = ColorPairGrayBlack,
535+
[MEMORY_1] = ColorPair(Green, Black),
536+
[MEMORY_2] = ColorPair(Cyan, Black),
537+
[MEMORY_3] = ColorPair(Yellow, Black),
538+
[MEMORY_4] = ColorPair(Magenta, Black),
539+
[MEMORY_5] = ColorPairGrayBlack,
540+
[MEMORY_6] = ColorPair(Red, Black),
541541
[HUGEPAGE_1] = ColorPair(Green, Black),
542542
[HUGEPAGE_2] = ColorPair(Yellow, Black),
543543
[HUGEPAGE_3] = ColorPair(Red, Black),
@@ -650,12 +650,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
650650
[SWAP_FRONTSWAP] = A_BOLD | ColorPair(Black, Blue),
651651
[GRAPH_1] = A_BOLD | ColorPair(Cyan, Blue),
652652
[GRAPH_2] = ColorPair(Cyan, Blue),
653-
[MEMORY_USED] = A_BOLD | ColorPair(Green, Blue),
654-
[MEMORY_BUFFERS] = A_BOLD | ColorPair(Cyan, Blue),
655-
[MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Cyan, Blue),
656-
[MEMORY_CACHE] = A_BOLD | ColorPair(Yellow, Blue),
657-
[MEMORY_SHARED] = A_BOLD | ColorPair(Magenta, Blue),
658-
[MEMORY_COMPRESSED] = A_BOLD | ColorPair(Black, Blue),
653+
[MEMORY_1] = A_BOLD | ColorPair(Green, Blue),
654+
[MEMORY_2] = A_BOLD | ColorPair(Cyan, Blue),
655+
[MEMORY_3] = A_BOLD | ColorPair(Yellow, Blue),
656+
[MEMORY_4] = A_BOLD | ColorPair(Magenta, Blue),
657+
[MEMORY_5] = A_BOLD | ColorPair(Black, Blue),
658+
[MEMORY_6] = A_BOLD | ColorPair(Red, Blue),
659659
[HUGEPAGE_1] = A_BOLD | ColorPair(Green, Blue),
660660
[HUGEPAGE_2] = A_BOLD | ColorPair(Yellow, Blue),
661661
[HUGEPAGE_3] = A_BOLD | ColorPair(Red, Blue),
@@ -768,12 +768,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
768768
[SWAP_FRONTSWAP] = ColorPair(Yellow, Black),
769769
[GRAPH_1] = A_BOLD | ColorPair(Green, Black),
770770
[GRAPH_2] = ColorPair(Green, Black),
771-
[MEMORY_USED] = ColorPair(Green, Black),
772-
[MEMORY_BUFFERS] = ColorPair(Blue, Black),
773-
[MEMORY_BUFFERS_TEXT] = A_BOLD | ColorPair(Blue, Black),
774-
[MEMORY_CACHE] = ColorPair(Yellow, Black),
775-
[MEMORY_SHARED] = ColorPair(Magenta, Black),
776-
[MEMORY_COMPRESSED] = ColorPair(Yellow, Black),
771+
[MEMORY_1] = ColorPair(Green, Black),
772+
[MEMORY_2] = ColorPair(Blue, Black),
773+
[MEMORY_3] = ColorPair(Yellow, Black),
774+
[MEMORY_4] = ColorPair(Magenta, Black),
775+
[MEMORY_5] = ColorPair(Yellow, Black),
776+
[MEMORY_6] = ColorPair(Red, Black),
777777
[HUGEPAGE_1] = ColorPair(Green, Black),
778778
[HUGEPAGE_2] = ColorPair(Yellow, Black),
779779
[HUGEPAGE_3] = ColorPair(Red, Black),
@@ -881,12 +881,12 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
881881
[SWAP_FRONTSWAP] = A_BOLD | ColorPairGrayBlack,
882882
[GRAPH_1] = A_BOLD,
883883
[GRAPH_2] = A_NORMAL,
884-
[MEMORY_USED] = A_BOLD | ColorPair(Yellow, Black),
885-
[MEMORY_BUFFERS] = A_NORMAL,
886-
[MEMORY_BUFFERS_TEXT] = A_NORMAL,
887-
[MEMORY_CACHE] = A_NORMAL,
888-
[MEMORY_SHARED] = A_NORMAL,
889-
[MEMORY_COMPRESSED] = A_BOLD | ColorPairGrayBlack,
884+
[MEMORY_1] = A_BOLD | ColorPair(Yellow, Black),
885+
[MEMORY_2] = A_NORMAL,
886+
[MEMORY_3] = A_NORMAL,
887+
[MEMORY_4] = A_NORMAL,
888+
[MEMORY_5] = A_BOLD | ColorPairGrayBlack,
889+
[MEMORY_6] = A_NORMAL,
890890
[HUGEPAGE_1] = A_BOLD,
891891
[HUGEPAGE_2] = A_NORMAL,
892892
[HUGEPAGE_3] = A_BOLD | ColorPair(Cyan, Black),

CRT.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ typedef enum ColorElements_ {
9393
BAR_SHADOW,
9494
GRAPH_1,
9595
GRAPH_2,
96-
MEMORY_USED,
97-
MEMORY_BUFFERS,
98-
MEMORY_BUFFERS_TEXT,
99-
MEMORY_CACHE,
100-
MEMORY_SHARED,
101-
MEMORY_COMPRESSED,
96+
MEMORY_1,
97+
MEMORY_2,
98+
MEMORY_3,
99+
MEMORY_4,
100+
MEMORY_5,
101+
MEMORY_6,
102102
HUGEPAGE_1,
103103
HUGEPAGE_2,
104104
HUGEPAGE_3,
@@ -161,8 +161,6 @@ typedef enum ColorElements_ {
161161
LAST_COLORELEMENT
162162
} ColorElements;
163163

164-
#define NUMBER_OF_DYNAMIC_COLORS 9 // number of DYNAMIC_<ColorName> entries in ColorElements
165-
166164
void CRT_fatalError(const char* note) ATTR_NORETURN;
167165

168166
#ifdef NDEBUG

Machine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct Machine_ {
5050
bool topologyOk;
5151
#endif
5252

53-
/* NOTE: memory details were moved to the OS-specific machine subclass */
53+
memory_t totalMem;
5454

5555
memory_t totalSwap;
5656
memory_t usedSwap;

MemoryMeter.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
htop - MemoryMeter.c
33
(C) 2004-2011 Hisham H. Muhammad
4+
(C) 2025 htop dev team
45
Released under the GNU GPLv2+, see the COPYING file
56
in the source distribution for its full text.
67
*/
@@ -20,8 +21,14 @@ in the source distribution for its full text.
2021
#include "RichString.h"
2122

2223

23-
extern const int Platform_memoryMeter_attributes[]; // OS-specific
24-
24+
static const int MemoryMeter_attributes[] = {
25+
MEMORY_1,
26+
MEMORY_2,
27+
MEMORY_3,
28+
MEMORY_4,
29+
MEMORY_5,
30+
MEMORY_6
31+
};
2532

2633
static void MemoryMeter_updateValues(Meter* this) {
2734
char* buffer = this->txtBuffer;
@@ -72,7 +79,7 @@ static void MemoryMeter_display(const Object* cast, RichString* out) {
7279
Meter_humanUnit(buffer, this->total, sizeof(buffer));
7380
RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
7481

75-
/* print the OS-specific memory classes in the order supplied by their implementation */
82+
/* print the memory classes in the order supplied (specific to each platform) */
7683
for (unsigned int memoryClassIdx = 0; memoryClassIdx < Platform_numberOfMemoryClasses; memoryClassIdx++) {
7784
if (!settings->showCachedMemory && Platform_memoryClasses[memoryClassIdx].countsAsCache)
7885
continue; // skip reclaimable cache memory classes if "show cached memory" is not ticked
@@ -94,10 +101,10 @@ const MeterClass MemoryMeter_class = {
94101
.updateValues = MemoryMeter_updateValues,
95102
.defaultMode = BAR_METERMODE,
96103
.supportedModes = METERMODE_DEFAULT_SUPPORTED,
97-
.maxItems = NUMBER_OF_DYNAMIC_COLORS, // all the range of DYNAMIC_xxxx colors are allowed
104+
.maxItems = 6, // maximum of MEMORY_N settings
98105
.isPercentChart = true,
99106
.total = 100.0,
100-
.attributes = Platform_memoryMeter_attributes, // OS-specific
107+
.attributes = MemoryMeter_attributes,
101108
.name = "Memory",
102109
.uiName = "Memory",
103110
.caption = "Mem"

MemoryMeter.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
/*
44
htop - MemoryMeter.h
55
(C) 2004-2011 Hisham H. Muhammad
6+
(C) 2025 htop dev team
67
Released under the GNU GPLv2+, see the COPYING file
78
in the source distribution for its full text.
89
*/
910

1011
#include "Meter.h"
1112

1213
typedef struct MemoryClass_s {
13-
const char *label; // e.g. "used", "shared", "compressed", etc. Should reflect the system-specific 'top' classes
14-
bool countsAsUsed; // whether this memory class counts as "used" memory
15-
bool countsAsCache; // whether this memory class can be reclaimed under pressure (and displayed when "show cached memory" is checked)
16-
ColorElements color; // one of the DYNAMIC_xxx CRT color values
14+
const char *label; // e.g. "wired", "shared", "compressed" - platform-specific memory classe names
15+
bool countsAsUsed; // memory class counts as "used" memory
16+
bool countsAsCache; // memory class reclaimed under pressure (displayed with "show cached memory")
17+
ColorElements color; // one of the MEMORY CRT color values
1718
} MemoryClass;
1819

19-
extern const MemoryClass Platform_memoryClasses[]; // defined in the platform-specific code
20-
extern const unsigned int Platform_numberOfMemoryClasses; // defined in the platform-specific code
21-
2220
extern const MeterClass MemoryMeter_class;
2321

2422
#endif

darwin/Platform.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,25 @@ const SignalItem Platform_signals[] = {
114114

115115
const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
116116

117-
const MemoryClass Platform_memoryClasses[] = {
118-
#define MEMORY_CLASS_WIRED 0
119-
{ .label = "wired", .countsAsUsed = true, .countsAsCache = false, .color = DYNAMIC_RED }, // pages wired down to physical memory (kernel)
120-
#define MEMORY_CLASS_SPECULATIVE 1
121-
{ .label = "speculative", .countsAsUsed = true, .countsAsCache = true, .color = DYNAMIC_MAGENTA }, // readahead optimization caches
122-
#define MEMORY_CLASS_ACTIVE 2
123-
{ .label = "active", .countsAsUsed = true, .countsAsCache = false, .color = DYNAMIC_GREEN }, // userland pages actively being used
124-
#define MEMORY_CLASS_PURGEABLE 3
125-
{ .label = "purgeable", .countsAsUsed = false, .countsAsCache = true, .color = DYNAMIC_YELLOW }, // userland pages voluntarily marked "discardable" by apps
126-
#define MEMORY_CLASS_COMPRESSED 4
127-
{ .label = "compressed", .countsAsUsed = true, .countsAsCache = false, .color = DYNAMIC_BLUE }, // userland pages being compressed (means memory pressure++)
128-
#define MEMORY_CLASS_INACTIVE 5
129-
{ .label = "inactive", .countsAsUsed = true, .countsAsCache = true, .color = DYNAMIC_GRAY }, // pages no longer used; macOS counts them as "used" anyway...
117+
enum {
118+
MEMORY_CLASS_WIRED = 0,
119+
MEMORY_CLASS_SPECULATIVE,
120+
MEMORY_CLASS_ACTIVE,
121+
MEMORY_CLASS_PURGEABLE,
122+
MEMORY_CLASS_COMPRESSED,
123+
MEMORY_CLASS_INACTIVE,
130124
}; // N.B. the chart will display categories in this order
131125

132-
const unsigned int Platform_numberOfMemoryClasses = ARRAYSIZE(Platform_memoryClasses);
133-
134-
const int Platform_memoryMeter_attributes[] = {
135-
Platform_memoryClasses[0].color,
136-
Platform_memoryClasses[1].color,
137-
Platform_memoryClasses[2].color,
138-
Platform_memoryClasses[3].color,
139-
Platform_memoryClasses[4].color,
140-
Platform_memoryClasses[5].color
141-
}; // there MUST be as many entries in this attributes array as memory classes
126+
const MemoryClass Platform_memoryClasses[] = {
127+
[MEMORY_CLASS_WIRED] = { .label = "wired", .countsAsUsed = true, .countsAsCache = false, .color = MEMORY_1 }, // pages wired down to physical memory (kernel)
128+
[MEMORY_CLASS_SPECULATIVE] = { .label = "speculative", .countsAsUsed = true, .countsAsCache = true, .color = MEMORY_2 }, // readahead optimization caches
129+
[MEMORY_CLASS_ACTIVE] = { .label = "active", .countsAsUsed = true, .countsAsCache = false, .color = MEMORY_3 }, // userland pages actively being used
130+
[MEMORY_CLASS_PURGEABLE] = { .label = "purgeable", .countsAsUsed = false, .countsAsCache = true, .color = MEMORY_4 }, // userland pages voluntarily marked "discardable" by apps
131+
[MEMORY_CLASS_COMPRESSED] = { .label = "compressed", .countsAsUsed = true, .countsAsCache = false, .color = MEMORY_5 }, // userland pages being compressed (means memory pressure++)
132+
[MEMORY_CLASS_INACTIVE] = { .label = "inactive", .countsAsUsed = true, .countsAsCache = true, .color = MEMORY_6 }, // pages no longer used; macOS counts them as "used" anyway...
133+
};
142134

135+
const unsigned int Platform_numberOfMemoryClasses = ARRAYSIZE(Platform_memoryClasses);
143136

144137
const MeterClass* const Platform_meterTypes[] = {
145138
&CPUMeter_class,
@@ -444,8 +437,6 @@ void Platform_setSwapValues(Meter* mtr) {
444437

445438
mtr->total = swapused.xsu_total / 1024;
446439
mtr->values[SWAP_METER_USED] = swapused.xsu_used / 1024;
447-
// mtr->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux"
448-
// mtr->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
449440
}
450441

451442
void Platform_setZfsArcValues(Meter* this) {

darwin/Platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ in the source distribution for its full text.
1616
#include "CPUMeter.h"
1717
#include "DiskIOMeter.h"
1818
#include "Hashtable.h"
19+
#include "MemoryMeter.h"
1920
#include "NetworkIOMeter.h"
2021
#include "ProcessLocksScreen.h"
2122
#include "SignalsPanel.h"
@@ -34,6 +35,10 @@ extern const SignalItem Platform_signals[];
3435

3536
extern const unsigned int Platform_numberOfSignals;
3637

38+
extern const MemoryClass Platform_memoryClasses[];
39+
40+
extern const unsigned int Platform_numberOfMemoryClasses;
41+
3742
extern const MeterClass* const Platform_meterTypes[];
3843

3944
bool Platform_init(void);

dragonflybsd/DragonFlyBSDMachine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ typedef struct DragonFlyBSDMachine_ {
4242
int pageSizeKb;
4343
int kernelFScale;
4444

45-
memory_t totalMem;
4645
memory_t wiredMem;
4746
memory_t buffersMem;
4847
memory_t activeMem;

dragonflybsd/DragonFlyBSDProcessTable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void ProcessTable_goThroughEntries(ProcessTable* super) {
217217
proc->time = (kproc->kp_lwp.kl_uticks + kproc->kp_lwp.kl_sticks + kproc->kp_lwp.kl_iticks) / 10000;
218218

219219
proc->percent_cpu = 100.0 * ((double)kproc->kp_lwp.kl_pctcpu / (double)dhost->kernelFScale);
220-
proc->percent_mem = 100.0 * proc->m_resident / (double)(dhost->totalMem);
220+
proc->percent_mem = 100.0 * proc->m_resident / (double)(host->totalMem);
221221
Process_updateCPUFieldWidths(proc->percent_cpu);
222222

223223
if (proc->percent_cpu > 0.1) {

0 commit comments

Comments
 (0)