]> rtime.felk.cvut.cz Git - hercules2020/jailhouse-build.git/blobdiff - test/memguard-test.c
Test memguard call return value.
[hercules2020/jailhouse-build.git] / test / memguard-test.c
index d24e43d652731b2caa2f3527465d1e603b5a7339..53cc8447a29bf10ccb6b4cad2cd8a8b682788b04 100644 (file)
@@ -8,31 +8,66 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
+#include <inttypes.h>
 
 #ifndef SYS_prem_memguard_check
 #define SYS_prem_memguard_check 793
 #endif
 
+/* Return value:
+ * 0-31  - Total time
+ * 32-55 - Sum of memory events
+ * 61    - Memory budget overrun
+ * 62    - Timeout
+ * 63    - Error */
+#define RET_TIM_POS                    0
+#define RET_MEM_POS                    32
+#define RET_OVER_MEM_POS       61
+#define RET_OVER_TIM_POS       62
+
+#define RET_TIM_MASK           0xFFFFFFFFlu
+#define RET_MEM_MASK           0xFFFFFFlu
+#define RET_OVER_MEM_MASK      0x1lu
+#define RET_OVER_TIM_MASK      0x1lu
+
 #define RANDOM_ARRAY_SIZE 16777216 // 16M * 8 = 128 MB array
 
 uint64_t random_values[RANDOM_ARRAY_SIZE];
 volatile uint64_t sum;
 
+
+
 enum prem_phase {
        PREM_COMPATIBLE = 0,
        PREM_MEMORY     = 1,
        PREM_COMPUTE    = 2,
 };
 
+struct test_case{
+       uint64_t budget_time;
+       uint64_t budget_memory;
+       uint8_t flags;
+       uint64_t measured_time;
+       uint64_t measured_memory;
+       uint8_t status;
+};
+
+
+
+static inline void decode_retval(uint64_t retval, uint64_t * timval, uint64_t * cntval){
+       *timval = (retval >> RET_TIM_POS) & RET_TIM_MASK;
+       *cntval = (retval >> RET_MEM_POS) & RET_MEM_MASK;
+}
+
 void compute_kernel(int time_ms){
     struct timespec ts;
     uint64_t current_us, end_us;
     clock_gettime(CLOCK_MONOTONIC, &ts);
     end_us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000 + time_ms * 1000;
-    do{
-        clock_gettime(CLOCK_MONOTONIC, &ts);
-        current_us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-    }while(current_us < end_us);
+    do {
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       current_us = ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+    } while (current_us < end_us);
 }
 
 long memory_kernel(long lines){
@@ -44,7 +79,7 @@ long memory_kernel(long lines){
 
 void random_memory_kernel(long lines) {
     uint32_t index;
-    
+
     for(long i=0; i<lines; i++) {
         index = (uint32_t)rand();
         index = index % RANDOM_ARRAY_SIZE;
@@ -52,69 +87,93 @@ void random_memory_kernel(long lines) {
     }
 }
 
-long prem_memguard_check(unsigned long phase,
+/* Memguard flags */
+#define MGF_PERIODIC  (1 << 0) /* Chooses between periodic or one-shot budget replenishment */
+#define MGF_RET_STATS (1 << 1) /* Return statistics since the last call (cache misses, time, ...?) */
+#define MGF_MASK_INT  (1 << 2) /* Mask (disable) low priority interrupts until next memguard call */
+
+long prem_memguard_check(unsigned long timeout,
                    unsigned long memory_budget,
-                   unsigned long timeout)
+                   unsigned long flags)
 {
-       return syscall(SYS_prem_memguard_check, phase, memory_budget, timeout);
+    return syscall(SYS_prem_memguard_check, timeout, memory_budget, flags);
 }
 
 int main(int argc, char *argv[])
 {
     cpu_set_t set;
-       int cpu = 0;
+    int cpu = 0;
+    long retval;
+    uint64_t timval, cntval;
 
-       if (argc > 1)
-               cpu = atoi(argv[1]);
+    if (argc > 1)
+       cpu = atoi(argv[1]);
 
-       /* Ensure that memory phase starts and ends on the same CPU */
-       CPU_ZERO(&set);
-       CPU_SET(cpu, &set);
-       if (sched_setaffinity(getpid(), sizeof(set), &set) < 0)
-               err(1, "sched_setaffinity");
-       printf("Pinned to CPU %d\n", cpu);
+    /* Ensure that memory phase starts and ends on the same CPU */
+    CPU_ZERO(&set);
+    CPU_SET(cpu, &set);
+    if (sched_setaffinity(getpid(), sizeof(set), &set) < 0)
+       err(1, "sched_setaffinity");
+    printf("Pinned to CPU %d\n", cpu);
 
     printf("X1\n");
     fflush(stdout);
-       //prem_memguard_check(950000, 1933440000, 1);
-    prem_memguard_check(950000, 1933440000, 1);
+    //prem_memguard_check(950000, 1933440000, 1);
+    retval = prem_memguard_check(100000, 1000, MGF_PERIODIC | MGF_MASK_INT);
     compute_kernel(1000);
-    prem_memguard_check(0, 0, 1);
-    
+    //prem_memguard_check(0, 0, 1);
+
+       /* First result does not make sense */
+    decode_retval(retval, &timval, &cntval);
+       printf("tim: %lu cnt: %lu\n", timval, cntval);
+       
     printf("X2\n");
     fflush(stdout);
     //prem_memguard_check(1050000, 2136960000, 1);
-    prem_memguard_check(1050000, 2136960000, 1);
+    retval = prem_memguard_check(1050000, 2136960000, 0);
     compute_kernel(1000);
-    prem_memguard_check(0, 0, 1);
+    //prem_memguard_check(0, 0, 1);
     
+    decode_retval(retval, &timval, &cntval);
+       printf("tim: %lu cnt: %lu\n", timval, cntval);
+
     printf("X3\n");
     fflush(stdout);
     //prem_memguard_check(1000000, 500, 1);
-    prem_memguard_check(1000000, 5000000, 1);
+    retval = prem_memguard_check(100000, 500, MGF_PERIODIC);
     memory_kernel(100000000);
-    prem_memguard_check(0, 0, 1);
+    //prem_memguard_check(0, 0, 1);
     
+    decode_retval(retval, &timval, &cntval);
+       printf("tim: %lu cnt: %lu\n", timval, cntval);
+
     printf("X4\n");
     fflush(stdout);
     //prem_memguard_check(1000000, 1500, 1);
-    prem_memguard_check(1000000, 15000000, 1);
+    retval = prem_memguard_check(1000000, 15000000, 0);
     memory_kernel(100000000);
-    prem_memguard_check(0, 0, 1);
+    //prem_memguard_check(0, 0, 1);
     
+    decode_retval(retval, &timval, &cntval);
+       printf("tim: %lu cnt: %lu\n", timval, cntval);
+
     printf("X5\n");
     fflush(stdout);
     //prem_memguard_check(1000000, 10000, 1);
-    prem_memguard_check(1000000, 100000, 1);
+    retval = prem_memguard_check(1000000, 100000, 0);
     random_memory_kernel(100000000);
-    prem_memguard_check(0, 0, 1);
+    //prem_memguard_check(0, 0, 1);
     
+    decode_retval(retval, &timval, &cntval);
+       printf("tim: %lu cnt: %lu\n", timval, cntval);
+
     printf("X6\n");
     fflush(stdout);
     //prem_memguard_check(1000000, 10000000, 1);
-    prem_memguard_check(1000000, 100000000, 1);
+    retval = prem_memguard_check(1000000, 100000000, 0);
     random_memory_kernel(100000000);
-    prem_memguard_check(0, 0, 1);
 
-       return 0;
+    prem_memguard_check(0, 0, 0);
+
+    return 0;
 }