mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Better error messages for memory & swap
This commit is contained in:
@@ -5,6 +5,9 @@ void ffDetectMemoryImpl(FFMemoryResult* memory);
|
||||
|
||||
static void calculatePercentage(FFMemoryStorage* storage)
|
||||
{
|
||||
if(storage->error.length == 0)
|
||||
return;
|
||||
|
||||
if(storage->bytesTotal == 0)
|
||||
storage->percentage = 0;
|
||||
else
|
||||
@@ -14,6 +17,9 @@ static void calculatePercentage(FFMemoryStorage* storage)
|
||||
const FFMemoryResult* ffDetectMemory()
|
||||
{
|
||||
FF_DETECTION_INTERNAL_GUARD(FFMemoryResult,
|
||||
ffStrbufInitA(&result.ram.error, 0);
|
||||
ffStrbufInitA(&result.swap.error, 0);
|
||||
|
||||
ffDetectMemoryImpl(&result);
|
||||
calculatePercentage(&result.ram);
|
||||
calculatePercentage(&result.swap);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
typedef struct FFMemoryStorage
|
||||
{
|
||||
FFstrbuf error;
|
||||
uint64_t bytesUsed;
|
||||
uint64_t bytesTotal;
|
||||
uint8_t percentage;
|
||||
|
||||
@@ -4,27 +4,49 @@
|
||||
#include <string.h>
|
||||
#include <mach/mach.h>
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
static void detectRam(FFMemoryStorage* ram)
|
||||
{
|
||||
memset(memory, 0, sizeof(FFMemoryResult));
|
||||
|
||||
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0);
|
||||
if(ram->bytesTotal == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.memsize");
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t pagesize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
|
||||
if(pagesize == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
|
||||
return;
|
||||
}
|
||||
|
||||
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
|
||||
vm_statistics_data_t vmstat;
|
||||
if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) (&vmstat), &count) != KERN_SUCCESS)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read vm statistics");
|
||||
return;
|
||||
}
|
||||
|
||||
memory->ram.bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
|
||||
}
|
||||
|
||||
static void detectSwap(FFMemoryStorage* swap)
|
||||
{
|
||||
struct xsw_usage swap;
|
||||
size_t size = sizeof(swap);
|
||||
if (sysctlbyname("vm.swapusage", &swap, &size, 0, 0) == -1)
|
||||
if(sysctlbyname("vm.swapusage", &swap, &size, 0, 0) != 0)
|
||||
{
|
||||
ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage");
|
||||
return;
|
||||
}
|
||||
|
||||
memory->swap.bytesTotal = swap.xsu_total;
|
||||
memory->swap.bytesUsed = swap.xsu_used;
|
||||
}
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
{
|
||||
detectRam(&memory->ram);
|
||||
detectSwap(&memory->swap);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,34 @@
|
||||
#include "memory.h"
|
||||
#include "common/sysctl.h"
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
static void detectRam(FFMemoryStorage* ram)
|
||||
{
|
||||
uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
|
||||
if(pageSize == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
|
||||
return;
|
||||
}
|
||||
|
||||
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize;
|
||||
|
||||
if(ram->bytesTotal == 0)
|
||||
{
|
||||
ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count");
|
||||
return;
|
||||
}
|
||||
|
||||
memory->ram.bytesUsed = memory->ram.bytesTotal
|
||||
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize
|
||||
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize
|
||||
;
|
||||
|
||||
memory->swap.bytesTotal = 0;
|
||||
memory->swap.bytesUsed = 0;
|
||||
}
|
||||
|
||||
static void detectSwap(FFMemoryStorage* swap)
|
||||
{
|
||||
ffStrbufAppendS(&swap->error, "Not implemented");
|
||||
}
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
{
|
||||
detectRam(&memory->ram);
|
||||
detectSwap(&memory->swap);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
{
|
||||
memset(memory, 0, sizeof(FFMemoryResult));
|
||||
|
||||
FILE* meminfo = fopen("/proc/meminfo", "r");
|
||||
if(meminfo == NULL)
|
||||
{
|
||||
ffStrbufAppendS(&memory->ram.error, "Failed to open /proc/meminfo");
|
||||
ffStrbufAppendS(&memory->swap.error, "Failed to open /proc/meminfo");
|
||||
return;
|
||||
}
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
@@ -41,7 +43,10 @@ void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
fclose(meminfo);
|
||||
|
||||
memory->ram.bytesTotal = memTotal * (uint64_t) 1024;
|
||||
memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
|
||||
if(memory->ram.bytesTotal == 0)
|
||||
ffStrbufAppendS(&memory->ram.error, "Failed to read MemTotal");
|
||||
else
|
||||
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;
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
|
||||
{
|
||||
if(storage->bytesUsed == 0 && storage->bytesTotal == 0)
|
||||
if(storage->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, name, 0, moduleArgs, "Failed to detect memory");
|
||||
ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user