Make swap_move() only be defined for INDIRECT_SWAP where it only moves the table...
authorNick Downing <nick@ndcode.org>
Mon, 3 Jun 2019 09:35:51 +0000 (19:35 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 3 Jun 2019 09:35:51 +0000 (19:35 +1000)
process.c
process.h
process_test_run.c
swap.c

index 40a1350..260452e 100644 (file)
--- a/process.c
+++ b/process.c
@@ -44,19 +44,62 @@ void process_init(int n, int spare) {
   victim = &lru_head;
 }
 
-// note: swap_out argument can be negative, indicates a no-op
-static bool do_swap_out(int swap_out) {
-  struct process *process;
-  int core_blocks, swap_blocks;
-  int blocks, swap_base, core_base;
-  int paras;
+static void do_swap_read_write(
+  int core_base,
+  int swap_base,
+  int blocks,
+  bool dir
+) {
 #ifndef INDIRECT_CORE
-#ifdef INDIRECT_SWAP
-  int i, j, core_block, swap_block;
+#ifndef INDIRECT_SWAP
+  swap_read_write(core_base, swap_base, blocks, dir);
+#else /* INDIRECT_SWAP */
+  int i;
+
+ printf("blocks");
+  for (i = 0; i < blocks; ++i)
+ {
+  printf(" %s%d", dir ? "->" : "<-", swap_table_mem[swap_base + i]);
+    swap_read_write(
+      core_base + i,
+      swap_table_mem[swap_base + i],
+      1,
+      dir
+    );
+ }
+ printf("\n");
 #endif /* INDIRECT_SWAP */
 #else /* INDIRECT_CORE */
-  int i, j, core_block, swap_block;
+  int i;
+
+ printf("blocks");
+  for (i = 0; i < blocks; ++i)
+ {
+#ifndef INDIRECT_SWAP
+  printf(" %d%s", core_table_mem[core_base + i], dir ? "->" : "<-");
+#else /* INDIRECT_SWAP */
+  printf(" %d%s%d", core_table_mem[core_base + i], dir ? "->" : "<-", swap_table_mem[swap_base + i]);
+#endif
+    swap_read_write(
+      core_table_mem[core_base + i],
+#ifndef INDIRECT_SWAP
+      swap_base + i,
+#else /* INDIRECT_SWAP */
+      swap_table_mem[swap_base + i],
+#endif
+      1,
+      dir
+    );
+ }
+ printf("\n");
 #endif /* INDIRECT_CORE */
+}
+
+// note: swap_out argument can be negative, indicates a no-op
+static bool do_swap_out(int swap_out) {
+  struct process *process;
+  int core_blocks, swap_blocks, blocks;
+  int swap_base, core_base;
 
   for (; swap_out > 0; swap_out -= blocks) {
     assert(victim->prev != &lru_head);
@@ -97,93 +140,16 @@ static bool do_swap_out(int swap_out) {
 #else /* INDIRECT_CORE */
     core_base = process->core_item.limit - swap_blocks;
 #endif /* INDIRECT_CORE */
-    paras = blocks << BLOCK_PARAS_SHIFT;
 
     // transfer data to swap
-#ifndef INDIRECT_CORE
-#ifndef INDIRECT_SWAP
- printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, swap_base, swap_base + blocks);
-    core_to_swap_copy(
-      core_base << BLOCK_PARAS_SHIFT,
-      swap_base << BLOCK_PARAS_SHIFT,
-      paras
-#else /* INDIRECT_SWAP */
-    rassert(
-      block_pool_alloc(&swap_block_pool, swap_table_mem + swap_base, blocks)
-#endif /* INDIRECT_SWAP */
-    );
-#ifdef INDIRECT_SWAP
- printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, swap_base, swap_base + blocks);
-    j = paras >> BLOCK_PARAS_SHIFT;
- printf("blocks");
-    for (i = 0; i < j; ++i) {
-      core_block = core_base + i;
-      swap_block = swap_base + i;
-      swap_block = swap_table_mem[swap_block];
- printf(" %d", swap_block);
-      core_to_swap_copy(
-        core_block << BLOCK_PARAS_SHIFT,
-        swap_block << BLOCK_PARAS_SHIFT,
-        BLOCK_PARAS
-      );
-    }
-    j = (int)paras & (BLOCK_PARAS - 1);
-    if (j) {
-      core_block = core_base + i;
-      swap_block = swap_base + i;
-      swap_block = swap_table_mem[swap_block];
- printf(" %d", swap_block);
- //putchar('*');
-      core_to_swap_copy(
-        core_block << BLOCK_PARAS_SHIFT,
-        swap_block << BLOCK_PARAS_SHIFT,
-        j
-      );
-    }
- printf("\n");
-#endif /* INDIRECT_SWAP */
-#else /* INDIRECT_CORE */
 #ifdef INDIRECT_SWAP
     rassert(
       block_pool_alloc(&swap_block_pool, swap_table_mem + swap_base, blocks)
     );
 #endif /* INDIRECT_SWAP */
  printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, swap_base, swap_base + blocks);
-    j = paras >> BLOCK_PARAS_SHIFT;
- printf("blocks");
-    for (i = 0; i < j; ++i) {
-      core_block = core_base + i;
-      core_block = core_table_mem[core_block];
- printf(" %d", core_block);
-      swap_block = swap_base + i;
-#ifdef INDIRECT_SWAP
-      swap_block = swap_table_mem[swap_block];
- printf(",%d", swap_block);
-#endif /* INDIRECT_SWAP */
-      core_to_swap_copy(
-        core_block << BLOCK_PARAS_SHIFT,
-        swap_block << BLOCK_PARAS_SHIFT,
-        BLOCK_PARAS
-      );
-    }
-    j = (int)paras & (BLOCK_PARAS - 1);
-    if (j) {
-      core_block = core_base + i;
-      core_block = core_table_mem[core_block];
- printf(" %d", core_block);
-      swap_block = swap_base + i;
-#ifdef INDIRECT_SWAP
-      swap_block = swap_table_mem[swap_block];
- printf(",%d", swap_block);
-#endif /* INDIRECT_SWAP */
- //putchar('*');
-      core_to_swap_copy(
-        core_block << BLOCK_PARAS_SHIFT,
-        swap_block << BLOCK_PARAS_SHIFT,
-        j
-      );
-    }
- printf("\n");
+    do_swap_read_write(core_base, swap_base, blocks, true);
+#ifdef INDIRECT_CORE
     block_pool_free(&core_block_pool, core_table_mem + core_base, blocks);
 #endif /* INDIRECT_CORE */
 
@@ -427,14 +393,6 @@ bool process_realloc(struct process *process, int paras, bool dir) {
 void process_run(struct process *process) {
   int core_blocks, swap_blocks, blocks;
   int swap_base, core_base;
-  int paras;
-#ifndef INDIRECT_CORE
-#ifdef INDIRECT_SWAP
-  int i, j;
-#endif /* INDIRECT_SWAP */
-#else /* INDIRECT_CORE */
-  int i, j;
-#endif /* INDIRECT_CORE */
 
   // must be already allocated
   assert(process->flags & PROCESS_FLAGS_ACTIVE);
@@ -495,88 +453,18 @@ void process_run(struct process *process) {
     process->core_blocks = core_blocks;
 #endif
     swap_blocks -= blocks;
-    paras = blocks << BLOCK_PARAS_SHIFT;
 
     // transfer data to core
 #ifdef INDIRECT_CORE
-    block_pool_alloc(&core_block_pool, core_table_mem + core_base, blocks);
+    rassert(
+      block_pool_alloc(&core_block_pool, core_table_mem + core_base, blocks)
+    );
 #endif /* INDIRECT_CORE */
  printf("read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + blocks, core_base, core_base + blocks);
-#ifndef INDIRECT_CORE
-#ifndef INDIRECT_SWAP
-    swap_to_core_copy(
-      swap_base << BLOCK_PARAS_SHIFT,
-      core_base << BLOCK_PARAS_SHIFT,
-      paras
-    );
-#else /* INDIRECT_SWAP */
-    j = paras >> BLOCK_PARAS_SHIFT;
- printf("blocks");
-    for (i = 0; i < j; ++i) {
-      int core_block = core_base + i;
-      int swap_block = swap_base + i;
-      swap_block = swap_table_mem[swap_block];
- printf(" %d", swap_block);
-      swap_to_core_copy(
-        swap_block << BLOCK_PARAS_SHIFT,
-        core_block << BLOCK_PARAS_SHIFT,
-        BLOCK_PARAS
-      );
-    }
-    j = (int)paras & (BLOCK_PARAS - 1);
-    if (j) {
-      int core_block = core_base + i;
-      int swap_block = swap_base + i;
-      swap_block = swap_table_mem[swap_block];
- printf(" %d", swap_block);
-      swap_to_core_copy(
-        swap_block << BLOCK_PARAS_SHIFT,
-        core_block << BLOCK_PARAS_SHIFT,
-        j
-      );
-    }
- printf("\n");
-    block_pool_free(&swap_block_pool, swap_table_mem + swap_base, blocks);
-#endif /* INDIRECT_SWAP */
-#else /* INDIRECT_CORE */
-    j = paras >> BLOCK_PARAS_SHIFT;
- printf("blocks");
-    for (i = 0; i < j; ++i) {
-      int core_block = core_base + i;
-      core_block = core_table_mem[core_block];
- printf(" %d", core_block);
-      int swap_block = swap_base + i;
-#ifdef INDIRECT_SWAP
-      swap_block = swap_table_mem[swap_block];
- printf(",%d", swap_block);
-#endif /* INDIRECT_SWAP */
-      swap_to_core_copy(
-        swap_block << BLOCK_PARAS_SHIFT,
-        core_block << BLOCK_PARAS_SHIFT,
-        BLOCK_PARAS
-      );
-    }
-    j = (int)paras & (BLOCK_PARAS - 1);
-    if (j) {
-      int core_block = core_base + i;
-      core_block = core_table_mem[core_block];
- printf(" %d", core_block);
-      int swap_block = swap_base + i;
-#ifdef INDIRECT_SWAP
-      swap_block = swap_table_mem[swap_block];
- printf(",%d", swap_block);
-#endif /* INDIRECT_SWAP */
-      swap_to_core_copy(
-        swap_block << BLOCK_PARAS_SHIFT,
-        core_block << BLOCK_PARAS_SHIFT,
-        j
-      );
-    }
- printf("\n");
+    do_swap_read_write(core_base, swap_base, blocks, false);
 #ifdef INDIRECT_SWAP
     block_pool_free(&swap_block_pool, swap_table_mem + swap_base, blocks);
 #endif /* INDIRECT_SWAP */
-#endif /* INDIRECT_CORE */
   } while (swap_blocks);
 
   // insert at head of LRU list
index 3e5084e..81bfa3a 100644 (file)
--- a/process.h
+++ b/process.h
@@ -66,7 +66,6 @@ void process_free(struct process *process);
 void process_get_state(struct process *process, struct process_state *state);
  
 // abstract
-void core_to_swap_copy(int src_base, int dest_base, int paras);
-void swap_to_core_copy(int src_base, int dest_base, int paras);
+void swap_read_write(int core_base, int swap_base, int blocks, bool dir);
 
 #endif
index f4592a1..86387d4 100644 (file)
@@ -89,55 +89,36 @@ void core_copy_up(int src_limit, int dest_limit, int paras) {
 }
 #endif /* ! INDIRECT_CORE */
 
-#ifndef INDIRECT_SWAP
-void swap_copy(int src_base, int dest_base, int paras) {
+// dir == false for read, true for write
+void swap_read_write(int core_base, int swap_base, int blocks, bool dir) {
   int i;
 
- //printf("swap_copy %d(%d) %d(%d) %d(%d)\n", src_base, src_base >> BLOCK_PARAS_SHIFT, dest_base, dest_base >> BLOCK_PARAS_SHIFT, paras, (paras + (BLOCK_PARAS - 1)) >> BLOCK_PARAS_SHIFT);
-  for (i = 0; i < paras; ++i) {
-    swap_block_mem[dest_base + i] = swap_block_mem[src_base + i];
-    swap_block_mem[src_base + i] = 0xaaaaaaaa;
+ //printf("swap_read_write core [%d,%d) %s swap [%d,%d)\n", core_base, core_base + blocks, dir ? "->" : "<-", swap_base, swap_base + blocks);
+  core_base <<= BLOCK_PARAS_SHIFT;
+  swap_base <<= BLOCK_PARAS_SHIFT;
+  blocks <<= BLOCK_PARAS_SHIFT;
+#define paras blocks
+  if (dir) {
+    for (i = 0; i < paras; ++i)
+      rassert(swap_block_mem[swap_base + i] == 0xaaaaaaaa);
+    memcpy(
+      swap_block_mem + swap_base,
+      core_block_mem + core_base,
+      paras * sizeof(int)
+    );
+    memset(core_block_mem + core_base, 0xaa, paras * sizeof(int));
   }
-}
-
-void swap_copy_up(int src_limit, int dest_limit, int paras) {
-  int i;
-
- //printf("swap_copy_up %d(%d) %d(%d) %d(%d)\n", src_limit, src_limit >> BLOCK_PARAS_SHIFT, dest_limit, dest_limit >> BLOCK_PARAS_SHIFT, paras, (paras + (BLOCK_PARAS - 1)) >> BLOCK_PARAS_SHIFT);
-  paras = -paras;
-  for (i = -1; i >= paras; --i) {
-    swap_block_mem[dest_limit + i] = swap_block_mem[src_limit + i];
-    swap_block_mem[src_limit + i] = 0xaaaaaaaa;
+  else {
+    for (i = 0; i < paras; ++i)
+      rassert(core_block_mem[core_base + i] == 0xaaaaaaaa);
+    memcpy(
+      core_block_mem + core_base,
+      swap_block_mem + swap_base,
+      paras * sizeof(int)
+    );
+    memset(swap_block_mem + swap_base, 0xaa, paras * sizeof(int));
   }
-}
-#endif /* ! INDIRECT_SWAP */
-
-void core_to_swap_copy(int src_base, int dest_base, int paras) {
-  int i;
-
- //printf("core_to_swap_copy %d(%d) %d(%d) %d(%d)\n", src_base, src_base >> BLOCK_PARAS_SHIFT, dest_base, dest_base >> BLOCK_PARAS_SHIFT, paras, (paras + (BLOCK_PARAS - 1)) >> BLOCK_PARAS_SHIFT);
-  for (i = 0; i < paras; ++i)
-    rassert(swap_block_mem[dest_base + i] == 0xaaaaaaaa);
-  memcpy(
-    swap_block_mem + dest_base,
-    core_block_mem + src_base,
-    paras * sizeof(int)
-  );
-  memset(core_block_mem + src_base, 0xaa, paras * sizeof(int));
-}
-
-void swap_to_core_copy(int src_base, int dest_base, int paras) {
-  int i;
-
- //printf("swap_to_core_copy %d(%d) %d(%d) %d(%d)\n", src_base, src_base >> BLOCK_PARAS_SHIFT, dest_base, dest_base >> BLOCK_PARAS_SHIFT, paras, (paras + (BLOCK_PARAS - 1)) >> BLOCK_PARAS_SHIFT);
-  for (i = 0; i < paras; ++i)
-    rassert(core_block_mem[dest_base + i] == 0xaaaaaaaa);
-  memcpy(
-    core_block_mem + dest_base,
-    swap_block_mem + src_base,
-    paras * sizeof(int)
-  );
-  memset(swap_block_mem + src_base, 0xaa, paras * sizeof(int));
+#undef paras
 }
 
 int main(int argc, char **argv) {
diff --git a/swap.c b/swap.c
index e3a3797..ff42f2c 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -11,44 +11,22 @@ struct pool_head swap_table;
 #ifdef INDIRECT_SWAP
 int *swap_table_mem;
 struct block_pool swap_block_pool;
-#endif /* INDIRECT_SWAP */
 
 static void swap_move(struct pool_item *item, int new_base) {
   int base, blocks;
-#ifndef INDIRECT_SWAP
-  struct process *process;
-  int paras;
-#else /* INDIRECT_SWAP */
   int i;
-#endif /* INDIRECT_SWAP */
 
   base = item->base;
   blocks = item->limit - base;
-  //printf(
-  //  "swap_move [%d,%d) to [%d,%d)\n",
-  //  base,
-  //  base + blocks,
-  //  new_base,
-  //  new_base + blocks
-  //);
+ //printf("swap_move [%d,%d) to [%d,%d)\n", base, base + blocks, new_base, new_base + blocks);
   assert(new_base <= base || new_base >= base + blocks);
-#ifndef INDIRECT_SWAP
-  // see if incomplete last block, if so don't copy extra part
-  process =
-    (struct process *)((char *)item - offsetof(struct process, swap_item));
-  paras = blocks << BLOCK_PARAS_SHIFT;
-  if (paras > process->paras)
-    paras = process->paras;
 
-  // copy by abstract routine
-  swap_copy(base << BLOCK_PARAS_SHIFT, new_base << BLOCK_PARAS_SHIFT, paras);
-#else /* INDIRECT_SWAP */
   for (i = 0; i < blocks; ++i) {
     swap_table_mem[new_base + i] = swap_table_mem[base + i];
     swap_table_mem[base + i] = 0x55555555;
   }
-#endif /* INDIRECT_SWAP */
 }
+#endif /* INDIRECT_SWAP */
 
 #ifndef INDIRECT_SWAP
 void swap_init(int n_blocks)
@@ -61,10 +39,11 @@ void swap_init(int n_blocks, int table_size)
     0,
 #ifndef INDIRECT_SWAP
     n_blocks,
+    NULL,
 #else /* INDIRECT_SWAP */
     table_size,
-#endif /* INDIRECT_SWAP */
     swap_move,
+#endif /* INDIRECT_SWAP */
     NULL
   );