Make process hash init and verify be bytewise rather than blockwise
authorNick Downing <nick@ndcode.org>
Thu, 4 Apr 2019 09:49:17 +0000 (20:49 +1100)
committerNick Downing <nick@ndcode.org>
Thu, 4 Apr 2019 09:52:35 +0000 (20:52 +1100)
core.h
process_test_run.c

diff --git a/core.h b/core.h
index 6fc8630..d7403a3 100644 (file)
--- a/core.h
+++ b/core.h
@@ -4,7 +4,7 @@
 #include <stdint.h>
 #include "pool.h"
 
-#define INDIRECT_CORE 1
+//#define INDIRECT_CORE 1
 #define MOVEABLE_CORE 1
 #define BLOCK_SIZE 0x1000
 #define BLOCK_SHIFT 12
index 8c66859..4ae4086 100644 (file)
@@ -7,68 +7,57 @@
 #include "rassert.h"
 #include "swap.h"
 
-void core_hash_init(int process, int base, int blocks, int offset) {
-  for (int i = 0; i < blocks; ++i) {
+void core_hash_init(int process, long base, long size, long offset) {
+  for (long i = 0; i < size; ++i) {
+    int addr = base + i;
 #ifdef INDIRECT_CORE
-    int core_block = core_table_mem[base + i];
-#else
-    int core_block = base + i;
+    addr =
+      (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
+      (addr & (BLOCK_SIZE - 1));
 #endif
-    for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + (i + offset) * 29 + j * 37;
-      hash = (hash & 0xffffffffLL) + (hash >> 32);
-      hash = (hash & 0xffffLL) + (hash >> 16);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      rassert(core_block_mem[core_block * BLOCK_SIZE + j] == 0xaa);
-      core_block_mem[core_block * BLOCK_SIZE + j] = (uint8_t)hash;
-    }
+    long long hash = process * 17 + (i + offset) * 29;
+    hash = (hash & 0xffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffLL) + (hash >> 16);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    rassert(core_block_mem[addr] == 0xaa);
+    core_block_mem[addr] = (uint8_t)hash;
   }
 }
 
-void core_hash_verify(int process, int base, int blocks, int offset) {
-  for (int i = 0; i < blocks; ++i) {
+void core_hash_verify(int process, long base, long size, long offset) {
+  for (long i = 0; i < size; ++i) {
+    int addr = base + i;
 #ifdef INDIRECT_CORE
-    int core_block = core_table_mem[base + i];
-#else
-    int core_block = base + i;
+    addr =
+      (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
+      (addr & (BLOCK_SIZE - 1));
 #endif
-    for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + (i + offset) * 29 + j * 37;
-      hash = (hash & 0xffffffffLL) + (hash >> 32);
-      hash = (hash & 0xffffLL) + (hash >> 16);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      rassert(core_block_mem[core_block * BLOCK_SIZE + j] == (uint8_t)hash);
-    }
-    memset(
-      core_block_mem + core_block * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
+    long long hash = process * 17 + (i + offset) * 29;
+    hash = (hash & 0xffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffLL) + (hash >> 16);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    rassert(core_block_mem[addr] == (uint8_t)hash);
+    core_block_mem[addr] = 0xaa;
   }
 }
 
-void swap_hash_verify(int process, int base, int blocks, int offset) {
-  for (int i = 0; i < blocks; ++i) {
+void swap_hash_verify(int process, long base, long size, long offset) {
+  for (long i = 0; i < size; ++i) {
+    int addr = base + i;
 #ifdef INDIRECT_SWAP
-    int swap_block = swap_table_mem[base + i];
-#else
-    int swap_block = base + i;
+    addr =
+      (swap_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
+      (addr & (BLOCK_SIZE - 1));
 #endif
-    for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + (i + offset) * 29 + j * 37;
-      hash = (hash & 0xffffffffLL) + (hash >> 32);
-      hash = (hash & 0xffffLL) + (hash >> 16);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      hash = (hash & 0xffLL) + (hash >> 8);
-      rassert(swap_block_mem[swap_block * BLOCK_SIZE + j] == (uint8_t)hash);
-    }
-    memset(
-      swap_block_mem + swap_block * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
+    long long hash = process * 17 + (i + offset) * 29;
+    hash = (hash & 0xffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffLL) + (hash >> 16);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    hash = (hash & 0xffLL) + (hash >> 8);
+    rassert(swap_block_mem[addr] == (uint8_t)hash);
+    swap_block_mem[addr] = 0xaa;
   }
 }
 
@@ -164,7 +153,12 @@ int main(int argc, char **argv) {
               core_base + blocks
           );
  printf("new core [%d,%d)\n", core_base, core_base + blocks);
-          core_hash_init(process, core_base, blocks, 0);
+          core_hash_init(
+            process,
+            (long)core_base << BLOCK_SHIFT,
+            (long)blocks << BLOCK_SHIFT,
+            0L
+          );
 #ifdef PREALLOCATE_SWAP
  printf("new swap [%d,%d)\n", PER_PROCESS_SWAP_ITEM(processes + process).base, PER_PROCESS_SWAP_ITEM(processes + process).limit);
 #endif
@@ -219,9 +213,9 @@ int main(int argc, char **argv) {
         int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
         core_hash_verify(
           process,
-          core_base + blocks,
-          actual_old_blocks - blocks,
-          blocks
+          (long)(core_base + blocks) << BLOCK_SHIFT,
+          (long)(actual_old_blocks - blocks) << BLOCK_SHIFT,
+          (long)blocks << BLOCK_SHIFT
         );
         bool result = process_realloc(processes + process, size);
         printf(
@@ -248,9 +242,9 @@ int main(int argc, char **argv) {
 #endif
             core_hash_init(
               process,
-              core_base + actual_old_blocks,
-              blocks - actual_old_blocks,
-              actual_old_blocks
+              (long)(core_base + actual_old_blocks) << BLOCK_SHIFT,
+              (long)(blocks - actual_old_blocks) << BLOCK_SHIFT,
+              (long)actual_old_blocks << BLOCK_SHIFT
             );
           }
         }
@@ -317,8 +311,18 @@ int main(int argc, char **argv) {
         rassert(core_blocks + swap_blocks == actual_old_blocks);
  printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + core_blocks, swap_base, swap_base + swap_blocks);
  fflush(stdout);
-        core_hash_verify(process, core_base, core_blocks, swap_blocks);
-        swap_hash_verify(process, swap_base, swap_blocks, 0);
+        core_hash_verify(
+          process,
+          (long)core_base << BLOCK_SHIFT,
+          (long)core_blocks << BLOCK_SHIFT,
+          (long)swap_blocks << BLOCK_SHIFT
+        );
+        swap_hash_verify(
+          process,
+          (long)swap_base << BLOCK_SHIFT,
+          (long)swap_blocks << BLOCK_SHIFT,
+          0L
+        );
         process_free(processes + process);
         processes[process].size = -1L;
         printf("... ok\n");
@@ -366,11 +370,21 @@ done:
         swap_base,
         swap_base + swap_blocks
       );
-      core_hash_verify(i, core_base, core_blocks, swap_blocks);
+      core_hash_verify(
+        i,
+        (long)core_base << BLOCK_SHIFT,
+        (long)core_blocks << BLOCK_SHIFT,
+        (long)swap_blocks << BLOCK_SHIFT
+      );
 #ifdef INDIRECT_CORE
       core_block_free(core_table_mem + core_base, core_blocks);
 #endif
-      swap_hash_verify(i, swap_base, swap_blocks, 0);
+      swap_hash_verify(
+        i,
+        (long)swap_base << BLOCK_SHIFT,
+        (long)swap_blocks << BLOCK_SHIFT,
+        0L
+      );
 #ifdef INDIRECT_SWAP
       swap_block_free(swap_table_mem + swap_base, swap_blocks);
 #endif