-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
289 lines (236 loc) · 8.43 KB
/
Makefile
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
# Very simple makefile to build the library and example code
# Configurable flags:
ifeq ($(USE_TCMALLOC),)
# default value: assume you have a package like "gperftools-libs" installed on your system
USE_TCMALLOC=1
endif
# Start of Makefile
CC=g++
CXXFLAGS_OPT= -fPIC -std=c++11 -Iinclude -O3 -pthread -Wno-attributes -Wno-unused-result
CXXFLAGS_DBG= -fPIC -std=c++11 -Iinclude -g -O0 -pthread -Wno-attributes -Wno-unused-result
LIB_HDR = \
include/malloc_tag.h \
include/private/malloc_tree_node.h \
include/private/malloc_tree_registry.h \
include/private/malloc_tree.h \
include/private/output_utils.h
LIB_SRC = \
src/malloc_tag.cpp \
src/malloc_tree.cpp \
src/malloc_tree_registry.cpp \
src/malloc_tree_node.cpp
LIBS = \
src/libmalloc_tag.so
BINS = \
examples/minimal/minimal \
examples/multithread/multithread \
tests/unit_tests
ifeq ($(USE_TCMALLOC),1)
BINS += examples/malloctag_and_tcmalloc/malloctag_and_tcmalloc
endif
LIB_OBJ = $(subst .cpp,.o,$(LIB_SRC))
LIB_VER = 1
SHOW_JSON=0
SHOW_DOT=0
PYTHON_MODULE_PATH=tools
# Targets
all: $(BINS) $(LIBS)
@echo
@echo "Build succeeded!"
@echo
@echo "Run:"
@echo " make minimal_example"
@echo "for a short tutorial (read comments in the source code!)"
format_check:
# if you have clang-format >= 10.0.0, this will work:
@clang-format --dry-run --Werror $(LIB_HDR) $(LIB_SRC)
tests:
$(MAKE) cpp_tests
$(MAKE) python_tests
cpp_tests: $(BINS)
@echo "Starting C++ UNIT TESTS application"
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/tests/dummystats.json \
tests/unit_tests
jq . tests/dummystats.json >tests/dummystats.json.tmp && \
mv tests/dummystats.json.tmp tests/dummystats.json
python_tests:
$(MAKE) -C tools/malloc_tag python_tests
python_package:
python3 -m build
minimal_example: $(BINS)
@echo "Starting example application"
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/minimal/minimal_stats.json \
examples/minimal/minimal
# produce the .dot output file:
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_json2dot/json2dot.py \
-o examples/minimal/minimal_stats.dot \
examples/minimal/minimal_stats.json
# produce the .svg output file:
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_json2dot/json2dot.py \
-o examples/minimal/minimal_stats.dot.svg \
examples/minimal/minimal_stats.json
ifeq ($(SHOW_JSON),1)
@echo
@echo "JSON of output stats:"
@jq . $(PWD)/examples/minimal/minimal_stats.json
@echo
endif
ifeq ($(SHOW_DOT),1)
@echo
@echo "Graphviz DOT output stats (copy-paste that into https://dreampuf.github.io/):"
@cat $(PWD)/examples/minimal/minimal_stats.dot
@echo
endif
# stracing is Always a Good Thing to learn more about basic things like e.g. dynamic linker, mmap(), brk(), etc
minimal_strace: $(BINS)
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/minimal/minimal_stats.json \
strace -e trace=%memory,%file \
examples/minimal/minimal
minimal_debug: $(BINS)
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/minimal/minimal_stats.json \
gdb \
examples/minimal/minimal
minimal_valgrind: $(BINS)
# IMPORTANT: the minimal example is leaking memory on-purpose, so valgrind WILL detect some memory
# that has been "lost" at the end of the program
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/minimal/minimal_stats.json \
valgrind \
examples/minimal/minimal
#
# If you want to experiment decreasing the glibc VIRT memory usage in multithreaded apps,
# you can add
# GLIBC_TUNABLES=glibc.malloc.arena_max=1
# to the env vars used below:
#
multithread_example: $(BINS)
@echo "Starting example application"
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/multithread/multithread_stats.json \
examples/multithread/multithread
# produce the .dot output file
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_json2dot/json2dot.py \
-o examples/multithread/multithread_stats.dot \
examples/multithread/multithread_stats.json
# produce the .svg output file
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_json2dot/json2dot.py \
-o examples/multithread/multithread_stats.dot.svg \
examples/multithread/multithread_stats.json
# aggregate JSON snapshot and produce a .svg file for the aggregation result
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_postprocess/postprocess.py \
-o examples/multithread/multithread_stats.aggregated.json \
-c examples/multithread/postprocess_agg_rules.json \
examples/multithread/multithread_stats.json
PYTHONPATH=$(PYTHON_MODULE_PATH):$(PYTHONPATH) \
tools/malloc_tag/mtag_json2dot/json2dot.py \
-o examples/multithread/multithread_stats.aggregated.svg \
examples/multithread/multithread_stats.aggregated.json
ifeq ($(SHOW_JSON),1)
@echo
@echo "JSON of output stats:"
@jq . $(PWD)/examples/multithread/multithread_stats.json
@echo
endif
ifeq ($(SHOW_DOT),1)
@echo
@echo "Graphviz DOT output stats (copy-paste that into https://dreampuf.github.io/):"
@cat $(PWD)/examples/multithread/multithread_stats.dot
@echo
endif
multithread_strace: $(BINS)
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/multithread/multithread_stats.json \
strace -e trace=%memory,%file -t -f \
examples/multithread/multithread
multithread_debug: $(BINS)
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/multithread/multithread_stats.json \
gdb \
examples/multithread/multithread
multithread_valgrind: $(BINS)
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/multithread/multithread_stats.json \
valgrind \
examples/multithread/multithread
ifeq ($(USE_TCMALLOC),1)
tcmalloc_example: $(BINS)
@echo "Starting example application"
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/malloctag_and_tcmalloc/malloctag_and_tcmalloc_stats.json \
examples/malloctag_and_tcmalloc/malloctag_and_tcmalloc
tcmalloc_debug: $(BINS)
@echo "Starting example application"
LD_LIBRARY_PATH=$(PWD)/src:$(LD_LIBRARY_PATH) \
MTAG_STATS_OUTPUT_JSON=$(PWD)/examples/malloctag_and_tcmalloc/malloctag_and_tcmalloc_stats.json \
gdb \
examples/malloctag_and_tcmalloc/malloctag_and_tcmalloc
endif
# build and run all examples at once
examples: \
minimal_example \
multithread_example
benchmarks:
@echo TODO
clean:
find -name *.so* -exec rm {} \;
find -name *.o -exec rm {} \;
rm -f $(BINS)
install:
ifndef DESTDIR
@echo "*** ERROR: please call this makefile supplying explicitly the DESTDIR variable. E.g. use DESTDIR=/usr or DESTDIR=/usr/local"
@exit 1
endif
@echo "Installing malloc-tag header into $(DESTDIR)/include"
@mkdir --parents $(DESTDIR)/include
@cp -fv include/malloc_tag.h $(DESTDIR)/include/
@echo "Installing malloc-tag library into $(DESTDIR)/lib"
@mkdir --parents $(DESTDIR)/lib
@cp -fv src/libmalloc_tag.so* $(DESTDIR)/lib/
.PHONY: all minimal_example multithread_example examples tests clean install
# Rules
%.o: %.cpp $(LIB_HDR)
$(CC) $(CXXFLAGS_DBG) $(DEBUGFLAGS) -c -o $@ $<
# rule to COMPILE the malloc_tag code
src/%: src/%.o
$(CC) -o $@ $^ -pthread
# rule to LINK the malloc_tag library
src/libmalloc_tag.so: $(LIB_HDR) $(LIB_OBJ)
$(CC) $(LINKER_FLAGS) \
$(LIB_OBJ) \
-shared -Wl,-soname,libmalloc_tag.so.$(LIB_VER) \
-ldl \
-o $@
cd src && ln -sf libmalloc_tag.so libmalloc_tag.so.$(LIB_VER)
# rule to COMPILE test code
tests/%: tests/%.o
$(CC) -o $@ $^ -pthread
# rule to LINK test code
TESTS_OBJECTS:=$(patsubst %.cpp,%.o,$(wildcard tests/*.cpp))
tests/unit_tests: $(TESTS_OBJECTS) $(LIBS)
$(CC) -o tests/unit_tests \
$(TESTS_OBJECTS) \
-ldl -Lsrc -lmalloc_tag -lgtest -pthread
define example_build_targets
# rule to COMPILE example code
examples/$(1)/%: examples/$(1)/%.o
$(CC) -o $@ $^ -pthread
# rule to LINK example code
examples/$(1)/$(1): examples/$(1)/$(1).o $(LIBS)
$(CC) -o examples/$(1)/$(1) \
examples/$(1)/$(1).o \
-ldl -Lsrc -lmalloc_tag -pthread $(2)
endef
# create rules to build each available example:
$(eval $(call example_build_targets,minimal))
$(eval $(call example_build_targets,multithread))
# malloctag_and_tcmalloc example needs an extra library (-ltcmalloc)
$(eval $(call example_build_targets,malloctag_and_tcmalloc,-ltcmalloc))