From be7f8f41179ca3fb6b27a7a82d2bf8aea671d369 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Thu, 8 Sep 2022 14:51:31 +0200 Subject: [PATCH] Swap module #225 --- README.md | 2 +- src/common/init.c | 1 + src/data/config_user.txt | 3 +++ src/data/modules.txt | 1 + src/detection/memory/memory.c | 15 ++++++++---- src/detection/memory/memory.h | 8 ++++++- src/detection/memory/memory_apple.c | 8 ++++--- src/detection/memory/memory_linux.c | 17 ++++++++++---- src/fastfetch.c | 16 +++++++++++++ src/fastfetch.h | 2 ++ src/flashfetch.c | 2 ++ src/modules/memory.c | 36 +++++++++++++++++++---------- 12 files changed, 84 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 137f86507..bfe235878 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ All categories not listed here should work without needing a specific implementa ##### Available Modules ``` -Title, Separator, OS, Host, Kernel, Uptime, Processes, Packages, Shell, Resolution, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Disk, Battery, Player, Media, Vulkan, OpenGL, OpenCL, LocalIP, PublicIP, DateTime, Date, Time, Locale, Colors, Break, Custom +Title, Separator, OS, Host, Kernel, Uptime, Processes, Packages, Shell, Resolution, DE, WM, WMTheme, Theme, Icons, Font, Cursor, Terminal, Terminal Font, CPU, CPUUsage, GPU, Memory, Swap, Disk, Battery, Player, Media, Vulkan, OpenGL, OpenCL, LocalIP, PublicIP, DateTime, Date, Time, Locale, Colors, Break, Custom ``` ##### Logos diff --git a/src/common/init.c b/src/common/init.c index 1ce13deea..3a769de11 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -177,6 +177,7 @@ static void defaultConfig(FFinstance* instance) initModuleArg(&instance->config.cpuUsage); initModuleArg(&instance->config.gpu); initModuleArg(&instance->config.memory); + initModuleArg(&instance->config.swap); initModuleArg(&instance->config.disk); initModuleArg(&instance->config.battery); initModuleArg(&instance->config.locale); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index a5bc431aa..23ff7e688 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -206,6 +206,7 @@ #--cpu-usage-key CPU Usage #--gpu-key GPU {1} #--memory-key Memory +#--swap-key Swap #--disk-key Disk ({1}) #--battery-key Battery {1} #--locale-key Locale @@ -244,6 +245,7 @@ #--cpu-usage-format #--gpu-format #--memory-format +#--swap-format #--disk-format #--battery-format #--locale-format @@ -282,6 +284,7 @@ #--cpu-usage-error #--gpu-error #--memory-error +#--swap-error #--disk-error #--battery-error #--locale-error diff --git a/src/data/modules.txt b/src/data/modules.txt index 3c73bceb5..88528fcc2 100644 --- a/src/data/modules.txt +++ b/src/data/modules.txt @@ -27,6 +27,7 @@ PublicIP Resolution Separator Shell +Swap Terminal TerminalFont Theme diff --git a/src/detection/memory/memory.c b/src/detection/memory/memory.c index 4d9f8374f..bc6981186 100644 --- a/src/detection/memory/memory.c +++ b/src/detection/memory/memory.c @@ -3,14 +3,19 @@ void ffDetectMemoryImpl(FFMemoryResult* memory); +static void calculatePercentage(FFMemoryStorage* storage) +{ + if(storage->bytesTotal == 0) + storage->percentage = 0; + else + storage->percentage = (uint8_t) ((storage->bytesUsed / (long double) storage->bytesTotal) * 100.0); +} + 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; + calculatePercentage(&result.ram); + calculatePercentage(&result.swap); ); } diff --git a/src/detection/memory/memory.h b/src/detection/memory/memory.h index 2ab0a3808..85beaf255 100644 --- a/src/detection/memory/memory.h +++ b/src/detection/memory/memory.h @@ -5,11 +5,17 @@ #include "fastfetch.h" -typedef struct FFMemoryResult +typedef struct FFMemoryStorage { uint64_t bytesUsed; uint64_t bytesTotal; uint8_t percentage; +} FFMemoryStorage; + +typedef struct FFMemoryResult +{ + FFMemoryStorage ram; + FFMemoryStorage swap; } FFMemoryResult; const FFMemoryResult* ffDetectMemory(); diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 6e8bf2682..306061313 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -1,13 +1,15 @@ #include "memory.h" #include "common/settings.h" + +#include #include void ffDetectMemoryImpl(FFMemoryResult* memory) { - memory->bytesTotal = (uint64_t) ffSettingsGetAppleInt64("hw.memsize", 0); + memset(memory, 0, sizeof(FFMemoryResult)); - memory->bytesUsed = 0; + memory->ram.bytesTotal = (uint64_t) ffSettingsGetAppleInt64("hw.memsize", 0); uint32_t pagesize = (uint32_t) ffSettingsGetAppleInt("hw.pagesize", 0); if(pagesize == 0) @@ -18,5 +20,5 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) (&vmstat), &count) != KERN_SUCCESS) return; - memory->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize; + memory->ram.bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize; } diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index 869a5f61a..e6cf87bb2 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -1,11 +1,11 @@ #include "memory.h" #include +#include void ffDetectMemoryImpl(FFMemoryResult* memory) { - memory->bytesUsed = 0; - memory->bytesTotal = 0; + memset(memory, 0, sizeof(FFMemoryResult)); FILE* meminfo = fopen("/proc/meminfo", "r"); if(meminfo == NULL) @@ -19,7 +19,9 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) memFree = 0, buffers = 0, cached = 0, - sReclaimable = 0; + sReclaimable = 0, + swapTotal = 0, + swapFree = 0; while (getline(&line, &len, meminfo) != EOF) { @@ -29,6 +31,8 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) sscanf(line, "Buffers: %u", &buffers); sscanf(line, "Cached: %u", &cached); sscanf(line, "SReclaimable: %u", &sReclaimable); + sscanf(line, "SwapTotal: %u", &swapTotal); + sscanf(line, "SwapFree: %u", &swapFree); } if(line != NULL) @@ -36,6 +40,9 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) fclose(meminfo); - memory->bytesTotal = memTotal * (uint64_t) 1024; - memory->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; + memory->ram.bytesTotal = memTotal * (uint64_t) 1024; + memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; + + memory->swap.bytesTotal = swapTotal * (uint64_t) 1024; + memory->swap.bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024; } diff --git a/src/fastfetch.c b/src/fastfetch.c index 81206c294..4e74c9abd 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -282,6 +282,14 @@ static inline void printCommandHelp(const char* command) "Percentage used" ); } + else if(strcasecmp(command, "swap-format") == 0) + { + constructAndPrintCommandHelpFormat("swap", "{} / {} ({}%)", 3, + "Used size", + "Total size", + "Percentage used" + ); + } else if(strcasecmp(command, "disk-format") == 0) { constructAndPrintCommandHelpFormat("disk", "{}GiB / {}GiB ({4}%)", 4, @@ -1082,6 +1090,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.memory.outputFormat); else if(strcasecmp(key, "--memory-error") == 0) optionParseString(key, value, &instance->config.memory.errorFormat); + else if(strcasecmp(key, "--swap-key") == 0) + optionParseString(key, value, &instance->config.swap.key); + else if(strcasecmp(key, "--swap-format") == 0) + optionParseString(key, value, &instance->config.swap.outputFormat); + else if(strcasecmp(key, "--swap-error") == 0) + optionParseString(key, value, &instance->config.swap.errorFormat); else if(strcasecmp(key, "--disk-key") == 0) optionParseString(key, value, &instance->config.disk.key); else if(strcasecmp(key, "--disk-format") == 0) @@ -1351,6 +1365,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintGPU(instance); else if(strcasecmp(line, "memory") == 0) ffPrintMemory(instance); + else if(strcasecmp(line, "swap") == 0) + ffPrintSwap(instance); else if(strcasecmp(line, "disk") == 0) ffPrintDisk(instance); else if(strcasecmp(line, "battery") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index e37a4187a..d47a689e6 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -113,6 +113,7 @@ typedef struct FFconfig FFModuleArgs cpuUsage; FFModuleArgs gpu; FFModuleArgs memory; + FFModuleArgs swap; FFModuleArgs disk; FFModuleArgs battery; FFModuleArgs locale; @@ -248,6 +249,7 @@ void ffPrintCPU(FFinstance* instance); void ffPrintCPUUsage(FFinstance* instance); void ffPrintGPU(FFinstance* instance); void ffPrintMemory(FFinstance* instance); +void ffPrintSwap(FFinstance* instance); //Also in modules/memory.c void ffPrintDisk(FFinstance* instance); void ffPrintBattery(FFinstance* instance); void ffPrintLocale(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index d08c59799..a57134526 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -37,6 +37,7 @@ int main(int argc, char** argv) //ffPrintCPUUsage(&instance); ffPrintGPU(&instance); ffPrintMemory(&instance); + //ffPrintSwap(&instance); ffPrintDisk(&instance); ffPrintBattery(&instance); //ffPrintPlayer(&instance); @@ -49,6 +50,7 @@ int main(int argc, char** argv) //ffPrintTime(&instance); //ffPrintVulkan(&instance); //ffPrintOpenGL(&instance); + //ffPrintOpenCL(&instance); ffPrintBreak(&instance); ffPrintColors(&instance); diff --git a/src/modules/memory.c b/src/modules/memory.c index c549a58ac..40fe380f4 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -4,40 +4,52 @@ #include "detection/memory/memory.h" #define FF_MEMORY_MODULE_NAME "Memory" +#define FF_SWAP_MODULE_NAME "Swap" + #define FF_MEMORY_NUM_FORMAT_ARGS 3 -void ffPrintMemory(FFinstance* instance) +static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage) { - const FFMemoryResult* memory = ffDetectMemory(); - - if(memory->bytesUsed == 0 && memory->bytesTotal == 0) + if(storage->bytesUsed == 0 && storage->bytesTotal == 0) { - ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "Failed to detect memory"); + ffPrintError(instance, name, 0, moduleArgs, "Failed to detect memory"); return; } FFstrbuf usedPretty; ffStrbufInit(&usedPretty); - ffParseSize(memory->bytesUsed, instance->config.binaryPrefixType, &usedPretty); + ffParseSize(storage->bytesUsed, instance->config.binaryPrefixType, &usedPretty); FFstrbuf totalPretty; ffStrbufInit(&totalPretty); - ffParseSize(memory->bytesTotal, instance->config.binaryPrefixType, &totalPretty); + ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty); - if(instance->config.memory.outputFormat.length == 0) + if(moduleArgs->outputFormat.length == 0) { - ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key); - printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, memory->percentage); + ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key); + printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, storage->percentage); } else { - ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){ + ffPrintFormat(instance, name, 0, moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, - {FF_FORMAT_ARG_TYPE_UINT8, &memory->percentage}, + {FF_FORMAT_ARG_TYPE_UINT8, &storage->percentage}, }); } ffStrbufDestroy(&usedPretty); ffStrbufDestroy(&totalPretty); } + +void ffPrintMemory(FFinstance* instance) +{ + const FFMemoryResult* memory = ffDetectMemory(); + printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &memory->ram); +} + +void ffPrintSwap(FFinstance* instance) +{ + const FFMemoryResult* memory = ffDetectMemory(); + printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &memory->swap); +}