File: DiskIOMeter.c

package info (click to toggle)
pcp 7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,748 kB
  • sloc: ansic: 1,483,656; sh: 182,366; xml: 160,462; cpp: 83,813; python: 24,980; perl: 18,327; yacc: 6,877; lex: 2,864; makefile: 2,738; awk: 165; fortran: 60; java: 52
file content (361 lines) | stat: -rw-r--r-- 11,063 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
htop - DiskIOMeter.c
(C) 2020 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 "DiskIOMeter.h"

#include <stdbool.h>

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


typedef struct DiskIOMeterData_ {
   Meter* diskIORateMeter;
   Meter* diskIOTimeMeter;
} DiskIOMeterData;

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

static const int DiskIOTimeMeter_attributes[] = {
   METER_VALUE_NOTICE,
};

static MeterRateStatus status = RATESTATUS_INIT;
static double cached_read_diff;
static char cached_read_diff_str[6];
static double cached_write_diff;
static char cached_write_diff_str[6];
static uint64_t cached_num_disks;
static double cached_utilisation_diff;
static double cached_utilisation_norm;

static void DiskIOUpdateCache(const Machine* host) {
   static uint64_t cached_last_update;

   uint64_t passedTimeInMs = host->realtimeMs - cached_last_update;

   /* update only every 500ms to have a sane span for rate calculation */
   if (passedTimeInMs <= 500)
      return;

   DiskIOData data;
   bool hasNewData = Platform_getDiskIO(&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)
      return;

   static uint64_t cached_read_total;
   static uint64_t cached_write_total;
   static uint64_t cached_msTimeSpend_total;

   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 */
      } else {
         diff = 0;
      }
      cached_read_diff = diff;
      Meter_humanUnit(cached_read_diff_str, cached_read_diff / ONE_K, 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 */
      } else {
         diff = 0;
      }
      cached_write_diff = diff;
      Meter_humanUnit(cached_write_diff_str, cached_write_diff / ONE_K, sizeof(cached_write_diff_str));

      cached_num_disks = data.numDisks;
      cached_utilisation_diff = 0.0;
      cached_utilisation_norm = 0.0;
      if (data.totalMsTimeSpend > cached_msTimeSpend_total) {
         diff = data.totalMsTimeSpend - cached_msTimeSpend_total;
         cached_utilisation_diff = 100.0 * (double)diff / passedTimeInMs;
         if (data.numDisks > 0) {
            cached_utilisation_norm = (double)diff / (passedTimeInMs * data.numDisks);
            cached_utilisation_norm = MINIMUM(cached_utilisation_norm, 1.0);
         }
      }
   }

   cached_read_total = data.totalBytesRead;
   cached_write_total = data.totalBytesWritten;
   cached_msTimeSpend_total = data.totalMsTimeSpend;
}

static void DiskIORateMeter_updateValues(Meter* this) {
   DiskIOUpdateCache(this->host);

   this->values[0] = cached_read_diff;
   this->values[1] = cached_write_diff;

   switch (status) {
      case RATESTATUS_NODATA:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data");
         return;
      case RATESTATUS_INIT:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "init");
         return;
      case RATESTATUS_STALE:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "stale");
         return;
      case RATESTATUS_DATA:
         break;
   }

   xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "r:%siB/s w:%siB/s", cached_read_diff_str, cached_write_diff_str);
}

static void DiskIORateMeter_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;
   }

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

static void DiskIOTimeMeter_updateValues(Meter* this) {
   DiskIOUpdateCache(this->host);

   this->values[0] = cached_utilisation_norm;

   switch (status) {
      case RATESTATUS_NODATA:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data");
         return;
      case RATESTATUS_INIT:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "init");
         return;
      case RATESTATUS_STALE:
         xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "stale");
         return;
      case RATESTATUS_DATA:
         break;
   }

   char numDisksStr[12];
   numDisksStr[0] = '\0';
   if (cached_num_disks > 1 && cached_num_disks < 1000) {
      xSnprintf(numDisksStr, sizeof(numDisksStr), " (%udisks)", (unsigned int)cached_num_disks);
   }

   xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.1f%%%s", cached_utilisation_diff, numDisksStr);
}

static void DiskIOTimeMeter_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[16];

   int color = cached_utilisation_diff > 40.0 ? METER_VALUE_NOTICE : METER_VALUE;
   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], " busy");

   if (cached_num_disks > 1 && cached_num_disks < 1000) {
      RichString_appendAscii(out, CRT_colors[METER_TEXT], " (");
      len = xSnprintf(buffer, sizeof(buffer), "%u", (unsigned int)cached_num_disks);
      RichString_appendnAscii(out, CRT_colors[METER_VALUE], buffer, len);
      RichString_appendAscii(out, CRT_colors[METER_TEXT], " disks)");
   }
}

static void DiskIOMeter_display(const Object* cast, RichString* out) {
   DiskIORateMeter_display(cast, out);

   switch (status) {
      case RATESTATUS_NODATA:
      case RATESTATUS_INIT:
      case RATESTATUS_STALE:
         return;
      case RATESTATUS_DATA:
         break;
   }

   RichString_appendAscii(out, CRT_colors[METER_TEXT], "; ");
   DiskIOTimeMeter_display(cast, out);
}

static void DiskIOMeter_updateValues(Meter* this) {
   DiskIOMeterData* data = this->meterData;

   Meter_updateValues(data->diskIORateMeter);
   Meter_updateValues(data->diskIOTimeMeter);
}

static void DiskIOMeter_draw(Meter* this, int x, int y, int w) {
   DiskIOMeterData* data = this->meterData;

   assert(data->diskIORateMeter->draw);
   assert(data->diskIOTimeMeter->draw);

   switch (this->mode) {
   case TEXT_METERMODE:
   case LED_METERMODE:
      data->diskIORateMeter->draw(this, x, y, w);
      return;
   }

   /* Use the same width for each sub meter to align with CPU meter */
   const int colwidth = w / 2;
   const int diff = w % 2;

   data->diskIORateMeter->draw(data->diskIORateMeter, x, y, colwidth);
   data->diskIOTimeMeter->draw(data->diskIOTimeMeter, x + colwidth + diff, y, colwidth);
}

static void DiskIOMeter_init(Meter* this) {
   if (!this->meterData) {
      this->meterData = xCalloc(1, sizeof(DiskIOMeterData));
   }

   DiskIOMeterData* data = this->meterData;

   if (!data->diskIORateMeter)
      data->diskIORateMeter = Meter_new(this->host, 0, (const MeterClass*) Class(DiskIORateMeter));
   if (!data->diskIOTimeMeter)
      data->diskIOTimeMeter = Meter_new(this->host, 0, (const MeterClass*) Class(DiskIOTimeMeter));

   if (Meter_initFn(data->diskIORateMeter)) {
      Meter_init(data->diskIORateMeter);
   }
   if (Meter_initFn(data->diskIOTimeMeter)) {
      Meter_init(data->diskIOTimeMeter);
   }
}

static void DiskIOMeter_updateMode(Meter* this, MeterModeId mode) {
   DiskIOMeterData* data = this->meterData;

   this->mode = mode;

   Meter_setMode(data->diskIORateMeter, mode);
   Meter_setMode(data->diskIOTimeMeter, mode);

   this->h = MAXIMUM(data->diskIORateMeter->h, data->diskIOTimeMeter->h);
}

static void DiskIOMeter_done(Meter* this) {
   DiskIOMeterData* data = this->meterData;

   Meter_delete((Object*)data->diskIORateMeter);
   Meter_delete((Object*)data->diskIOTimeMeter);

   free(data);
}

const MeterClass DiskIORateMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = DiskIORateMeter_display
   },
   .updateValues = DiskIORateMeter_updateValues,
   .defaultMode = TEXT_METERMODE,
   .supportedModes = METERMODE_DEFAULT_SUPPORTED,
   .maxItems = 2,
   .isPercentChart = false,
   .total = 1.0,
   .attributes = DiskIORateMeter_attributes,
   .name = "DiskIORate",
   .uiName = "Disk IO Rate",
   .description = "Disk IO read & write bytes per second",
   .caption = "Dsk: "
};

const MeterClass DiskIOTimeMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = DiskIOTimeMeter_display
   },
   .updateValues = DiskIOTimeMeter_updateValues,
   .defaultMode = TEXT_METERMODE,
   .supportedModes = METERMODE_DEFAULT_SUPPORTED,
   .maxItems = 1,
   .isPercentChart = true,
   .total = 1.0,
   .attributes = DiskIOTimeMeter_attributes,
   .name = "DiskIOTime",
   .uiName = "Disk IO Time",
   .description = "Disk percent time busy",
   .caption = "Dsk: "
};

const MeterClass DiskIOMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete,
      .display = DiskIOMeter_display
   },
   .updateValues = DiskIOMeter_updateValues,
   .defaultMode = TEXT_METERMODE,
   .supportedModes = METERMODE_DEFAULT_SUPPORTED,
   .isMultiColumn = true,
   .name = "DiskIO",
   .uiName = "Disk IO",
   .description = "Disk IO rate & time combined display",
   .caption = "Dsk: ",
   .draw = DiskIOMeter_draw,
   .init = DiskIOMeter_init,
   .updateMode = DiskIOMeter_updateMode,
   .done = DiskIOMeter_done
};