Skip to content

Commit

Permalink
Introduce "single value" model for monochrome graph
Browse files Browse the repository at this point in the history
The text labels for CPU, memory and swap go to the trouble of figuring
out a subset of the values to sum, for a single-value display.

But the monochrome graph simply summed all `curItems` values. This is
less sophisticated than the label behaviour.

Add a `Meter::single_value` member that can be used to output a meter's
single-value sum, and make the graph display use it instead of its own
"all-bars" sum where available.

Implement this in `MemoryMeter`, `SwapMeter` and `CPUMeter`.

This has the effect of making a memory graph not show cached memory, so
will not be permanently at the top when the cache is heavily used, and
the CPU graph will obey the `account_guest_in_cpu_meter` setting. In
both cases, the graph becomes consistent with the text label of the bar.
  • Loading branch information
kjbracey2 committed Jul 15, 2023
1 parent 5558c01 commit c6539b2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ static void CPUMeter_updateValues(Meter* this) {
double percent = Platform_setCPUValues(this, cpu);
if (isnan(percent)) {
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "offline");
this->single_value = 0;
return;
}

this->single_value = percent;

char cpuUsageBuffer[8] = { 0 };
char cpuFrequencyBuffer[16] = { 0 };
char cpuTemperatureBuffer[16] = { 0 };
Expand Down
6 changes: 3 additions & 3 deletions MemoryMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ static void MemoryMeter_updateValues(Meter* this) {
this->curItems = MEMORY_METER_AVAILABLE;

/* we actually want to show "used + compressed" */
double used = this->values[MEMORY_METER_USED];
this->single_value = this->values[MEMORY_METER_USED];
if (!isnan(this->values[MEMORY_METER_COMPRESSED])) {
used += this->values[MEMORY_METER_COMPRESSED];
this->single_value += this->values[MEMORY_METER_COMPRESSED];
}

written = Meter_humanUnit(buffer, used, size);
written = Meter_humanUnit(buffer, this->single_value, size);
METER_BUFFER_CHECK(buffer, size, written);

METER_BUFFER_APPEND_CHR(buffer, size, '/');
Expand Down
5 changes: 4 additions & 1 deletion Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Meter* Meter_new(const Machine* host, unsigned int param, const MeterClass* type
this->curItems = type->maxItems;
this->curAttributes = NULL;
this->values = type->maxItems ? xCalloc(type->maxItems, sizeof(double)) : NULL;
this->single_value = NAN;
this->total = type->total;
this->caption = xStrdup(type->caption);
if (Meter_initFn(this)) {
Expand Down Expand Up @@ -330,7 +331,9 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
for (int i = 0; i < nValues - 1; i++)
data->values[i] = data->values[i + 1];

if (Meter_comprisedValues(this)) {
if (!isnan(this->single_value)) {
data->values[nValues - 1] = this->single_value;
else if (Meter_comprisedValues(this)) {
data->values[nValues - 1] = (this->curItems > 0) ? this->values[this->curItems - 1] : 0.0;
} else {
double value = 0.0;
Expand Down
1 change: 1 addition & 0 deletions Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct Meter_ {
const int* curAttributes;
char txtBuffer[METER_TXTBUFFER_LEN];
double* values;
double single_value;
double total;
void* meterData;
};
Expand Down
4 changes: 3 additions & 1 deletion SwapMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ static void SwapMeter_updateValues(Meter* this) {
this->values[SWAP_METER_FRONTSWAP] = NAN; /* 'frontswap' not present on all platforms */
Platform_setSwapValues(this);

written = Meter_humanUnit(buffer, this->values[SWAP_METER_USED], size);
this->single_value = this->values[SWAP_METER_USED];

written = Meter_humanUnit(buffer, this->single_value, size);
METER_BUFFER_CHECK(buffer, size, written);

METER_BUFFER_APPEND_CHR(buffer, size, '/');
Expand Down

0 comments on commit c6539b2

Please sign in to comment.