Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCP: add File Descriptor Meter #992

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 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 Expand Up @@ -373,6 +375,7 @@ endif
# --------------------------

pcp_platform_headers = \
FileDescriptorMeter.h \
linux/PressureStallMeter.h \
linux/ZramMeter.h \
linux/ZramStats.h \
Expand All @@ -388,6 +391,7 @@ pcp_platform_headers = \
zfs/ZfsCompressedArcMeter.h

pcp_platform_sources = \
FileDescriptorMeter.c \
linux/PressureStallMeter.c \
linux/ZramMeter.c \
pcp/PCPDynamicColumn.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
2 changes: 2 additions & 0 deletions pcp/PCPMetric.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ typedef enum PCPMetric_ {
PCP_ZRAM_CAPACITY, /* zram.capacity */
PCP_ZRAM_ORIGINAL, /* zram.mm_stat.data_size.original */
PCP_ZRAM_COMPRESSED, /* zram.mm_stat.data_size.compressed */
PCP_VFS_FILES_COUNT, /* vfs.files.count */
PCP_VFS_FILES_MAX, /* vfs.files.max */

PCP_PROC_PID, /* proc.psinfo.pid */
PCP_PROC_PPID, /* proc.psinfo.ppid */
Expand Down
15 changes: 15 additions & 0 deletions pcp/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ in the source distribution for its full text.
#include "DiskIOMeter.h"
#include "DynamicColumn.h"
#include "DynamicMeter.h"
#include "FileDescriptorMeter.h"
#include "HostnameMeter.h"
#include "LoadAverageMeter.h"
#include "Macros.h"
Expand Down Expand Up @@ -114,6 +115,7 @@ const MeterClass* const Platform_meterTypes[] = {
&DiskIOMeter_class,
&NetworkIOMeter_class,
&SysArchMeter_class,
&FileDescriptorMeter_class,
NULL
};

Expand Down Expand Up @@ -189,6 +191,8 @@ static const char* Platform_metricNames[] = {
[PCP_ZRAM_CAPACITY] = "zram.capacity",
[PCP_ZRAM_ORIGINAL] = "zram.mm_stat.data_size.original",
[PCP_ZRAM_COMPRESSED] = "zram.mm_stat.data_size.compressed",
[PCP_VFS_FILES_COUNT] = "vfs.files.count",
[PCP_VFS_FILES_MAX] = "vfs.files.max",

[PCP_PROC_PID] = "proc.psinfo.pid",
[PCP_PROC_PPID] = "proc.psinfo.ppid",
Expand Down Expand Up @@ -699,6 +703,17 @@ bool Platform_getNetworkIO(NetworkIOData* data) {
return true;
}

void Platform_getFileDescriptors(double* used, double* max) {
*used = NAN;
smalinux marked this conversation as resolved.
Show resolved Hide resolved
*max = 65536;
smalinux marked this conversation as resolved.
Show resolved Hide resolved

pmAtomValue value;
if (PCPMetric_values(PCP_VFS_FILES_COUNT, &value, 1, PM_TYPE_32) != NULL)
*used = value.l;
if (PCPMetric_values(PCP_VFS_FILES_MAX, &value, 1, PM_TYPE_32) != NULL)
*max = value.l;
}

void Platform_getBattery(double* level, ACPresence* isOnAC) {
*level = NAN;
*isOnAC = AC_ERROR;
Expand Down
2 changes: 2 additions & 0 deletions pcp/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ extern pmOptions opts;

size_t Platform_addMetric(PCPMetric id, const char* name);

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

void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec);

void Platform_gettime_monotonic(uint64_t* msec);
Expand Down