Skip to content

Commit

Permalink
Create new File Descriptor meter
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBE authored and cgzones committed Feb 19, 2023
1 parent 8387df1 commit e0229b2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = ColorPair(Cyan, Black),
[PRESSURE_STALL_SIXTY] = A_BOLD | ColorPair(Cyan, Black),
[PRESSURE_STALL_TEN] = A_BOLD | ColorPair(White, Black),
[FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
[FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
[ZFS_MFU] = A_BOLD | ColorPair(Blue, Black),
[ZFS_MRU] = ColorPair(Yellow, Black),
[ZFS_ANON] = ColorPair(Magenta, Black),
Expand Down Expand Up @@ -311,6 +313,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = A_DIM,
[PRESSURE_STALL_SIXTY] = A_NORMAL,
[PRESSURE_STALL_TEN] = A_BOLD,
[FILE_DESCRIPTOR_USED] = A_BOLD,
[FILE_DESCRIPTOR_MAX] = A_BOLD,
[ZFS_MFU] = A_NORMAL,
[ZFS_MRU] = A_NORMAL,
[ZFS_ANON] = A_DIM,
Expand Down Expand Up @@ -419,6 +423,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = ColorPair(Black, White),
[PRESSURE_STALL_SIXTY] = ColorPair(Black, White),
[PRESSURE_STALL_TEN] = ColorPair(Black, White),
[FILE_DESCRIPTOR_USED] = ColorPair(Green, White),
[FILE_DESCRIPTOR_MAX] = ColorPair(Blue, White),
[ZFS_MFU] = ColorPair(Cyan, White),
[ZFS_MRU] = ColorPair(Yellow, White),
[ZFS_ANON] = ColorPair(Magenta, White),
Expand Down Expand Up @@ -527,6 +533,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = ColorPair(Black, Black),
[PRESSURE_STALL_SIXTY] = ColorPair(Black, Black),
[PRESSURE_STALL_TEN] = ColorPair(Black, Black),
[FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
[FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
[ZFS_MFU] = ColorPair(Cyan, Black),
[ZFS_MRU] = ColorPair(Yellow, Black),
[ZFS_ANON] = A_BOLD | ColorPair(Magenta, Black),
Expand Down Expand Up @@ -635,6 +643,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = A_BOLD | ColorPair(Black, Blue),
[PRESSURE_STALL_SIXTY] = A_NORMAL | ColorPair(White, Blue),
[PRESSURE_STALL_TEN] = A_BOLD | ColorPair(White, Blue),
[FILE_DESCRIPTOR_USED] = A_BOLD | ColorPair(Green, Blue),
[FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Red, Blue),
[ZFS_MFU] = A_BOLD | ColorPair(White, Blue),
[ZFS_MRU] = A_BOLD | ColorPair(Yellow, Blue),
[ZFS_ANON] = A_BOLD | ColorPair(Magenta, Blue),
Expand Down Expand Up @@ -741,6 +751,8 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
[PRESSURE_STALL_THREEHUNDRED] = ColorPair(Green, Black),
[PRESSURE_STALL_SIXTY] = ColorPair(Green, Black),
[PRESSURE_STALL_TEN] = A_BOLD | ColorPair(Green, Black),
[FILE_DESCRIPTOR_USED] = ColorPair(Green, Black),
[FILE_DESCRIPTOR_MAX] = A_BOLD | ColorPair(Blue, Black),
[ZFS_MFU] = ColorPair(Blue, Black),
[ZFS_MRU] = ColorPair(Yellow, Black),
[ZFS_ANON] = ColorPair(Magenta, Black),
Expand Down
2 changes: 2 additions & 0 deletions CRT.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ typedef enum ColorElements_ {
PRESSURE_STALL_TEN,
PRESSURE_STALL_SIXTY,
PRESSURE_STALL_THREEHUNDRED,
FILE_DESCRIPTOR_USED,
FILE_DESCRIPTOR_MAX,
ZFS_MFU,
ZFS_MRU,
ZFS_ANON,
Expand Down
80 changes: 80 additions & 0 deletions FileDescriptorMeter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
htop - FileDescriptorMeter.c
(C) 2022 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "FileDescriptorMeter.h"

#include <math.h>
#include <stdbool.h>
#include <string.h>

#include "CRT.h"
#include "Meter.h"
#include "Object.h"
#include "Platform.h"
#include "RichString.h"
#include "XUtils.h"


static const int FileDescriptorMeter_attributes[] = {
FILE_DESCRIPTOR_USED,
FILE_DESCRIPTOR_MAX
};

static void FileDescriptorMeter_updateValues(Meter* this) {
this->values[0] = 0;
this->values[1] = 1;

Platform_getFileDescriptors(&this->values[0], &this->values[1]);

/* only print bar for first value */
this->curItems = 1;

/* Use maximum value for scaling of bar mode */
this->total = this->values[1];

if (isnan(this->values[0])) {
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "unknown/unknown");
} else {
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.0lf/%.0lf", this->values[0], this->values[1]);
}
}

static void FileDescriptorMeter_display(const Object* cast, RichString* out) {
const Meter* this = (const Meter*)cast;
char buffer[50];
int len;

if (isnan(this->values[0])) {
RichString_appendAscii(out, CRT_colors[METER_TEXT], "unknown");
return;
}

RichString_appendAscii(out, CRT_colors[METER_TEXT], "used: ");
len = xSnprintf(buffer, sizeof(buffer), "%.0lf", this->values[0]);
RichString_appendnAscii(out, CRT_colors[FILE_DESCRIPTOR_USED], buffer, len);

RichString_appendAscii(out, CRT_colors[METER_TEXT], " max: ");
len = xSnprintf(buffer, sizeof(buffer), "%.0lf", this->values[1]);
RichString_appendnAscii(out, CRT_colors[FILE_DESCRIPTOR_MAX], buffer, len);
}

const MeterClass FileDescriptorMeter_class = {
.super = {
.extends = Class(Meter),
.delete = Meter_delete,
.display = FileDescriptorMeter_display,
},
.updateValues = FileDescriptorMeter_updateValues,
.defaultMode = TEXT_METERMODE,
.maxItems = 2,
.total = 100.0,
.attributes = FileDescriptorMeter_attributes,
.name = "FileDescriptors",
.uiName = "File Descriptors",
.caption = "FDs: ",
.description = "Number of allocated/available file descriptors"
};
15 changes: 15 additions & 0 deletions FileDescriptorMeter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef HEADER_FileDescriptorMeter
#define HEADER_FileDescriptorMeter
/*
htop - FileDescriptorMeter.h
(C) 2022 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "Meter.h"


extern const MeterClass FileDescriptorMeter_class;

#endif
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ myhtopsources = \
DynamicColumn.c \
DynamicMeter.c \
EnvScreen.c \
FileDescriptorMeter.c \
FunctionBar.c \
Hashtable.c \
Header.c \
Expand Down Expand Up @@ -110,6 +111,7 @@ myhtopheaders = \
DynamicColumn.h \
DynamicMeter.h \
EnvScreen.h \
FileDescriptorMeter.h \
FunctionBar.h \
Hashtable.h \
Header.h \
Expand Down

0 comments on commit e0229b2

Please sign in to comment.