FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
numalloc/source-reducememory/libnumalloc.cpp at master · UTSASRG/numalloc · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
UTSASRG
/
numalloc
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
3
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
numalloc
/
source-reducememory
/
libnumalloc.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
347 lines (297 loc) · 9.99 KB
Breadcrumbs
numalloc
/
source-reducememory
/
libnumalloc.cpp
Copy path
File metadata and controls
347 lines (297 loc) · 9.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* FreeGuard: A Faster Secure Heap Allocator
* Copyright (C) 2017 Sam Silvestro, Hongyu Liu, Corey Crosser,
* Zhiqiang Lin, and Tongping Liu
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @file liblight.cpp: main file, includes memory interception functions.
* @author Tongping Liu <http://www.cs.utsa.edu/~tongpingliu/>
* @author Sam Silvestro <sam.silvestro@utsa.edu>
*/
#
include
<
dlfcn.h
>
#
include
<
sys/mman.h
>
#
include
<
numa.h
>
#
include
<
string.h
>
#
include
"
real.hh
"
#
include
"
xthread.hh
"
#
include
"
numaheap.hh
"
#
include
"
mm.hh
"
#
include
"
xdefines.hh
"
#
include
"
perthread.hh
"
#
define
GET_TIME
0
#
if
GET_TIME
volatile
unsigned
long
long
allocs =
0
;
volatile
unsigned
long
long
allocsfor48 =
0
;
volatile
unsigned
long
long
origTime =
0
;
volatile
unsigned
long
long
totalAllocCycles =
0
;
volatile
unsigned
long
long
totalFreeCycles =
0
;
#
endif
inline
unsigned
long
long
rdtscp
() {
unsigned
int
lo, hi;
asm
volatile
(
"
rdtscp
"
:
"
=a
"
(lo),
"
=d
"
(hi)
/*
outputs
*/
:
"
a
"
(
0
)
/*
inputs
*/
:
"
%ebx
"
,
"
%ecx
"
);
/*
clobbers
*/
unsigned
long
long
retval = ((
unsigned
long
long
)lo) | (((
unsigned
long
long
)hi) <<
32
);
return
retval;
}
//
Define a thread local variable--current. Based on the evaluation,
//
http://david-grs.github.io/tls_performance_overhead_cost_linux/,
//
TLS is very efficient. I also have verified this by myself.
thread_local
thread_t
* current;
char
localBuffer[
4096
];
char
* localPtr =
NULL
;
char
* localPtrEnd;
//
Variables used by our pre-init private allocator
typedef
enum
{
E_HEAP_INIT_NOT
=
0
,
E_HEAP_INIT_WORKING
,
E_HEAP_INIT_DONE
} eHeapInitStatus;
eHeapInitStatus heapInitStatus =
E_HEAP_INIT_NOT
;
typedef
int
(*
main_fn_t
)(
int
,
char
**,
char
**);
extern
"
C
"
int
__libc_start_main
(
main_fn_t
,
int
,
char
**,
void
(*)(), void (*)(), void (*)(), void*) __attribute__((weak, alias(
"
light_libc_start_main
"
)));
extern
"
C
"
int
light_libc_start_main
(
main_fn_t
main_fn,
int
argc,
char
** argv,
void
(*init)(), void (*fini)(), void (*rtld_fini)(), void* stack_end) {
//
real run
auto
real_libc_start_main = (
decltype
(__libc_start_main)*)
dlsym
(
RTLD_NEXT
,
"
__libc_start_main
"
);
//
only for main thread
//
current->startFrame = (char *)__builtin_frame_address(0);
return
real_libc_start_main
(main_fn, argc, argv, init, fini, rtld_fini, stack_end);
}
extern
"
C
"
{
void
xxfree
(
void
*);
void
*
xxmalloc
(
size_t
);
//
/Jin
void
*
xxcalloc
(
size_t
,
size_t
);
void
*
xxrealloc
(
void
*,
size_t
);
void
*
xxvalloc
(
size_t
);
void
*
xxaligned_alloc
(
size_t
,
size_t
);
void
*
xxmemalign
(
size_t
,
size_t
);
void
*
xxpvalloc
(
size_t
);
void
*
xxalloca
(
size_t
);
int
xxposix_memalign
(
void
**,
size_t
,
size_t
);
//
Function aliases
void
free
(
void
*) __attribute__ ((weak, alias(
"
xxfree
"
)));
void
*
malloc
(
size_t
) __attribute__ ((weak, alias(
"
xxmalloc
"
)));
//
/Jin
void
*
calloc
(
size_t
,
size_t
) __attribute__ ((weak, alias(
"
xxcalloc
"
)));
void
*
realloc
(
void
*,
size_t
) __attribute__ ((weak, alias(
"
xxrealloc
"
)));
void
*
valloc
(
size_t
) __attribute__ ((weak, alias(
"
xxvalloc
"
)));
void
*
aligned_alloc
(
size_t
,
size_t
) __attribute__ ((weak,
alias
(
"
xxaligned_alloc
"
)));
void
*
memalign
(
size_t
,
size_t
) __attribute__ ((weak, alias(
"
xxmemalign
"
)));
void
*
pvalloc
(
size_t
) __attribute__ ((weak, alias(
"
xxpvalloc
"
)));
void
*
alloca
(
size_t
) __attribute__ ((weak, alias(
"
xxalloca
"
)));
int
posix_memalign
(
void
**,
size_t
,
size_t
) __attribute__ ((weak,
alias
(
"
xxposix_memalign
"
)));
}
void
heapinitialize
();
__attribute__
((constructor)) void initializer() {
#
if
GET_TIME
origTime =
rdtscp
();
#
endif
//
/Jin
if
(localPtr ==
NULL
) {
localPtr = localBuffer;
localPtrEnd = &localBuffer[
4096
];
Real::initializer
();
heapInitStatus =
E_HEAP_INIT_WORKING
;
}
heapinitialize
();
}
__attribute__
((destructor)) void finalizer() {
#
if
GET_TIME
fprintf
(stderr,
"
total alloc cycles %lld, free cycles %lld
\n
"
, totalAllocCycles, totalFreeCycles);
fprintf
(stderr,
"
total allocs for 48 is %llx
\n
"
, allocsfor48);
unsigned
long
long
lastTime =
rdtscp
() - origTime;
fprintf
(stderr,
"
total runtime is %lld, percentage %lf
"
, lastTime, ((
double
)lastTime)/((
double
)(allocsfor48)));
#
endif
}
void
*
operator
new
(
size_t
sz) {
return
xxmalloc
(sz);
}
void
*
operator
new
(
size_t
sz,
const
std::
nothrow_t
&) throw() {
return
xxmalloc
(sz);
}
void
operator
delete
(
void
* ptr) __THROW {
xxfree
(ptr);
}
void
*
operator
new
[] (
size_t
sz) {
return
xxmalloc
(sz);
}
//
Heap initialization function
void
heapinitialize
() {
if
(heapInitStatus ==
E_HEAP_INIT_WORKING
) {
//
Including the number of nodes, and the size of freed memory if possible
NumaHeap::getInstance
().
initialize
();
//
Thread initialization, which can be occurred before or after numa heap initialization
xthread::getInstance
().
initialize
();
//
fprintf(stderr, "heap is initialized now\n");
heapInitStatus =
E_HEAP_INIT_DONE
;
}
//
///Jin
//
else {
//
while(heapInitStatus != E_HEAP_INIT_DONE);
//
}
}
void
*
xxmalloc
(
size_t
size) {
//
If this is the first allocation before the initialization,
//
then we will try to use a local buffer, since Real::initializer()
//
will invoke malloc as well.
if
(localPtr ==
NULL
) {
localPtr = localBuffer;
localPtrEnd = &localBuffer[
4096
];
Real::initializer
();
//
/Jin
heapInitStatus =
E_HEAP_INIT_WORKING
;
}
void
* ptr =
NULL
;
switch
(heapInitStatus) {
case
E_HEAP_INIT_NOT
:
ptr = localPtr;
localPtr += size;
if
(localPtr > localPtrEnd) {
//
fprintf(stderr, "Making the temporary buffer larger\n");
abort
();
}
break
;
case
E_HEAP_INIT_WORKING
:
ptr =
Real::malloc
(size);
break
;
case
E_HEAP_INIT_DONE
:
#
if
GET_TIME
unsigned
long
long
start =
rdtscp
();
#
endif
//
fprintf(stderr, "allocate right now with size %lx\n", size);
ptr =
NumaHeap::getInstance
().
allocate
(size);
#
if
GET_TIME
//
if(size != 57) {
//
fprintf(stderr, "malloc size %ld ret %p\n", size, ptr);
//
}
unsigned
long
long
length =
rdtscp
() - start;
totalAllocCycles += length;
if
((allocs %
1000000
) ==
0
) {
fprintf
(stderr,
"
Runtime is %lld totalAllocCycles %lld, allocs %lld. current length %lld
\n
"
,
rdtscp
()-origTime, totalAllocCycles, allocs, length);
}
//
fprintf(stderr, "totalAllocCycles %lld length %lld\n", totalAllocCycles, length);
allocs++;
if
(allocs ==
15000000
) {
exit
(
0
); }
//
fprintf(stderr, "malloc size %ld ptr %p\n", size, ptr);
#
endif
break
;
}
return
ptr;
}
void
xxfree
(
void
* ptr) {
if
(ptr ==
NULL
) {
return
;
}
if
(heapInitStatus ==
E_HEAP_INIT_WORKING
) {
Real::free
(ptr);
}
else
{
#
if
GET_TIME
unsigned
long
long
start =
rdtscp
();
#
endif
//
Perform the free operation
NumaHeap::getInstance
().
deallocate
(ptr);
#
if
GET_TIME
totalFreeCycles +=
rdtscp
() - start;
#
endif
}
}
//
/Jin
void
*
xxcalloc
(
size_t
nelem,
size_t
elsize) {
void
* ptr =
NULL
;
ptr =
xxmalloc
(nelem * elsize);
if
(ptr !=
NULL
) {
memset
(ptr,
0
, nelem * elsize);
}
return
ptr;
}
void
*
xxrealloc
(
void
* ptr,
size_t
sz) {
if
(heapInitStatus ==
E_HEAP_INIT_WORKING
) {
return
Real::realloc
(ptr, sz);
}
//
If the pointer is null, call is equivalent to malloc(sz).
if
(ptr ==
NULL
) {
return
xxmalloc
(sz);
}
//
If the pointer is non-null and size is zero, call is equivalent
//
to free(ptr).
if
(sz ==
0
) {
xxfree
(ptr);
return
NULL
;
}
//
If the object is unknown to us, return NULL to indicate error.
size_t
oldSize =
NumaHeap::getInstance
().
getSize
(ptr);
//
/Jin
void
* newObject;
if
(oldSize == (
size_t
)-
1
) {
void
* tmp =
Real::realloc
(ptr, sz);
newObject =
xxmalloc
(sz);
memcpy
(newObject, tmp, sz);
Real::free
(tmp);
}
else
{
newObject =
xxmalloc
(sz);
memcpy
(newObject, ptr, oldSize < sz ? oldSize : sz);
xxfree
(ptr);
}
return
newObject;
}
void
*
xxalloca
(
size_t
size) {
PRERR
(
"
%s CALLED
"
, __FUNCTION__);
return
NULL
;
}
void
*
xxvalloc
(
size_t
size) {
PRERR
(
"
%s CALLED
"
, __FUNCTION__);
return
NULL
;
}
int
xxposix_memalign
(
void
**memptr,
size_t
alignment,
size_t
size) {
void
* alignedObject =
xxmemalign
(alignment, size);
*memptr = alignedObject;
return
0
;
}
void
*
xxaligned_alloc
(
size_t
alignment,
size_t
size) {
PRERR
(
"
%s CALLED
"
, __FUNCTION__);
return
NULL
;
}
void
*
xxmemalign
(
size_t
alignment,
size_t
size) {
if
(size ==
0
) {
return
NULL
;
}
size_t
allocObjectSize = alignment + size;
void
* object =
xxmalloc
(allocObjectSize);
unsigned
long
residualBytes = (
unsigned
long
)object % alignment;
void
* alignedObject = (
void
*)((
char
*)object + residualBytes);
return
alignedObject;
}
void
*
xxpvalloc
(
size_t
size) {
PRERR
(
"
%s CALLED
"
, __FUNCTION__);
return
NULL
;
}
//
Intercept thread creation
int
pthread_create
(
pthread_t
* tid,
const
pthread_attr_t
* attr,
void
*(*start_routine)(
void
*), void * arg) {
if
(heapInitStatus !=
E_HEAP_INIT_DONE
) {
heapinitialize
();
}
return
xthread::getInstance
().
thread_create
(tid, attr, start_routine, arg);
}
int
pthread_join
(
pthread_t
tid,
void
** retval) {
return
xthread::getInstance
().
thread_join
(tid, retval);
}
Back
|
FazBrowse Home
|
New Git URL