Skip to content

Commit

Permalink
Cache NetworkIOMeter values in 'double' type...
Browse files Browse the repository at this point in the history
..and change the meter value unit from KiB/s to bytes/second.

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

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

Expand Down Expand Up @@ -70,12 +70,11 @@ static void NetworkIOMeter_updateValues(Meter* this) {
if (data.bytesReceived > cached_rxb_total) {
diff = data.bytesReceived - cached_rxb_total;
diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */
diff /= ONE_K; /* convert to KiB/s */
cached_rxb_diff = (uint32_t)diff;
cached_rxb_diff = diff;
} else {
cached_rxb_diff = 0;
}
Meter_humanUnit(cached_rxb_diff_str, cached_rxb_diff, sizeof(cached_rxb_diff_str));
Meter_humanUnit(cached_rxb_diff_str, cached_rxb_diff / ONE_K, sizeof(cached_rxb_diff_str));

if (data.packetsReceived > cached_rxp_total) {
diff = data.packetsReceived - cached_rxp_total;
Expand All @@ -88,12 +87,11 @@ static void NetworkIOMeter_updateValues(Meter* this) {
if (data.bytesTransmitted > cached_txb_total) {
diff = data.bytesTransmitted - cached_txb_total;
diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */
diff /= ONE_K; /* convert to KiB/s */
cached_txb_diff = (uint32_t)diff;
cached_txb_diff = diff;
} else {
cached_txb_diff = 0;
}
Meter_humanUnit(cached_txb_diff_str, cached_txb_diff, sizeof(cached_txb_diff_str));
Meter_humanUnit(cached_txb_diff_str, cached_txb_diff / ONE_K, sizeof(cached_txb_diff_str));

if (data.packetsTransmitted > cached_txp_total) {
diff = data.packetsTransmitted - cached_txp_total;
Expand Down

0 comments on commit 6ec86f5

Please sign in to comment.