Swap module

#225
This commit is contained in:
Linus Dierheimer
2022-09-08 14:51:31 +02:00
parent dd60e8aa75
commit be7f8f4117
12 changed files with 84 additions and 27 deletions
+1 -1
View File
@@ -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
+1
View File
@@ -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);
+3
View File
@@ -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
+1
View File
@@ -27,6 +27,7 @@ PublicIP
Resolution
Separator
Shell
Swap
Terminal
TerminalFont
Theme
+10 -5
View File
@@ -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);
);
}
+7 -1
View File
@@ -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();
+5 -3
View File
@@ -1,13 +1,15 @@
#include "memory.h"
#include "common/settings.h"
#include <string.h>
#include <mach/mach.h>
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;
}
+12 -5
View File
@@ -1,11 +1,11 @@
#include "memory.h"
#include <stdlib.h>
#include <string.h>
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;
}
+16
View File
@@ -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)
+2
View File
@@ -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);
+2
View File
@@ -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);
+24 -12
View File
@@ -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);
}