Memory: refactor, don't cache results

This commit is contained in:
李通洲
2022-12-28 15:55:31 +08:00
parent 0a83423731
commit 6ff67e87eb
16 changed files with 26 additions and 46 deletions
-2
View File
@@ -240,10 +240,8 @@ set(LIBFASTFETCH_SRC
src/detection/host/host.c
src/detection/locale/locale.c
src/detection/media/media.c
src/detection/memory/memory.c
src/detection/os/os.c
src/detection/packages/packages.c
src/detection/swap/swap.c
src/detection/terminalfont/terminalfont.c
src/detection/terminalshell/terminalshell.c
src/detection/title.c
-13
View File
@@ -1,13 +0,0 @@
#include "memory.h"
#include "detection/internal.h"
void ffDetectMemoryImpl(FFMemoryStorage* memory);
const FFMemoryStorage* ffDetectMemory()
{
FF_DETECTION_INTERNAL_GUARD(FFMemoryStorage,
ffStrbufInit(&result.error);
ffDetectMemoryImpl(&result);
);
}
+1 -1
View File
@@ -5,6 +5,6 @@
#include "detection/storage.h"
const FFMemoryStorage* ffDetectMemory();
void ffDetectMemory(FFMemoryStorage* result);
#endif
+1 -1
View File
@@ -4,7 +4,7 @@
#include <string.h>
#include <mach/mach.h>
void ffDetectMemoryImpl(FFMemoryStorage* ram)
void ffDetectMemory(FFMemoryStorage* ram)
{
ram->bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0);
if(ram->bytesTotal == 0)
+1 -1
View File
@@ -1,7 +1,7 @@
#include "memory.h"
#include "common/sysctl.h"
void ffDetectMemoryImpl(FFMemoryStorage* ram)
void ffDetectMemory(FFMemoryStorage* ram)
{
uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
if(pageSize == 0)
+6 -6
View File
@@ -3,12 +3,12 @@
#include <stdlib.h>
#include <string.h>
void ffDetectMemoryImpl(FFMemoryStorage* swap)
void ffDetectMemory(FFMemoryStorage* ram)
{
FILE* meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL)
{
ffStrbufAppendS(&swap->error, "Failed to open /proc/meminfo");
ffStrbufAppendS(&ram->error, "Failed to open /proc/meminfo");
return;
}
@@ -37,9 +37,9 @@ void ffDetectMemoryImpl(FFMemoryStorage* swap)
fclose(meminfo);
swap->bytesTotal = memTotal * (uint64_t) 1024;
if(swap->bytesTotal == 0)
ffStrbufAppendS(&swap->error, "Failed to read MemTotal");
ram->bytesTotal = memTotal * (uint64_t) 1024;
if(ram->bytesTotal == 0)
ffStrbufAppendS(&ram->error, "Failed to read MemTotal");
else
swap->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
}
+1 -1
View File
@@ -1,6 +1,6 @@
#include "memory.h"
void ffDetectMemoryImpl(FFMemoryStorage* ram)
void ffDetectMemory(FFMemoryStorage* ram)
{
MEMORYSTATUSEX statex = {
.dwLength = sizeof(statex),
-1
View File
@@ -10,7 +10,6 @@ typedef struct FFMemoryStorage
FFstrbuf error;
uint64_t bytesUsed;
uint64_t bytesTotal;
uint8_t percentage;
} FFMemoryStorage;
#endif
-13
View File
@@ -1,13 +0,0 @@
#include "swap.h"
#include "detection/internal.h"
void ffDetectSwapImpl(FFMemoryStorage* swap);
const FFMemoryStorage* ffDetectSwap()
{
FF_DETECTION_INTERNAL_GUARD(FFMemoryStorage,
ffStrbufInit(&result.error);
ffDetectSwapImpl(&result);
);
}
+1 -1
View File
@@ -5,6 +5,6 @@
#include "detection/storage.h"
const FFMemoryStorage* ffDetectSwap();
void ffDetectSwap(FFMemoryStorage* result);
#endif
+1 -1
View File
@@ -3,7 +3,7 @@
#include <mach/mach.h>
void ffDetectSwapImpl(FFMemoryStorage* swap)
void ffDetectSwap(FFMemoryStorage* swap)
{
struct xsw_usage xsw;
size_t size = sizeof(xsw);
+1 -1
View File
@@ -1,7 +1,7 @@
#include "swap.h"
#include "common/sysctl.h"
void ffDetectSwapImpl(FFMemoryStorage* swap)
void ffDetectSwap(FFMemoryStorage* swap)
{
swap->bytesTotal = (uint64_t)ffSysctlGetInt64("vm.swap_total", 0);
swap->bytesUsed = (uint64_t)ffSysctlGetInt64("vm.swap_reserved", 0);
+1 -1
View File
@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
void ffDetectSwapImpl(FFMemoryStorage* swap)
void ffDetectSwap(FFMemoryStorage* swap)
{
FILE* meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL)
+1 -1
View File
@@ -9,7 +9,7 @@ extern "C" {
#include <ntstatus.h>
extern "C"
void ffDetectSwapImpl(FFMemoryStorage* swap)
void ffDetectSwap(FFMemoryStorage* swap)
{
SYSTEM_INFO sysInfo;
GetNativeSystemInfo(&sysInfo);
+1
View File
@@ -25,6 +25,7 @@
static inline void ffUnused(int dummy, ...) { (void) dummy; }
#define FF_UNUSED(...) ffUnused(0, __VA_ARGS__);
#define FF_UNUSED_PARAM __attribute__ ((__unused__))
#define FASTFETCH_LOGO_MAX_COLORS 9 //two digits would make parsing much more complicated (index 1 - 9)
+10 -2
View File
@@ -76,10 +76,18 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr
void ffPrintMemory(FFinstance* instance)
{
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, ffDetectMemory());
FFMemoryStorage result;
ffStrbufInit(&result.error);
ffDetectMemory(&result);
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &result);
ffStrbufDestroy(&result.error);
}
void ffPrintSwap(FFinstance* instance)
{
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, ffDetectSwap());
FFMemoryStorage result;
ffStrbufInit(&result.error);
ffDetectSwap(&result);
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &result);
ffStrbufDestroy(&result.error);
}