-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipa.h
278 lines (225 loc) · 7.89 KB
/
ipa.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef __HAVE_ALLOC_H
#define __HAVE_ALLOC_H
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <assert.h>
#include "ipa_hooks.h"
#include "stack.h"
#if __WORDSIZE == 64
#define ALIGNMENT (8) // I think?
#elif __WORDSIZE == 32
#define ALIGNMENT (4)
#else
#error "Need 32 or 64 b wordsize"
#endif
static const size_t class_sizes[] = {
16, 32, 48, 64, 80, 96, 112, 128, // je malloc small
160, 192, 224, 256,
320, 384, 448, 512,
640, 768, 896, 1024,
1280, 1536, 1792, 2048,
3072, 3560, 3584,
4096, (4096 * 2), (4096 * 3), (4096 * 4), (4096 * 5),
(4096 * 6), (4096 * 7), (4096 * 8), (4096 * 9),
1024 * 819, 2 * 1024 * 8192, 3 * 1024 * 8192, 4 * 1024 * 8192
};
#define NUM_CLASSES ((sizeof(class_sizes) / sizeof(size_t)))
// Header size macros
#define CLASS_TO_SIZE(x) (__size_to_class((unsigned) x)) // to actually be determined later
#define SIZE_TO_CLASS(x) (class_for_size((unsigned) x))
#define MAX_SIZE (class_sizes[NUM_CLASSES - 1])
#define MAX_REQUEST (ALIGN(MAX_SIZE - sizeof(block_t)))
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~(ALIGNMENT-1))
#define PAGE_ALIGN(size) (((size) + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1))
#define PAGE_SIZE 4096 //default Linux
static inline size_t __size_to_class(size_t x) {
assert(x >= 0 && x < NUM_CLASSES);
return class_sizes[x];
}
static inline const size_t class_for_size(size_t x) {
size_t s;
for (s = 0; s < NUM_CLASSES; s++) {
if (class_sizes[s] >= x) {
return s;
}
}
abort();
}
#if __SIZEOF_POINTER__ == 8
// 64b pointers
#define combined_page_t unsigned __int128
#else
#define combined_page_t uint64_t
#endif
typedef struct {
union {
struct {
volatile struct ipa_page_t * next_page;
volatile int next_pg_name;
};
combined_page_t combined;
};
} ipa_page_t;
typedef union {
#ifdef BOMALLOC_ALIGN_HEADERS
//for header alignment to cache line bounds
void * ____padding[64/sizeof(void*)];
#endif
struct {
void * payload; // This be set once and not made globally visable until set
size_t size; // This be set once and not made globally visable until set
volatile node_t seq_next;
volatile node_t spec_next;
pid_t allocator;
};
} header_t;
/*
Header pages contain a list of (possibly unallocated) headers
Each header correspondes to one block (contig. memory for prog data).
Additionally, the header page struct keeps track of the next unallocated
header index within this page.
*/
#if __SIZEOF_POINTER__ == 8
// 64b pointers
#define combinded_header_next_t unsigned __int128
#else
#define combinded_header_next_t uint64_t
#endif
#ifndef PAGES_PER_HPG
#define PAGES_PER_HPG 5
#endif
#define HEADERS_PER_PAGE ((( PAGES_PER_HPG * PAGE_SIZE) - \
(sizeof(size_t) + sizeof(void*) + sizeof(ipa_page_t))) / sizeof(header_t) \
)
typedef struct {
ipa_page_t next_page;
volatile struct header_page_t * next_header;
// unsigned number; // to be used used to help minimize the number of header pages?
size_t next_free;
header_t headers[HEADERS_PER_PAGE];
} header_page_t;
_Static_assert(sizeof(header_page_t) <= (PAGES_PER_HPG *PAGE_SIZE), "Header pages not configured correctly");
typedef struct {
header_t * header;
} block_t;
typedef struct {
ipa_page_t next_page;
volatile struct huge_block_t * next_block;
size_t huge_block_sz; //Note: this includes the block_t space
int file_name; // Might be able to eliminate this field
bool is_shared;
} huge_block_t;
_Static_assert(__builtin_offsetof(huge_block_t, next_page) == 0, "Bad struct: huge_block_t");
_Static_assert(__builtin_offsetof(header_page_t, next_page) == 0, "Bad struct: header_page_t");
// Used to collect statics with minimal cache impact
typedef volatile unsigned long stats_int_t __attribute__ ((aligned (64))); //64B aligned int
// Allocated at the start of the program in shared mem.
typedef struct {
ipa_page_t next_page;
#ifdef COLLECT_STATS
volatile stats_int_t allocations;
volatile stats_int_t frees;
volatile stats_int_t sbrks;
volatile unsigned long allocs_per_class[NUM_CLASSES];
volatile stats_int_t huge_allocations;
volatile stats_int_t header_pages;
volatile stats_int_t total_blocks; // this includes spec and non spec
volatile stats_int_t total_alloc; // total space allocated from the system (heap + mmap)
volatile stats_int_t time_malloc;
volatile stats_int_t num_unmaps;
volatile stats_int_t number_mmap; // how many pages where mmaped (headers & large)
volatile stats_int_t spec_sbrks;
volatile stats_int_t total_frames; //estimate of the total number of frames used
#endif
volatile ipa_stack_t seq_free[NUM_CLASSES]; // sequential free list
volatile ipa_stack_t spec_free[NUM_CLASSES]; // speculative free list
volatile size_t base; //where segment begins (cache)
volatile size_t spec_growth; // grow (B) done by spec group
volatile void * spec_base;
volatile unsigned next_name; // next header file to use
volatile header_page_t * header_pg; // the address of the first header mmap page
volatile huge_block_t * large_block; // pointer into the list of large blocks
volatile int dummy; // used in unit tests
} shared_data_t;
static inline void stats_collect(volatile stats_int_t * x, unsigned increment) {
#ifdef COLLECT_STATS
__sync_add_and_fetch(x, increment);
#endif
}
static inline void page_collect(volatile stats_int_t * x, unsigned s) {
double frames = ((double) s) / PAGE_SIZE;
stats_collect(x, ceil(frames));
}
// Extern data
extern shared_data_t * shared;
// Function prototypes
void ipa_init(void);
// Utility functions
static inline block_t * getblock(void * user_payload) {
return (block_t *) (((char*) user_payload) - sizeof(block_t));
}
static inline huge_block_t * gethugeblock(void * user_payload) {
return (huge_block_t *) (((char*) user_payload) - sizeof(huge_block_t));
}
static inline void * ipa_getpayload(volatile block_t * block) {
return (void *) (((char*) block) + sizeof(block_t));
}
static inline void * gethugepayload(volatile huge_block_t * block) {
return (void *) (((char*) block) + sizeof(huge_block_t));
}
#define SPEC_ALLOC_B (1<<0)
#define SEQ_ALLOC_B (1<<1)
static inline void * payload(volatile header_t * header) {
return (void *) (((size_t) header->payload) & ~(SPEC_ALLOC_B | SEQ_ALLOC_B));
}
static inline void record_mode_alloc(volatile header_t * header) {
if (speculating()) {
__sync_fetch_and_or((int *) &header->payload, SPEC_ALLOC_B);
} else {
__sync_fetch_and_or((int *) &header->payload, SEQ_ALLOC_B);
}
}
static inline bool seq_alloced(volatile header_t * header) {
return (((size_t) header->payload) & SEQ_ALLOC_B) != 0;
}
static inline bool spec_alloced(volatile header_t * header) {
return (((size_t) header->payload) & SPEC_ALLOC_B) != 0;
}
static inline void record_mode_free(volatile header_t * header) {
if (speculating()) {
__sync_fetch_and_and((int *) &header->payload, ~SPEC_ALLOC_B);
} else {
__sync_fetch_and_and((int *) &header->payload, ~SEQ_ALLOC_B);
}
}
// Functions to convert between node links to the header
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - __builtin_offsetof(type,member) );})
static inline volatile header_t * spec_node_to_header(volatile node_t * node) {
if (node == NULL) {
return NULL;
} else {
return container_of(node, volatile header_t, spec_next);
}
}
static inline volatile header_t * seq_node_to_header(volatile node_t * node) {
if (node == NULL) {
return NULL;
} else {
return container_of(node, volatile header_t, seq_next);
}
}
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
void * ipa_malloc(size_t);
void * ipacalloc(size_t, size_t);
void * iparealloc(void *, size_t);
void ipafree(void *);
size_t ipa_usable_space(void *);
void beginspec(void);
void endspec(bool);
void record_allocation(void *, size_t);
void ipa_teardown(void);
#endif