]> rtime.felk.cvut.cz Git - hercules2020/jailhouse-build.git/commitdiff
Update memgaurd test and add test for throttling correctness
authorMichal Sojka <michal.sojka@cvut.cz>
Mon, 5 Nov 2018 09:07:06 +0000 (10:07 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Mon, 5 Nov 2018 09:07:06 +0000 (10:07 +0100)
jailhouse
test/memguard-test.c

index ed386072a14157984f59fac37aa0a2682799241a..92ab6d47421d39155980908ef0d6cf04cd0d7c3d 160000 (submodule)
--- a/jailhouse
+++ b/jailhouse
@@ -1 +1 @@
-Subproject commit ed386072a14157984f59fac37aa0a2682799241a
+Subproject commit 92ab6d47421d39155980908ef0d6cf04cd0d7c3d
index d983d16e4c0d2469ff867674f2bdd2ce4f214190..e8bd33ea8ce385d5fb3887b804ef1b920bdd6ce0 100644 (file)
@@ -1,5 +1,3 @@
-/* Run this as: for i in $(seq 0 5); do prem-test $i & done */
-
 #define _GNU_SOURCE         /* See feature_test_macros(7) */
 #include <err.h>
 #include <sched.h>
 #include <stdlib.h>
 #include <error.h>
 
-#ifndef SYS_prem_memguard_check
-#define SYS_prem_memguard_check 793
+#ifndef SYS_memguard
+#define SYS_memguard 793
 #endif
 
 /* Return value:
- * 0-31  - Total time
- * 32-55 - Sum of memory events
+ * 0-31  - Sum of memory events
+ * 32-55 - Total time
  * 61    - Memory budget overrun
  * 62    - Timeout
  * 63    - Error */
-#define MGRET_TIM_POS                  0
-#define MGRET_MEM_POS                  32
+#define MGRET_MEM_POS          0
+#define MGRET_TIM_POS          32
 #define MGRET_OVER_MEM_POS     61
 #define MGRET_OVER_TIM_POS     62
+#define MGRET_ERROR_POS                63
 
-#define MGRET_TIM_MASK         0xFFFFFFFFlu
-#define MGRET_MEM_MASK         0xFFFFFFlu
-#define MGRET_OVER_MEM_MASK    0x1lu
-#define MGRET_OVER_TIM_MASK    0x1lu
+#define MGRET_TIM_MASK         (0x00FFFFFFul << MGRET_TIM_POS)
+#define MGRET_MEM_MASK         (0xFFFFFFFFul << MGRET_MEM_POS)
+#define MGRET_OVER_MEM_MASK    (1ul << MGRET_OVER_MEM_POS)
+#define MGRET_OVER_TIM_MASK    (1ul << MGRET_OVER_TIM_POS)
+#define MGRET_ERROR_MASK       (1ul << MGRET_ERROR_POS)
 
-#define MAX_CORES           6
+#define MAX_CORES              6
 
 char memory[128*10204*1024];
 
@@ -51,8 +51,8 @@ struct mg_ret {
 static struct mg_ret mgret(uint64_t retval)
 {
        struct mg_ret mgr = {};
-       mgr.time = (retval >> MGRET_TIM_POS) & MGRET_TIM_MASK;
-       mgr.mem = (retval >> MGRET_MEM_POS) & MGRET_MEM_MASK;
+       mgr.time = (retval & MGRET_TIM_MASK) >> MGRET_TIM_POS;
+       mgr.mem = (retval & MGRET_MEM_MASK) >> MGRET_MEM_POS;
        mgr.time_ovf = (retval >> MGRET_OVER_TIM_POS) & 1;
        mgr.mem_ovf = (retval >> MGRET_OVER_MEM_POS) & 1;
        return mgr;
@@ -112,12 +112,19 @@ void read_memory_rnd(long lines)
        (1                                                                     \
         << 1) /* Mask (disable) low priority interrupts until next memguard call */
 
-long prem_memguard_check(unsigned long timeout, unsigned long memory_budget,
-                        unsigned long flags)
+long memguard(unsigned long timeout, unsigned long memory_budget,
+             unsigned long flags)
 {
-       return syscall(SYS_prem_memguard_check, timeout, memory_budget, flags);
+       return syscall(SYS_memguard, timeout, memory_budget, flags);
 }
 
+void wvtest_pass(bool cond, const char* file, int line, const char* str)
+{
+       printf("! %s:%d  %s %s\n", file, line, str, cond ? "ok" : "FAILURE");
+}
+
+#define WVPASS(cond) wvtest_pass(cond, __FILE__, __LINE__, #cond)
+
 static void print_test_info(uint64_t timeout_us, uint64_t mem_budget, uint64_t flags,
                            int64_t retval, const char *code)
 {
@@ -133,23 +140,17 @@ static void print_test_info(uint64_t timeout_us, uint64_t mem_budget, uint64_t f
               r.time, r.time_ovf ? '!' : ' ',
               r.mem, r.mem_ovf ? '!' : ' ',
               __FILE__);
+       WVPASS((retval & MGRET_ERROR_MASK) == 0);
 }
 
-void wvtest_pass(bool cond, const char* file, int line, const char* str)
-{
-       printf("! %s:%d  %s %s\n", file, line, str, cond ? "ok" : "FAILURE");
-}
-
-#define WVPASS(cond) wvtest_pass(cond, __FILE__, __LINE__, #cond)
-
-#define MGTEST(timeout_us, mem_budget, flags, code)                            \
-       ({                                                                     \
-               long retval;                                                   \
-               retval = prem_memguard_check(timeout_us, mem_budget, flags);   \
-               code;                                                          \
-               retval = prem_memguard_check(0, 0, 0);                         \
+#define MGTEST(timeout_us, mem_budget, flags, code)                    \
+       ({                                                              \
+               long retval;                                            \
+               retval = memguard(timeout_us, mem_budget, flags);       \
+               code;                                                   \
+               retval = memguard(0, 0, 0);                             \
                print_test_info(timeout_us, mem_budget, flags, retval, #code); \
-               mgret(retval);                                                 \
+               mgret(retval);                                          \
        })
 
 void *test_thread(void *ptr)
@@ -157,7 +158,8 @@ void *test_thread(void *ptr)
        cpu_set_t set;
        int cpu = (intptr_t)ptr;
 
-       /* Ensure that memory phase starts and ends on the same CPU */
+       /* Ensure that our test thread does not migrate to another CPU
+        * during memguarding */
        CPU_ZERO(&set);
        CPU_SET(cpu, &set);
        if (sched_setaffinity(0, sizeof(set), &set) < 0)
@@ -166,9 +168,13 @@ void *test_thread(void *ptr)
        pthread_barrier_wait(&barrier);
 
        struct mg_ret r;
+
        for (uint64_t flags = 0; flags < 4; flags++) {
                compute_kernel(1); /* warm up */
 
+               r = MGTEST(500, 10000, flags, compute_kernel(17*1000*1000));
+               continue;
+
                ///////////////////////////////////////////////////////
                r = MGTEST(5000, 10000, flags, compute_kernel(1000));
                WVPASS(!r.time_ovf);
@@ -189,27 +195,27 @@ void *test_thread(void *ptr)
                ///////////////////////////////////////////////////////
                r = MGTEST(100000, 500000, flags, read_memory(100000));
                WVPASS(!r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
                r = MGTEST(100000, 50000,  flags, read_memory(100000));
                WVPASS(r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
                r = MGTEST(100000, 500000, flags, read_memory_rnd(100000));
                WVPASS(!r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
                r = MGTEST(100000, 50000,  flags, read_memory_rnd(100000));
                WVPASS(r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
                r = MGTEST(100000, 500000, flags, write_memory(100000));
                WVPASS(!r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
                r = MGTEST(100000, 50000,  flags, write_memory(100000));
                WVPASS(r.mem_ovf);
-               WVPASS(r.mem >= 100000);
+               WVPASS(r.mem >= 90000);
 
 
                ///////////////////////////////////////////////////////
@@ -240,6 +246,15 @@ void *test_thread(void *ptr)
                printf("\n");
        }
 
+       /* Throttling tests */
+       struct mg_ret r1, r2, r3;
+       r1 = MGTEST(10000, 9000, MGF_PERIODIC | MGF_MASK_INT, read_memory(100000));
+       WVPASS(r1.time > 100*1000);
+       r2 = MGTEST(10000, 3000, MGF_PERIODIC | MGF_MASK_INT, read_memory(100000));
+       WVPASS(r2.time > 2 * r1.time);
+       r3 = MGTEST(10000, 1000, MGF_PERIODIC | MGF_MASK_INT, read_memory(100000));
+       WVPASS(r3.time > 2 * r2.time);
+
        return NULL;
 }