#include "core.h"
#include "rassert.h"
-struct pool_head core_head;
-int *core_mem;
+struct pool_head core_table;
+int *core_table_mem;
void core_move(struct pool_item *item, int new_base) {
int base = item->base;
);
assert(new_base < base || new_base >= base + size);
for (int i = 0; i < size; ++i) {
- core_mem[new_base + i] = core_mem[base + i];
- core_mem[base + i] = 0xaaaaaaaa;
+ core_table_mem[new_base + i] = core_table_mem[base + i];
+ core_table_mem[base + i] = 0xaaaaaaaa;
}
}
);
assert(new_limit > limit || new_limit <= limit + neg_size);
for (int i = -1; i >= neg_size; --i) {
- core_mem[new_limit + i] = core_mem[limit + i];
- core_mem[limit + i] = 0xaaaaaaaa;
+ core_table_mem[new_limit + i] = core_table_mem[limit + i];
+ core_table_mem[limit + i] = 0xaaaaaaaa;
}
}
-void core_init(int core, int spare) {
- pool_init(&core_head, 0, core, spare, core_move, core_move_up);
+void core_init(int table_size, int table_spare) {
+ pool_init(
+ &core_table,
+ 0,
+ table_size,
+ table_spare,
+ core_move,
+ core_move_up
+ );
- core_mem = malloc(core * sizeof(int));
- memset(core_mem, 0xaa, core * sizeof(int));
+ core_table_mem = malloc(table_size * sizeof(int));
+ memset(core_table_mem, 0xaa, table_size * sizeof(int));
}
#define MOVEABLE_CORE 1
#ifdef MOVEABLE_CORE
-#define core_alloc(item, size) pool_alloc_moveable(&core_head, item, size)
-#define core_realloc(item, size) pool_realloc_moveable(&core_head, item, size)
+#define core_table_alloc(item, size) \
+ pool_alloc_moveable(&core_table, item, size)
+#define core_table_realloc(item, size) \
+ pool_realloc_moveable(&core_table, item, size)
#else
-#define core_alloc(item, size) pool_alloc(&core_head, item, size)
-#define core_realloc(item, size) pool_realloc(&core_head, item, size)
+#define core_table_alloc(item, size) \
+ pool_alloc(&core_table, item, size)
+#define core_table_realloc(item, size) \
+ pool_realloc(&core_table, item, size)
#endif
-#define core_free(item) pool_free(&core_head, item)
+#define core_table_free(item) pool_free(&core_table, item)
-extern struct pool_head core_head;
-extern int *core_mem;
+extern struct pool_head core_table;
+extern int *core_table_mem;
-void core_init(int core, int spare);
+void core_init(int n_table, int table_spare);
#endif
// won't work when compiled with NDEBUG
static void check_invariants() {
#if defined(PREALLOCATE_CORE) && defined(PREALLOCATE_SWAP)
- int core_head_avail =
- core_head.item.base -
- core_head.item.limit -
- core_head.spare;
- int swap_head_avail =
- swap_head.item.base -
- swap_head.item.limit -
- swap_head.spare;
+ int core_table_avail =
+ core_table.item.base -
+ core_table.item.limit -
+ core_table.spare;
+ int swap_table_avail =
+ swap_table.item.base -
+ swap_table.item.limit -
+ swap_table.spare;
int avail = (
- core_head_avail < swap_head_avail ? core_head_avail : swap_head_avail
+ core_table_avail < swap_table_avail ? core_table_avail : swap_table_avail
) - process_spare;
#elif defined(PREALLOCATE_CORE)
- int core_head_avail =
- core_head.item.base -
- core_head.item.limit -
- core_head.spare;
- int avail = core_head_avail - process_spare;
+ int core_table_avail =
+ core_table.item.base -
+ core_table.item.limit -
+ core_table.spare;
+ int avail = core_table_avail - process_spare;
#elif defined(PREALLOCATE_SWAP)
- int swap_head_avail =
- swap_head.item.base -
- swap_head.item.limit -
- swap_head.spare;
- int avail = swap_head_avail - process_spare;
+ int swap_table_avail =
+ swap_table.item.base -
+ swap_table.item.limit -
+ swap_table.spare;
+ int avail = swap_table_avail - process_spare;
#else
- int core_head_avail =
- core_head.item.base -
- core_head.item.limit -
- core_head.spare;
- int swap_head_avail =
- swap_head.item.base -
- swap_head.item.limit -
- swap_head.spare;
- int avail = core_head_avail + swap_head_avail - process_spare;
+ int core_table_avail =
+ core_table.item.base -
+ core_table.item.limit -
+ core_table.spare;
+ int swap_table_avail =
+ swap_table.item.base -
+ swap_table.item.limit -
+ swap_table.spare;
+ int avail = core_table_avail + swap_table_avail - process_spare;
#endif
if (victim == NULL) {
#ifdef PREALLOCATE_CORE
#if defined(PREALLOCATE_CORE) && defined(PREALLOCATE_SWAP)
process_avail = (
- core_head.avail < swap_head.avail ? core_head.avail : swap_head.avail
+ core_table.avail < swap_table.avail ? core_table.avail : swap_table.avail
) - spare;
#elif defined(PREALLOCATE_CORE)
- process_avail = core_head.avail - spare;
+ process_avail = core_table.avail - spare;
#elif defined(PREALLOCATE_SWAP)
- process_avail = swap_head.avail - spare;
+ process_avail = swap_table.avail - spare;
#else
- process_avail = core_head.avail + swap_head.avail - spare;
+ process_avail = core_table.avail + swap_table.avail - spare;
#endif
process_spare = spare;
// increase swap allocation
victim_swap_size += size;
#ifndef PREALLOCATE_SWAP
- rassert(swap_realloc(&victim_swap_item, victim_swap_size));
+ rassert(swap_table_realloc(&victim_swap_item, victim_swap_size));
#endif
goto loop_entry;
}
// add to swap pool, using dedicated swap item
victim_swap_size = size;
#ifndef PREALLOCATE_SWAP
- rassert(swap_alloc(&victim_swap_item, victim_swap_size));
+ rassert(swap_table_alloc(&victim_swap_item, victim_swap_size));
#endif
loop_entry:
if (victim_core_size) {
#ifndef PREALLOCATE_CORE
// no, reduce core allocation, using dedicated core item
- rassert(core_realloc(&victim_core_item, victim_core_size));
+ rassert(core_table_realloc(&victim_core_item, victim_core_size));
#endif
// as an optimization, skip the calculation of swap_out -= size
printf("victimized %d\n", (int)(victim - processes));
#ifndef PREALLOCATE_CORE
// remove from core pool, using dedicated core item
- core_free(&victim_core_item);
+ core_table_free(&victim_core_item);
#endif
#ifndef PREALLOCATE_SWAP
if (
process_avail < size
#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
- || !swap_alloc(&process->swap_item, size)
+ || !swap_table_alloc(&process->swap_item, size)
#endif
)
return false;
- printf("%d %d %d\n", process_avail, core_head.avail, swap_head.avail);
+ printf("%d %d %d\n", process_avail, core_table.avail, swap_table.avail);
// free up as much core as we need to
- swap_out = size - core_head.avail;
+ swap_out = size - core_table.avail;
#ifndef PREALLOCATE_SWAP
- assert(swap_out <= swap_head.avail);
+ assert(swap_out <= swap_table.avail);
#endif
do_swap_out(swap_out);
// allocate core and possible swap
#ifdef MOVEABLE_CORE
rassert(
- core_alloc(
+ core_table_alloc(
#ifdef PREALLOCATE_CORE
&process->core_item,
#else
);
#else
if (
- !core_alloc(
+ !core_table_alloc(
#ifdef PREALLOCATE_CORE
&process->core_item,
#else
)
) {
#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
- swap_free(&process->swap_item);
+ swap_table_free(&process->swap_item);
#endif
return false;
}
#endif
#if defined(PREALLOCATE_SWAP) && defined(MOVEABLE_SWAP)
- rassert(swap_alloc(&process->swap_item, size));
+ rassert(swap_table_alloc(&process->swap_item, size));
#endif
// insert at head of LRU list
if (process_avail < size_change)
return false;
#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
- swap_free(&process->swap_item);
- if (!swap_alloc(&process->swap_item, size)) {
- rassert(swap_alloc(&process->swap_item, process->size));
+ swap_table_free(&process->swap_item);
+ if (!swap_table_alloc(&process->swap_item, size)) {
+ rassert(swap_table_alloc(&process->swap_item, process->size));
return false;
}
#endif
// free up as much core as we need to
- swap_out = size_change - core_head.avail;
+ swap_out = size_change - core_table.avail;
#ifndef PREALLOCATE_SWAP
- assert(swap_out <= swap_head.avail);
+ assert(swap_out <= swap_table.avail);
#endif
do_swap_out(swap_out);
// allocate core and possible swap
#ifdef MOVEABLE_CORE
rassert(
- core_realloc(
+ core_table_realloc(
#ifdef PREALLOCATE_CORE
&process->core_item,
#else
);
#else
if (
- !core_realloc(
+ !core_table_realloc(
#ifdef PREALLOCATE_CORE
&process->core_item,
#else
)
) {
#if defined(PREALLOCATE_SWAP) && !defined(MOVEABLE_SWAP)
- swap_free(&process->swap_item);
- rassert(swap_alloc(&process->swap_item, process->size));
+ swap_table_free(&process->swap_item);
+ rassert(swap_table_alloc(&process->swap_item, process->size));
#endif
return false;
}
#endif
#if defined(PREALLOCATE_SWAP) && defined(MOVEABLE_SWAP)
- swap_free(&process->swap_item);
- rassert(swap_alloc(&process->swap_item, size));
+ swap_table_free(&process->swap_item);
+ rassert(swap_table_alloc(&process->swap_item, size));
#endif
// track total allocation
// calculate amount to swap
size = process_swap_size;
- swap_out = size - core_head.avail;
- if (swap_out > swap_head.avail) {
- size += swap_head.avail - swap_out;
- swap_out = swap_head.avail;
+ swap_out = size - core_table.avail;
+ if (swap_out > swap_table.avail) {
+ size += swap_table.avail - swap_out;
+ swap_out = swap_table.avail;
}
// free up as much core as we can
#ifndef PREALLOCATE_SWAP
- assert(swap_out <= swap_head.avail);
+ assert(swap_out <= swap_table.avail);
#endif
do_swap_out(swap_out);
// add to core pool, using dedicated core item
process_core_size = size;
#ifndef PREALLOCATE_CORE
- rassert(core_alloc(&process_core_item, process_core_size));
+ rassert(core_table_alloc(&process_core_item, process_core_size));
#endif
goto loop_entry_full;
}
do {
#ifndef PREALLOCATE_SWAP
// reduce swap allocation
- rassert(swap_realloc(&process_swap_item, process_swap_size));
+ rassert(swap_table_realloc(&process_swap_item, process_swap_size));
loop_entry_partial:
#endif
// calculate how much to swap
size = process_swap_size;
- swap_out = size - core_head.avail;
- if (swap_out > swap_head.avail) {
- size += swap_head.avail - swap_out;
- swap_out = swap_head.avail;
+ swap_out = size - core_table.avail;
+ if (swap_out > swap_table.avail) {
+ size += swap_table.avail - swap_out;
+ swap_out = swap_table.avail;
}
// free up as much core as we can
// increase core allocation
process_core_size += size;
#ifndef PREALLOCATE_CORE
- rassert(core_realloc(&process_core_item, process_core_size));
+ rassert(core_table_realloc(&process_core_item, process_core_size));
#endif
loop_entry_full:
#ifndef PREALLOCATE_SWAP
// remove from swap pool, using dedicated swap item
- swap_free(&process_swap_item);
+ swap_table_free(&process_swap_item);
#endif
}
assert(process->size != -1);
#ifdef PREALLOCATE_CORE
- core_free(&process->core_item);
+ core_table_free(&process->core_item);
#endif
#ifdef PREALLOCATE_SWAP
- swap_free(&process->swap_item);
+ swap_table_free(&process->swap_item);
#endif
// see whether fully in core, victim, or fully in swap
process->lru_item.next->prev = process->lru_item.prev;
#ifndef PREALLOCATE_CORE
- core_free(&process->pool_item);
+ core_table_free(&process->pool_item);
#endif
}
else if (process == victim) {
#ifdef PREALLOCATE_CORE
victim_core_size = 0;
#else
- core_free(&victim_core_item);
+ core_table_free(&victim_core_item);
#endif
#ifdef PREALLOCATE_SWAP
victim_swap_size = 0;
#else
- swap_free(&victim_swap_item);
+ swap_table_free(&victim_swap_item);
#endif
}
#ifndef PREALLOCATE_SWAP
else
// fully in swap, remove from swap pool
- swap_free(&process->pool_item);
+ swap_table_free(&process->pool_item);
#endif
// track total allocation
int main(int argc, char **argv) {
if (argc < 7) {
- printf("usage: %s n_processes core_size core_spare swap_size swap_spare spare\n", argv[0]);
+ printf("usage: %s n_processes core_table_size core_table_spare swap_table_size swap_table_spare spare\n", argv[0]);
exit(EXIT_FAILURE);
}
- int n_processes0 = atoi(argv[1]);
- int core_size0 = atoi(argv[2]);
- int core_spare0 = atoi(argv[3]);
- int swap_size0 = atoi(argv[4]);
- int swap_spare0 = atoi(argv[5]);
- int spare0 = atoi(argv[6]);
+ int n_processes = atoi(argv[1]);
+ int core_table_size = atoi(argv[2]);
+ int core_table_spare = atoi(argv[3]);
+ int swap_table_size = atoi(argv[4]);
+ int swap_table_spare = atoi(argv[5]);
+ int spare = atoi(argv[6]);
- core_init(core_size0, core_spare0);
- swap_init(swap_size0, swap_spare0);
- process_init(n_processes0, spare0);
+ core_init(core_table_size, core_table_spare);
+ swap_init(swap_table_size, swap_table_spare);
+ process_init(n_processes, spare);
for (int i = 0; i < n_processes; ++i)
processes[i].size = -1;
while (true) {
- //printf("avail %d %d %d\n", process_avail, core_head.avail, swap_head.avail);
+ //printf("avail %d %d %d\n", process_avail, core_table.avail, swap_table.avail);
char buf[256];
switch (scanf("%s", buf)) {
case -1:
#endif
printf("new core [%d,%d)\n", core_base, core_base + size);
for (int i = 0; i < size; ++i) {
- rassert(core_mem[core_base + i] == 0xaaaaaaaa);
+ rassert(core_table_mem[core_base + i] == 0xaaaaaaaa);
long long hash = process * 17 + i * 29;
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
- core_mem[core_base + i] = (int)hash;
+ core_table_mem[core_base + i] = (int)hash;
}
#ifdef PREALLOCATE_SWAP
printf("new swap [%d,%d)\n", ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
#endif
printf("new core [%d,%d)\n", core_base, core_base + size);
for (int i = actual_old_size; i < size; ++i) {
- rassert(core_mem[core_base + i] == 0xaaaaaaaa);
+ rassert(core_table_mem[core_base + i] == 0xaaaaaaaa);
long long hash = process * 17 + i * 29;
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
- core_mem[core_base + i] = (int)hash;
+ core_table_mem[core_base + i] = (int)hash;
}
for (int i = size; i < actual_old_size; ++i) {
long long hash = process * 17 + i * 29;
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
- rassert(core_mem[core_base + i] == (int)hash);
- core_mem[core_base + i] = 0xaaaaaaaa;
+ rassert(core_table_mem[core_base + i] == (int)hash);
+ core_table_mem[core_base + i] = 0xaaaaaaaa;
}
#ifdef PREALLOCATE_SWAP
printf("new swap [%d,%d)\n", ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
if (i < core_size) {
- rassert(core_mem[core_base + i] == (int)hash);
- core_mem[core_base + i] = 0xaaaaaaaa;
+ rassert(core_table_mem[core_base + i] == (int)hash);
+ core_table_mem[core_base + i] = 0xaaaaaaaa;
}
else {
- rassert(swap_mem[swap_base + i - core_size] == (int)hash);
- swap_mem[swap_base + i - core_size] = 0xaaaaaaaa;
+ rassert(swap_table_mem[swap_base + i - core_size] == (int)hash);
+ swap_table_mem[swap_base + i - core_size] = 0xaaaaaaaa;
}
}
}
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
if (j < core_size) {
- rassert(core_mem[core_base + j] == (int)hash);
- core_mem[core_base + j] = 0xaaaaaaaa;
+ rassert(core_table_mem[core_base + j] == (int)hash);
+ core_table_mem[core_base + j] = 0xaaaaaaaa;
}
else {
- rassert(swap_mem[swap_base + j - core_size] == (int)hash);
- swap_mem[swap_base + j - core_size] = 0xaaaaaaaa;
+ rassert(swap_table_mem[swap_base + j - core_size] == (int)hash);
+ swap_table_mem[swap_base + j - core_size] = 0xaaaaaaaa;
}
}
}
}
- for (int i = 0; i < core_head.item.base; ++i)
- rassert(core_mem[i] == 0xaaaaaaaa);
- for (int i = 0; i < swap_head.item.base; ++i)
- rassert(swap_mem[i] == 0xaaaaaaaa);
+ for (int i = 0; i < core_table.item.base; ++i)
+ rassert(core_table_mem[i] == 0xaaaaaaaa);
+ for (int i = 0; i < swap_table.item.base; ++i)
+ rassert(swap_table_mem[i] == 0xaaaaaaaa);
return 0;
}
#include "swap.h"
#include "rassert.h"
-struct pool_head swap_head;
-int *swap_mem;
+struct pool_head swap_table;
+int *swap_table_mem;
// swap address native wrt. pool and complemented wrt. backing store,
// means that swap_move() moves upward in backing store, do backward
);
assert(new_limit >= limit || new_limit <= limit + neg_size);
for (int i = -1; i >= neg_size; --i) {
- swap_mem[new_limit + i] = swap_mem[limit + i];
- swap_mem[limit + i] = 0xaaaaaaaa;
+ swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
+ swap_table_mem[limit + i] = 0xaaaaaaaa;
}
}
);
assert(new_base <= base || new_base >= base + size);
for (int i = 0; i < size; ++i) {
- swap_mem[new_base + i] = swap_mem[base + i];
- swap_mem[base + i] = 0xaaaaaaaa;
+ swap_table_mem[new_base + i] = swap_table_mem[base + i];
+ swap_table_mem[base + i] = 0xaaaaaaaa;
}
}
-void swap_init(int swap, int spare) {
- pool_init(&swap_head, ~swap, -1, spare, swap_move, swap_move_up);
+void swap_init(int table_size, int table_spare) {
+ pool_init(
+ &swap_table,
+ ~table_size,
+ -1,
+ table_spare,
+ swap_move,
+ swap_move_up
+ );
- swap_mem = malloc(swap * sizeof(int));
- memset(swap_mem, 0xaa, swap * sizeof(int));
+ swap_table_mem = malloc(table_size * sizeof(int));
+ memset(swap_table_mem, 0xaa, table_size * sizeof(int));
}
// swap address complemented wrt. pool and native wrt. backing store
void swap_read(int swap_base, int core_base, int size) {
printf("swap_read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + size, core_base, core_base + size);
for (int i = 0; i < size; ++i) {
- assert(core_mem[core_base + i] == 0xaaaaaaaa);
- core_mem[core_base + i] = swap_mem[swap_base + i];
- swap_mem[swap_base + i] = 0xaaaaaaaa;
+ assert(core_table_mem[core_base + i] == 0xaaaaaaaa);
+ core_table_mem[core_base + i] = swap_table_mem[swap_base + i];
+ swap_table_mem[swap_base + i] = 0xaaaaaaaa;
}
}
void swap_write(int swap_base, int core_base, int size) {
printf("swap_write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + size, swap_base, swap_base + size);
for (int i = 0; i < size; ++i) {
- assert(swap_mem[swap_base + i] == 0xaaaaaaaa);
- swap_mem[swap_base + i] = core_mem[core_base + i];
- core_mem[core_base + i] = 0xaaaaaaaa;
+ assert(swap_table_mem[swap_base + i] == 0xaaaaaaaa);
+ swap_table_mem[swap_base + i] = core_table_mem[core_base + i];
+ core_table_mem[core_base + i] = 0xaaaaaaaa;
}
}
#define MOVEABLE_SWAP 1
#ifdef MOVEABLE_SWAP
-#define swap_alloc(item, size) pool_alloc_moveable(&swap_head, item, size)
-#define swap_realloc(item, size) pool_realloc_moveable(&swap_head, item, size)
+#define swap_table_alloc(item, size) \
+ pool_alloc_moveable(&swap_table, item, size)
+#define swap_table_realloc(item, size) \
+ pool_realloc_moveable(&swap_table, item, size)
#else
-#define swap_alloc(item, size) pool_alloc(&swap_head, item, size)
-#define swap_realloc(item, size) pool_realloc(&swap_head, item, size)
+#define swap_table_alloc(item, size) \
+ pool_alloc(&swap_table, item, size)
+#define swap_table_realloc(item, size) \
+ pool_realloc(&swap_table, item, size)
#endif
-#define swap_free(item) pool_free(&swap_head, item)
+#define swap_table_free(item) pool_free(&swap_table, item)
-extern struct pool_head swap_head;
-extern int *swap_mem;
+extern struct pool_head swap_table;
+extern int *swap_table_mem;
-void swap_init(int swap, int spare);
+void swap_init(int n_table, int table_spare);
void swap_read(int swap_base, int core_base, int size);
void swap_write(int swap_base, int core_base, int size);