From 6a44515cf28066089ae51c064ec04d3dc7a46f24 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Mon, 5 Sep 2022 10:57:09 +0200 Subject: [PATCH] Separate memory detection and printing logic --- CMakeLists.txt | 4 +++ src/detection/memory/memory.c | 16 ++++++++++ src/detection/memory/memory.h | 17 ++++++++++ src/detection/memory/memory_apple.c | 7 +++++ src/detection/memory/memory_linux.c | 41 ++++++++++++++++++++++++ src/modules/memory.c | 49 +++++------------------------ 6 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 src/detection/memory/memory.c create mode 100644 src/detection/memory/memory.h create mode 100644 src/detection/memory/memory_apple.c create mode 100644 src/detection/memory/memory_linux.c diff --git a/CMakeLists.txt b/CMakeLists.txt index dffa3f069..f23be3551 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,6 +167,7 @@ set(LIBFASTFETCH_SRC src/detection/host/host.c src/detection/os/os.c src/detection/cpu/cpu.c + src/detection/memory/memory.c src/detection/displayserver/displayServer.c src/detection/displayserver/wayland.c src/detection/displayserver/xcb.c @@ -218,18 +219,21 @@ if(APPLE) src/detection/host/host_apple.c src/detection/os/os_apple.c src/detection/cpu/cpu_apple.c + src/detection/memory/memory_apple.c ) elseif(ANDROID) list(APPEND LIBFASTFETCH_SRC src/detection/host/host_android.c src/detection/os/os_android.c src/detection/cpu/cpu_linux.c + src/detection/memory/memory_linux.c ) elseif(UNIX) list(APPEND LIBFASTFETCH_SRC src/detection/host/host_linux.c src/detection/os/os_linux.c src/detection/cpu/cpu_linux.c + src/detection/memory/memory_linux.c ) else() message(FATAL_ERROR "Unsupported platform") diff --git a/src/detection/memory/memory.c b/src/detection/memory/memory.c new file mode 100644 index 000000000..4d9f8374f --- /dev/null +++ b/src/detection/memory/memory.c @@ -0,0 +1,16 @@ +#include "memory.h" +#include "detection/internal.h" + +void ffDetectMemoryImpl(FFMemoryResult* memory); + +const FFMemoryResult* ffDetectMemory() +{ + FF_DETECTION_INTERNAL_GUARD(FFMemoryResult, + ffDetectMemoryImpl(&result); + + if(result.bytesTotal > 0) + result.percentage = (uint8_t) ((result.bytesUsed / (long double) result.bytesTotal) * 100.0); + else + result.percentage = 0; + ); +} diff --git a/src/detection/memory/memory.h b/src/detection/memory/memory.h new file mode 100644 index 000000000..2ab0a3808 --- /dev/null +++ b/src/detection/memory/memory.h @@ -0,0 +1,17 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_memory_memory +#define FF_INCLUDED_detection_memory_memory + +#include "fastfetch.h" + +typedef struct FFMemoryResult +{ + uint64_t bytesUsed; + uint64_t bytesTotal; + uint8_t percentage; +} FFMemoryResult; + +const FFMemoryResult* ffDetectMemory(); + +#endif diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c new file mode 100644 index 000000000..5dc287005 --- /dev/null +++ b/src/detection/memory/memory_apple.c @@ -0,0 +1,7 @@ +#include "memory.h" + +void ffDetectMemoryImpl(FFMemoryResult* memory) +{ + memory->bytesUsed = 0; + memory->bytesTotal = 0; +} diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c new file mode 100644 index 000000000..869a5f61a --- /dev/null +++ b/src/detection/memory/memory_linux.c @@ -0,0 +1,41 @@ +#include "memory.h" + +#include + +void ffDetectMemoryImpl(FFMemoryResult* memory) +{ + memory->bytesUsed = 0; + memory->bytesTotal = 0; + + FILE* meminfo = fopen("/proc/meminfo", "r"); + if(meminfo == NULL) + return; + + char* line = NULL; + size_t len = 0; + + uint32_t memTotal = 0, + shmem = 0, + memFree = 0, + buffers = 0, + cached = 0, + sReclaimable = 0; + + while (getline(&line, &len, meminfo) != EOF) + { + sscanf(line, "MemTotal: %u", &memTotal); + sscanf(line, "Shmem: %u", &shmem); + sscanf(line, "MemFree: %u", &memFree); + sscanf(line, "Buffers: %u", &buffers); + sscanf(line, "Cached: %u", &cached); + sscanf(line, "SReclaimable: %u", &sReclaimable); + } + + if(line != NULL) + free(line); + + fclose(meminfo); + + memory->bytesTotal = memTotal * (uint64_t) 1024; + memory->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; +} diff --git a/src/modules/memory.c b/src/modules/memory.c index 0da2529a7..c549a58ac 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -1,73 +1,40 @@ #include "fastfetch.h" #include "common/printing.h" #include "common/parsing.h" - -#include +#include "detection/memory/memory.h" #define FF_MEMORY_MODULE_NAME "Memory" #define FF_MEMORY_NUM_FORMAT_ARGS 3 -// Impl inspired by: https://github.com/sam-barr/paleofetch/blob/b7c58a52c0de39b53c9b5f417889a5886d324bfa/paleofetch.c#L544 void ffPrintMemory(FFinstance* instance) { - FILE* meminfo = fopen("/proc/meminfo", "r"); - if(meminfo == NULL) { - ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "fopen(\"""/proc/meminfo\", \"r\") == NULL"); - return; - } + const FFMemoryResult* memory = ffDetectMemory(); - char* line = NULL; - size_t len = 0; - - uint32_t total = 0, - shared = 0, - memfree = 0, - buffers = 0, - cached = 0, - reclaimable = 0; - - while (getline(&line, &len, meminfo) != -1) { - sscanf(line, "MemTotal: %u", &total); - sscanf(line, "Shmem: %u", &shared); - sscanf(line, "MemFree: %u", &memfree); - sscanf(line, "Buffers: %u", &buffers); - sscanf(line, "Cached: %u", &cached); - sscanf(line, "SReclaimable: %u", &reclaimable); - } - - if(line != NULL) - free(line); - - fclose(meminfo); - - uint32_t used = total + shared - memfree - buffers - cached - reclaimable; - uint8_t percentage = (uint8_t) ((used / (double) total) * 100); - - if(used == 0 && total == 0) + if(memory->bytesUsed == 0 && memory->bytesTotal == 0) { - ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "/proc/meminfo could't be parsed"); + ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "Failed to detect memory"); return; } FFstrbuf usedPretty; ffStrbufInit(&usedPretty); - ffParseSize(used * (uint64_t) 1024, instance->config.binaryPrefixType, &usedPretty); + ffParseSize(memory->bytesUsed, instance->config.binaryPrefixType, &usedPretty); FFstrbuf totalPretty; ffStrbufInit(&totalPretty); - ffParseSize(total * (uint64_t) 1024, instance->config.binaryPrefixType, &totalPretty); + ffParseSize(memory->bytesTotal, instance->config.binaryPrefixType, &totalPretty); if(instance->config.memory.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key); - printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, percentage); + printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, memory->percentage); } else { ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, - {FF_FORMAT_ARG_TYPE_UINT8, &percentage}, + {FF_FORMAT_ARG_TYPE_UINT8, &memory->percentage}, }); }