From abfad7cc291ece28878bd00cdafd8b979666269b Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Thu, 12 Sep 2019 12:28:31 +0200 Subject: [PATCH] Make memory tests percentage-based For target independence. Change-Id: Ifa40c2b864d010beca4344c2cb2f166d6cc0909e --- tests/config.json | 6 +++--- tests/test-stability-mem.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/config.json b/tests/config.json index ff472b4..c98b887 100644 --- a/tests/config.json +++ b/tests/config.json @@ -20,9 +20,9 @@ "report" : 0, "sampling_rate": 1.0, "apply_to_children" : 0, - "mem_limit_avg" : 0.30, - "mem_limit_peak" : 0.60, - "comment_to_above" : "% of reference target (1 GB on rpi3)", + "mem_limit_avg" : 0.20, + "mem_limit_peak" : 0.40, + "comment_to_above" : "don't use values close to 1 (risks OOM killer)", "mem_limit_period" : 3 }, "test-stability-fd" : { diff --git a/tests/test-stability-mem.cpp b/tests/test-stability-mem.cpp index f0e413d..930c9b6 100644 --- a/tests/test-stability-mem.cpp +++ b/tests/test-stability-mem.cpp @@ -4,12 +4,18 @@ #include // C++ +#include +#include #include #include // POSIX #include +static size_t total_ram_kb; // /proc/meminfo returns in kB +static size_t total_ram_b () { return total_ram_kb * 1024; } +static size_t total_ram_mb () { return total_ram_kb / 1024; } + static void * do_mem (size_t to_alloc) { /* Allocate memory and pretend we're using it (so as to make it actually @@ -26,14 +32,27 @@ static void * do_mem (size_t to_alloc) return nullptr; } +static void get_total_ram () +{ + std::ifstream meminfo ("/proc/meminfo"); + + std::string entry_name; + meminfo >> entry_name >> total_ram_kb; + + if (entry_name != "MemTotal:") + throw std::runtime_error ("Unexpected format of /proc/meminfo"); + + std::cout << "Total RAM: " << total_ram_mb () << " MB" << std::endl; +} + unsigned long long operator "" _MB (unsigned long long n) { return 1024 * 1024 * n; } static void * mem_heavy_peak (void * arg) -{ return do_mem (750_MB); } +{ return do_mem (0.5f * total_ram_b ()); } static void * mem_heavy_avg (void * arg) -{ return do_mem (400_MB); } +{ return do_mem (0.3f * total_ram_b ()); } static void * mem_light (void * arg) { return do_mem (25_MB); } @@ -62,6 +81,8 @@ bool run_mem_test (thread_func_t * func, size_t test_time, dbus_signal_handler * int main (int argc, char ** argv) { + get_total_ram (); + const std::vector > test_cases { { []() { /* Do a reasonable amount of memory (re)allocation. @@ -72,13 +93,13 @@ int main (int argc, char ** argv) /* Allocate and keep lots of memory. * Expect a signal. */ return ! run_mem_test (mem_heavy_peak, TEST_TIME_PEAK, & handler_peak); - } , "heavy allocation (750 MB + overhead), expecting peak signal" + } , "heavy allocation (50% of total (" + std::to_string(size_t (0.5 * total_ram_mb ())) + " MB) + overhead), expecting peak signal" } , { []() { /* Allocate an amount of memory not high enough * to trigger peak, but keep it long enough to * trigger average. */ return ! run_mem_test (mem_heavy_avg, TEST_TIME_AVG, & handler_avg); - } , "medium allocation (400 MB + overhead), expecting avg signal" + } , "medium allocation (30% of total (" + std::to_string(size_t (0.3 * total_ram_mb ())) + " MB) + overhead), expecting avg signal" } }; -- 2.7.4