/indirect_core_indirect_swap/process_test.txt
/indirect_core_indirect_swap/process_test_gen
/indirect_core_indirect_swap/process_test_run
-/indirect_core_inode_swap/*.c
-/indirect_core_inode_swap/*.h
-/indirect_core_inode_swap/fs.bin
-/indirect_core_inode_swap/process_test.txt
-/indirect_core_inode_swap/process_test_gen
-/indirect_core_inode_swap/process_test_run
-/indirect_core_preallocate_swap/*.c
-/indirect_core_preallocate_swap/*.h
-/indirect_core_preallocate_swap/process_test.txt
-/indirect_core_preallocate_swap/process_test_gen
-/indirect_core_preallocate_swap/process_test_run
+/indirect_core_contiguous_swap/*.c
+/indirect_core_contiguous_swap/*.h
+/indirect_core_contiguous_swap/process_test.txt
+/indirect_core_contiguous_swap/process_test_gen
+/indirect_core_contiguous_swap/process_test_run
/inode_test.txt
/inode_test_gen
/inode_test_run
/moveable_core_indirect_swap/process_test.txt
/moveable_core_indirect_swap/process_test_gen
/moveable_core_indirect_swap/process_test_run
-/moveable_core_inode_swap/*.c
-/moveable_core_inode_swap/*.h
-/moveable_core_inode_swap/fs.bin
-/moveable_core_inode_swap/process_test.txt
-/moveable_core_inode_swap/process_test_gen
-/moveable_core_inode_swap/process_test_run
-/moveable_core_preallocate_swap/*.c
-/moveable_core_preallocate_swap/*.h
-/moveable_core_preallocate_swap/process_test.txt
-/moveable_core_preallocate_swap/process_test_gen
-/moveable_core_preallocate_swap/process_test_run
+/moveable_core_contiguous_swap/*.c
+/moveable_core_contiguous_swap/*.h
+/moveable_core_contiguous_swap/process_test.txt
+/moveable_core_contiguous_swap/process_test_gen
+/moveable_core_contiguous_swap/process_test_run
/pool_test.txt
/pool_test_gen
/pool_test_run
process_test_gen.o: process_test_gen.c process_test.h
-process_test_run: process_test_run.o process.o process_test.h core.o fuzix_fs_or_swap.o block_pool.o pool.o
+process_test_run: process_test_run.o process.o process_test.h core.o swap.o block_pool.o pool.o
${CC} ${CFLAGS} -o $@ $^
process_test_run.o: process_test_run.c process.h core.h swap.h pool.h fuzix_fs.h util.h
process.o: process.c process.h core.h swap.h pool.h fuzix_fs.h
core.o: core.c core.h pool.h
-fuzix_fs_or_swap.o: fuzix_fs_or_swap.c fuzix_fs.c swap.h core.h pool.h fuzix_fs.h
+swap.o: swap.c fuzix_fs.c swap.h core.h pool.h fuzix_fs.h
block_pool.o: block_pool.c block_pool.h
# ucp no longer compiles because many fs routines commented to save space
loop_entry:
if (c) goto found;
}
-#if 1
for (j = 0; ; ++j) {
assert(j <= block_pool->next >> 3);
c = block_pool->bitmap[j];
if (c) goto found;
}
-#else
- k = (block_pool->next >> 3) + 1;
- for (j = 0; j < k; ++j) {
- c = block_pool->bitmap[j];
- if (c) goto found;
- }
- core_block_free(table, i);
- return false;
-#endif
found:
for (k = 0; (c & 1) == 0; ++k)
block_pool->bitmap[j] &= ~bits[k];
block_pool->next = (j << 3) | k;
-#ifndef NDEBUG // makes us remove the following line in gen.sh as appropriate
assert(table[i] == 0x55555555);
-#endif
table[i] = block_pool->next++;
if (block_pool->next >= block_pool->n_blocks)
block_pool->next = 0;
j >>= 3;
assert((block_pool->bitmap[j] & bits[k]) == 0);
block_pool->bitmap[j] |= bits[k];
-#ifndef NDEBUG
table[i] = 0x55555555;
-#endif
}
block_pool->avail += size;
#include <stddef.h>
//#include <stdio.h>
#include <stdlib.h>
-#ifndef NDEBUG
#include <string.h>
-#endif
#include "core.h"
#include "process.h"
#include "rassert.h"
#ifdef INDIRECT_CORE
int *core_table_mem;
struct block_pool core_block_pool;
-#endif
-#ifdef PREALLOCATE_CORE
int victim_core_blocks;
-#endif
+#endif /* INDIRECT_CORE */
static void core_move(struct pool_item *item, int new_base) {
int base, blocks;
-#ifdef INDIRECT_CORE
- int i;
-#else
+#ifndef INDIRECT_CORE
struct process *process;
long size;
-#endif
+#else /* INDIRECT_CORE */
+ int i;
+#endif /* INDIRECT_CORE */
base = item->base;
blocks = item->limit - base;
// new_base + blocks
//);
assert(new_base <= base || new_base >= base + blocks);
-#ifdef INDIRECT_CORE
- for (i = 0; i < blocks; ++i) {
- core_table_mem[new_base + i] = core_table_mem[base + i];
-#ifndef NDEBUG
- core_table_mem[base + i] = 0x55555555;
-#endif
- }
-#else
+#ifndef INDIRECT_CORE
// see if incomplete last block, if so don't copy extra part
process =
(struct process *)((char *)item - offsetof(struct process, core_item));
// copy by abstract routine
core_copy((long)base << BLOCK_SHIFT, (long)new_base << BLOCK_SHIFT, size);
-#endif
}
-#ifdef MOVEABLE_CORE
static void core_move_up(struct pool_item *item, int new_limit) {
int limit, blocks;
-#ifdef INDIRECT_CORE
- int i;
-#else
long limit1, new_limit1, size;
struct process *process;
long trim;
-#endif
limit = item->limit;
blocks = limit - item->base;
// new_limit
//);
assert(new_limit >= limit || new_limit <= limit - blocks);
-#ifdef INDIRECT_CORE
- blocks = -blocks;
- for (i = -1; i >= blocks; --i) {
- core_table_mem[new_limit + i] = core_table_mem[limit + i];
-#ifndef NDEBUG
- core_table_mem[limit + i] = 0x55555555;
-#endif
- }
-#else
// set up transfer parameters
limit1 = (long)limit << BLOCK_SHIFT;
new_limit1 = (long)new_limit << BLOCK_SHIFT;
limit1 -= trim;
new_limit1 -= trim;
size = process->size;
+#else /* INDIRECT_CORE */
+ for (i = 0; i < blocks; ++i) {
+ core_table_mem[new_base + i] = core_table_mem[base + i];
+ core_table_mem[base + i] = 0x55555555;
+#endif /* INDIRECT_CORE */
}
+#ifndef INDIRECT_CORE
// copy by abstract routine
core_copy_up(limit1, new_limit1, size);
-#endif
+#endif /* ! INDIRECT_CORE */
}
-#endif
-#ifdef INDIRECT_CORE
-void core_init(int n_blocks, int table_size)
-#else
+#ifndef INDIRECT_CORE
void core_init(int n_blocks)
-#endif
+#else /* INDIRECT_CORE */
+void core_init(int n_blocks, int table_size)
+#endif /* INDIRECT_CORE */
{
pool_init(
&core_table,
0,
-#ifdef INDIRECT_CORE
- table_size,
-#else
+#ifndef INDIRECT_CORE
n_blocks,
-#endif
+#else /* INDIRECT_CORE */
+ table_size,
+#endif /* INDIRECT_CORE */
core_move,
-#ifdef MOVEABLE_CORE
+#ifndef INDIRECT_CORE
core_move_up
-#else
+#else /* INDIRECT_CORE */
NULL
-#endif
+#endif /* INDIRECT_CORE */
);
#ifdef INDIRECT_CORE
core_table_mem = malloc(table_size * sizeof(int));
rassert(core_table_mem);
-#ifndef NDEBUG
memset(core_table_mem, 0x55, table_size * sizeof(int));
-#endif
block_pool_init(&core_block_pool, n_blocks);
-#endif
+#endif /* INDIRECT_CORE */
}
#ifndef _CORE_H
#define _CORE_H 1
-//#define PREALLOCATE_CORE 1
//#define INDIRECT_CORE 1
-#define MOVEABLE_CORE 1
#include <stdint.h>
#ifdef INDIRECT_CORE
#include "block_pool.h"
-#endif
+#endif /* INDIRECT_CORE */
#include "pool.h"
-#ifdef INDIRECT_CORE
-#define CORE_AVAIL core_block_pool.avail
-#else
-#define CORE_AVAIL core_table.avail
-#endif
-
extern struct pool_head core_table;
#ifdef INDIRECT_CORE
extern int *core_table_mem;
extern struct block_pool core_block_pool;
-#endif
+#endif /* INDIRECT_CORE */
-#ifdef PREALLOCATE_CORE
-extern int victim_core_blocks;
-#endif
-
-#ifdef INDIRECT_CORE
-void core_init(int n_table, int n_blocks);
-#else
+#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);
-#endif
+#else /* INDIRECT_CORE */
+void core_init(int n_table, int n_blocks);
+#endif /* INDIRECT_CORE */
#endif
+++ /dev/null
-#include "process.h"
-#ifdef INODE_SWAP
-#include "fuzix_fs.c"
-#include "util.c"
-#else
-#include "swap.c"
-#endif
sed -e 's/^#define MOVEABLE_POOL 1/\/\/#define MOVEABLE_POOL 1/' -i pool.c
-# we will only use non-moveable swap, so it must be preallocated,
-# this is because the swap-move routines are too hard to implement
-sed -e 's/^\/\/#define PREALLOCATE_SWAP 1/#define PREALLOCATE_SWAP 1/' -i swap.h
-sed -e 's/^#define MOVEABLE_SWAP 1/\/\/#define MOVEABLE_SWAP 1/' -i swap.h
-
-sed -e 's/^\/\/#define PREALLOCATE_CORE 1/#define PREALLOCATE_CORE 1/' -i core.h
sed -e 's/^\/\/#define INDIRECT_CORE 1/#define INDIRECT_CORE 1/' -i core.h
-sed -e 's/^#define MOVEABLE_CORE 1/\/\/#define MOVEABLE_CORE 1/' -i core.h
-
-sed -e 's/^#define INODE_SWAP 1/\/\/#define INODE_SWAP 1/' -i process.h
sed -e 's/^#define INDIRECT_SWAP 1/\/\/#define INDIRECT_SWAP 1/' -i swap.h
-./cpp.sh indirect_core_preallocate_swap "$@"
-cp rassert.h indirect_core_preallocate_swap
+./cpp.sh indirect_core_contiguous_swap "$@"
+cp rassert.h indirect_core_contiguous_swap
sed -e 's/^\/\/#define INDIRECT_SWAP 1/#define INDIRECT_SWAP 1/' -i swap.h
./cpp.sh indirect_core_indirect_swap "$@"
cp rassert.h indirect_core_indirect_swap
-sed -e 's/^\/\/#define INODE_SWAP 1/#define INODE_SWAP 1/' -i process.h
-./cpp.sh indirect_core_inode_swap "$@"
-rm indirect_core_inode_swap/swap.[ch]
-cp rassert.h fuzix_fs.[ch] util.[ch] indirect_core_inode_swap
-
sed -e 's/^\/\/#define MOVEABLE_POOL 1/#define MOVEABLE_POOL 1/' -i pool.c
-sed -e 's/^#define PREALLOCATE_CORE 1/\/\/#define PREALLOCATE_CORE 1/' -i core.h
sed -e 's/^#define INDIRECT_CORE 1/\/\/#define INDIRECT_CORE 1/' -i core.h
-sed -e 's/^\/\/#define MOVEABLE_CORE 1/#define MOVEABLE_CORE 1/' -i core.h
-
-sed -e 's/^#define INODE_SWAP 1/\/\/#define INODE_SWAP 1/' -i process.h
sed -e 's/^#define INDIRECT_SWAP 1/\/\/#define INDIRECT_SWAP 1/' -i swap.h
-./cpp.sh moveable_core_preallocate_swap "$@"
-rm moveable_core_preallocate_swap/block_pool.[ch]
-cp rassert.h moveable_core_preallocate_swap
+./cpp.sh moveable_core_contiguous_swap "$@"
+rm moveable_core_contiguous_swap/block_pool.[ch]
+cp rassert.h moveable_core_contiguous_swap
sed -e 's/^\/\/#define INDIRECT_SWAP 1/#define INDIRECT_SWAP 1/' -i swap.h
./cpp.sh moveable_core_indirect_swap "$@"
cp rassert.h moveable_core_indirect_swap
-
-sed -e 's/^\/\/#define INODE_SWAP 1/#define INODE_SWAP 1/' -i process.h
-./cpp.sh moveable_core_inode_swap "$@"
-rm moveable_core_inode_swap/block_pool.[ch] moveable_core_inode_swap/swap.[ch]
-cp rassert.h fuzix_fs.[ch] util.[ch] moveable_core_inode_swap
-
-sed -e 's/^#define INODE_SWAP 1/\/\/#define INODE_SWAP 1/' -i process.h
--- /dev/null
+#!/usr/bin/env python3
+
+import sys
+
+nest = 0
+for line in sys.stdin:
+ if line[:3] == '#if':
+ nest += 1
+ elif line[:6] == '#endif':
+ nest -= 1
+ sys.stdout.write(line.rstrip() + ('@' if nest else '\n'))
--- /dev/null
+#!/bin/sh
+mkdir $4
+for i in $2/*.[ch]
+do
+ f=`basename $i`
+ if test -f $3/$f
+ then
+ sed -e 's/^#/##/' <$2/$f >a
+ sed -e 's/^#/##/' <$3/$f >b
+ diff --ifdef=$1 a b |./ifdef.py |sed -e 's/^#if\([^@]*\)@@/@#if\1@/; s/@@#else\([^@]*\)/@#else\1@/; s/@@#endif\([^@]*\)/@#endif\1@/' >$4/$f
+ else
+ sed -e 's/^#/##/' <$2/$f >$4/$f
+ fi
+done
+for i in $3/*.[ch]
+do
+ f=`basename $i`
+ if ! test -f $2/$f
+ then
+ sed -e 's/^#/##/' <$3/$f >$4/$f
+ fi
+done
+++ /dev/null
-CFLAGS=-g -Wall
-
-all: process_test_gen process_test_run
-
-process_test_gen: process_test_gen.o
- ${CC} ${CFLAGS} -o $@ $^
-
-process_test_gen.o: process_test_gen.c process_test.h
-
-process_test_run: process_test_run.o process.o core.o fuzix_fs.o util.o block_pool.o pool.o
- ${CC} ${CFLAGS} -o $@ $^
-
-process_test_run.o: process_test_run.c process.h core.h fuzix_fs.h util.h pool.h
-process.o: process.c process.h core.h fuzix_fs.h util.h pool.h
-core.o: core.c core.h pool.h
-fuzix_fs.o: fuzix_fs.c fuzix_fs.h
-block_pool.o: block_pool.c block_pool.h
-pool.o: pool.c pool.h
-
-clean:
- rm -f *.o process_test_run
+++ /dev/null
-#!/bin/sh
-
-echo "generate test script"
-./process_test_gen 16 248 1024 32 >process_test.txt
-
-echo "run test script"
-rm -f fs.bin
-# need 1536 x 0.5 kbyte for 192 x 4 = 768 kbytes, plus 1 x 0.5 kbyte root dir
-../mkfs fs.bin 63 1600
-./process_test_run 16 64 fs.bin 8 384 <process_test.txt
--- /dev/null
+#!/bin/sh
+./ifdef.sh INDIRECT_SWAP indirect_core_preallocate_swap indirect_core_indirect_swap indirect_core
+./ifdef.sh INDIRECT_SWAP moveable_core_preallocate_swap moveable_core_indirect_swap moveable_core
+./ifdef.sh INDIRECT_CORE moveable_core indirect_core new
+sed -e 's/@/\n/g' -i new/*.[ch]
+sed -e 's/^#\+/#/' -i new/*.[ch]
+++ /dev/null
-CFLAGS=-g -Wall
-
-all: process_test_gen process_test_run
-
-process_test_gen: process_test_gen.o
- ${CC} ${CFLAGS} -o $@ $^
-
-process_test_gen.o: process_test_gen.c process_test.h
-
-process_test_run: process_test_run.o process.o core.o fuzix_fs.o util.o pool.o
- ${CC} ${CFLAGS} -o $@ $^
-
-process_test_run.o: process_test_run.c process.h core.h fuzix_fs.h util.h pool.h
-process.o: process.c process.h core.h fuzix_fs.h util.h pool.h
-core.o: core.c core.h pool.h
-fuzix_fs.o: fuzix_fs.c fuzix_fs.h
-pool.c: pool.o pool.h
-
-clean:
- rm -f *.o process_test_run
+++ /dev/null
-#!/bin/sh
-
-echo "generate test script"
-./process_test_gen 16 248 1024 32 >process_test.txt
-
-echo "run test script"
-rm -f fs.bin
-# need 1536 x 0.5 kbyte for 192 x 4 = 768 kbytes, plus 1 x 0.5 kbyte root dir
-../mkfs fs.bin 63 1600
-./process_test_run 16 64 fs.bin 8 <process_test.txt
avail = head->avail - size_change;
if (avail < 0)
return false;
-
+
// work in specified direction, then reverse
dir = (mode & POOL_ALLOC_MODE_LAST_FIT) != 0;
while (true) {
//printf("back %p(%d)->%p(%d)\n", p, p->limit, q, q->base);
}
- // see if target fits in current gap
+ // see if target fits in current gap
gap = q->base - p->limit;
#ifdef TRY_BLOCKER
if (target_size <= gap) {
#include "process.h"
#include "rassert.h"
-#ifdef INODE_SWAP
-#define UCP 1
-#include "fuzix_fs.h"
-#else
#include "swap.h"
-#endif
struct process *processes;
int n_processes;
-#ifdef INODE_SWAP
-long process_avail, process_spare;
-#else
int process_avail;
-#endif
struct lru_item lru_head;
struct process *victim;
-#ifdef INODE_SWAP
-// adds indirect and double indirect block(s) to estimate disk size of file
-// also does another estimation based on the internal fragmentation of core
-// returns the larger of the two estimates
-static long estimate_size(long size) {
- long core_size;
- int blocks, disk_blocks;
- long disk_size;
-
- core_size = (size + (BLOCK_SIZE - 1)) & ~(BLOCK_SIZE - 1);
- assert(size < (long)DISK_BLOCK_SIZE << 16);
- blocks = (int)((size + (DISK_BLOCK_SIZE - 1)) >> DISK_BLOCK_SHIFT);
- disk_blocks = blocks;
- if (blocks > 18) {
- // has indirect blocks
- blocks = (blocks + (DISK_INDIRECT_SIZE - 1 - 18)) >> DISK_INDIRECT_SHIFT;
- disk_blocks += blocks;
- if (blocks > 1) {
- // has double indirect disk_blocks
- blocks = (blocks + (DISK_INDIRECT_SIZE - 1 - 1)) >> DISK_INDIRECT_SHIFT;
- assert(blocks == 1);
- disk_blocks += blocks;
- }
- }
- disk_size = (long)disk_blocks << DISK_BLOCK_SHIFT;
- return core_size >= disk_size ? core_size : disk_size;
-}
-
-// basically the inverse of the above, estimates size of files that could be
-// created if space is divided into at most n files, but a bit conservatively
-static long estimate_free(void) {/*int n) {*/
- int blocks;
-
- // at most one partial indirect and double indirect block per file
- blocks = fs_tab[0].s_tfree - 2;/*(n << 1);*/
- // max number of FULL indirect blocks (ignore the 18 direct blocks)
- blocks -= blocks / (DISK_INDIRECT_SIZE + 1);
- return (long)blocks << DISK_BLOCK_SHIFT;
-}
-#endif
-
void process_init(int n, int spare) {
-#ifndef NDEBUG
int i;
-#endif
-#if defined(INODE_SWAP) && defined(PREALLOCATE_CORE)
- long core_table_avail;
-#endif
-
+
processes = calloc(n, sizeof(struct process));
rassert(processes);
n_processes = n;
-#ifndef NDEBUG
for (i = 0; i < n_processes; ++i)
processes[i].size = -1L;
-#endif
-#ifdef INODE_SWAP
- process_avail =
- ((long)(CORE_AVAIL - spare) << BLOCK_SHIFT) +
- ((long)fs_tab[0].s_tfree << DISK_BLOCK_SHIFT);
-#ifdef PREALLOCATE_CORE
- core_table_avail = (long)core_table.avail << BLOCK_SHIFT;
- if (process_avail > core_table_avail)
- process_avail = core_table_avail;
-#endif
- //printf("process_avail %ld\n", process_avail);
-#else
- process_avail = CORE_AVAIL + SWAP_AVAIL - spare;
-#ifdef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
+#ifndef INDIRECT_SWAP
+ process_avail = core_table.avail + swap_table.avail - spare;
+#else /* INDIRECT_SWAP */
+ process_avail = core_table.avail + swap_block_pool.avail - spare;
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
+#ifndef INDIRECT_SWAP
+ process_avail = core_block_pool.avail + swap_table.avail - spare;
+#else /* INDIRECT_SWAP */
+ process_avail = core_block_pool.avail + swap_block_pool.avail - spare;
+#endif /* INDIRECT_SWAP */
if (process_avail > core_table.avail)
process_avail = core_table.avail;
-#endif
-#ifdef PREALLOCATE_SWAP
+#endif /* INDIRECT_CORE */
if (process_avail > swap_table.avail)
process_avail = swap_table.avail;
-#endif
//printf("process_avail %d\n", process_avail);
-#endif
lru_head.prev = &lru_head;
lru_head.next = &lru_head;
}
static bool do_swap_out(int swap_out) {
-#ifndef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
int victim_core_blocks;
-#endif
-#ifndef PREALLOCATE_SWAP
- int victim_swap_blocks;
-#endif
-#ifdef INODE_SWAP
- int blocks, core_base;
-#else
+#endif /* ! INDIRECT_CORE */
int blocks, swap_base, core_base;
-#endif
long size;
-#ifdef INODE_SWAP
-#ifdef INDIRECT_CORE
- int i, j, core_block;
-#endif
-#elif defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+#ifndef INDIRECT_CORE
+#ifdef INDIRECT_SWAP
+ int i, j, core_block, swap_block;
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
int i, j, core_block, swap_block;
-#endif
+#endif /* INDIRECT_CORE */
// loop entry code for the case of an existing victim
if (swap_out > 0) {
if (victim) {
// calculate amount to swap out
-#if !defined(PREALLOCATE_SWAP) || defined(INDIRECT_SWAP)
-#ifdef INODE_SWAP
- blocks = (int)(estimate_free(/*1*/) >> BLOCK_SHIFT);
-#else
- blocks = SWAP_AVAIL;
-#endif
+#ifndef INDIRECT_CORE
+#ifdef INDIRECT_SWAP
+ blocks = swap_block_pool.avail;
if (blocks == 0)
return false;
if (blocks > swap_out)
blocks = swap_out;
-#endif
-#ifndef PREALLOCATE_CORE
+#endif /* INDIRECT_SWAP */
victim_core_blocks =
victim->core_item.limit - victim->core_item.base;
-#endif
-#if defined(PREALLOCATE_SWAP) && !defined(INDIRECT_SWAP)
+#ifndef INDIRECT_SWAP
blocks = swap_out < victim_core_blocks ? swap_out : victim_core_blocks;
-#else
+#else /* INDIRECT_SWAP */
if (blocks > victim_core_blocks)
blocks = victim_core_blocks;
-#endif
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
+#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)
+ return false;
+ if (blocks > swap_out)
+ blocks = 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
-#ifndef PREALLOCATE_SWAP
- victim_swap_blocks =
-#ifdef INODE_SWAP
- (int)(victim->swap_inode->c_node.i_size >> BLOCK_SHIFT);
-#else
- victim->swap_item.limit - victim->swap_item.base;
-#endif
-#endif
-#if !defined(INODE_SWAP) && !defined(PREALLOCATE_SWAP)
- rassert(
- pool_alloc(
- &swap_table,
- &victim->swap_item,
- victim_swap_blocks + blocks,
-#ifdef MOVEABLE_SWAP
- POOL_ALLOC_MODE_MOVEABLE |
- POOL_ALLOC_MODE_REALLOC
-#else
- POOL_ALLOC_MODE_REALLOC
-#endif
- )
- );
-#endif
goto loop_entry;
}
// loop for the case of no existing victim
do {
// calculate amount to swap out
-#if !defined(PREALLOCATE_SWAP) || defined(INDIRECT_SWAP)
-#ifdef INODE_SWAP
- blocks = (int)(estimate_free(/*1*/) >> BLOCK_SHIFT);
-#else
- blocks = SWAP_AVAIL;
-#endif
+#ifdef INDIRECT_SWAP
+ blocks = swap_block_pool.avail;
if (blocks == 0)
return false;
if (blocks > swap_out)
blocks = swap_out;
-#endif
+#endif /* INDIRECT_SWAP */
assert(lru_head.prev != &lru_head);
victim = (struct process *)lru_head.prev;
victim_core_blocks =
victim->core_item.limit - victim->core_item.base;
-#if defined(PREALLOCATE_SWAP) && !defined(INDIRECT_SWAP)
+#ifndef INDIRECT_SWAP
blocks = swap_out < victim_core_blocks ? swap_out : victim_core_blocks;
-#else
+#else /* INDIRECT_SWAP */
if (blocks > victim_core_blocks)
blocks = victim_core_blocks;
-#endif
+#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;
-#if !defined(INODE_SWAP) && !defined(PREALLOCATE_SWAP)
- rassert(
- pool_alloc(
- &swap_table,
- &victim->swap_item,
- blocks,
-#ifdef MOVEABLE_SWAP
- POOL_ALLOC_MODE_MOVEABLE
-#else
- 0
-#endif
- )
- );
-#endif
// remove from LRU list
victim->lru_item.prev->next = victim->lru_item.next;
loop_entry:
// calculate transfer parameters
-#ifndef INODE_SWAP
-#ifdef PREALLOCATE_SWAP
swap_base = victim->swap_item.base + victim_swap_blocks;
-#else
- swap_base = victim->swap_item.limit - blocks;
-#endif
-#endif
-#ifdef PREALLOCATE_CORE
- core_base = victim->core_item.base + victim_swap_blocks;
-#else
+#ifndef INDIRECT_CORE
core_base = victim->core_item.base;
-#endif
+#else /* INDIRECT_CORE */
+ core_base = victim->core_item.base + victim_swap_blocks;
+#endif /* INDIRECT_CORE */
size = (long)blocks << BLOCK_SHIFT;
// see if last transfer for victim
size += ((victim->size - 1L) | -BLOCK_SIZE) + 1L;
// transfer data to swap
-#ifdef INDIRECT_SWAP
- rassert(
- block_pool_alloc(&swap_block_pool, swap_table_mem + swap_base, blocks)
- );
-#endif
-#ifdef INODE_SWAP
- //printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, victim_swap_blocks, victim_swap_blocks + blocks);
-#else
+#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);
-#endif
-#if !defined(INDIRECT_CORE) && !defined(INDIRECT_SWAP)
-#ifdef INODE_SWAP
- swap_write(
- victim->swap_inode,
- (long)victim_swap_blocks << BLOCK_SHIFT,
- (long)core_base << BLOCK_SHIFT,
- size
- );
-#else
core_to_swap_copy(
(long)core_base << BLOCK_SHIFT,
(long)swap_base << BLOCK_SHIFT,
size
+#else /* INDIRECT_SWAP */
+ rassert(
+ block_pool_alloc(&swap_block_pool, swap_table_mem + swap_base, blocks)
+#endif /* INDIRECT_SWAP */
);
-#endif
-#else
+#ifdef INDIRECT_SWAP
+ //printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, swap_base, swap_base + blocks);
j = (int)(size >> BLOCK_SHIFT);
//printf("blocks");
for (i = 0; i < j; ++i) {
core_block = core_base + i;
-#ifdef INDIRECT_CORE
- core_block = core_table_mem[core_block];
- //printf(" %d", core_block);
-#endif
-#ifdef INODE_SWAP
- swap_write(
- victim->swap_inode,
- (long)(victim_swap_blocks + i) << BLOCK_SHIFT,
+ swap_block = swap_base + i;
+ swap_block = swap_table_mem[swap_block];
+ //printf(" %d", swap_block);
+ core_to_swap_copy(
(long)core_block << BLOCK_SHIFT,
+ (long)swap_block << BLOCK_SHIFT,
BLOCK_SIZE
);
-#else
+ }
+ j = (int)size & (BLOCK_SIZE - 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(
+ (long)core_block << BLOCK_SHIFT,
+ (long)swap_block << BLOCK_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 = (int)(size >> BLOCK_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];
-#ifdef INDIRECT_CORE
//printf(",%d", swap_block);
-#else
- //printf(" %d", swap_block);
-#endif
-#endif
+#endif /* INDIRECT_SWAP */
core_to_swap_copy(
(long)core_block << BLOCK_SHIFT,
(long)swap_block << BLOCK_SHIFT,
BLOCK_SIZE
);
-#endif
}
j = (int)size & (BLOCK_SIZE - 1);
if (j) {
core_block = core_base + i;
-#ifdef INDIRECT_CORE
core_block = core_table_mem[core_block];
//printf(" %d", core_block);
-#endif
-#ifdef INODE_SWAP
- swap_write(
- victim->swap_inode,
- (long)(victim_swap_blocks + i) << BLOCK_SHIFT,
- (long)core_block << BLOCK_SHIFT,
- j
- );
-#else
swap_block = swap_base + i;
#ifdef INDIRECT_SWAP
swap_block = swap_table_mem[swap_block];
-#ifdef INDIRECT_CORE
//printf(",%d", swap_block);
-#else
- //printf(" %d", swap_block);
-#endif
-#endif
+#endif /* INDIRECT_SWAP */
//putchar('*');
core_to_swap_copy(
(long)core_block << BLOCK_SHIFT,
(long)swap_block << BLOCK_SHIFT,
j
);
-#endif
}
//printf("\n");
-#endif
-#ifdef INDIRECT_CORE
block_pool_free(&core_block_pool, core_table_mem + core_base, blocks);
-#endif
+#endif /* INDIRECT_CORE */
victim_swap_blocks += blocks;
// see if victim fully swapped out
if (victim_core_blocks) {
-#ifndef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
// no, reduce core allocation
rassert(
pool_alloc(
&victim->core_item,
victim_core_blocks,
POOL_ALLOC_MODE_LAST_FIT |
-#ifdef MOVEABLE_CORE
POOL_ALLOC_MODE_MOVEABLE |
-#endif
POOL_ALLOC_MODE_REALLOC
)
);
-#endif
+#endif /* ! INDIRECT_CORE */
// in this case there can't be any further victim to swap
// report whether aborted due to swap space or user request
}
//printf("victimized %d\n", (int)(victim - processes));
-#ifndef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
// remove from core pool
pool_free(&core_table, &victim->core_item);
-#endif
+#endif /* ! INDIRECT_CORE */
victim = NULL;
swap_out -= blocks;
}
bool process_alloc(struct process *process, long size) {
-#ifdef INODE_SWAP
- long estimated_size;
- int i;
-#endif
int blocks;
// must not be already allocated
assert(process->size == -1L);
// check blocks
-#ifdef INODE_SWAP
- estimated_size = estimate_size(size);
- if (
- process_avail < estimated_size ||
- (process->swap_inode = i_open(0, 0)) == NULL
- )
- return false;
- //printf("open inode %d\n", process->swap_inode->c_num);
-
- // initialize swap inode
- process->swap_inode->c_node.i_uid = udata.u_euid;
- process->swap_inode->c_node.i_gid = udata.u_egid;
-
- process->swap_inode->c_node.i_mode = F_REG; /* For the time being */
- process->swap_inode->c_node.i_nlink = 0;
- process->swap_inode->c_node.i_size = 0;
- for (i = 0; i < 20; ++i)
- process->swap_inode->c_node.i_addr[i] = 0;
- wr_inode(process->swap_inode);
-
- blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-#else
blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
if (
-#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
process_avail < blocks ||
!pool_alloc(
&swap_table,
blocks,
0
)
-#else
- process_avail < blocks
-#endif
)
return false;
-#endif
// free up as much core as we need to
- rassert(do_swap_out(blocks - CORE_AVAIL));
+#ifndef INDIRECT_CORE
+ rassert(do_swap_out(blocks - core_table.avail));
+#else /* INDIRECT_CORE */
+ rassert(do_swap_out(blocks - core_block_pool.avail));
+#endif /* INDIRECT_CORE */
// allocate core and possible swap
-#ifdef MOVEABLE_CORE
+#ifndef INDIRECT_CORE
rassert(
pool_alloc(
- &core_table,
- &process->core_item,
- blocks,
- POOL_ALLOC_MODE_MOVEABLE
- )
- );
-#else
+#else /* INDIRECT_CORE */
if (
!pool_alloc(
+#endif /* INDIRECT_CORE */
&core_table,
&process->core_item,
blocks,
+#ifndef INDIRECT_CORE
+ POOL_ALLOC_MODE_MOVEABLE
+#else /* INDIRECT_CORE */
0
)
) {
-#ifdef INODE_SWAP
- //printf("deref inode %d\n", process->swap_inode->c_num);
- i_deref(process->swap_inode);
-#elif defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
pool_free(&swap_table, &process->swap_item);
-#endif
return false;
}
-#endif
-#if defined(PREALLOCATE_SWAP) && defined(MOVEABLE_SWAP)
- rassert(
- pool_alloc(
- &swap_table,
- &process->swap_item,
- blocks,
- POOL_ALLOC_MODE_MOVEABLE
- )
- );
-#endif
-#ifdef INDIRECT_CORE
// populate new table with physical blocks
rassert(
block_pool_alloc(
&core_block_pool,
core_table_mem + process->core_item.base,
blocks
+#endif /* INDIRECT_CORE */
)
);
-#endif
-
+
// insert at head of LRU list
process->lru_item.prev = &lru_head;
process->lru_item.next = lru_head.next;
// track total allocation
process->size = size;
-#ifdef INODE_SWAP
- process_avail -= estimated_size;
-#else
process_avail -= blocks;
-#endif
return true;
}
bool process_realloc(struct process *process, long size) {
-#ifdef INODE_SWAP
- long estimated_size, old_estimated_size, estimated_size_change;
-#endif
int blocks, old_blocks, blocks_change;
// must be already allocated
assert(process->lru_item.prev != NULL);
// check blocks
-#ifdef INODE_SWAP
- estimated_size = estimate_size(size);
- old_estimated_size = estimate_size(process->size);
- estimated_size_change = estimated_size - old_estimated_size;
- if (process_avail < estimated_size_change)
- return false;
-
- blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
- old_blocks = (int)((process->size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
- blocks_change = blocks - old_blocks;
-#else
blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
old_blocks = (int)((process->size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
blocks_change = blocks - old_blocks;
if (process_avail < blocks_change)
return false;
-#endif
-
-#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
+
// this is a bit clunky, we want realloc without copying old contents,
// so we free, try to allocate larger, if that fails, allocate original
pool_free(&swap_table, &process->swap_item);
);
return false;
}
-#endif
// free up as much core as we need to
- rassert(do_swap_out(blocks_change - CORE_AVAIL));
+#ifndef INDIRECT_CORE
+ rassert(do_swap_out(blocks_change - core_table.avail));
+#else /* INDIRECT_CORE */
+ rassert(do_swap_out(blocks_change - core_block_pool.avail));
+#endif /* INDIRECT_CORE */
// reallocate core and possible swap
-#ifdef MOVEABLE_CORE
+#ifndef INDIRECT_CORE
rassert(
pool_alloc(
- &core_table,
- &process->core_item,
- blocks,
- POOL_ALLOC_MODE_MOVEABLE |
- POOL_ALLOC_MODE_REALLOC
- )
- );
-#else
+#else /* INDIRECT_CORE */
if (
!pool_alloc(
+#endif /* INDIRECT_CORE */
&core_table,
&process->core_item,
blocks,
+#ifndef INDIRECT_CORE
+ POOL_ALLOC_MODE_MOVEABLE |
+ POOL_ALLOC_MODE_REALLOC
+#else /* INDIRECT_CORE */
POOL_ALLOC_MODE_REALLOC
+#endif /* INDIRECT_CORE */
)
+#ifndef INDIRECT_CORE
+ );
+#else /* INDIRECT_CORE */
) {
-#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
pool_free(&swap_table, &process->swap_item);
rassert(
pool_alloc(
0
)
);
-#endif
return false;
}
-#endif
-#if defined(PREALLOCATE_SWAP) && defined(MOVEABLE_SWAP)
- pool_free(&swap_table, &process->swap_item);
- rassert(
- pool_alloc(
- &swap_table,
- &process->swap_item,
- blocks,
- POOL_ALLOC_MOVEABLE
- )
- );
-#endif
-#ifdef INDIRECT_CORE
// populate new table with physical blocks or discard them
if (blocks_change >= 0)
rassert(
core_table_mem + process->core_item.limit,
-blocks_change
);
-#endif
-
+#endif /* INDIRECT_CORE */
+
// track total allocation
process->size = size;
-#ifdef INODE_SWAP
- process_avail -= estimated_size_change;
-#else
process_avail -= blocks_change;
-#endif
return true;
}
void process_run(struct process *process) {
int blocks, process_core_blocks, process_swap_blocks;
-#ifdef INODE_SWAP
- int core_base;
-#else
int swap_base, core_base;
-#endif
long size;
-#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+#ifndef INDIRECT_CORE
+#ifdef INDIRECT_SWAP
+ int i, j;
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
int i, j;
-#endif
+#endif /* INDIRECT_CORE */
// must be already allocated
assert(process->size != -1L);
if (process != victim) {
// fully in swap, only the swap item is meaningful
process_swap_blocks =
-#ifdef INODE_SWAP
- (int)(
- ((long)process->swap_inode->c_node.i_size + (BLOCK_SIZE - 1)) >>
- BLOCK_SHIFT
- );
-#else
process->swap_item.limit - process->swap_item.base;
-#endif
// free up as much core as we can
- do_swap_out(process_swap_blocks - CORE_AVAIL);
- blocks = CORE_AVAIL;
+#ifndef INDIRECT_CORE
+ do_swap_out(process_swap_blocks - core_table.avail);
+ blocks = core_table.avail;
+#else /* INDIRECT_CORE */
+ do_swap_out(process_swap_blocks - core_block_pool.avail);
+ blocks = core_block_pool.avail;
+#endif /* INDIRECT_CORE */
if (blocks > process_swap_blocks)
blocks = process_swap_blocks;
// add to core pool
process_core_blocks = 0;
-#ifndef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
rassert(
pool_alloc(
&core_table,
&process->core_item,
blocks,
-#ifdef MOVEABLE_CORE
POOL_ALLOC_MODE_MOVEABLE
-#else
- 0
-#endif
)
);
-#endif
+#endif /* ! INDIRECT_CORE */
goto loop_entry_full;
}
// victim, take over the dedicated pool items
-#ifdef PREALLOCATE_CORE
- process_core_blocks = victim_core_blocks;
-#else
+#ifndef INDIRECT_CORE
process_core_blocks =
victim->core_item.limit - victim->core_item.base;
-#endif
-#ifdef PREALLOCATE_SWAP
+#else /* INDIRECT_CORE */
+ process_core_blocks = victim_core_blocks;
+#endif /* INDIRECT_CORE */
process_swap_blocks = victim_swap_blocks;
-#else
- process_swap_blocks =
-#ifdef INODE_SWAP
- (int)(victim->swap_inode->c_node.i_size >> BLOCK_SHIFT);
-#else
- victim->swap_item.limit - victim->swap_item.base;
-#endif
-#endif
victim = NULL;
-#ifndef PREALLOCATE_SWAP
- goto loop_entry_partial;
-#endif
// loop for case of partially in core, partially in swap
do {
-#ifndef PREALLOCATE_SWAP
- // reduce swap allocation
-#ifdef INODE_SWAP
- udata.u_offset = (long)process_swap_blocks << BLOCK_SHIFT;
- f_trunc(process->swap_inode);
-#else
- rassert(
- pool_alloc(
- &swap_table,
- &process->swap_item,
- process_swap_blocks,
-#ifdef MOVEABLE_SWAP
- POOL_ALLOC_MODE_MOVEABLE |
- POOL_ALLOC_MODE_REALLOC
-#else
- POOL_ALLOC_MODE_REALLOC
-#endif
- )
- );
-#endif
-
- loop_entry_partial:
-#endif
// free up as much core as we can
- do_swap_out(process_swap_blocks - CORE_AVAIL);
- blocks = CORE_AVAIL;
+#ifndef INDIRECT_CORE
+ do_swap_out(process_swap_blocks - core_table.avail);
+ blocks = core_table.avail;
+#else /* INDIRECT_CORE */
+ do_swap_out(process_swap_blocks - core_block_pool.avail);
+ blocks = core_block_pool.avail;
+#endif /* INDIRECT_CORE */
if (blocks > process_swap_blocks)
blocks = process_swap_blocks;
// increase core allocation
-#ifndef PREALLOCATE_CORE
+#ifndef INDIRECT_CORE
rassert(
pool_alloc(
- &core_table,
+ &core_table,
&process->core_item,
process_core_blocks + blocks,
POOL_ALLOC_MODE_LAST_FIT |
-#ifdef MOVEABLE_CORE
POOL_ALLOC_MODE_MOVEABLE |
-#endif
POOL_ALLOC_MODE_REALLOC
)
);
-#endif
+#endif /* ! INDIRECT_CORE */
loop_entry_full:
// calculate transfer parameters
process_swap_blocks -= blocks;
-#ifndef INODE_SWAP
-#ifdef PREALLOCATE_SWAP
swap_base = process->swap_item.base + process_swap_blocks;
-#else
- swap_base = process->swap_item.limit - blocks;
-#endif
-#endif
-#ifdef PREALLOCATE_CORE
- core_base = process->core_item.base + process_swap_blocks;
-#else
+#ifndef INDIRECT_CORE
core_base = process->core_item.base;
-#endif
+#else /* INDIRECT_CORE */
+ core_base = process->core_item.base + process_swap_blocks;
+#endif /* INDIRECT_CORE */
size = (long)blocks << BLOCK_SHIFT;
// transfer data to core
#ifdef INDIRECT_CORE
block_pool_alloc(&core_block_pool, core_table_mem + core_base, blocks);
-#endif
-#ifdef INODE_SWAP
- //printf("read swap [%d,%d) to core [%d,%d)\n", process_swap_blocks, process_swap_blocks + blocks, core_base, core_base + blocks);
-#else
+#endif /* INDIRECT_CORE */
//printf("read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + blocks, core_base, core_base + blocks);
-#endif
-#if !defined(INDIRECT_CORE) && !defined(INDIRECT_SWAP)
-#ifdef INODE_SWAP
- swap_read(
- process->swap_inode,
- (long)process_swap_blocks << BLOCK_SHIFT,
- (long)core_base << BLOCK_SHIFT,
- size
- );
-#else
+#ifndef INDIRECT_CORE
+#ifndef INDIRECT_SWAP
swap_to_core_copy(
(long)swap_base << BLOCK_SHIFT,
(long)core_base << BLOCK_SHIFT,
size
);
-#endif
-#else
+#else /* INDIRECT_SWAP */
j = (int)(size >> BLOCK_SHIFT);
//printf("blocks");
for (i = 0; i < j; ++i) {
int core_block = core_base + i;
-#ifdef INDIRECT_CORE
- core_block = core_table_mem[core_block];
- //printf(" %d", core_block);
-#endif
-#ifdef INODE_SWAP
- swap_read(
- process->swap_inode,
- (long)(process_swap_blocks + i) << BLOCK_SHIFT,
+ int swap_block = swap_base + i;
+ swap_block = swap_table_mem[swap_block];
+ //printf(",%d", swap_block);
+ swap_to_core_copy(
+ (long)swap_block << BLOCK_SHIFT,
(long)core_block << BLOCK_SHIFT,
BLOCK_SIZE
);
-#else
+ }
+ j = (int)size & (BLOCK_SIZE - 1);
+ if (j) {
+ int core_block = core_base + i;
int swap_block = swap_base + i;
-#ifdef INDIRECT_SWAP
swap_block = swap_table_mem[swap_block];
+ //printf(",%d", swap_block);
+ swap_to_core_copy(
+ (long)swap_block << BLOCK_SHIFT,
+ (long)core_block << BLOCK_SHIFT,
+ j
+ );
+ }
+ //printf("\n");
+ block_pool_free(&swap_block_pool, swap_table_mem + swap_base, blocks);
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
+ j = (int)(size >> BLOCK_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);
-#else
- //printf(" %d", swap_block);
-#endif
-#endif
+#endif /* INDIRECT_SWAP */
swap_to_core_copy(
(long)swap_block << BLOCK_SHIFT,
(long)core_block << BLOCK_SHIFT,
BLOCK_SIZE
);
-#endif
}
j = (int)size & (BLOCK_SIZE - 1);
if (j) {
int core_block = core_base + i;
-#ifdef INDIRECT_CORE
core_block = core_table_mem[core_block];
//printf(" %d", core_block);
-#endif
-#ifdef INODE_SWAP
- swap_read(
- process->swap_inode,
- (long)(process_swap_blocks + i) << BLOCK_SHIFT,
- (long)core_block << BLOCK_SHIFT,
- j
- );
-#else
int swap_block = swap_base + i;
#ifdef INDIRECT_SWAP
swap_block = swap_table_mem[swap_block];
-#ifdef INDIRECT_SWAP
//printf(",%d", swap_block);
-#else
- //printf(" %d", swap_block);
-#endif
-#endif
+#endif /* INDIRECT_SWAP */
swap_to_core_copy(
(long)swap_block << BLOCK_SHIFT,
(long)core_block << BLOCK_SHIFT,
- j
+ j
);
-#endif
}
//printf("\n");
-#endif
#ifdef INDIRECT_SWAP
block_pool_free(&swap_block_pool, swap_table_mem + swap_base, blocks);
-#endif
+#endif /* INDIRECT_SWAP */
+#endif /* INDIRECT_CORE */
} while (process_swap_blocks);
-
-#ifdef INODE_SWAP
- // delete swap data, retain swap inode
- udata.u_offset = 0;
- f_trunc(process->swap_inode);
-#elif !defined(PREALLOCATE_SWAP)
- // remove from swap pool
- pool_free(&swap_table, &process->swap_item);
-#endif
}
// insert at head of LRU list
}
void process_free(struct process *process) {
+#ifndef INDIRECT_CORE
#ifdef INDIRECT_SWAP
int swap_base;
-#ifndef PREALLOCATE_SWAP
- int victim_swap_blocks;
-#endif
-#endif
-#ifdef INDIRECT_CORE
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
+
+#ifdef INDIRECT_SWAP
+ int swap_base;
+#endif /* INDIRECT_SWAP */
int core_base;
-#ifndef PREALLOCATE_CORE
- int victim_core_blocks;
-#endif
-#endif
+#endif /* INDIRECT_CORE */
// must be already allocated
assert(process->size != -1L);
process->lru_item.prev->next = process->lru_item.next;
process->lru_item.next->prev = process->lru_item.prev;
-#ifdef INDIRECT_CORE
+#ifndef INDIRECT_CORE
+ pool_free(&core_table, &process->core_item);
+#else /* INDIRECT_CORE */
int core_base = process->core_item.base;
block_pool_free(
&core_block_pool,
core_table_mem + core_base,
process->core_item.limit - core_base
);
-#endif
-#ifndef PREALLOCATE_CORE
- pool_free(&core_table, &process->core_item);
-#endif
+#endif /* INDIRECT_CORE */
}
else if (process == victim) {
// victim, remove from core and swap pools
#ifdef INDIRECT_SWAP
swap_base = victim->swap_item.base;
-#ifndef PREALLOCATE_SWAP
- victim_swap_blocks = victim->swap_item.limit - swap_base;
-#endif
block_pool_free(
&swap_block_pool,
swap_table_mem + swap_base,
victim_swap_blocks
);
-#endif
-#if !defined(INODE_SWAP) && !defined(PREALLOCATE_SWAP)
- pool_free(&swap_table, &victim->swap_item);
-#endif
-#ifdef INDIRECT_CORE
-#ifdef PREALLOCATE_CORE
-#if defined(PREALLOCATE_SWAP) || defined(INDIRECT_SWAP)
+#endif /* INDIRECT_SWAP */
+#ifndef INDIRECT_CORE
+ pool_free(&core_table, &victim->core_item);
+#else /* INDIRECT_CORE */
core_base = victim->core_item.base + victim_swap_blocks;
-#elif defined(INODE_SWAP)
- core_base =
- victim->core_item.base +
- (int)(victim->swap_inode->c_node.i_size >> BLOCK_SHIFT);
-#else
- core_base =
- victim->core_item.base +
- victim->swap_item.limit -
- victim->swap_item.base;
-#endif
-#else
- core_base = victim->core_item.base;
- victim_core_blocks = victim->core_item.limit - core_base;
-#endif
block_pool_free(
&core_block_pool,
core_table_mem + core_base,
victim_core_blocks
);
-#endif
-#ifndef PREALLOCATE_CORE
- pool_free(&core_table, &victim->core_item);
-#endif
+#endif /* INDIRECT_CORE */
victim = NULL;
}
else {
swap_table_mem + swap_base,
process->swap_item.limit - swap_base
);
-#endif
-#if !defined(INODE_SWAP) && !defined(PREALLOCATE_SWAP)
- pool_free(&swap_table, &process->swap_item);
-#endif
+#endif /* INDIRECT_SWAP */
}
// free the per-process table entries
-#ifdef PREALLOCATE_CORE
+#ifdef INDIRECT_CORE
pool_free(&core_table, &process->core_item);
-#endif
-#ifdef PREALLOCATE_SWAP
+#endif /* INDIRECT_CORE */
pool_free(&swap_table, &process->swap_item);
-#elif defined(INODE_SWAP)
- //printf("deref inode %d\n", process->swap_inode->c_num);
- i_deref(process->swap_inode);
-#endif
-
+
// track total allocation
-#ifdef INODE_SWAP
- process_avail += estimate_size(process->size);
-#else
process_avail += (int)((process->size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-#endif
-#ifndef NDEBUG
process->size = -1L;
-#endif
}
#ifndef _PROCESS_H
#define _PROCESS_H 1
-#ifdef __FUZIX__
-#define BLOCK_SIZE 0x10
-#define BLOCK_SHIFT 4
-#else
#define BLOCK_SIZE 0x1000
#define BLOCK_SHIFT 12
-#endif
-
-//#define INODE_SWAP 1
-#ifdef INODE_SWAP
-#define DISK_BLOCK_SIZE 0x200
-#define DISK_BLOCK_SHIFT 9
-#define DISK_INDIRECT_SIZE 0x100
-#define DISK_INDIRECT_SHIFT 8
-#endif
#include "pool.h"
extern struct process {
struct lru_item lru_item; // must be first
struct pool_item core_item;
-#ifdef INODE_SWAP
- struct cinode *swap_inode;
-#else
struct pool_item swap_item;
-#endif
long size; // brk level
} *processes;
int n_processes;
-#ifdef INODE_SWAP
-extern long process_avail;
-#else
extern int process_avail;
-#endif
extern struct lru_item lru_head;
bool process_realloc(struct process *process, long size);
void process_run(struct process *process);
void process_free(struct process *process);
-
+
// abstract
-#ifdef INODE_SWAP
-void swap_read(
- struct cinode *inode,
- long offset,
- long core_base,
- long size
-);
-void swap_write(
- struct cinode *inode,
- long offset,
- long core_base,
- long size
-);
-#else
void core_to_swap_copy(long src_base, long dest_base, long size);
void swap_to_core_copy(long src_base, long dest_base, long size);
-#endif
#endif
#include <stdbool.h>
-#define PROCESS_TEST_TYPE_ALLOC 0
-#define PROCESS_TEST_TYPE_REALLOC 1
-#define PROCESS_TEST_TYPE_RUN 2
-#define PROCESS_TEST_TYPE_FREE 3
-
struct process_test {
int type;
int process;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef __FUZIX__
-#include <unistd.h>
-#endif
#include "process_test.h"
#include "rassert.h"
-#ifdef __FUZIX__
-#define BLOCK_SIZE 0x10
-#define BLOCK_SHIFT 4
-#else
#define BLOCK_SIZE 0x1000
#define BLOCK_SHIFT 12
-#endif
int rand_int(int n) {
return (int)((long long)rand() * n / (RAND_MAX + 1LL));
int pool_used = 0;
for (int i = 0; i < n_events; ++i) {
struct process_test test;
-#ifdef __FUZIX__
- memset(&test, 0, sizeof(test));
-#endif
test.process = rand_int(n_processes);
test.old_size = processes[test.process];
int blocks = (int)((test.size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
test.success = pool_used + blocks <= pool_blocks;
-#ifdef __FUZIX__
- test.type = PROCESS_TEST_TYPE_ALLOC;
- write(1, &test, sizeof(test));
-#else
printf(
"alloc %d %ld %s\n",
test.process,
test.size,
test.success ? "true" : "false"
);
-#endif
if (test.success) {
processes[test.process] = test.size;
else {
int old_blocks = (int)((test.old_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
if (rand_int(64)) {
-#ifdef __FUZIX__
- test.type = PROCESS_TEST_TYPE_RUN;
- write(1, &test, sizeof(test));
-#else
printf("run %d\n", test.process);
-#endif
if (rand_int(8) == 0) {
test.size = rand_long(process_size + 1);
int blocks = (int)((test.size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
test.success = pool_used + blocks - old_blocks <= pool_blocks;
-#ifdef __FUZIX__
- test.type = PROCESS_TEST_TYPE_REALLOC;
- write(1, &test, sizeof(test));
-#else
printf(
"realloc %d %ld %ld %s\n",
test.process,
test.size,
test.success ? "true" : "false"
);
-#endif
if (test.success) {
processes[test.process] = test.size;
}
}
else {
-#ifdef __FUZIX__
- test.type = PROCESS_TEST_TYPE_FREE;
- write(1, &test, sizeof(test));
-#else
printf("free %d %ld\n", test.process, test.old_size);
-#endif
processes[test.process] = -1;
pool_used -= old_blocks;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef __FUZIX__
-#include <unistd.h>
-#endif
#include "core.h"
#include "process.h"
#include "process_test.h"
#include "rassert.h"
-#ifdef INODE_SWAP
-#define UCP 1
-#include "fuzix_fs.h"
-#include "util.h"
-#else
#include "swap.h"
-#endif
-#ifdef __FUZIX__
-#define TRANSFER_SIZE 0x400
-#else
#define TRANSFER_SIZE 0x8000
-#endif
uint8_t *core_block_mem;
-#ifndef INODE_SWAP
uint8_t *swap_block_mem;
-#endif
void core_hash_init(int process, long base, long size, long offset) {
long i, addr;
-#ifdef __FUZIX__
- int hash;
-#else
long long hash;
-#endif
//printf("core_hash_init %d %ld(%d) %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT), offset, (int)(offset >> BLOCK_SHIFT));
for (i = 0L; i < size; ++i) {
addr =
(core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
(addr & (BLOCK_SIZE - 1));
-#endif
-#ifdef __FUZIX__
- hash = process * 17 + ((int)i + (int)offset) * 29;
-#else
+#endif /* INDIRECT_CORE */
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);
-#endif
rassert(core_block_mem[addr] == 0xaa);
core_block_mem[addr] = (uint8_t)hash;
}
void core_hash_verify(int process, long base, long size, long offset) {
long i, addr;
-#ifdef __FUZIX__
- int hash;
-#else
long long hash;
-#endif
//printf("core_hash_verify %d %ld(%d) %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT), offset, (int)(offset >> BLOCK_SHIFT));
for (i = 0L; i < size; ++i) {
addr =
(core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
(addr & (BLOCK_SIZE - 1));
-#endif
-#ifdef __FUZIX__
- hash = process * 17 + ((int)i + (int)offset) * 29;
-#else
+#endif /* INDIRECT_CORE */
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);
-#endif
rassert(core_block_mem[addr] == (uint8_t)hash);
core_block_mem[addr] = 0xaa;
}
}
-#ifdef INODE_SWAP
-void swap_hash_verify(int process, struct cinode *inode) {
- long offset, count;
- uint8_t buf[TRANSFER_SIZE];
- long i;
-#ifdef __FUZIX__
- int hash;
-#else
- long long hash;
-#endif
-
- for (
- offset = 0;
- (count = (long)inode->c_node.i_size - offset) > 0;
- offset += count
- ) {
- if (count > TRANSFER_SIZE)
- count = TRANSFER_SIZE;
-
- udata.u_base = (char *)buf;
- udata.u_count = count;
- udata.u_offset = offset;
- udata.u_error = 0;
- rassert(readi(inode) == count && udata.u_error == 0);
-
- for (i = 0; i < count; ++i) {
-#ifdef __FUZIX__
- hash = process * 17 + ((int)i + (int)offset) * 29;
-#else
- 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);
-#endif
- rassert(buf[i] == (uint8_t)hash);
- }
- }
-}
-#else
void swap_hash_verify(int process, long base, long size) {
long i, addr;
-#ifdef __FUZIX__
- int hash;
-#else
long long hash;
-#endif
//printf("swap_hash_verify %d %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
for (i = 0L; i < size; ++i) {
addr =
(swap_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
(addr & (BLOCK_SIZE - 1));
-#endif
-#ifdef __FUZIX__
- hash = process * 17 + (int)i * 29;
-#else
+#endif /* INDIRECT_SWAP */
hash = process * 17 + i * 29;
hash = (hash & 0xffffffffLL) + (hash >> 32);
hash = (hash & 0xffffLL) + (hash >> 16);
hash = (hash & 0xffLL) + (hash >> 8);
hash = (hash & 0xffLL) + (hash >> 8);
-#endif
rassert(swap_block_mem[addr] == (uint8_t)hash);
swap_block_mem[addr] = 0xaa;
}
}
-#endif
#ifndef INDIRECT_CORE
void core_copy(long src_base, long dest_base, long size) {
core_block_mem[src_limit + i] = 0xaa;
}
}
-#endif
+#endif /* ! INDIRECT_CORE */
-#ifdef INODE_SWAP
-void swap_read(
- struct cinode *inode,
- long offset,
- long core_base,
- long size
-) {
- long i, count;
-
- //printf("swap_read %d %ld %ld %ld\n", inode->c_num, offset, core_base, size);
- for (i = 0; i < size; ++i)
- rassert(core_block_mem[core_base + i] == 0xaa);
-
- for (i = 0; (count = size - i) > 0; i += count) {
- if (count > TRANSFER_SIZE)
- count = TRANSFER_SIZE;
-
- udata.u_base = (char *)(core_block_mem + core_base + i);
- udata.u_count = (int)count;
- udata.u_offset = (uint32_t)(offset + i);
- udata.u_error = 0;
- rassert(readi(inode) == (int)count && udata.u_error == 0);
- }
-}
-
-void swap_write(
- struct cinode *inode,
- long offset,
- long core_base,
- long size
-) {
- long i, count;
-
- //printf("swap_write %d %ld %ld %ld\n", inode->c_num, offset, core_base, size);
- for (i = 0; (count = size - i) > 0; i += count) {
- if (count > TRANSFER_SIZE)
- count = TRANSFER_SIZE;
-
- udata.u_base = (char *)(core_block_mem + core_base + i);
- udata.u_count = (int)count;
- udata.u_offset = (uint32_t)(offset + i);
- udata.u_error = 0;
- rassert(writei(inode) == (int)count && udata.u_error == 0);
- }
-
- memset(core_block_mem + core_base, 0xaa, size);
-}
-#else
#ifndef INDIRECT_SWAP
void swap_copy(long src_base, long dest_base, long size) {
long i;
swap_block_mem[src_limit + i] = 0xaa;
}
}
-#endif
+#endif /* ! INDIRECT_SWAP */
void core_to_swap_copy(long src_base, long dest_base, long size) {
long i;
memcpy(core_block_mem + dest_base, swap_block_mem + src_base, size);
memset(swap_block_mem + src_base, 0xaa, size);
}
-#endif
int main(int argc, char **argv) {
int n_processes;
int n_core_blocks;
-#ifdef INODE_SWAP
- char *fs_path;
-#else
int n_swap_blocks;
-#endif
int spare;
-#ifdef INDIRECT_CORE
+#ifndef INDIRECT_CORE
+#ifndef INDIRECT_SWAP
+ int i, j;
+#else /* INDIRECT_SWAP */
+ int swap_table_blocks;
+ int i, j, k;
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
int core_table_blocks;
-#endif
#ifdef INDIRECT_SWAP
int swap_table_blocks;
-#endif
-#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+#endif /* INDIRECT_SWAP */
int i, j, k;
-#else
- int i, j;
-#endif
+#endif /* INDIRECT_CORE */
struct process_test test;
-#ifndef __FUZIX__
char buf[256];
-#endif
bool result;
long actual_old_size;
int old_blocks, blocks, actual_old_blocks;
if (argc < 5) {
printf(
"usage: %s n_processes n_core_blocks "
-#ifdef INODE_SWAP
- "fs_path "
-#else
"n_swap_blocks "
-#endif
"spare"
-#if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
- "[core_table_blocks [swap_table_blocks]]"
-#elif defined(INDIRECT_CORE)
- "[core_table_blocks]"
-#elif defined(INDIRECT_SWAP)
+#ifndef INDIRECT_CORE
+#ifdef INDIRECT_SWAP
"[swap_table_blocks]"
-#endif
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
+#ifndef INDIRECT_SWAP
+ "[core_table_blocks]"
+#else /* INDIRECT_SWAP */
+ "[core_table_blocks [swap_table_blocks]]"
+#endif /* INDIRECT_SWAP */
+#endif /* INDIRECT_CORE */
"\n",
argv[0]
);
}
n_processes = atoi(argv[1]);
n_core_blocks = atoi(argv[2]);
-#ifdef INODE_SWAP
- fs_path = argv[3];
-#else
n_swap_blocks = atoi(argv[3]);
-#endif
spare = atoi(argv[4]);
-#if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
+#ifndef INDIRECT_CORE
+#ifdef INDIRECT_SWAP
+ swap_table_blocks = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
+#endif /* INDIRECT_SWAP */
+#else /* INDIRECT_CORE */
core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
+#ifdef INDIRECT_SWAP
swap_table_blocks = argc >= 7 ? atoi(argv[6]) : n_swap_blocks;
-#elif defined(INDIRECT_CORE)
- core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
-#elif defined(INDIRECT_SWAP)
- swap_table_blocks = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
-#endif
+#endif /* INDIRECT_SWAP */
+#endif /* INDIRECT_CORE */
-#ifdef INDIRECT_CORE
- core_init(n_core_blocks, core_table_blocks);
-#else
+#ifndef INDIRECT_CORE
core_init(n_core_blocks);
-#endif
+#else /* INDIRECT_CORE */
+ core_init(n_core_blocks, core_table_blocks);
+#endif /* INDIRECT_CORE */
core_block_mem = malloc((long)n_core_blocks << BLOCK_SHIFT);
rassert(core_block_mem);
memset(core_block_mem, 0xaa, (long)n_core_blocks << BLOCK_SHIFT);
-#ifdef INODE_SWAP
- fd_open(fs_path);
- xfs_init(0);
- printf("free inodes %d blocks %d\n", fs_tab[0].s_tinode, fs_tab[0].s_tfree);
-#else
-#ifdef INDIRECT_SWAP
- swap_init(n_swap_blocks, swap_table_blocks);
-#else
+#ifndef INDIRECT_SWAP
swap_init(n_swap_blocks);
-#endif
+#else /* INDIRECT_SWAP */
+ swap_init(n_swap_blocks, swap_table_blocks);
+#endif /* INDIRECT_SWAP */
swap_block_mem = malloc((long)n_swap_blocks << BLOCK_SHIFT);
rassert(swap_block_mem);
memset(swap_block_mem, 0xaa, (long)n_swap_blocks << BLOCK_SHIFT);
-#endif
process_init(n_processes, spare);
for (i = 0; i < n_processes; ++i)
while (true) {
//printf("avail %d %d(%d) %d(%d)\n", process_avail, CORE_AVAIL, core_table.avail, SWAP_AVAIL, swap_table.avail);
-#ifdef __FUZIX__
- if (read(0, &test, sizeof(test)) != sizeof(test))
- break;
-#else
switch ((unsigned int)scanf("%s", buf)) { // cast because of ACK bug
case -1:
goto done;
rassert(false);
}
if (strcmp(buf, "alloc") == 0) {
- test.type = PROCESS_TEST_TYPE_ALLOC;
+ test.type = 0;
rassert(
scanf(
"%d %ld %s",
rassert(false);
}
else if (strcmp(buf, "realloc") == 0) {
- test.type = PROCESS_TEST_TYPE_REALLOC;
+ test.type = 1;
rassert(
scanf(
"%d %ld %ld %s",
rassert(false);
}
else if (strcmp(buf, "run") == 0) {
- test.type = PROCESS_TEST_TYPE_RUN;
+ test.type = 2;
rassert(scanf("%d", &test.process) == 1);
rassert(test.process >= 0 && test.process < n_processes);
- }
+ }
else if (strcmp(buf, "free") == 0) {
- test.type = PROCESS_TEST_TYPE_FREE;
+ test.type = 3;
rassert(scanf("%d %ld", &test.process, &test.old_size) == 2);
rassert(test.process >= 0 && test.process < n_processes);
rassert(test.old_size >= 0);
}
else
rassert(false);
-#endif
switch (test.type) {
- case PROCESS_TEST_TYPE_ALLOC:
+ case 0:
printf(
"alloc %d %ld %s\n",
test.process,
test.size,
0L
);
-#ifdef PREALLOCATE_SWAP
//printf("new swap [%d,%d)\n", processes[test.process].swap_item.base, processes[test.process].swap_item.limit);
-#endif
}
}
break;
- case PROCESS_TEST_TYPE_REALLOC:
+ case 1:
printf(
"realloc %d %ld %ld %s\n",
test.process,
);
rassert(actual_old_blocks <= old_blocks);
}
-#ifdef PREALLOCATE_SWAP
//printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + actual_old_blocks, processes[test.process].swap_item.base, processes[test.process].swap_item.limit);
-#else
- //printf("old core [%d,%d)\n", core_base, core_base + actual_old_blocks);
-#endif
core_hash_verify(
test.process,
((long)core_base << BLOCK_SHIFT) + test.size,
processes[test.process].core_item.limit ==
core_base + blocks
);
-#ifdef PREALLOCATE_SWAP
//printf("new core [%d,%d) swap [%d,%d)\n", core_base, core_base + blocks, processes[test.process].swap_item.base, processes[test.process].swap_item.limit);
-#else
- //printf("new core [%d,%d)\n", core_base, core_base + blocks);
-#endif
core_hash_init(
test.process,
((long)core_base << BLOCK_SHIFT) + actual_old_size,
}
}
break;
- case PROCESS_TEST_TYPE_RUN:
+ case 2:
printf("run %d\n", test.process);
if (processes[test.process].size == -1L)
printf("... not allocated, ignore\n");
printf("... ok\n");
}
break;
- case PROCESS_TEST_TYPE_FREE:
+ case 3:
printf("free %d %ld\n", test.process, test.old_size);
if (processes[test.process].size == -1L)
printf("... not allocated, ignore\n");
core_size = processes[test.process].size;
}
else {
-#ifdef INODE_SWAP
- swap_base = 0;
- swap_size = (long)processes[test.process].swap_inode->c_node.i_size;
- swap_blocks = (int)((swap_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-#endif
if (processes + test.process == victim) { // victim
-#ifndef INODE_SWAP
swap_base = victim->swap_item.base;
-#ifdef PREALLOCATE_SWAP
swap_blocks = victim_swap_blocks;
-#else
- swap_blocks = victim->swap_item.limit - victim->swap_item.base;
-#endif
swap_size = swap_blocks << BLOCK_SHIFT;
-#endif
-#ifdef PREALLOCATE_CORE
- core_base = victim->core_item.base + swap_blocks;
- core_blocks = victim_core_blocks;
-#else
+#ifndef INDIRECT_CORE
core_base = victim->core_item.base;
core_blocks = victim->core_item.limit - core_base;
-#endif
+#else /* INDIRECT_CORE */
+ core_base = victim->core_item.base + swap_blocks;
+ core_blocks = victim_core_blocks;
+#endif /* INDIRECT_CORE */
core_size = victim->size - swap_size;
}
else { // fully in swap
-#ifndef INODE_SWAP
swap_base = processes[test.process].swap_item.base;
swap_blocks = processes[test.process].swap_item.limit - swap_base;
swap_size = processes[test.process].size;
-#endif
core_base = -1;
core_blocks = 0;
core_size = 0L;
);
swap_hash_verify(
test.process,
-#ifdef INODE_SWAP
- processes[test.process].swap_inode
-#else
(long)swap_base << BLOCK_SHIFT,
swap_size
-#endif
);
process_free(processes + test.process);
processes[test.process].size = -1L;
}
}
-#ifndef __FUZIX__
done:
-#endif
printf("final state:\n");
for (i = 0; i < n_processes; ++i) {
if (processes[i].size == -1L)
core_size = processes[i].size;
}
else {
-#ifdef INODE_SWAP
- swap_base = 0;
- swap_size = (long)processes[i].swap_inode->c_node.i_size;
- swap_blocks = (int)((swap_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-#endif
if (processes + i == victim) { // victim
-#ifndef INODE_SWAP
swap_base = victim->swap_item.base;
-#ifdef PREALLOCATE_SWAP
swap_blocks = victim_swap_blocks;
-#else
- swap_blocks = victim->swap_item.limit - victim->swap_item.base;
-#endif
swap_size = swap_blocks << BLOCK_SHIFT;
-#endif
-#ifdef PREALLOCATE_CORE
- core_base = victim->core_item.base + swap_blocks;
- core_blocks = victim_core_blocks;
-#else
+#ifndef INDIRECT_CORE
core_base = victim->core_item.base;
core_blocks = victim->core_item.limit - core_base;
-#endif
+#else /* INDIRECT_CORE */
+ core_base = victim->core_item.base + swap_blocks;
+ core_blocks = victim_core_blocks;
+#endif /* INDIRECT_CORE */
core_size = victim->size - swap_size;
}
else { // fully in swap
-#ifndef INODE_SWAP
swap_base = processes[i].swap_item.base;
swap_blocks = processes[i].swap_item.limit - swap_base;
swap_size = processes[i].size;
-#endif
core_base = -1;
core_blocks = 0;
core_size = 0L;
core_table_mem + core_base,
core_blocks
);
-#endif
+#endif /* INDIRECT_CORE */
swap_hash_verify(
i,
-#ifdef INODE_SWAP
- processes[i].swap_inode
-#else
(long)swap_base << BLOCK_SHIFT,
swap_size
-#endif
);
-#ifdef INODE_SWAP
- //printf("deref inode %d\n", processes[i].swap_inode->c_num);
- i_deref(processes[i].swap_inode);
-#elif defined(INDIRECT_SWAP)
+#ifdef INDIRECT_SWAP
block_pool_free(
&swap_block_pool,
swap_table_mem + swap_base,
swap_blocks
);
-#endif
+#endif /* INDIRECT_SWAP */
}
}
#ifdef INDIRECT_CORE
-#ifndef NDEBUG
for (i = 0; i < core_table_blocks; ++i)
rassert(core_table_mem[i] == 0x55555555);
-#endif
{
k = core_block_pool.n_blocks >> 3;
for (j = 0; j < k; ++j)
if (k)
rassert(core_block_pool.bitmap[j] == (uint8_t)~(-1 << k));
}
-#endif
+#endif /* INDIRECT_CORE */
j = n_core_blocks << BLOCK_SHIFT;
for (i = 0; i < j; ++i)
rassert(core_block_mem[i] == 0xaa);
-#ifdef INODE_SWAP
- printf("free inodes %d blocks %d\n", fs_tab[0].s_tinode, fs_tab[0].s_tfree);
-#else
#ifdef INDIRECT_SWAP
-#ifndef NDEBUG
for (i = 0; i < swap_table_blocks; ++i)
rassert(swap_table_mem[i] == 0x55555555);
-#endif
{
k = swap_block_pool.n_blocks >> 3;
for (j = 0; j < k; ++j)
if (k)
rassert(swap_block_pool.bitmap[j] == (uint8_t)~(-1 << k));
}
-#endif
+#endif /* INDIRECT_SWAP */
j = n_swap_blocks << BLOCK_SHIFT;
for (i = 0; i < j; ++i)
rassert(swap_block_mem[i] == 0xaa);
-#endif
return 0;
}
#!/bin/sh
(cd indirect_core_indirect_swap && make clean && make && ./o.sh)
-(cd indirect_core_inode_swap && make clean && make && ./o.sh)
-(cd indirect_core_preallocate_swap && make clean && make && ./o.sh)
+(cd indirect_core_contiguous_swap && make clean && make && ./o.sh)
(cd moveable_core_indirect_swap && make clean && make && ./o.sh)
-(cd moveable_core_inode_swap && make clean && make && ./o.sh)
-(cd moveable_core_preallocate_swap && make clean && make && ./o.sh)
+(cd moveable_core_contiguous_swap && make clean && make && ./o.sh)
#include <stddef.h>
//#include <stdio.h>
#include <stdlib.h>
-#ifndef NDEBUG
#include <string.h>
-#endif
#include "swap.h"
#include "process.h"
#include "rassert.h"
#ifdef INDIRECT_SWAP
int *swap_table_mem;
struct block_pool swap_block_pool;
-#endif
+#endif /* INDIRECT_SWAP */
-#ifdef PREALLOCATE_SWAP
int victim_swap_blocks;
-#endif
static void swap_move(struct pool_item *item, int new_base) {
int base, blocks;
-#ifdef INDIRECT_SWAP
- int i;
-#else
+#ifndef INDIRECT_SWAP
struct process *process;
long size;
-#endif
+#else /* INDIRECT_SWAP */
+ int i;
+#endif /* INDIRECT_SWAP */
base = item->base;
blocks = item->limit - base;
// new_base + blocks
//);
assert(new_base <= base || new_base >= base + blocks);
-#ifdef INDIRECT_SWAP
- for (i = 0; i < blocks; ++i) {
- swap_table_mem[new_base + i] = swap_table_mem[base + i];
-#ifndef NDEBUG
- swap_table_mem[base + i] = 0x55555555;
-#endif
- }
-#else
+#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));
// copy by abstract routine
swap_copy((long)base << BLOCK_SHIFT, (long)new_base << BLOCK_SHIFT, size);
-#endif
-}
-
-#ifdef MOVEABLE_SWAP
-static void swap_move_up(struct pool_item *item, int new_limit) {
- int limit, blocks;
-#ifdef INDIRECT_SWAP
- int i;
-#else
- long limit1, new_limit1, size;
- struct process *process;
- long trim;
-#endif
-
- limit = item->limit;
- blocks = limit - item->base;
- //printf(
- // "swap_move_up [%d,%d) to [%d,%d)\n",
- // limit - blocks,
- // limit,
- // new_limit - blocks,
- // new_limit
- //);
- assert(new_limit >= limit || new_limit <= limit - blocks);
-#ifdef INDIRECT_SWAP
- blocks = -blocks;
- for (i = -1; i >= blocks; --i) {
- swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
-#ifndef NDEBUG
- swap_table_mem[limit + i] = 0x55555555;
-#endif
- }
-#else
- // set up transfer parameters
- limit1 = (long)limit << BLOCK_SHIFT;
- new_limit1 = (long)new_limit << BLOCK_SHIFT;
- size = (long)blocks << BLOCK_SHIFT;
-
- // see if incomplete last block, if so don't copy extra part
- process =
- (struct process *)((char *)item - offsetof(struct process, swap_item));
- trim = size - process->size;
- if (trim > 0) {
- limit1 -= trim;
- new_limit1 -= trim;
- size = process->size;
+#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;
}
-
- // copy by abstract routine
- swap_copy_up(limit1, new_limit1, size);
-#endif
+#endif /* INDIRECT_SWAP */
}
-#endif
-#ifdef INDIRECT_SWAP
-void swap_init(int n_blocks, int table_size)
-#else
+#ifndef INDIRECT_SWAP
void swap_init(int n_blocks)
-#endif
+#else /* INDIRECT_SWAP */
+void swap_init(int n_blocks, int table_size)
+#endif /* INDIRECT_SWAP */
{
pool_init(
&swap_table,
0,
-#ifdef INDIRECT_SWAP
- table_size,
-#else
+#ifndef INDIRECT_SWAP
n_blocks,
-#endif
+#else /* INDIRECT_SWAP */
+ table_size,
+#endif /* INDIRECT_SWAP */
swap_move,
-#ifdef MOVEABLE_SWAP
- swap_move_up
-#else
NULL
-#endif
);
#ifdef INDIRECT_SWAP
swap_table_mem = malloc(table_size * sizeof(int));
rassert(swap_table_mem);
-#ifndef NDEBUG
memset(swap_table_mem, 0x55, table_size * sizeof(int));
-#endif
block_pool_init(&swap_block_pool, n_blocks);
-#endif
+#endif /* INDIRECT_SWAP */
}
#ifndef _SWAP_H
#define _SWAP_H 1
-#define PREALLOCATE_SWAP 1
#define INDIRECT_SWAP 1
-//#define MOVEABLE_SWAP 1
#include <stdint.h>
#ifdef INDIRECT_SWAP
#include "block_pool.h"
-#endif
+#endif /* INDIRECT_SWAP */
#include "pool.h"
-#ifdef INDIRECT_SWAP
-#define SWAP_AVAIL swap_block_pool.avail
-#else
-#define SWAP_AVAIL swap_table.avail
-#endif
-
extern struct pool_head swap_table;
#ifdef INDIRECT_SWAP
extern int *swap_table_mem;
extern struct block_pool swap_block_pool;
-#endif
+#endif /* INDIRECT_SWAP */
-#ifdef PREALLOCATE_SWAP
extern int victim_swap_blocks;
-#endif
-#ifdef INDIRECT_SWAP
-void swap_init(int n_table, int n_blocks);
-#else
+#ifndef INDIRECT_SWAP
void swap_init(int n_table);
// abstract
void swap_copy(long src_base, long dest_base, long size);
void swap_copy_up(long src_limit, long dest_limit, long size);
-#endif
+#else /* INDIRECT_SWAP */
+void swap_init(int n_table, int n_blocks);
+#endif /* INDIRECT_SWAP */
#endif