Linux: overhaul memory partition

Use similar calculation than procps.
Show AvailableMemory in text mode.
Use total minus available memory instead of manually computed used-
memory as fraction part in bar mode (if available).
This commit is contained in:
Christian Göttsche
2021-01-06 18:11:24 +01:00
parent 0d67263b36
commit 3d497a3760
7 changed files with 103 additions and 63 deletions

View File

@ -7,6 +7,8 @@ in the source distribution for its full text.
#include "MemoryMeter.h"
#include <math.h>
#include "CRT.h"
#include "Object.h"
#include "Platform.h"
@ -21,9 +23,15 @@ static const int MemoryMeter_attributes[] = {
static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) {
int written;
/* available memory is not supported on all platforms */
this->values[3] = NAN;
Platform_setMemoryValues(this);
written = Meter_humanUnit(buffer, this->values[0], size);
/* Do not print available memory in bar mode */
this->curItems = 3;
written = Meter_humanUnit(buffer, isnan(this->values[3]) ? this->values[0] : this->total - this->values[3], size);
METER_BUFFER_CHECK(buffer, size, written);
METER_BUFFER_APPEND_CHR(buffer, size, '/');
@ -34,18 +42,29 @@ static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void MemoryMeter_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[MEMORY_USED], buffer);
Meter_humanUnit(buffer, this->values[1], sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_TEXT], " buffers:");
RichString_appendAscii(out, CRT_colors[MEMORY_BUFFERS_TEXT], buffer);
Meter_humanUnit(buffer, this->values[2], sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_TEXT], " cache:");
RichString_appendAscii(out, CRT_colors[MEMORY_CACHE], buffer);
/* available memory is not supported on all platforms */
if (!isnan(this->values[3])) {
Meter_humanUnit(buffer, this->values[3], sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_TEXT], " available:");
RichString_appendAscii(out, CRT_colors[METER_VALUE], buffer);
}
}
const MeterClass MemoryMeter_class = {
@ -56,7 +75,7 @@ const MeterClass MemoryMeter_class = {
},
.updateValues = MemoryMeter_updateValues,
.defaultMode = BAR_METERMODE,
.maxItems = 3,
.maxItems = 4,
.total = 100.0,
.attributes = MemoryMeter_attributes,
.name = "Memory",