Tidy up debugging code
authorNick Downing <nick@ndcode.org>
Sat, 16 Mar 2019 14:03:15 +0000 (01:03 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 16 Mar 2019 14:06:20 +0000 (01:06 +1100)
o.sh
process.c

diff --git a/o.sh b/o.sh
index 9871ee5..95e207f 100755 (executable)
--- a/o.sh
+++ b/o.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 echo "generate test script"
-./process_test_gen 16 224 1024 32 >process_test.txt
+./process_test_gen 16 224 65536 32 >process_test.txt
 
 echo "run test"
 ./process_test_run 16 64 4 192 12 16 <process_test.txt
index 120e598..530445b 100644 (file)
--- a/process.c
+++ b/process.c
@@ -1,6 +1,8 @@
 #include <stdio.h> // temporary
 #include <stdlib.h>
+#ifndef NDEBUG
 #include <string.h>
+#endif
 #include "core.h"
 #include "process.h"
 #include "rassert.h"
@@ -17,6 +19,7 @@ struct process *victim;
 struct pool_item victim_core_item, victim_swap_item;
 
 #if 1
+// won't work when compiled with NDEBUG
 static void check_invariants() {
   int avail =
     core_head.item.base -
@@ -27,18 +30,21 @@ static void check_invariants() {
     swap_head.spare -
     process_spare;
   if (victim) {
-    assert(victim_core_item.prev);
-    assert(victim_swap_item.prev);
+    assert(victim_core_item.prev && victim_core_item.next);
+    assert(victim_swap_item.prev && victim_core_item.next);
   }
   else {
-    assert(victim_core_item.prev == NULL);
-    assert(victim_swap_item.prev == NULL);
+    assert(victim_core_item.prev == NULL && victim_core_item.next == NULL);
+    assert(victim_swap_item.prev == NULL && victim_swap_item.prev == NULL);
   }
   for (int i = 0; i < n_processes; ++i)
     if (processes[i].size != -1) {
       if (processes + i == victim) {
         assert(processes[i].lru_item.prev == NULL);
-        assert(processes[i].pool_item.prev == NULL);
+        assert(
+          processes[i].pool_item.prev == NULL &&
+          processes[i].pool_item.next == NULL
+        );
         assert(
           processes[i].size ==
             victim_core_item.limit -
@@ -80,8 +86,10 @@ void process_init(int n, int spare) {
   lru_head.next = &lru_head;
 
   victim = NULL;
+#ifndef NDEBUG
   memset(&victim_core_item, 0, sizeof(victim_core_item));
   memset(&victim_swap_item, 0, sizeof(victim_swap_item));
+#endif
  check_invariants();
 }
 
@@ -98,11 +106,9 @@ static void do_swap_out(int swap_out) {
  printf("victimized %d\n", (int)(victim - processes));
       // remove from core pool, using dedicated core item
       pool_free(&core_head, &victim_core_item);
-      victim_core_item.prev = NULL;
 
       // move dedicated to per-process swap item
       pool_move_item(&victim_swap_item, &victim->pool_item);
-      victim_swap_item.prev = NULL; 
 
     no_victim:
       // take next least recently used process as victim
@@ -117,7 +123,6 @@ static void do_swap_out(int swap_out) {
 
       // move per-process to dedicated core item
       pool_move_item(&victim->pool_item, &victim_core_item);
-      victim->pool_item.prev = NULL; 
 
       // add to swap pool, using dedicated swap item
       rassert(pool_alloc_moveable(&swap_head, &victim_swap_item, 0));
@@ -232,25 +237,21 @@ void process_run(struct process *process) {
   }
   else {
     // no, need to swap some in
+#ifndef NDEBUG
     memset(&process_core_item, 0, sizeof(process_core_item));
     memset(&process_swap_item, 0, sizeof(process_swap_item));
+#endif
  
     // see whether victim or fully in swap
     if (process == victim) {
       // victim, take over the dedicated pool items
       victim = NULL;
-
       pool_move_item(&victim_core_item, &process_core_item);
-      victim_core_item.prev = NULL;
-
       pool_move_item(&victim_swap_item, &process_swap_item);
-      victim_swap_item.prev = NULL;
     }
     else {
       // fully in swap, take over only the per-process swap item
       pool_move_item(&process->pool_item, &process_swap_item);
-      process->pool_item.prev = NULL;
-
       rassert(pool_alloc_moveable(&core_head, &process_core_item, 0));
     }
 
@@ -299,11 +300,9 @@ void process_run(struct process *process) {
 
     // move dedicated to per-process core item
     pool_move_item(&process_core_item, &process->pool_item);
-    //process_core_item.prev = NULL; 
 
     // remove from swap pool, using dedicated swap item
     pool_free(&swap_head, &process_swap_item);
-    //process_swap_item.prev = NULL;
   }
 
   // insert at head of LRU list
@@ -327,22 +326,16 @@ void process_free(struct process *process) {
     process->lru_item.next->prev = process->lru_item.prev;
 
     pool_free(&core_head, &process->pool_item);
-    process->pool_item.prev = NULL;
   }
   else if (process == victim) {
     // victim, free the dedicated pool items
     victim = NULL;
-
     pool_free(&core_head, &victim_core_item);
-    victim_core_item.prev = NULL;
-
     pool_free(&swap_head, &victim_swap_item);
-    victim_swap_item.prev = NULL;
   }
   else {
     // fully in swap, remove from swap pool
     pool_free(&swap_head, &process->pool_item);
-    process->pool_item.prev = NULL;
   }
 
   // track total allocation