Make memory tests percentage-based 21/213821/1 submit/tizen/20190912.115203
authorMichal Bloch <m.bloch@samsung.com>
Thu, 12 Sep 2019 10:28:31 +0000 (12:28 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 12 Sep 2019 10:52:16 +0000 (12:52 +0200)
For target independence.

Change-Id: Ifa40c2b864d010beca4344c2cb2f166d6cc0909e

tests/config.json
tests/test-stability-mem.cpp

index ff472b4..c98b887 100644 (file)
@@ -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" : {
index f0e413d..930c9b6 100644 (file)
@@ -4,12 +4,18 @@
 #include <cstdlib>
 
 // C++
+#include <fstream>
+#include <iostream>
 #include <thread>
 #include <vector>
 
 // POSIX
 #include <sys/sysinfo.h>
 
+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 <std::pair <bool (*) (), std::string>> 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"
                }
        };