Skip to content

Commit a9cb178

Browse files
Better error messages for memory & swap
1 parent ec4bd5d commit a9cb178

File tree

6 files changed

+64
-13
lines changed

6 files changed

+64
-13
lines changed

src/detection/memory/memory.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ void ffDetectMemoryImpl(FFMemoryResult* memory);
55

66
static void calculatePercentage(FFMemoryStorage* storage)
77
{
8+
if(storage->error.length == 0)
9+
return;
10+
811
if(storage->bytesTotal == 0)
912
storage->percentage = 0;
1013
else
@@ -14,6 +17,9 @@ static void calculatePercentage(FFMemoryStorage* storage)
1417
const FFMemoryResult* ffDetectMemory()
1518
{
1619
FF_DETECTION_INTERNAL_GUARD(FFMemoryResult,
20+
ffStrbufInitA(&result.ram.error, 0);
21+
ffStrbufInitA(&result.swap.error, 0);
22+
1723
ffDetectMemoryImpl(&result);
1824
calculatePercentage(&result.ram);
1925
calculatePercentage(&result.swap);

src/detection/memory/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
typedef struct FFMemoryStorage
99
{
10+
FFstrbuf error;
1011
uint64_t bytesUsed;
1112
uint64_t bytesTotal;
1213
uint8_t percentage;

src/detection/memory/memory_apple.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,49 @@
44
#include <string.h>
55
#include <mach/mach.h>
66

7-
void ffDetectMemoryImpl(FFMemoryResult* memory)
7+
static void detectRam(FFMemoryStorage* ram)
88
{
9-
memset(memory, 0, sizeof(FFMemoryResult));
10-
119
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0);
10+
if(ram->bytesTotal == 0)
11+
{
12+
ffStrbufAppendS(&ram->error, "Failed to read hw.memsize");
13+
return;
14+
}
1215

1316
uint32_t pagesize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
1417
if(pagesize == 0)
18+
{
19+
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
1520
return;
21+
}
1622

1723
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
1824
vm_statistics_data_t vmstat;
1925
if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) (&vmstat), &count) != KERN_SUCCESS)
26+
{
27+
ffStrbufAppendS(&ram->error, "Failed to read vm statistics");
2028
return;
29+
}
2130

2231
memory->ram.bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
32+
}
2333

34+
static void detectSwap(FFMemoryStorage* swap)
35+
{
2436
struct xsw_usage swap;
2537
size_t size = sizeof(swap);
26-
if (sysctlbyname("vm.swapusage", &swap, &size, 0, 0) == -1)
38+
if(sysctlbyname("vm.swapusage", &swap, &size, 0, 0) != 0)
39+
{
40+
ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage");
2741
return;
42+
}
43+
2844
memory->swap.bytesTotal = swap.xsu_total;
2945
memory->swap.bytesUsed = swap.xsu_used;
3046
}
47+
48+
void ffDetectMemoryImpl(FFMemoryResult* memory)
49+
{
50+
detectRam(&memory->ram);
51+
detectSwap(&memory->swap);
52+
}

src/detection/memory/memory_bsd.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
#include "memory.h"
22
#include "common/sysctl.h"
33

4-
void ffDetectMemoryImpl(FFMemoryResult* memory)
4+
static void detectRam(FFMemoryStorage* ram)
55
{
66
uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0);
7+
if(pageSize == 0)
8+
{
9+
ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize");
10+
return;
11+
}
712

813
memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize;
9-
14+
if(ram->bytesTotal == 0)
15+
{
16+
ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count");
17+
return;
18+
}
19+
1020
memory->ram.bytesUsed = memory->ram.bytesTotal
1121
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize
1222
- (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize
1323
;
24+
}
1425

15-
memory->swap.bytesTotal = 0;
16-
memory->swap.bytesUsed = 0;
26+
static void detectSwap(FFMemoryStorage* swap)
27+
{
28+
ffStrbufAppendS(&swap->error, "Not implemented");
29+
}
30+
void ffDetectMemoryImpl(FFMemoryResult* memory)
31+
{
32+
detectRam(&memory->ram);
33+
detectSwap(&memory->swap);
1734
}

src/detection/memory/memory_linux.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
void ffDetectMemoryImpl(FFMemoryResult* memory)
77
{
8-
memset(memory, 0, sizeof(FFMemoryResult));
9-
108
FILE* meminfo = fopen("/proc/meminfo", "r");
119
if(meminfo == NULL)
10+
{
11+
ffStrbufAppendS(&memory->ram.error, "Failed to open /proc/meminfo");
12+
ffStrbufAppendS(&memory->swap.error, "Failed to open /proc/meminfo");
1213
return;
14+
}
1315

1416
char* line = NULL;
1517
size_t len = 0;
@@ -41,7 +43,10 @@ void ffDetectMemoryImpl(FFMemoryResult* memory)
4143
fclose(meminfo);
4244

4345
memory->ram.bytesTotal = memTotal * (uint64_t) 1024;
44-
memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
46+
if(memory->ram.bytesTotal == 0)
47+
ffStrbufAppendS(&memory->ram.error, "Failed to read MemTotal");
48+
else
49+
memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
4550

4651
memory->swap.bytesTotal = swapTotal * (uint64_t) 1024;
4752
memory->swap.bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024;

src/modules/memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
1212
{
13-
if(storage->bytesUsed == 0 && storage->bytesTotal == 0)
13+
if(storage->error.length > 0)
1414
{
15-
ffPrintError(instance, name, 0, moduleArgs, "Failed to detect memory");
15+
ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars);
1616
return;
1717
}
1818

0 commit comments

Comments
 (0)