Line data Source code
1 1 : /** 2 : * @file mm/buddy/buddy.h 3 : * 4 : * @brief A Buddy System implementation 5 : * 6 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com> 7 : * SPDX-License-Identifier: GPL-3.0-only 8 : */ 9 : #pragma once 10 : 11 : #include <datatypes/array.h> 12 : #include <datatypes/bitmap.h> 13 : 14 : #include <assert.h> 15 : #include <stddef.h> 16 : #include <stdint.h> 17 : 18 0 : #define B_TOTAL_EXP 17U 19 0 : #define B_BLOCK_EXP 6U 20 0 : #define B_LOG_INCREMENTAL_THRESHOLD 0.5 21 0 : #define B_LOG_FREQUENCY 50 22 : 23 : /// The checkpointable memory context assigned to a single LP 24 1 : struct mm_state { // todo incremental checkpoints 25 : /// The array of checkpoints 26 1 : dyn_array( 27 : /// Binds a checkpoint together with a reference index 28 : struct mm_log { 29 : /// The reference index, used to identify this checkpoint 30 : array_count_t ref_i; 31 : /// A pointer to the actual checkpoint 32 : struct mm_checkpoint *c; 33 : } 34 : ) logs; 35 : /// The count of allocated bytes 36 1 : uint_fast32_t used_mem; 37 : /// The checkpointed binary tree representing the buddy system 38 1 : uint_least8_t longest[(1U << (B_TOTAL_EXP - B_BLOCK_EXP + 1))]; // last char is actually unused 39 : /// The memory buffer served to the model 40 1 : unsigned char base_mem[1U << B_TOTAL_EXP]; 41 : #ifdef ROOTSIM_INCREMENTAL 42 : /// The bytes count of the memory dirtied by writes 43 1 : uint_fast32_t dirty_mem; 44 : /// Keeps track of memory blocks which have been dirtied by a write 45 1 : block_bitmap dirty[ 46 : bitmap_required_size( 47 : // this tracks writes to the allocation tree 48 : (1 << (B_TOTAL_EXP - 2 * B_BLOCK_EXP + 1)) + 49 : // while this tracks writes to the actual memory buffer 50 : (1 << (B_TOTAL_EXP - B_BLOCK_EXP)) 51 : 52 : ) 53 : ]; 54 : #endif 55 : }; 56 : 57 : /// A restorable checkpoint of the memory context assigned to a single LP 58 1 : struct mm_checkpoint { // todo only log longest[] if changed, or incrementally 59 : #ifdef ROOTSIM_INCREMENTAL 60 : /// If set this checkpoint is incremental, else it is a full one 61 1 : bool is_incremental; 62 : /// The checkpoint of the dirty bitmap 63 1 : block_bitmap dirty [ 64 : bitmap_required_size( 65 : // this tracks writes to the allocation tree 66 : (1 << (B_TOTAL_EXP - 2 * B_BLOCK_EXP + 1)) + 67 : // while this tracks writes to the actual memory buffer 68 : (1 << (B_TOTAL_EXP - B_BLOCK_EXP)) 69 : ) 70 : ]; 71 : #endif 72 : /// The used memory in bytes when this checkpoint was taken 73 1 : uint_fast32_t used_mem; 74 : /// The checkpointed binary tree representing the buddy system 75 1 : uint_least8_t longest[(1U << (B_TOTAL_EXP - B_BLOCK_EXP + 1))]; 76 : /// The checkpointed memory buffer assigned to the model 77 1 : unsigned char base_mem[]; 78 : }; 79 : 80 : static_assert( 81 : offsetof(struct mm_state, longest) == 82 : offsetof(struct mm_state, base_mem) - 83 : sizeof(((struct mm_state *)0)->longest), 84 : "longest and base_mem are not contiguous, this will break incremental checkpointing"); 85 : 86 :