-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmemory_manager.cpp
432 lines (378 loc) · 12.3 KB
/
memory_manager.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*++
Copyright (c) 2015 Microsoft Corporation
--*/
#include<iostream>
#include<stdlib.h>
#include<climits>
#include "util/mutex.h"
#include "util/trace.h"
#include "util/memory_manager.h"
#include "util/error_codes.h"
#include "util/debug.h"
#include "util/scoped_timer.h"
#ifdef __GLIBC__
# include <malloc.h>
# define HAS_MALLOC_USABLE_SIZE
#elif defined(__APPLE__)
# include <malloc/malloc.h>
# define HAS_MALLOC_USABLE_SIZE
# define malloc_usable_size malloc_size
#elif defined(__FreeBSD__)
# include <malloc_np.h>
# define HAS_MALLOC_USABLE_SIZE
#elif defined(_WINDOWS)
# include <malloc.h>
# define HAS_MALLOC_USABLE_SIZE
# define malloc_usable_size _msize
#endif
// The following two function are automatically generated by the mk_make.py script.
// The script collects ADD_INITIALIZER and ADD_FINALIZER commands in the .h files.
// For example, rational.h contains
// ADD_INITIALIZER('rational::initialize();')
// ADD_FINALIZER('rational::finalize();')
// Thus, any executable or shared object (DLL) that depends on rational.h
// will have an automatically generated file mem_initializer.cpp containing
// mem_initialize()
// mem_finalize()
// and these functions will include the statements:
// rational::initialize();
//
// rational::finalize();
void mem_initialize();
void mem_finalize();
// If PROFILE_MEMORY is defined, Z3 will display the amount of memory used, and the number of synchronization steps during finalization
// #define PROFILE_MEMORY
out_of_memory_error::out_of_memory_error():z3_error(ERR_MEMOUT) {
}
static DECLARE_INIT_MUTEX(g_memory_mux);
static atomic<bool> g_memory_out_of_memory(false);
static bool g_memory_initialized = false;
static long long g_memory_alloc_size = 0;
static long long g_memory_max_size = 0;
static long long g_memory_max_used_size = 0;
static long long g_memory_watermark = 0;
static long long g_memory_alloc_count = 0;
static long long g_memory_max_alloc_count = 0;
static bool g_exit_when_out_of_memory = false;
static char const * g_out_of_memory_msg = "ERROR: out of memory";
void memory::exit_when_out_of_memory(bool flag, char const * msg) {
g_exit_when_out_of_memory = flag;
if (flag && msg)
g_out_of_memory_msg = msg;
}
static void throw_out_of_memory() {
g_memory_out_of_memory = true;
if (g_exit_when_out_of_memory) {
std::cerr << g_out_of_memory_msg << "\n";
exit(ERR_MEMOUT);
}
else {
throw out_of_memory_error();
}
}
static void throw_alloc_counts_exceeded() {
std::cout << "Maximal allocation counts " << g_memory_max_alloc_count << " have been exceeded\n";
exit(ERR_ALLOC_EXCEEDED);
}
#ifdef PROFILE_MEMORY
static unsigned g_synch_counter = 0;
class mem_usage_report {
public:
~mem_usage_report() {
std::cerr << "(memory :max " << g_memory_max_used_size
<< " :allocs " << g_memory_alloc_count
<< " :final " << g_memory_alloc_size
<< " :synch " << g_synch_counter << ")" << std::endl;
}
};
mem_usage_report g_info;
#endif
void memory::initialize(size_t max_size) {
static mutex init_mux;
lock_guard lock(init_mux);
// only update the maximum size if max_size != UINT_MAX
if (max_size != UINT_MAX)
g_memory_max_size = max_size;
if (g_memory_initialized)
return;
g_memory_out_of_memory = false;
mem_initialize();
g_memory_initialized = true;
}
bool memory::is_out_of_memory() {
return g_memory_out_of_memory;
}
void memory::set_high_watermark(size_t watermark) {
// This method is only safe to invoke at initialization time, that is, before the threads are created.
g_memory_watermark = watermark;
}
bool memory::above_high_watermark() {
if (g_memory_watermark == 0)
return false;
lock_guard lock(*g_memory_mux);
return g_memory_watermark < g_memory_alloc_size;
}
// The following methods are only safe to invoke at
// initialization time, that is, before threads are created.
void memory::set_max_size(size_t max_size) {
g_memory_max_size = max_size;
}
void memory::set_max_alloc_count(size_t max_count) {
g_memory_max_alloc_count = max_count;
}
static bool g_finalizing = false;
void memory::finalize(bool shutdown) {
if (g_memory_initialized) {
g_finalizing = true;
mem_finalize();
// we leak the mutex since we need it to be always live since memory may
// be reinitialized again
//delete g_memory_mux;
g_memory_initialized = false;
g_finalizing = false;
if (shutdown) {
scoped_timer::finalize();
}
}
}
unsigned long long memory::get_allocation_size() {
long long r;
{
lock_guard lock(*g_memory_mux);
r = g_memory_alloc_size;
}
if (r < 0)
r = 0;
return r;
}
unsigned long long memory::get_max_used_memory() {
unsigned long long r;
{
lock_guard lock(*g_memory_mux);
r = g_memory_max_used_size;
}
return r;
}
#if defined(_WINDOWS)
#include <windows.h>
#endif
unsigned long long memory::get_max_memory_size() {
#if defined(_WINDOWS)
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
return statex.ullTotalPhys;
#else
// 16 GB
return 1ull << 34ull;
#endif
}
unsigned long long memory::get_allocation_count() {
return g_memory_alloc_count;
}
void memory::display_max_usage(std::ostream & os) {
unsigned long long mem = get_max_used_memory();
os << "max. heap size: "
<< static_cast<double>(mem)/static_cast<double>(1024*1024)
<< " Mbytes\n";
}
void memory::display_i_max_usage(std::ostream & os) {
unsigned long long mem = get_max_used_memory();
std::cout << "MEMORY "
<< static_cast<double>(mem)/static_cast<double>(1024*1024)
<< "\n";
}
#if Z3DEBUG
void memory::deallocate(char const * file, int line, void * p) {
deallocate(p);
TRACE_CODE(if (!g_finalizing) TRACE("memory", tout << "dealloc " << std::hex << p << std::dec << " " << file << ":" << line << "\n";););
}
void * memory::allocate(char const* file, int line, char const* obj, size_t s) {
void * r = allocate(s);
TRACE("memory", tout << "alloc " << std::hex << r << std::dec << " " << file << ":" << line << " " << obj << " " << s << "\n";);
return r;
}
#endif
#ifndef SINGLE_THREAD
// ==================================
// ==================================
// THREAD LOCAL VERSION
// ==================================
// ==================================
// We only integrate the local thread counters with the global one
// when the local counter > SYNCH_THRESHOLD
#define SYNCH_THRESHOLD 100000
thread_local long long g_memory_thread_alloc_size = 0;
thread_local long long g_memory_thread_alloc_count = 0;
static void synchronize_counters(bool allocating) {
#ifdef PROFILE_MEMORY
g_synch_counter++;
#endif
bool out_of_mem = false;
bool counts_exceeded = false;
{
lock_guard lock(*g_memory_mux);
g_memory_alloc_size += g_memory_thread_alloc_size;
g_memory_alloc_count += g_memory_thread_alloc_count;
if (g_memory_alloc_size > g_memory_max_used_size)
g_memory_max_used_size = g_memory_alloc_size;
if (g_memory_max_size != 0 && g_memory_alloc_size > g_memory_max_size)
out_of_mem = true;
if (g_memory_max_alloc_count != 0 && g_memory_alloc_count > g_memory_max_alloc_count)
counts_exceeded = true;
}
g_memory_thread_alloc_size = 0;
if (out_of_mem && allocating) {
throw_out_of_memory();
}
if (counts_exceeded && allocating) {
throw_alloc_counts_exceeded();
}
}
void memory::deallocate(void * p) {
#ifdef HAS_MALLOC_USABLE_SIZE
size_t sz = malloc_usable_size(p);
void * real_p = p;
#else
size_t * sz_p = reinterpret_cast<size_t*>(p) - 1;
size_t sz = *sz_p;
void * real_p = reinterpret_cast<void*>(sz_p);
#endif
g_memory_thread_alloc_size -= sz;
free(real_p);
if (g_memory_thread_alloc_size < -SYNCH_THRESHOLD) {
synchronize_counters(false);
}
}
void * memory::allocate(size_t s) {
#ifndef HAS_MALLOC_USABLE_SIZE
s = s + sizeof(size_t); // we allocate an extra field!
#endif
g_memory_thread_alloc_size += s;
g_memory_thread_alloc_count += 1;
if (g_memory_thread_alloc_size > SYNCH_THRESHOLD) {
synchronize_counters(true);
}
void * r = malloc(s);
if (r == nullptr) {
throw_out_of_memory();
return nullptr;
}
#ifdef HAS_MALLOC_USABLE_SIZE
g_memory_thread_alloc_size += malloc_usable_size(r) - s;
return r;
#else
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
#endif
}
void* memory::reallocate(void *p, size_t s) {
#ifdef HAS_MALLOC_USABLE_SIZE
size_t sz = malloc_usable_size(p);
void * real_p = p;
// We may be lucky and malloc gave us enough space
if (sz >= s)
return p;
#else
size_t *sz_p = reinterpret_cast<size_t*>(p)-1;
size_t sz = *sz_p;
void *real_p = reinterpret_cast<void*>(sz_p);
s = s + sizeof(size_t); // we allocate an extra field!
#endif
g_memory_thread_alloc_size += s - sz;
g_memory_thread_alloc_count += 1;
if (g_memory_thread_alloc_size > SYNCH_THRESHOLD) {
synchronize_counters(true);
}
void *r = realloc(real_p, s);
if (r == nullptr) {
throw_out_of_memory();
return nullptr;
}
#ifdef HAS_MALLOC_USABLE_SIZE
g_memory_thread_alloc_size += malloc_usable_size(r) - s;
return r;
#else
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
#endif
}
#else
// ==================================
// ==================================
// SINGLE-THREAD MODE
// ==================================
// ==================================
// allocate & deallocate without locking
void memory::deallocate(void * p) {
#ifdef HAS_MALLOC_USABLE_SIZE
size_t sz = malloc_usable_size(p);
void * real_p = p;
#else
size_t * sz_p = reinterpret_cast<size_t*>(p) - 1;
size_t sz = *sz_p;
void * real_p = reinterpret_cast<void*>(sz_p);
#endif
g_memory_alloc_size -= sz;
free(real_p);
}
void * memory::allocate(size_t s) {
#ifndef HAS_MALLOC_USABLE_SIZE
s = s + sizeof(size_t); // we allocate an extra field!
#endif
g_memory_alloc_size += s;
g_memory_alloc_count += 1;
if (g_memory_alloc_size > g_memory_max_used_size)
g_memory_max_used_size = g_memory_alloc_size;
if (g_memory_max_size != 0 && g_memory_alloc_size > g_memory_max_size)
throw_out_of_memory();
if (g_memory_max_alloc_count != 0 && g_memory_alloc_count > g_memory_max_alloc_count)
throw_alloc_counts_exceeded();
void * r = malloc(s);
if (r == nullptr) {
throw_out_of_memory();
return nullptr;
}
#ifdef HAS_MALLOC_USABLE_SIZE
g_memory_alloc_size += malloc_usable_size(r) - s;
return r;
#else
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
#endif
}
void* memory::reallocate(void *p, size_t s) {
#ifdef HAS_MALLOC_USABLE_SIZE
size_t sz = malloc_usable_size(p);
void * real_p = p;
// We may be lucky and malloc gave us enough space
if (sz >= s)
return p;
#else
size_t * sz_p = reinterpret_cast<size_t*>(p) - 1;
size_t sz = *sz_p;
void * real_p = reinterpret_cast<void*>(sz_p);
s = s + sizeof(size_t); // we allocate an extra field!
#endif
g_memory_alloc_size += s - sz;
g_memory_alloc_count += 1;
if (g_memory_alloc_size > g_memory_max_used_size)
g_memory_max_used_size = g_memory_alloc_size;
if (g_memory_max_size != 0 && g_memory_alloc_size > g_memory_max_size)
throw_out_of_memory();
if (g_memory_max_alloc_count != 0 && g_memory_alloc_count > g_memory_max_alloc_count)
throw_alloc_counts_exceeded();
void *r = realloc(real_p, s);
if (r == nullptr) {
throw_out_of_memory();
return nullptr;
}
#ifdef HAS_MALLOC_USABLE_SIZE
g_memory_alloc_size += malloc_usable_size(r) - s;
return r;
#else
*(static_cast<size_t*>(r)) = s;
return static_cast<size_t*>(r) + 1; // we return a pointer to the location after the extra field
#endif
}
#endif