diff options
author | Christian Göttsche <cgzones@googlemail.com> | 2021-01-06 18:11:24 +0100 |
---|---|---|
committer | Christian Göttsche <cgzones@googlemail.com> | 2021-02-07 12:41:52 +0100 |
commit | 3d497a3760626d398fffc8f4594e8b9658f67d55 (patch) | |
tree | 31f5f760e5039d220606461929ac48f365aff269 /MemoryMeter.c | |
parent | 0d67263b36f0f1b45a2f10ec80c15766ca218ce5 (diff) |
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).
Diffstat (limited to 'MemoryMeter.c')
-rw-r--r-- | MemoryMeter.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/MemoryMeter.c b/MemoryMeter.c index e4754427..dd219bc0 100644 --- a/MemoryMeter.c +++ b/MemoryMeter.c @@ -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", |