From e7372d18a1a661d8c3dba9f51e1f17b5f94171a7 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 10 Jan 2024 11:17:08 +0100 Subject: New upstream version 3.3.0 --- DiskIOMeter.c | 134 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 64 deletions(-) (limited to 'DiskIOMeter.c') diff --git a/DiskIOMeter.c b/DiskIOMeter.c index 12c87de..8d658de 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -5,18 +5,19 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "DiskIOMeter.h" #include -#include #include "CRT.h" +#include "Machine.h" #include "Macros.h" -#include "Meter.h" #include "Object.h" #include "Platform.h" -#include "ProcessList.h" #include "RichString.h" +#include "Row.h" #include "XUtils.h" @@ -27,25 +28,22 @@ static const int DiskIOMeter_attributes[] = { }; static MeterRateStatus status = RATESTATUS_INIT; -static uint32_t cached_read_diff; -static uint32_t cached_write_diff; +static char cached_read_diff_str[6]; +static char cached_write_diff_str[6]; static double cached_utilisation_diff; static void DiskIOMeter_updateValues(Meter* this) { - const ProcessList* pl = this->pl; + const Machine* host = this->host; static uint64_t cached_last_update; - uint64_t passedTimeInMs = pl->realtimeMs - cached_last_update; + uint64_t passedTimeInMs = host->realtimeMs - cached_last_update; + bool hasNewData = false; + DiskIOData data; /* update only every 500ms to have a sane span for rate calculation */ if (passedTimeInMs > 500) { - static uint64_t cached_read_total; - static uint64_t cached_write_total; - static uint64_t cached_msTimeSpend_total; - uint64_t diff; - - DiskIOData data; - if (!Platform_getDiskIO(&data)) { + hasNewData = Platform_getDiskIO(&data); + if (!hasNewData) { status = RATESTATUS_NODATA; } else if (cached_last_update == 0) { status = RATESTATUS_INIT; @@ -55,40 +53,55 @@ static void DiskIOMeter_updateValues(Meter* this) { status = RATESTATUS_DATA; } - cached_last_update = pl->realtimeMs; + cached_last_update = host->realtimeMs; + } - if (status == RATESTATUS_NODATA) { - xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data"); - return; - } + if (hasNewData) { + static uint64_t cached_read_total; + static uint64_t cached_write_total; + static uint64_t cached_msTimeSpend_total; - if (data.totalBytesRead > cached_read_total) { - diff = data.totalBytesRead - cached_read_total; - diff /= 1024; /* Meter_humanUnit() expects unit in kilo */ - cached_read_diff = (uint32_t)diff; - } else { - cached_read_diff = 0; + if (status != RATESTATUS_INIT) { + uint64_t diff; + + if (data.totalBytesRead > cached_read_total) { + diff = data.totalBytesRead - cached_read_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + } else { + diff = 0; + } + Meter_humanUnit(cached_read_diff_str, diff, sizeof(cached_read_diff_str)); + + if (data.totalBytesWritten > cached_write_total) { + diff = data.totalBytesWritten - cached_write_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + } else { + diff = 0; + } + Meter_humanUnit(cached_write_diff_str, diff, sizeof(cached_write_diff_str)); + + if (data.totalMsTimeSpend > cached_msTimeSpend_total) { + diff = data.totalMsTimeSpend - cached_msTimeSpend_total; + cached_utilisation_diff = 100.0 * (double)diff / passedTimeInMs; + cached_utilisation_diff = MINIMUM(cached_utilisation_diff, 100.0); + } else { + cached_utilisation_diff = 0.0; + } } - cached_read_total = data.totalBytesRead; - if (data.totalBytesWritten > cached_write_total) { - diff = data.totalBytesWritten - cached_write_total; - diff /= 1024; /* Meter_humanUnit() expects unit in kilo */ - cached_write_diff = (uint32_t)diff; - } else { - cached_write_diff = 0; - } + cached_read_total = data.totalBytesRead; cached_write_total = data.totalBytesWritten; - - if (data.totalMsTimeSpend > cached_msTimeSpend_total) { - diff = data.totalMsTimeSpend - cached_msTimeSpend_total; - cached_utilisation_diff = 100.0 * (double)diff / passedTimeInMs; - } else { - cached_utilisation_diff = 0.0; - } cached_msTimeSpend_total = data.totalMsTimeSpend; } + this->values[0] = cached_utilisation_diff; + + if (status == RATESTATUS_NODATA) { + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data"); + return; + } if (status == RATESTATUS_INIT) { xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "init"); return; @@ -98,44 +111,37 @@ static void DiskIOMeter_updateValues(Meter* this) { return; } - this->values[0] = cached_utilisation_diff; - this->total = MAXIMUM(this->values[0], 100.0); /* fix total after (initial) spike */ - - char bufferRead[12], bufferWrite[12]; - Meter_humanUnit(bufferRead, cached_read_diff, sizeof(bufferRead)); - Meter_humanUnit(bufferWrite, cached_write_diff, sizeof(bufferWrite)); - snprintf(this->txtBuffer, sizeof(this->txtBuffer), "%sB %sB %.1f%%", bufferRead, bufferWrite, cached_utilisation_diff); + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "r:%siB/s w:%siB/s %.1f%%", cached_read_diff_str, cached_write_diff_str, cached_utilisation_diff); } static void DiskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) { switch (status) { - case RATESTATUS_NODATA: - RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data"); - return; - case RATESTATUS_INIT: - RichString_writeAscii(out, CRT_colors[METER_VALUE], "initializing..."); - return; - case RATESTATUS_STALE: - RichString_writeAscii(out, CRT_colors[METER_VALUE_WARN], "stale data"); - return; - case RATESTATUS_DATA: - break; + case RATESTATUS_NODATA: + RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data"); + return; + case RATESTATUS_INIT: + RichString_writeAscii(out, CRT_colors[METER_VALUE], "initializing..."); + return; + case RATESTATUS_STALE: + RichString_writeAscii(out, CRT_colors[METER_VALUE_WARN], "stale data"); + return; + case RATESTATUS_DATA: + break; } char buffer[16]; - int len; int color = cached_utilisation_diff > 40.0 ? METER_VALUE_NOTICE : METER_VALUE; - len = xSnprintf(buffer, sizeof(buffer), "%.1f%%", cached_utilisation_diff); + int len = xSnprintf(buffer, sizeof(buffer), "%.1f%%", cached_utilisation_diff); RichString_appendnAscii(out, CRT_colors[color], buffer, len); RichString_appendAscii(out, CRT_colors[METER_TEXT], " read: "); - Meter_humanUnit(buffer, cached_read_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], cached_read_diff_str); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], "iB/s"); RichString_appendAscii(out, CRT_colors[METER_TEXT], " write: "); - Meter_humanUnit(buffer, cached_write_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], cached_write_diff_str); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s"); } const MeterClass DiskIOMeter_class = { -- cgit v1.2.3