2020-09-13 17:46:34 +00:00
|
|
|
/*
|
|
|
|
htop - DiskIOMeter.c
|
2020-12-04 12:42:00 +00:00
|
|
|
(C) 2020 htop dev team
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2020-09-13 17:46:34 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DiskIOMeter.h"
|
|
|
|
|
2020-11-18 13:26:30 +00:00
|
|
|
#include <stdbool.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
2020-09-13 17:46:34 +00:00
|
|
|
|
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
2020-09-13 17:46:34 +00:00
|
|
|
#include "Platform.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "RichString.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2020-09-13 17:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const int DiskIOMeter_attributes[] = {
|
2020-10-16 17:44:53 +00:00
|
|
|
METER_VALUE_NOTICE,
|
|
|
|
METER_VALUE_IOREAD,
|
|
|
|
METER_VALUE_IOWRITE,
|
2020-09-13 17:46:34 +00:00
|
|
|
};
|
|
|
|
|
2020-10-20 19:40:51 +00:00
|
|
|
static bool hasData = false;
|
2020-09-13 17:46:34 +00:00
|
|
|
static unsigned long int cached_read_diff = 0;
|
|
|
|
static unsigned long int cached_write_diff = 0;
|
|
|
|
static double cached_utilisation_diff = 0.0;
|
|
|
|
|
2020-11-24 17:31:03 +00:00
|
|
|
static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
|
2020-09-13 17:46:34 +00:00
|
|
|
static unsigned long long int cached_last_update = 0;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
unsigned long long int timeInMilliSeconds = (unsigned long long int)tv.tv_sec * 1000 + (unsigned long long int)tv.tv_usec / 1000;
|
2020-10-20 19:40:51 +00:00
|
|
|
unsigned long long int passedTimeInMs = timeInMilliSeconds - cached_last_update;
|
2020-09-13 17:46:34 +00:00
|
|
|
|
|
|
|
/* update only every 500ms */
|
2020-10-20 19:40:51 +00:00
|
|
|
if (passedTimeInMs > 500) {
|
2020-11-21 23:53:12 +00:00
|
|
|
static unsigned long int cached_read_total = 0;
|
|
|
|
static unsigned long int cached_write_total = 0;
|
|
|
|
static unsigned long int cached_msTimeSpend_total = 0;
|
|
|
|
|
2020-10-20 19:40:51 +00:00
|
|
|
cached_last_update = timeInMilliSeconds;
|
|
|
|
|
2020-10-21 15:06:32 +00:00
|
|
|
DiskIOData data;
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-10-21 15:06:32 +00:00
|
|
|
hasData = Platform_getDiskIO(&data);
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!hasData) {
|
|
|
|
this->values[0] = 0;
|
|
|
|
xSnprintf(buffer, len, "no data");
|
|
|
|
return;
|
|
|
|
}
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (data.totalBytesRead > cached_read_total) {
|
|
|
|
cached_read_diff = (data.totalBytesRead - cached_read_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
} else {
|
|
|
|
cached_read_diff = 0;
|
|
|
|
}
|
2020-10-21 15:06:32 +00:00
|
|
|
cached_read_total = data.totalBytesRead;
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (data.totalBytesWritten > cached_write_total) {
|
|
|
|
cached_write_diff = (data.totalBytesWritten - cached_write_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
} else {
|
|
|
|
cached_write_diff = 0;
|
|
|
|
}
|
2020-10-21 15:06:32 +00:00
|
|
|
cached_write_total = data.totalBytesWritten;
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (data.totalMsTimeSpend > cached_msTimeSpend_total) {
|
|
|
|
cached_utilisation_diff = 100 * (double)(data.totalMsTimeSpend - cached_msTimeSpend_total) / passedTimeInMs;
|
|
|
|
} else {
|
|
|
|
cached_utilisation_diff = 0.0;
|
|
|
|
}
|
2020-10-21 15:06:32 +00:00
|
|
|
cached_msTimeSpend_total = data.totalMsTimeSpend;
|
2020-09-13 17:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(buffer, len, "%sB %sB %.1f%%", bufferRead, bufferWrite, cached_utilisation_diff);
|
|
|
|
}
|
|
|
|
|
2021-02-15 16:42:22 +00:00
|
|
|
static void DiskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!hasData) {
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data");
|
2020-10-20 19:40:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:46:34 +00:00
|
|
|
char buffer[16];
|
|
|
|
|
2020-10-16 17:44:53 +00:00
|
|
|
int color = cached_utilisation_diff > 40.0 ? METER_VALUE_NOTICE : METER_VALUE;
|
2020-09-13 17:46:34 +00:00
|
|
|
xSnprintf(buffer, sizeof(buffer), "%.1f%%", cached_utilisation_diff);
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeAscii(out, CRT_colors[color], buffer);
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " read: ");
|
2020-09-13 17:46:34 +00:00
|
|
|
Meter_humanUnit(buffer, cached_read_diff, sizeof(buffer));
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer);
|
2020-09-13 17:46:34 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " write: ");
|
2020-09-13 17:46:34 +00:00
|
|
|
Meter_humanUnit(buffer, cached_write_diff, sizeof(buffer));
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer);
|
2020-09-13 17:46:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterClass DiskIOMeter_class = {
|
2020-09-13 17:46:34 +00:00
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
2021-02-15 16:42:22 +00:00
|
|
|
.display = DiskIOMeter_display
|
2020-09-13 17:46:34 +00:00
|
|
|
},
|
|
|
|
.updateValues = DiskIOMeter_updateValues,
|
|
|
|
.defaultMode = TEXT_METERMODE,
|
|
|
|
.maxItems = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = DiskIOMeter_attributes,
|
|
|
|
.name = "DiskIO",
|
|
|
|
.uiName = "Disk IO",
|
|
|
|
.caption = "Disk IO: "
|
|
|
|
};
|