The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
dymelor.h
Go to the documentation of this file.
1 
34 #pragma once
35 
36 #include <math.h>
37 #include <string.h>
38 
39 #include <core/core.h>
40 #include <datatypes/bitmap.h>
41 #include <mm/state.h>
42 #include <core/timer.h>
43 
44 /**************************************
45  * DyMeLoR definitions and structures *
46  **************************************/
47 
48 //ADDED BY MAT 0x00000200000000000
49 #define LP_PREALLOCATION_INITIAL_ADDRESS (void *)0x0000008000000000
50 #define MASK 0x00000001 // Mask used to check, set and unset bits
51 
52 #define MIN_CHUNK_SIZE 128 // Size (in bytes) of the smallest chunk provideable by DyMeLoR
53 #define MAX_CHUNK_SIZE 4194304 // Size (in bytes) of the biggest one. Notice that if this number
54  // is too large, performance (and memory usage) might be affected.
55  // If it is too small, large amount of memory requests by the
56  // application level software (i.e, larger than this number)
57  // will fail, as DyMeLoR will not be able to handle them!
58 
59 #define NUM_AREAS (log2(MAX_CHUNK_SIZE) - log2(MIN_CHUNK_SIZE) + 1) // Number of initial malloc_areas available (will be increased at runtime if needed)
60 #define MAX_NUM_AREAS (NUM_AREAS * 32) // Maximum number of allocatable malloc_areas. If MAX_NUM_AREAS
61  // malloc_areas are filled at runtime, subsequent malloc() requests
62  // by the application level software will fail.
63 #define MAX_LIMIT_NUM_AREAS MAX_NUM_AREAS
64 #define MIN_NUM_CHUNKS 512 // Minimum number of chunks per malloc_area
65 #define MAX_NUM_CHUNKS 4096 // Maximum number of chunks per malloc_area
66 
67 #define MAX_LOG_THRESHOLD 1.7 // Threshold to check if a malloc_area is underused TODO: retest
68 #define MIN_LOG_THRESHOLD 1.7 // Threshold to check if a malloc_area is overused TODO: retest
69 
70 #ifndef INCREMENTAL_GRANULARITY
71 #define INCREMENTAL_GRANULARITY 50 // Number of incremental logs before a full log is forced
72 #endif
73 
74 // These macros are used to tune the statistical malloc_area diff
75 #define LITTLE_SIZE 32
76 #define CHECK_SIZE 0.25 // Must be <= 0.25!
77 
78 // This macro is used to retrieve a cache line in O(1)
79 #define GET_CACHE_LINE_NUMBER(P) ((unsigned long)((P >> 4) & (CACHE_SIZE - 1)))
80 
81 // Macros uset to check, set and unset special purpose bits
82 #define SET_LOG_MODE_BIT(m_area) (((malloc_area*)(m_area))->chunk_size |= (MASK << 0))
83 #define RESET_LOG_MODE_BIT(m_area) (((malloc_area*)(m_area))->chunk_size &= ~(MASK << 0))
84 #define CHECK_LOG_MODE_BIT(m_area) (((malloc_area*)(m_area))->chunk_size & (MASK << 0))
85 
86 #define SET_AREA_LOCK_BIT(m_area) (((malloc_area*)(m_area))->chunk_size |= (MASK << 1))
87 #define RESET_AREA_LOCK_BIT(m_area) (((malloc_area*)(m_area))->chunk_size &= ~(MASK << 1))
88 #define CHECK_AREA_LOCK_BIT(m_area) (((malloc_area*)(m_area))->chunk_size & (MASK << 1))
89 
90 #define UNTAGGED_CHUNK_SIZE(m_area) (((malloc_area*)(m_area))->chunk_size & ~((MASK << 0) | (MASK << 1)))
91 
92 #define POWEROF2(x) (1UL << (1 + (63 - __builtin_clzl((x) - 1))))
93 #define IS_POWEROF2(x) ((x) != 0 && ((x) & ((x) - 1)) == 0)
94 
96 struct _malloc_area {
97 #ifndef NDEBUG
98  atomic_t presence;
99 #endif
100  size_t chunk_size;
101  int alloc_chunks;
102  int dirty_chunks;
103  int next_chunk;
104  int num_chunks;
105  int idx;
106  int state_changed;
107  simtime_t last_access;
108  struct _malloc_area *self_pointer; // This pointer is used in a free operation. Each chunk points here. If malloc_area is moved, only this is updated.
109  rootsim_bitmap *use_bitmap;
110  rootsim_bitmap *dirty_bitmap;
111  void *area;
112  int prev;
113  int next;
114 };
115 
116 typedef struct _malloc_area malloc_area;
117 
121  size_t total_log_size;
122  size_t total_inc_size;
123  size_t bitmap_size;
124  size_t dirty_bitmap_size;
125  int num_areas;
126  int max_num_areas;
127  int busy_areas;
128  int dirty_areas;
129  simtime_t timestamp;
130  struct _malloc_area *areas;
131 };
132 
133 typedef struct _malloc_state malloc_state;
134 
135 #define is_incremental(ckpt) (((malloc_state *)ckpt)->is_incremental == true)
136 
137 #define get_top_pointer(ptr) ((unsigned long long *)((char *)ptr - sizeof(unsigned long long)))
138 #define get_area_top_pointer(ptr) ( (malloc_area **)(*get_top_pointer(ptr)) )
139 #define get_area(ptr) ( *(get_area_top_pointer(ptr)) )
140 
141 /***************
142  * EXPOSED API *
143  ***************/
144 
145 // DyMeLoR API
146 extern void set_force_full(unsigned int, int);
147 extern void dirty_mem(void *, int);
148 extern size_t get_state_size(int);
149 extern size_t get_log_size(malloc_state *);
150 extern size_t get_inc_log_size(void *);
151 extern int get_granularity(void);
152 extern size_t dirty_size(unsigned int, void *, double *);
153 extern malloc_state *malloc_state_init(void);
154 extern void malloc_state_wipe(malloc_state **);
155 extern void *do_malloc(struct lp_struct *, size_t);
156 extern void do_free(struct lp_struct *, void *ptr);
157 extern void *allocate_lp_memory(struct lp_struct *, size_t);
158 extern void free_lp_memory(struct lp_struct *, void *);
159 
160 // Checkpointing API
161 extern void *log_full(struct lp_struct *);
162 extern void *log_state(struct lp_struct *);
163 extern void log_restore(struct lp_struct *, state_t *);
164 extern void log_delete(void *);
165 
166 // Userspace API
167 extern void *__wrap_malloc(size_t);
168 extern void __wrap_free(void *);
169 extern void *__wrap_realloc(void *, size_t);
170 extern void *__wrap_calloc(size_t, size_t);
171 extern void clean_buffers_on_gvt(struct lp_struct *, simtime_t);
172 
173 /* Simulation Platform Memory APIs */
174 extern inline void *rsalloc(size_t);
175 extern inline void rsfree(void *);
176 extern inline void *rsrealloc(void *, size_t);
177 extern inline void *rscalloc(size_t, size_t);
178 
179 extern void ecs_init(void);
180 
181 // This is used to help ensure that the platform is not using malloc.
182 #pragma GCC poison malloc free realloc calloc
void dirty_mem(void *, int)
Definition: dymelor.c:453
void * log_full(struct lp_struct *)
Definition: checkpoints.c:76
malloc_state * malloc_state_init(void)
Definition: dymelor.c:77
void * do_malloc(struct lp_struct *, size_t)
Definition: dymelor.c:172
Structure for LP&#39;s state.
Definition: state.h:49
void __wrap_free(void *)
Definition: dymelor.c:602
void log_delete(void *)
Definition: checkpoints.c:374
void * log_state(struct lp_struct *)
Definition: checkpoints.c:195
Core ROOT-Sim functionalities.
void log_restore(struct lp_struct *, state_t *)
Definition: checkpoints.c:358
void * __wrap_calloc(size_t, size_t)
Definition: dymelor.c:677
void * __wrap_realloc(void *, size_t)
Definition: dymelor.c:628
Timers.
bool is_incremental
Tells if it is an incremental log or a full one (when used for logging)
Definition: dymelor.h:120
double simtime_t
This defines the type with whom timestamps are represented.
Definition: ROOT-Sim.h:55
LP state management.
Bitmap data type.
size_t get_log_size(malloc_state *)
Definition: dymelor.c:534
Definition of the memory map.
Definition: dymelor.h:119
unsigned char rootsim_bitmap
This defines a generic bitmap.
Definition: bitmap.h:43
void * __wrap_malloc(size_t)
Definition: dymelor.c:566
This structure let DyMeLoR handle one malloc area (for serving given-size memory requests) ...
Definition: dymelor.h:96