Skip to content

Commit

Permalink
NetworkIOMeter: Cache the formatted "KiB/s" number strings
Browse files Browse the repository at this point in the history
In NetworkIOMeter, the number strings of bytes per second are formatted
with Meter_humanUnit(). As the numbers are only updated every 500 ms,
it is good to cache the formatted strings. This saves code size.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Nov 5, 2023
1 parent 3c5025e commit 3b99732
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions NetworkIOMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ static const int NetworkIOMeter_attributes[] = {

static MeterRateStatus status = RATESTATUS_INIT;
static uint32_t cached_rxb_diff;
static char cached_rxb_diff_str[6];
static uint32_t cached_rxp_diff;
static uint32_t cached_txb_diff;
static char cached_txb_diff_str[6];
static uint32_t cached_txp_diff;

static void NetworkIOMeter_updateValues(Meter* this) {
Expand Down Expand Up @@ -73,6 +75,7 @@ static void NetworkIOMeter_updateValues(Meter* this) {
} else {
cached_rxb_diff = 0;
}
Meter_humanUnit(cached_rxb_diff_str, cached_rxb_diff, sizeof(cached_rxb_diff_str));

if (data.packetsReceived > cached_rxp_total) {
diff = data.packetsReceived - cached_rxp_total;
Expand All @@ -90,6 +93,7 @@ static void NetworkIOMeter_updateValues(Meter* this) {
} else {
cached_txb_diff = 0;
}
Meter_humanUnit(cached_txb_diff_str, cached_txb_diff, sizeof(cached_txb_diff_str));

if (data.packetsTransmitted > cached_txp_total) {
diff = data.packetsTransmitted - cached_txp_total;
Expand Down Expand Up @@ -125,11 +129,8 @@ static void NetworkIOMeter_updateValues(Meter* this) {
return;
}

char bufferBytesReceived[12], bufferBytesTransmitted[12];
Meter_humanUnit(bufferBytesReceived, cached_rxb_diff, sizeof(bufferBytesReceived));
Meter_humanUnit(bufferBytesTransmitted, cached_txb_diff, sizeof(bufferBytesTransmitted));
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s %u/%upkts/s",
bufferBytesReceived, bufferBytesTransmitted, cached_rxp_diff, cached_txp_diff);
cached_rxb_diff_str, cached_txb_diff_str, cached_rxp_diff, cached_txp_diff);
}

static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
Expand All @@ -151,13 +152,11 @@ static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* o
int len;

RichString_writeAscii(out, CRT_colors[METER_TEXT], "rx: ");
Meter_humanUnit(buffer, cached_rxb_diff, sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer);
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], cached_rxb_diff_str);
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], "iB/s");

RichString_appendAscii(out, CRT_colors[METER_TEXT], " tx: ");
Meter_humanUnit(buffer, cached_txb_diff, sizeof(buffer));
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer);
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], cached_txb_diff_str);
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s");

len = xSnprintf(buffer, sizeof(buffer), " (%u/%u pkts/s) ", cached_rxp_diff, cached_txp_diff);
Expand Down

0 comments on commit 3b99732

Please sign in to comment.