summaryrefslogtreecommitdiffstats
path: root/NetworkIOMeter.c
blob: 6bc2d08276d804c532529d19f3659d860be87fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
htop - NetworkIOMeter.c
(C) 2020-2023 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "NetworkIOMeter.h"

#include <stdbool.h>

#include "CRT.h"
#include "Machine.h"
#include "Macros.h"
#include "Meter.h"
#include "Object.h"
#include "Platform.h"
#include "RichString.h"
#include "Row.h"
#include "XUtils.h"


static const int NetworkIOMeter_attributes[] = {
   METER_VALUE_IOREAD,
   METER_VALUE_IOWRITE,
};

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

static void NetworkIOMeter_updateValues(Meter* this) {
   const Machine* host = this->host;

   static uint64_t cached_last_update = 0;
   uint64_t passedTimeInMs = host->realtimeMs - cached_last_update;
   bool hasNewData = false;
   NetworkIOData data;

   /* update only every 500ms to have a sane span for rate calculation */
   if (passedTimeInMs > 500) {
      hasNewData = Platform_getNetworkIO(&data);
      if (!hasNewData) {
         status = RATESTATUS_NODATA;
      } else if (cached_last_update == 0) {
         status = RATESTATUS_INIT;
      } else if (passedTimeInMs > 30000) {
         status = RATESTATUS_STALE;
      } else {
         status = RATESTATUS_DATA;
      }

      cached_last_update = host->realtimeMs;
   }

   if (hasNewData) {
      static uint64_t cached_rxb_total;
      static uint64_t cached_rxp_total;
      static uint64_t cached_txb_total;
      static uint64_t cached_txp_total;

      if (status != RATESTATUS_INIT) {
         uint64_t diff;

         if (data.bytesReceived > cached_rxb_total) {
            diff = data.bytesReceived - cached_rxb_total;
            diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */
            cached_rxb_diff = diff;
         } else {
            cached_rxb_diff = 0;
         }
         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;
            diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */
            cached_rxp_diff = (uint32_t)diff;
         } else {
            cached_rxp_diff = 0;
         }

         if (data.bytesTransmitted > cached_txb_total) {
            diff = data.bytesTransmitted - cached_txb_total;
            diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */
            cached_txb_diff = diff;
         } else {
            cached_txb_diff = 0;
         }
         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;
            diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */
            cached_txp_diff = (uint32_t)diff;
         } else {
            cached_txp_diff = 0;
         }
      }

      cached_rxb_total = data.bytesReceived;
      cached_rxp_total = data.packetsReceived;
      cached_txb_total = data.bytesTransmitted;
      cached_txp_total = data.packetsTransmitted;
   }

   this->values[0] = cached_rxb_diff;
   this->values[1] = cached_txb_diff;
   if (cached_rxb_diff + cached_txb_diff > this->total) {
      this->total = cached_rxb_diff + cached_txb_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;
   }
   if (status == RATESTATUS_STALE) {
      xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "stale");
      return;
   }

   xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s %u/%upkts/s",
      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) {
   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;
   }

   char buffer[64];

   RichString_writeAscii(out, CRT_colors[METER_TEXT], "rx: ");
   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: ");
   RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], cached_txb_diff_str);
   RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s");

   int len = xSnprintf(buffer, sizeof(buffer), " (%u/%u pkts/s) ", cached_rxp_diff, cached_txp_diff);
   RichString_appendnAscii(out, CRT_colors[METER_TEXT], buffer, len);
}

const MeterClass NetworkIOMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = NetworkIOMeter_display
   },
   .updateValues = NetworkIOMeter_updateValues,
   .defaultMode = TEXT_METERMODE,
   .maxItems = 2,
   .total = 100.0,
   .attributes = NetworkIOMeter_attributes,
   .name = "NetworkIO",
   .uiName = "Network IO",
   .caption = "Network: "
};