Improve handling of no data in Disk and Network IO Meters

This commit is contained in:
Christian Göttsche 2020-10-20 21:40:51 +02:00 committed by cgzones
parent 167adc0a2b
commit f757810f48
18 changed files with 119 additions and 61 deletions

6
CRT.c
View File

@ -94,6 +94,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = A_BOLD | ColorPair(Red,Black),
[METER_TEXT] = ColorPair(Cyan,Black),
[METER_VALUE] = A_BOLD | ColorPair(Cyan,Black),
[METER_VALUE_ERROR] = A_BOLD | ColorPair(Red,Black),
[METER_VALUE_NOTICE] = A_BOLD | ColorPair(White,Black),
[METER_VALUE_IOREAD] = ColorPair(Green,Black),
[METER_VALUE_IOWRITE] = ColorPair(Blue,Black),
@ -170,6 +171,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = A_BOLD,
[METER_TEXT] = A_NORMAL,
[METER_VALUE] = A_BOLD,
[METER_VALUE_ERROR] = A_BOLD,
[METER_VALUE_NOTICE] = A_BOLD,
[METER_VALUE_IOREAD] = A_NORMAL,
[METER_VALUE_IOWRITE] = A_NORMAL,
@ -246,6 +248,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = ColorPair(Red,White),
[METER_TEXT] = ColorPair(Blue,White),
[METER_VALUE] = ColorPair(Black,White),
[METER_VALUE_ERROR] = A_BOLD | ColorPair(Red,White),
[METER_VALUE_NOTICE] = A_BOLD | ColorPair(Yellow,White),
[METER_VALUE_IOREAD] = ColorPair(Green,White),
[METER_VALUE_IOWRITE] = ColorPair(Yellow,White),
@ -322,6 +325,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = ColorPair(Red,Black),
[METER_TEXT] = ColorPair(Blue,Black),
[METER_VALUE] = ColorPair(Blue,Black),
[METER_VALUE_ERROR] = A_BOLD | ColorPair(Red,Black),
[METER_VALUE_NOTICE] = A_BOLD | ColorPair(Yellow,Black),
[METER_VALUE_IOREAD] = ColorPair(Green,Black),
[METER_VALUE_IOWRITE] = ColorPair(Yellow,Black),
@ -398,6 +402,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = A_BOLD | ColorPair(Red,Blue),
[METER_TEXT] = ColorPair(Cyan,Blue),
[METER_VALUE] = A_BOLD | ColorPair(Cyan,Blue),
[METER_VALUE_ERROR] = A_BOLD | ColorPair(Red,Blue),
[METER_VALUE_NOTICE] = A_BOLD | ColorPair(White,Blue),
[METER_VALUE_IOREAD] = ColorPair(Green,Blue),
[METER_VALUE_IOWRITE] = ColorPair(Black,Blue),
@ -474,6 +479,7 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[LARGE_NUMBER] = A_BOLD | ColorPair(Red,Black),
[METER_TEXT] = ColorPair(Cyan,Black),
[METER_VALUE] = ColorPair(Green,Black),
[METER_VALUE_ERROR] = A_BOLD | ColorPair(Red,Black),
[METER_VALUE_NOTICE] = A_BOLD | ColorPair(Yellow,Black),
[METER_VALUE_IOREAD] = ColorPair(Green,Black),
[METER_VALUE_IOWRITE] = ColorPair(Blue,Black),

1
CRT.h
View File

@ -52,6 +52,7 @@ typedef enum ColorElements_ {
LARGE_NUMBER,
METER_TEXT,
METER_VALUE,
METER_VALUE_ERROR,
METER_VALUE_NOTICE,
METER_VALUE_IOREAD,
METER_VALUE_IOWRITE,

View File

@ -24,6 +24,7 @@ static const int DiskIOMeter_attributes[] = {
METER_VALUE_IOWRITE,
};
static bool hasData = false;
static unsigned long int cached_read_diff = 0;
static unsigned long int cached_write_diff = 0;
static double cached_utilisation_diff = 0.0;
@ -37,12 +38,20 @@ static void DiskIOMeter_updateValues(Meter* this, char* buffer, int len) {
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;
unsigned long long int passedTimeInMs = timeInMilliSeconds - cached_last_update;
/* update only every 500ms */
if (timeInMilliSeconds - cached_last_update > 500) {
if (passedTimeInMs > 500) {
cached_last_update = timeInMilliSeconds;
unsigned long int bytesRead, bytesWrite, msTimeSpend;
Platform_getDiskIO(&bytesRead, &bytesWrite, &msTimeSpend);
hasData = Platform_getDiskIO(&bytesRead, &bytesWrite, &msTimeSpend);
if (!hasData) {
this->values[0] = 0;
xSnprintf(buffer, len, "no data");
return;
}
cached_read_diff = (bytesRead - cached_read_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
cached_read_total = bytesRead;
@ -50,8 +59,7 @@ static void DiskIOMeter_updateValues(Meter* this, char* buffer, int len) {
cached_write_diff = (bytesWrite - cached_write_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
cached_write_total = bytesWrite;
cached_utilisation_diff = 100 * (double)(msTimeSpend - cached_msTimeSpend_total) / (timeInMilliSeconds - cached_last_update);
cached_last_update = timeInMilliSeconds;
cached_utilisation_diff = 100 * (double)(msTimeSpend - cached_msTimeSpend_total) / passedTimeInMs;
cached_msTimeSpend_total = msTimeSpend;
}
@ -65,6 +73,11 @@ static void DiskIOMeter_updateValues(Meter* this, char* buffer, int len) {
}
static void DIskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
if (!hasData) {
RichString_write(out, CRT_colors[METER_VALUE_ERROR], "no data");
return;
}
char buffer[16];
int color = cached_utilisation_diff > 40.0 ? METER_VALUE_NOTICE : METER_VALUE;

View File

@ -16,6 +16,7 @@ static const int NetworkIOMeter_attributes[] = {
METER_VALUE_IOWRITE,
};
static bool hasData = false;
static unsigned long int cached_rxb_diff = 0;
static unsigned long int cached_rxp_diff = 0;
static unsigned long int cached_txb_diff = 0;
@ -35,9 +36,15 @@ static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, i
/* update only every 500ms */
if (passedTimeInMs > 500) {
cached_last_update = timeInMilliSeconds;
unsigned long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted;
Platform_getNetworkIO(&bytesReceived, &packetsReceived, &bytesTransmitted, &packetsTransmitted);
hasData = Platform_getNetworkIO(&bytesReceived, &packetsReceived, &bytesTransmitted, &packetsTransmitted);
if (!hasData) {
xSnprintf(buffer, len, "no data");
return;
}
cached_rxb_diff = (bytesReceived - cached_rxb_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
cached_rxb_diff = 1000.0 * cached_rxb_diff / passedTimeInMs; /* convert to per second */
@ -52,8 +59,6 @@ static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, i
cached_txp_diff = packetsTransmitted - cached_txp_total;
cached_txp_total = packetsTransmitted;
cached_last_update = timeInMilliSeconds;
}
char bufferBytesReceived[12], bufferBytesTransmitted[12];
@ -63,6 +68,11 @@ static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, i
}
static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
if (!hasData) {
RichString_write(out, CRT_colors[METER_VALUE_ERROR], "no data");
return;
}
char buffer[64];
RichString_write(out, CRT_colors[METER_TEXT], "rx: ");

View File

@ -311,12 +311,15 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
// TODO
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -325,4 +328,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false;
}

View File

@ -48,9 +48,11 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -206,12 +206,15 @@ char* Platform_getProcessEnv(pid_t pid) {
return NULL;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
// TODO
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -220,4 +223,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false;
}

View File

@ -40,9 +40,11 @@ void Platform_setSwapValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -241,12 +241,15 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
// TODO
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -258,13 +261,9 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
const int countMib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };
r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0);
if (r < 0) {
*bytesReceived = 0;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return;
}
if (r < 0)
return false;
unsigned long int bytesReceivedSum = 0, packetsReceivedSum = 0, bytesTransmittedSum = 0, packetsTransmittedSum = 0;
@ -291,4 +290,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = packetsReceivedSum;
*bytesTransmitted = bytesTransmittedSum;
*packetsTransmitted = packetsTransmittedSum;
return true;
}

View File

@ -43,9 +43,11 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -302,14 +302,11 @@ void Platform_getPressureStall(const char *file, bool some, double* ten, double*
fclose(fd);
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
FILE *fd = fopen(PROCDIR "/diskstats", "r");
if (!fd) {
*bytesRead = 0;
*bytesWrite = 0;
*msTimeSpend = 0;
return;
}
if (!fd)
return false;
unsigned long int read_sum = 0, write_sum = 0, timeSpend_sum = 0;
char lineBuffer[256];
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
@ -347,27 +344,23 @@ void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWr
*bytesRead = 512 * read_sum;
*bytesWrite = 512 * write_sum;
*msTimeSpend = timeSpend_sum;
return true;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
FILE *fd = fopen(PROCDIR "/net/dev", "r");
if (!fd) {
*bytesReceived = 0;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return;
}
if (!fd)
return false;
unsigned long int bytesReceivedSum = 0, packetsReceivedSum = 0, bytesTransmittedSum = 0, packetsTransmittedSum = 0;
char lineBuffer[512];
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
char interfaceName[32];
unsigned long int bytesReceivedParsed, packetsReceivedParsed, bytesTransmittedParsed, packetsTransmittedParsed;
if (fscanf(fd, "%31s %lu %lu %*u %*u %*u %*u %*u %*u %lu %lu %*u %*u %*u %*u %*u %*u",
if (sscanf(lineBuffer, "%31s %lu %lu %*u %*u %*u %*u %*u %*u %lu %lu",
interfaceName,
&bytesReceivedParsed,
&packetsReceivedParsed,
@ -390,4 +383,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = packetsReceivedSum;
*bytesTransmitted = bytesTransmittedSum;
*packetsTransmitted = packetsTransmittedSum;
return true;
}

View File

@ -46,9 +46,11 @@ char* Platform_getProcessEnv(pid_t pid);
void Platform_getPressureStall(const char *file, bool some, double* ten, double* sixty, double* threehundred);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -288,12 +288,15 @@ char* Platform_getProcessEnv(pid_t pid) {
return env;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
// TODO
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -302,4 +305,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false;
}

View File

@ -41,9 +41,11 @@ void Platform_setSwapValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -264,12 +264,15 @@ char* Platform_getProcessEnv(pid_t pid) {
return envBuilder.env;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
// TODO
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -278,4 +281,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false;
}

View File

@ -63,9 +63,11 @@ void Platform_setZfsCompressedArcValues(Meter* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);

View File

@ -141,11 +141,14 @@ char* Platform_getProcessEnv(pid_t pid) {
return NULL;
}
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend) {
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend) {
*bytesRead = *bytesWrite = *msTimeSpend = 0;
return false;
}
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted) {
@ -153,4 +156,5 @@ void Platform_getNetworkIO(unsigned long int *bytesReceived,
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false;
}

View File

@ -47,9 +47,11 @@ bool Process_isThread(const Process* this);
char* Platform_getProcessEnv(pid_t pid);
void Platform_getDiskIO(unsigned long int *bytesRead, unsigned long int *bytesWrite, unsigned long int *msTimeSpend);
bool Platform_getDiskIO(unsigned long int *bytesRead,
unsigned long int *bytesWrite,
unsigned long int *msTimeSpend);
void Platform_getNetworkIO(unsigned long int *bytesReceived,
bool Platform_getNetworkIO(unsigned long int *bytesReceived,
unsigned long int *packetsReceived,
unsigned long int *bytesTransmitted,
unsigned long int *packetsTransmitted);