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 committed May 2, 2022
1 parent c7413fd commit 07fe4d1
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,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] = ColorPair(Red, Black),
[ZFS_MFU] = ColorPair(Blue, Black),
[ZFS_MRU] = ColorPair(Yellow, Black),
[ZFS_ANON] = ColorPair(Magenta, Black),
Expand Down Expand Up @@ -309,6 +311,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 @@ -416,6 +420,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(Red, White),
[ZFS_MFU] = ColorPair(Cyan, White),
[ZFS_MRU] = ColorPair(Yellow, White),
[ZFS_ANON] = ColorPair(Magenta, White),
Expand Down Expand Up @@ -523,6 +529,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] = ColorPair(Red, Black),
[ZFS_MFU] = ColorPair(Cyan, Black),
[ZFS_MRU] = ColorPair(Yellow, Black),
[ZFS_ANON] = A_BOLD | ColorPair(Magenta, Black),
Expand Down Expand Up @@ -630,6 +638,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 @@ -735,6 +745,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] = ColorPair(Red, 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 @@ -128,6 +128,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 @@ -152,6 +152,7 @@ myhtopheaders = \
# -----

linux_platform_headers = \
FileDescriptorMeter.h \
generic/gettime.h \
generic/hostname.h \
generic/uname.h \
Expand All @@ -174,6 +175,7 @@ linux_platform_headers = \
zfs/ZfsCompressedArcMeter.h

linux_platform_sources = \
FileDescriptorMeter.c \
generic/gettime.c \
generic/hostname.c \
generic/uname.c \
Expand Down
20 changes: 20 additions & 0 deletions linux/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in the source distribution for its full text.
#include "DateMeter.h"
#include "DateTimeMeter.h"
#include "DiskIOMeter.h"
#include "FileDescriptorMeter.h"
#include "HostnameMeter.h"
#include "HugePageMeter.h"
#include "LoadAverageMeter.h"
Expand Down Expand Up @@ -247,6 +248,7 @@ const MeterClass* const Platform_meterTypes[] = {
&NetworkIOMeter_class,
&SELinuxMeter_class,
&SystemdMeter_class,
&FileDescriptorMeter_class,
NULL
};

Expand Down Expand Up @@ -557,6 +559,24 @@ void Platform_getPressureStall(const char* file, bool some, double* ten, double*
fclose(fd);
}

void Platform_getFileDescriptors(double* used, double* max) {
*used = NAN;
*max = 65536;

FILE* fd = fopen(PROCDIR "/sys/fs/file-nr", "r");
if (!fd)
return;

unsigned long v1, v2, v3;
int total = fscanf(fd, "%lu %lu %lu", &v1, &v2, &v3);
if (total == 3) {
*used = v1;
*max = v3;
}

fclose(fd);
}

bool Platform_getDiskIO(DiskIOData* data) {
FILE* fd = fopen(PROCDIR "/diskstats", "r");
if (!fd)
Expand Down
2 changes: 2 additions & 0 deletions linux/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);

void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred);

void Platform_getFileDescriptors(double* used, double* max);

bool Platform_getDiskIO(DiskIOData* data);

bool Platform_getNetworkIO(NetworkIOData* data);
Expand Down

0 comments on commit 07fe4d1

Please sign in to comment.