-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplsalloc.cpp
executable file
·235 lines (202 loc) · 6.81 KB
/
plsalloc.cpp
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
/** $lic$
* Copyright (C) 2017-2020 by Massachusetts Institute of Technology
*
* This file is part of plsalloc.
*
* plsalloc is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* plsalloc was developed as part of the Swarm architecture project. If you use
* this software in your research, we request that you reference the Swarm
* MICRO 2018 paper ("Harmonizing Speculative and Non-Speculative Execution in
* Architectures for Ordered Parallelism", Jeffrey et al., MICRO-51, 2018) as
* the source of plsalloc in any publications that use this software, and that
* you send us a citation of your work.
*
* plsalloc is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* malloc interface around plsalloc */
#include <cstdlib>
#include <string.h>
#include <errno.h>
#include <malloc.h>
//#include <sys/mman.h>
#include <tuple>
#include "swarm/hooks.h"
/* Layout (NOTE: Keep in sync with simulator tracked/untracked segments) */
// 512 GB tracked + 512GB untracked (leave first 512GB of each segment to sim)
#define PLSALLOC_TRACKED_BASEADDR (0x0a8000000000ul)
#define PLSALLOC_UNTRACKED_BASEADDR (0x0b8000000000ul)
#include "alloc.h"
/* Helper methods for abort / commit handlers (all of which call dealloc) */
static void dealloc_task(uint64_t ts, void* p) { plsalloc::do_dealloc(p); }
template <bool onAbort>
static void enqueue_dealloc(void* ptr) {
// We can't import the Swarm API and swarm::enqueue because we're in a fairly
// restricted environment (e.g., we implement the malloc/free that
// memTupleRunners use, and don't want to acquire a dep on libswarm). So do a
// raw enqueue.
uint64_t numArgs = 1;
uint64_t hintFlags = EnqFlags::SAMEHINT | EnqFlags::CANTSPEC | EnqFlags::NOTIMESTAMP;
if (onAbort) hintFlags |= EnqFlags::RUNONABORT;
uint64_t magicOp = (MAGIC_OP_TASK_ENQUEUE_BEGIN + numArgs) | hintFlags;
sim_magic_op_2(magicOp, reinterpret_cast<uint64_t>(ptr), reinterpret_cast<uint64_t>(&dealloc_task));
}
static void on_abort_dealloc(void* ptr) {
if (sim_priv_isdoomed()) plsalloc::do_dealloc(ptr);
else enqueue_dealloc<true>(ptr);
}
static void on_commit_dealloc(void* ptr) {
if (sim_isirrevocable()) plsalloc::do_dealloc(ptr);
else enqueue_dealloc<false>(ptr);
}
/* External malloc interface */
void* malloc(size_t size) {
if (unlikely(!size)) return nullptr;
sim_priv_call();
void* p = plsalloc::do_alloc(size);
on_abort_dealloc(p);
sim_priv_ret();
return p;
}
void* calloc(size_t nmemb, size_t size) {
size_t sz = nmemb * size;
if (unlikely(!sz)) return nullptr;
sim_priv_call();
void* p = plsalloc::do_alloc(sz);
on_abort_dealloc(p);
sim_priv_ret();
memset(p, 0, sz);
return p;
}
void* realloc(void* ptr, size_t size) {
if (!ptr) return malloc(size);
sim_priv_call();
if (plsalloc::valid_chunk(ptr)) {
if (!size) {
sim_priv_ret();
on_commit_dealloc(ptr);
return nullptr;
}
size_t chunkSize = plsalloc::chunk_size(ptr);
// If it fits and we're not wasting too much space, do nothing
if (chunkSize >= size && chunkSize/2 <= size) return ptr;
void* newPtr = plsalloc::do_alloc(size);
on_abort_dealloc(newPtr);
sim_priv_ret();
memcpy(newPtr, ptr, (size < chunkSize) ? size : chunkSize);
on_commit_dealloc(ptr);
return newPtr;
} else {
sim_priv_ret();
// This is an invalid chunk, so take an exception
sim_serialize();
std::abort();
}
}
void free(void* ptr) {
if (!ptr) return;
on_commit_dealloc(ptr);
}
void cfree(void* ptr) { free(ptr); }
int posix_memalign(void **memptr, size_t alignment, size_t size) {
// NOTE: On failure, posix_memalign doesn't modify memptr
if (size == 0) {
*memptr = nullptr;
} else if (!alignment || (alignment & (alignment - 1))
|| (alignment % sizeof(void*))) {
return EINVAL;
} else {
// FIXME(dsm): Cache-line aligned for now
void* ptr = malloc(size);
if (!ptr) return ENOMEM;
*memptr = ptr;
}
return 0;
}
void* aligned_alloc(size_t alignment, size_t size) {
void* ptr;
int dc = posix_memalign(&ptr, alignment, size);
if (dc != 0) ptr = nullptr;
return ptr;
}
void* memalign(size_t alignment, size_t size) {
return aligned_alloc(alignment, size);
}
// The version of <string.h> header we have installed declares
// the argument of strdup to be non-null. Perhaps we should
// follow the standard and assert/assume src is non-null?
#pragma GCC diagnostic push
#if defined(__clang__)
#pragma GCC diagnostic ignored "-Wtautological-pointer-compare"
#elif defined(__GNUC__) && (__GNUC__ >= 6)
#pragma GCC diagnostic ignored "-Wnonnull-compare"
#endif
char* strdup(const char* src) {
if (src == nullptr) return nullptr;
size_t len = strlen(src);
char* dst = (char*)malloc(len);
memcpy(dst, src, len);
return dst;
}
#pragma GCC diagnostic pop
size_t malloc_usable_size(void* ptr) {
sim_priv_call();
if (plsalloc::valid_chunk(ptr)) {
size_t chunkSz = plsalloc::chunk_size(ptr);
sim_priv_ret();
return chunkSz;
} else {
sim_priv_ret();
// Invalid chunk, take an exception
sim_serialize();
std::abort();
return 0;
}
}
/* Unimplemented functions below. Programs rarely use these, so rather than
* implementing the library in full, we do these on demand */
static void abort_unimplemented(const char* fn) {
char buf[1024];
snprintf(buf, sizeof(buf), "Aborting: sim-alloc function unimplemented: %s", fn);
sim_magic_op_1(MAGIC_OP_WRITE_STD_OUT, reinterpret_cast<uint64_t>(&buf[0]));
std::abort();
}
void* valloc(size_t size) {
abort_unimplemented(__FUNCTION__);
return nullptr;
}
void* pvalloc(size_t size) {
abort_unimplemented(__FUNCTION__);
return nullptr;
}
void* malloc_get_state(void) {
abort_unimplemented(__FUNCTION__);
return nullptr;
}
int malloc_set_state(void*) {
abort_unimplemented(__FUNCTION__);
return -1;
}
int malloc_info(int, FILE*) {
abort_unimplemented(__FUNCTION__);
return -1;
}
void malloc_stats(void) {
abort_unimplemented(__FUNCTION__);
}
int malloc_trim(size_t) {
abort_unimplemented(__FUNCTION__);
return -1;
}
// http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html
// __malloc_hook
// __malloc_initialize_hook
// __free_hook