Swap: split code out from memory detection

1. Since swap module is disabled by default, swap detection is unnecessary in most cases.
2. On platform other than Linux, swap detection doesn't share code with memory.
3. Swap detection can be expensive. On Windows, it requires another WMI query which can lag heavily in some cases. Don't pay for you don't use.
This commit is contained in:
李通洲
2022-10-18 23:46:42 +08:00
parent 7bbd2aee94
commit 581c6f547d
15 changed files with 163 additions and 119 deletions
+15 -6
View File
@@ -2,12 +2,21 @@
#include "common/printing.h"
#include "common/parsing.h"
#include "detection/memory/memory.h"
#include "detection/swap/swap.h"
#define FF_MEMORY_MODULE_NAME "Memory"
#define FF_SWAP_MODULE_NAME "Swap"
#define FF_MEMORY_NUM_FORMAT_ARGS 3
static uint8_t calculatePercentage(const FFMemoryStorage* storage)
{
if(storage->error.length != 0 || storage->bytesTotal == 0)
return 0;
else
return (uint8_t) (((long double) storage->bytesUsed / (long double) storage->bytesTotal) * 100.0);
}
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
{
if(storage->error.length > 0)
@@ -24,20 +33,22 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr
ffStrbufInit(&totalPretty);
ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t percentage = calculatePercentage(storage);
if(moduleArgs->outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key);
if (storage->bytesTotal == 0)
puts("Disabled");
else
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, storage->percentage);
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, percentage);
}
else
{
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, &storage->percentage},
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
});
}
@@ -47,12 +58,10 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr
void ffPrintMemory(FFinstance* instance)
{
const FFMemoryResult* memory = ffDetectMemory();
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &memory->ram);
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, ffDetectMemory());
}
void ffPrintSwap(FFinstance* instance)
{
const FFMemoryResult* memory = ffDetectMemory();
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &memory->swap);
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, ffDetectSwap());
}