diff options
author | Nathan Scott <nathans@redhat.com> | 2021-03-03 10:59:28 +1100 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2021-03-04 13:40:59 +1100 |
commit | 61ef1134d97dbcf8b4ee069b36addf3c706ff55c (patch) | |
tree | 2cc478d92938b01f1d861350538742df6cea7e9e /generic | |
parent | 5b50ae3aa3a418f3f84ff2fdb172ab447753814f (diff) |
Move generic (shared) code into its own sub-directory
Code that is shared across some (but not all) platforms
is moved into a 'generic' home. Makefile.am cleanups to
match plus some minor alphabetic reordering/formatting.
As discussed in https://github.com/htop-dev/htop/pull/553
Diffstat (limited to 'generic')
-rw-r--r-- | generic/hostname.c | 16 | ||||
-rw-r--r-- | generic/hostname.h | 14 | ||||
-rw-r--r-- | generic/openzfs_sysctl.c | 98 | ||||
-rw-r--r-- | generic/openzfs_sysctl.h | 16 | ||||
-rw-r--r-- | generic/uname.c | 93 | ||||
-rw-r--r-- | generic/uname.h | 12 |
6 files changed, 249 insertions, 0 deletions
diff --git a/generic/hostname.c b/generic/hostname.c new file mode 100644 index 00000000..78404e43 --- /dev/null +++ b/generic/hostname.c @@ -0,0 +1,16 @@ +/* +htop - generic/hostname.c +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ +#include "config.h" // IWYU pragma: keep + +#include "generic/hostname.h" + +#include <unistd.h> + + +void Generic_hostname(char* buffer, size_t size) { + gethostname(buffer, size - 1); +} diff --git a/generic/hostname.h b/generic/hostname.h new file mode 100644 index 00000000..c6b941e8 --- /dev/null +++ b/generic/hostname.h @@ -0,0 +1,14 @@ +#ifndef HEADER_hostname +#define HEADER_hostname +/* +htop - generic/hostname.h +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include <stddef.h> + +void Generic_hostname(char* buffer, size_t size); + +#endif diff --git a/generic/openzfs_sysctl.c b/generic/openzfs_sysctl.c new file mode 100644 index 00000000..c7d79bcf --- /dev/null +++ b/generic/openzfs_sysctl.c @@ -0,0 +1,98 @@ +/* +htop - generic/openzfs_sysctl.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "generic/openzfs_sysctl.h" + +#include <stdlib.h> +#include <sys/types.h> // IWYU pragma: keep +#include <sys/sysctl.h> // needs <sys/types.h> for u_int with gcc + +#include "zfs/ZfsArcStats.h" + + +static int MIB_kstat_zfs_misc_arcstats_size[5]; +static int MIB_kstat_zfs_misc_arcstats_c_max[5]; +static int MIB_kstat_zfs_misc_arcstats_mfu_size[5]; +static int MIB_kstat_zfs_misc_arcstats_mru_size[5]; +static int MIB_kstat_zfs_misc_arcstats_anon_size[5]; +static int MIB_kstat_zfs_misc_arcstats_hdr_size[5]; +static int MIB_kstat_zfs_misc_arcstats_other_size[5]; +static int MIB_kstat_zfs_misc_arcstats_compressed_size[5]; +static int MIB_kstat_zfs_misc_arcstats_uncompressed_size[5]; + +void openzfs_sysctl_init(ZfsArcStats* stats) { + size_t len; + unsigned long long int arcSize; + + len = sizeof(arcSize); + if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arcSize, &len, NULL, 0) == 0 && arcSize != 0) { + stats->enabled = 1; + + len = 5; + sysctlnametomib("kstat.zfs.misc.arcstats.size", MIB_kstat_zfs_misc_arcstats_size, &len); + + sysctlnametomib("kstat.zfs.misc.arcstats.c_max", MIB_kstat_zfs_misc_arcstats_c_max, &len); + sysctlnametomib("kstat.zfs.misc.arcstats.mfu_size", MIB_kstat_zfs_misc_arcstats_mfu_size, &len); + sysctlnametomib("kstat.zfs.misc.arcstats.mru_size", MIB_kstat_zfs_misc_arcstats_mru_size, &len); + sysctlnametomib("kstat.zfs.misc.arcstats.anon_size", MIB_kstat_zfs_misc_arcstats_anon_size, &len); + sysctlnametomib("kstat.zfs.misc.arcstats.hdr_size", MIB_kstat_zfs_misc_arcstats_hdr_size, &len); + sysctlnametomib("kstat.zfs.misc.arcstats.other_size", MIB_kstat_zfs_misc_arcstats_other_size, &len); + + if (sysctlnametomib("kstat.zfs.misc.arcstats.compressed_size", MIB_kstat_zfs_misc_arcstats_compressed_size, &len) == 0) { + stats->isCompressed = 1; + sysctlnametomib("kstat.zfs.misc.arcstats.uncompressed_size", MIB_kstat_zfs_misc_arcstats_uncompressed_size, &len); + } else { + stats->isCompressed = 0; + } + } else { + stats->enabled = 0; + } +} + +void openzfs_sysctl_updateArcStats(ZfsArcStats* stats) { + size_t len; + + if (stats->enabled) { + len = sizeof(stats->size); + sysctl(MIB_kstat_zfs_misc_arcstats_size, 5, &(stats->size), &len, NULL, 0); + stats->size /= 1024; + + len = sizeof(stats->max); + sysctl(MIB_kstat_zfs_misc_arcstats_c_max, 5, &(stats->max), &len, NULL, 0); + stats->max /= 1024; + + len = sizeof(stats->MFU); + sysctl(MIB_kstat_zfs_misc_arcstats_mfu_size, 5, &(stats->MFU), &len, NULL, 0); + stats->MFU /= 1024; + + len = sizeof(stats->MRU); + sysctl(MIB_kstat_zfs_misc_arcstats_mru_size, 5, &(stats->MRU), &len, NULL, 0); + stats->MRU /= 1024; + + len = sizeof(stats->anon); + sysctl(MIB_kstat_zfs_misc_arcstats_anon_size, 5, &(stats->anon), &len, NULL, 0); + stats->anon /= 1024; + + len = sizeof(stats->header); + sysctl(MIB_kstat_zfs_misc_arcstats_hdr_size, 5, &(stats->header), &len, NULL, 0); + stats->header /= 1024; + + len = sizeof(stats->other); + sysctl(MIB_kstat_zfs_misc_arcstats_other_size, 5, &(stats->other), &len, NULL, 0); + stats->other /= 1024; + + if (stats->isCompressed) { + len = sizeof(stats->compressed); + sysctl(MIB_kstat_zfs_misc_arcstats_compressed_size, 5, &(stats->compressed), &len, NULL, 0); + stats->compressed /= 1024; + + len = sizeof(stats->uncompressed); + sysctl(MIB_kstat_zfs_misc_arcstats_uncompressed_size, 5, &(stats->uncompressed), &len, NULL, 0); + stats->uncompressed /= 1024; + } + } +} diff --git a/generic/openzfs_sysctl.h b/generic/openzfs_sysctl.h new file mode 100644 index 00000000..4cf5485e --- /dev/null +++ b/generic/openzfs_sysctl.h @@ -0,0 +1,16 @@ +#ifndef HEADER_openzfs_sysctl +#define HEADER_openzfs_sysctl +/* +htop - generic/openzfs_sysctl.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "zfs/ZfsArcStats.h" + +void openzfs_sysctl_init(ZfsArcStats* stats); + +void openzfs_sysctl_updateArcStats(ZfsArcStats* stats); + +#endif diff --git a/generic/uname.c b/generic/uname.c new file mode 100644 index 00000000..5b4fb2cf --- /dev/null +++ b/generic/uname.c @@ -0,0 +1,93 @@ +/* +htop - generic/uname.c +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ +#include "config.h" // IWYU pragma: keep + +#include "generic/uname.h" + +#include <stdio.h> +#ifdef HAVE_SYS_UTSNAME_H +#include <sys/utsname.h> +#endif + +#include "XUtils.h" + + +#ifndef OSRELEASEFILE +#define OSRELEASEFILE "/etc/os-release" +#endif + +static void parseOSRelease(char* buffer, size_t bufferLen) { + FILE* stream = fopen(OSRELEASEFILE, "r"); + if (!stream) { + xSnprintf(buffer, bufferLen, "No OS Release"); + return; + } + + char name[64] = {'\0'}; + char version[64] = {'\0'}; + char lineBuffer[256]; + while (fgets(lineBuffer, sizeof(lineBuffer), stream)) { + if (String_startsWith(lineBuffer, "PRETTY_NAME=\"")) { + const char* start = lineBuffer + strlen("PRETTY_NAME=\""); + const char* stop = strrchr(lineBuffer, '"'); + if (!stop || stop <= start) + continue; + String_safeStrncpy(buffer, start, MINIMUM(bufferLen, (size_t)(stop - start + 1))); + fclose(stream); + return; + } + if (String_startsWith(lineBuffer, "NAME=\"")) { + const char* start = lineBuffer + strlen("NAME=\""); + const char* stop = strrchr(lineBuffer, '"'); + if (!stop || stop <= start) + continue; + String_safeStrncpy(name, start, MINIMUM(sizeof(name), (size_t)(stop - start + 1))); + continue; + } + if (String_startsWith(lineBuffer, "VERSION=\"")) { + const char* start = lineBuffer + strlen("VERSION=\""); + const char* stop = strrchr(lineBuffer, '"'); + if (!stop || stop <= start) + continue; + String_safeStrncpy(version, start, MINIMUM(sizeof(version), (size_t)(stop - start + 1))); + continue; + } + } + fclose(stream); + + snprintf(buffer, bufferLen, "%s%s%s", name[0] ? name : "", name[0] && version[0] ? " " : "", version); +} + +char* Generic_uname(void) { + static char savedString[ + /* uname structure fields - manpages recommend sizeof */ + sizeof(uname_info.sysname) + + sizeof(uname_info.release) + + sizeof(uname_info.machine) + + 16/*markup*/ + + 128/*distro*/] = {'\0'}; + static bool loaded_data = false; + + if (!loaded_data) { + int uname_result = uname(&uname_info); + + char distro[128]; + parseOSRelease(distro, sizeof(distro)); + + if (uname_result == 0) { + size_t written = xSnprintf(savedString, sizeof(savedString), "%s %s [%s]", uname_info.sysname, uname_info.release, uname_info.machine); + if (!String_contains_i(savedString, distro) && sizeof(savedString) > written) + snprintf(savedString + written, sizeof(savedString) - written, " @ %s", distro); + } else { + snprintf(savedString, sizeof(savedString), "%s", distro); + } + + loaded_data = true; + } + + return savedString; +} diff --git a/generic/uname.h b/generic/uname.h new file mode 100644 index 00000000..820d0857 --- /dev/null +++ b/generic/uname.h @@ -0,0 +1,12 @@ +#ifndef HEADER_uname +#define HEADER_uname +/* +htop - generic/uname.h +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +char* Generic_uname(void); + +#endif |