Swap: split module code from memory.c

This commit is contained in:
李通洲
2023-05-11 20:39:54 +08:00
parent aea0986928
commit e837e1e1d1
15 changed files with 140 additions and 121 deletions
+17 -45
View File
@@ -3,47 +3,41 @@
#include "common/parsing.h"
#include "common/bar.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)
void ffPrintMemory(FFinstance* instance)
{
if(storage->error.length != 0 || storage->bytesTotal == 0)
return 0;
else
return (uint8_t) (((long double) storage->bytesUsed / (long double) storage->bytesTotal) * 100.0);
}
FFMemoryResult storage;
const char* error = ffDetectMemory(&storage);
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
{
if(storage->error.length > 0)
if(error)
{
ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars);
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "%s", error);
return;
}
FFstrbuf usedPretty;
FF_STRBUF_AUTO_DESTROY usedPretty;
ffStrbufInit(&usedPretty);
ffParseSize(storage->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty);
FFstrbuf totalPretty;
FF_STRBUF_AUTO_DESTROY totalPretty;
ffStrbufInit(&totalPretty);
ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t percentage = calculatePercentage(storage);
uint8_t percentage = storage.bytesTotal == 0
? 0
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
if(moduleArgs->outputFormat.length == 0)
if(instance->config.memory.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key);
if (storage->bytesTotal == 0)
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key);
if (storage.bytesTotal == 0)
puts("Disabled");
else
{
FFstrbuf str;
FF_STRBUF_AUTO_DESTROY str;
ffStrbufInit(&str);
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
@@ -60,36 +54,14 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);
ffStrbufDestroy(&str);
}
}
else
{
ffPrintFormat(instance, name, 0, moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
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},
});
}
ffStrbufDestroy(&usedPretty);
ffStrbufDestroy(&totalPretty);
}
void ffPrintMemory(FFinstance* instance)
{
FFMemoryStorage result;
ffStrbufInit(&result.error);
ffDetectMemory(&result);
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &result);
ffStrbufDestroy(&result.error);
}
void ffPrintSwap(FFinstance* instance)
{
FFMemoryStorage result;
ffStrbufInit(&result.error);
ffDetectSwap(&result);
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &result);
ffStrbufDestroy(&result.error);
}