p-allocator.cc 1.55 KB
Newer Older
Eddie Kohler's avatar
Eddie Kohler committed
1
2
3
4
5
6
7
8
#include "u-lib.hh"
#define ALLOC_SLOWDOWN 24

extern uint8_t end[];

uint8_t* heap_top;
uint8_t* stack_bottom;

James Foster's avatar
James Foster committed
9
10
11
12
// turn off optimization to facilitate debugging of forking new copies
#pragma GCC push_options
#pragma GCC optimize ("O0")

Eddie Kohler's avatar
Eddie Kohler committed
13
14
15
16
void process_main() {
    sys_kdisplay(KDISPLAY_MEMVIEWER);

    // Fork three new copies. (But ignore failures.)
James Foster's avatar
James Foster committed
17
18
19
20
21
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
    pid_t pid1 = sys_fork();
    pid_t pid2 = sys_fork();
#pragma GCC diagnostic pop
Eddie Kohler's avatar
Eddie Kohler committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    pid_t p = sys_getpid();
    srand(p);

    // The heap starts on the page right after the 'end' symbol,
    // whose address is the first address not allocated to process code
    // or data.
    heap_top = reinterpret_cast<uint8_t*>(
        round_up(reinterpret_cast<uintptr_t>(end), PAGESIZE)
    );

    // The bottom of the stack is the first address on the current
    // stack page (this process never needs more than one stack page).
    stack_bottom = reinterpret_cast<uint8_t*>(
        round_down(rdrsp() - 1, PAGESIZE)
    );

    while (true) {
        if (rand(0, ALLOC_SLOWDOWN - 1) < p) {
            if (heap_top == stack_bottom || sys_page_alloc(heap_top) < 0) {
                break;
            }
            *heap_top = p;      /* check we have write access to new page */
            heap_top += PAGESIZE;
        }
        sys_yield();
        if (rand() < RAND_MAX / 32) {
            sys_pause();
        }
    }

    // After running out of memory, do nothing forever
    while (true) {
        sys_yield();
    }
}
James Foster's avatar
James Foster committed
58
59

 #pragma GCC pop_options