Make victim_core_blocks be calculated on the fly, keep only victim_swap_blocks
authorNick Downing <nick@ndcode.org>
Fri, 31 May 2019 13:17:23 +0000 (23:17 +1000)
committerNick Downing <nick@ndcode.org>
Fri, 31 May 2019 13:17:23 +0000 (23:17 +1000)
core.c
core.h
process.c
process.h
process_test_run.c
swap.c
swap.h

diff --git a/core.c b/core.c
index 48fe5d9..b203875 100644 (file)
--- a/core.c
+++ b/core.c
@@ -11,8 +11,6 @@ struct pool_head core_table;
 #ifdef INDIRECT_CORE
 int *core_table_mem;
 struct block_pool core_block_pool;
-
-int victim_core_blocks;
 #endif /* INDIRECT_CORE */
 
 static void core_move(struct pool_item *item, int new_base) {
diff --git a/core.h b/core.h
index 7e2c1f6..506f786 100644 (file)
--- a/core.h
+++ b/core.h
@@ -17,11 +17,7 @@ extern struct block_pool core_block_pool;
 
 #ifndef INDIRECT_CORE
 void core_init(int n_table);
-#else /* INDIRECT_CORE */
-extern int victim_core_blocks;
-#endif /* INDIRECT_CORE */
 
-#ifndef INDIRECT_CORE
 // abstract
 void core_copy(long src_base, long dest_base, long size);
 void core_copy_up(long src_limit, long dest_limit, long size);
index 864fe7b..6e899c0 100644 (file)
--- a/process.c
+++ b/process.c
@@ -15,6 +15,7 @@ int process_avail;
 struct lru_item lru_head;
 
 struct process *victim;
+int victim_swap_blocks;
 
 void process_init(int n, int spare) {
   int i;
@@ -51,9 +52,7 @@ void process_init(int n, int spare) {
 }
 
 static bool do_swap_out(int swap_out) {
-#ifndef INDIRECT_CORE
   int victim_core_blocks;
-#endif /* ! INDIRECT_CORE */
   int blocks, swap_base, core_base;
   long size;
 #ifndef INDIRECT_CORE
@@ -68,26 +67,15 @@ static bool do_swap_out(int swap_out) {
   if (swap_out > 0) {
     if (victim) {
       // calculate amount to swap out
-#ifndef INDIRECT_CORE
-#ifdef INDIRECT_SWAP
-      blocks = swap_block_pool.avail;
-      if (blocks == 0)
-        return false;
-      if (blocks > swap_out)
-        blocks = swap_out;
-#endif /* INDIRECT_SWAP */
       victim_core_blocks =
-        victim->core_item.limit - victim->core_item.base;
-#ifndef INDIRECT_SWAP
-      blocks = swap_out < victim_core_blocks ? swap_out : victim_core_blocks;
-#else /* INDIRECT_SWAP */
-      if (blocks > victim_core_blocks)
-        blocks = victim_core_blocks;
-#endif /* INDIRECT_SWAP */
-#else /* INDIRECT_CORE */
+#ifndef INDIRECT_CORE
+       victim->core_item.limit - victim->core_item.base;
+#else
+       victim->core_item.limit - victim->core_item.base - victim_swap_blocks;
+#endif
 #ifndef INDIRECT_SWAP
       blocks = swap_out < victim_core_blocks ? swap_out : victim_core_blocks;
-#else /* INDIRECT_SWAP */
+#else
       blocks = swap_block_pool.avail;
       if (blocks == 0)
         return false;
@@ -96,7 +84,6 @@ static bool do_swap_out(int swap_out) {
       if (blocks > victim_core_blocks)
         blocks = victim_core_blocks;
 #endif /* INDIRECT_SWAP */
-#endif /* INDIRECT_CORE */
  printf("existing victim %d, swap out %d of %d\n", (int)(victim - processes), blocks, victim_core_blocks);
 
       // increase swap allocation
@@ -106,28 +93,27 @@ static bool do_swap_out(int swap_out) {
     // loop for the case of no existing victim
     do {
       // calculate amount to swap out
-#ifdef INDIRECT_SWAP
-      blocks = swap_block_pool.avail;
-      if (blocks == 0)
-        return false;
-      if (blocks > swap_out)
-        blocks = swap_out;
-#endif /* INDIRECT_SWAP */
       assert(lru_head.prev != &lru_head);
       victim = (struct process *)lru_head.prev;
+      victim_swap_blocks = 0;
       victim_core_blocks =
         victim->core_item.limit - victim->core_item.base;
 #ifndef INDIRECT_SWAP
       blocks = swap_out < victim_core_blocks ? swap_out : victim_core_blocks;
 #else /* INDIRECT_SWAP */
+      blocks = swap_block_pool.avail;
+      if (blocks == 0)
+ {
+  victim = NULL;
+        return false;
+ }
+      if (blocks > swap_out)
+        blocks = swap_out;
       if (blocks > victim_core_blocks)
         blocks = victim_core_blocks;
 #endif /* INDIRECT_SWAP */
  printf("new victim %d, swap out %d of %d\n", (int)(victim - processes), blocks, victim_core_blocks);
 
-      // add to swap pool
-      victim_swap_blocks = 0;
-
       // remove from LRU list
       victim->lru_item.prev->next = victim->lru_item.next;
       victim->lru_item.next->prev = victim->lru_item.prev;
@@ -501,11 +487,11 @@ void process_run(struct process *process) {
     }
 
     // victim, take over the dedicated pool items
-#ifndef INDIRECT_CORE
     process_core_blocks =
+#ifndef INDIRECT_CORE
       victim->core_item.limit - victim->core_item.base;
 #else /* INDIRECT_CORE */
-    process_core_blocks = victim_core_blocks;
+      victim->core_item.limit - victim->core_item.base - victim_swap_blocks;
 #endif /* INDIRECT_CORE */
     process_swap_blocks = victim_swap_blocks;
     victim = NULL;
@@ -697,7 +683,7 @@ void process_free(struct process *process) {
     block_pool_free(
       &core_block_pool,
       core_table_mem + core_base,
-      victim_core_blocks
+      victim->core_item.limit - core_base
     );
 #endif /* INDIRECT_CORE */
     victim = NULL;
index 6ac77f7..40df765 100644 (file)
--- a/process.h
+++ b/process.h
@@ -24,6 +24,7 @@ extern int process_avail;
 extern struct lru_item lru_head;
 
 extern struct process *victim;
+extern int victim_swap_blocks;
 
 void process_init(int n, int spare);
 bool process_alloc(struct process *process, long size);
index f9e9b8f..006f84b 100644 (file)
@@ -444,11 +444,10 @@ int main(int argc, char **argv) {
             swap_size = swap_blocks << BLOCK_SHIFT;
 #ifndef INDIRECT_CORE
             core_base = victim->core_item.base;
-            core_blocks = victim->core_item.limit - core_base;
 #else /* INDIRECT_CORE */
             core_base = victim->core_item.base + swap_blocks;
-            core_blocks = victim_core_blocks;
 #endif /* INDIRECT_CORE */
+            core_blocks = victim->core_item.limit - core_base;
             core_size = victim->size - swap_size;
           }
           else { // fully in swap
@@ -504,11 +503,10 @@ done:
           swap_size = swap_blocks << BLOCK_SHIFT;
 #ifndef INDIRECT_CORE
           core_base = victim->core_item.base;
-          core_blocks = victim->core_item.limit - core_base;
 #else /* INDIRECT_CORE */
           core_base = victim->core_item.base + swap_blocks;
-          core_blocks = victim_core_blocks;
 #endif /* INDIRECT_CORE */
+          core_blocks = victim->core_item.limit - core_base;
           core_size = victim->size - swap_size;
         }
         else { // fully in swap
diff --git a/swap.c b/swap.c
index 3511bab..a0d3573 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -13,8 +13,6 @@ int *swap_table_mem;
 struct block_pool swap_block_pool;
 #endif /* INDIRECT_SWAP */
 
-int victim_swap_blocks;
-
 static void swap_move(struct pool_item *item, int new_base) {
   int base, blocks;
 #ifndef INDIRECT_SWAP
diff --git a/swap.h b/swap.h
index ef608fb..11ed669 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -15,8 +15,6 @@ extern int *swap_table_mem;
 extern struct block_pool swap_block_pool;
 #endif /* INDIRECT_SWAP */
 
-extern int victim_swap_blocks;
-
 #ifndef INDIRECT_SWAP
 void swap_init(int n_table);