summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
authorBenny Baumann <BenBE@geshi.org>2023-02-05 02:40:42 +0100
committercgzones <cgzones@googlemail.com>2023-02-19 17:56:50 +0100
commit7a8c01f304b673dd89923e05d242aebb9cd0a500 (patch)
treec9b21f30b3acf18f6ca8e4b1b404015ca3c2ba9a /generic
parent377a06db0de3850573f2a8e2c33152cf3483ea61 (diff)
Implement File Descriptor Meter support for DragonflyBSD/FreeBSD/NetBSD
Diffstat (limited to 'generic')
-rw-r--r--generic/fdstat_sysctl.c83
-rw-r--r--generic/fdstat_sysctl.h13
2 files changed, 96 insertions, 0 deletions
diff --git a/generic/fdstat_sysctl.c b/generic/fdstat_sysctl.c
new file mode 100644
index 00000000..9e89a0da
--- /dev/null
+++ b/generic/fdstat_sysctl.c
@@ -0,0 +1,83 @@
+/*
+htop - generic/fdstat_sysctl.c
+(C) 2022-2023 htop dev team
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "generic/fdstat_sysctl.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include <sys/sysctl.h>
+#include <sys/types.h>
+
+#include "config.h"
+
+
+static void Generic_getFileDescriptors_sysctl_internal(
+ const char* sysctlname_maxfiles,
+ const char* sysctlname_numfiles,
+ size_t size_header,
+ size_t size_entry,
+ double* used,
+ double* max
+) {
+ *used = NAN;
+ *max = 65536;
+
+ int max_fd, open_fd;
+ size_t len;
+
+ len = sizeof(max_fd);
+ if (sysctlname_maxfiles && sysctlbyname(sysctlname_maxfiles, &max_fd, &len, NULL, 0) == 0) {
+ if (max_fd) {
+ *max = max_fd;
+ } else {
+ *max = NAN;
+ }
+ }
+
+ len = sizeof(open_fd);
+ if (sysctlname_numfiles && sysctlbyname(sysctlname_numfiles, &open_fd, &len, NULL, 0) == 0) {
+ *used = open_fd;
+ }
+
+ if (!isnan(*used)) {
+ return;
+ }
+
+ // If no sysctl arc available, try to guess from the file table size at kern.file
+ // The size per entry differs per OS, thus skip if we don't know:
+ if (!size_entry)
+ return;
+
+ len = 0;
+ if (sysctlbyname("kern.file", NULL, &len, NULL, 0) < 0)
+ return;
+
+ if (len < size_header)
+ return;
+
+ *used = (len - size_header) / size_entry;
+}
+
+void Generic_getFileDescriptors_sysctl(double* used, double* max) {
+#if defined(HTOP_DARWIN)
+ Generic_getFileDescriptors_sysctl_internal(
+ "kern.maxfiles", "kern.num_files", 0, 0, used, max);
+#elif defined(HTOP_DRAGONFLY)
+ Generic_getFileDescriptors_sysctl_internal(
+ "kern.maxfiles", NULL, 0, sizeof(struct kinfo_file), used, max);
+#elif defined(HTOP_FREEBSD)
+ Generic_getFileDescriptors_sysctl_internal(
+ "kern.maxfiles", "kern.openfiles", 0, 0, used, max);
+#elif defined(HTOP_NETBSD)
+ Generic_getFileDescriptors_sysctl_internal(
+ "kern.maxfiles", NULL, 0, sizeof(struct kinfo_file), used, max);
+#else
+#error Unknown platform: Please implement proper way to query open/max file information
+#endif
+}
diff --git a/generic/fdstat_sysctl.h b/generic/fdstat_sysctl.h
new file mode 100644
index 00000000..107fcabe
--- /dev/null
+++ b/generic/fdstat_sysctl.h
@@ -0,0 +1,13 @@
+#ifndef HEADER_fdstat_sysctl
+#define HEADER_fdstat_sysctl
+/*
+htop - generic/fdstat_sysctl.h
+(C) 2022-2023 htop dev team
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+
+void Generic_getFileDescriptors_sysctl(double* used, double* max);
+
+#endif

© 2014-2024 Faster IT GmbH | imprint | privacy policy