benchtests: Building benchmarks as static executables
[platform/upstream/glibc.git] / benchtests / Makefile
1 # Copyright (C) 2013-2021 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
3
4 # The GNU C Library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8
9 # The GNU C Library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with the GNU C Library; if not, see
16 # <https://www.gnu.org/licenses/>.
17
18 # Makefile for benchmark tests.  The only useful target here is `bench`.
19 # Add benchmark functions in alphabetical order.
20
21 subdir := benchtests
22
23 include ../Makeconfig
24 bench-math := acos acosh asin asinh atan atanh cos cosh exp exp2 log log2   \
25               modf pow rint sin sincos sinh sqrt tan tanh fmin fmax fminf   \
26               fmaxf powf trunc truncf roundeven roundevenf expf exp2f logf  \
27               log2f sincosf sinf cosf isnan isinf isfinite hypot logb logbf \
28               exp10f ilogb ilogbf cbrt erf erfc exp10 expm1 j0 j1 lgamma    \
29               log10 log1p tgamma y0 y1 atan2
30
31 ifneq (,$(filter yes,$(float96-fcts)))
32 bench-math += cbrtl
33 endif
34
35 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
36 bench-math += expf128 powf128 sinf128 ilogbf128
37 endif
38
39 bench-pthread := pthread_once thread_create pthread-locks
40
41 bench-string := ffs ffsll
42
43 ifeq (${BENCHSET},)
44 bench := $(bench-math) $(bench-pthread) $(bench-string)
45 else
46 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
47 endif
48
49 # String function benchmarks.
50 string-benchset := memccpy memchr memcmp memcpy memmem memmove \
51                    mempcpy memset rawmemchr stpcpy stpncpy strcasecmp strcasestr \
52                    strcat strchr strchrnul strcmp strcpy strcspn strlen \
53                    strncasecmp strncat strncmp strncpy strnlen strpbrk strrchr \
54                    strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok \
55                    strcoll memcpy-large memcpy-random memmove-large memset-large \
56                    memcpy-walk memset-walk memmove-walk
57
58 # Build and run locale-dependent benchmarks only if we're building natively.
59 ifeq (no,$(cross-compiling))
60 wcsmbs-benchset := wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat \
61                    wcscmp wcsncmp wcschr wcschrnul wcsrchr wcsspn wcspbrk wcscspn \
62                    wmemchr wmemset wmemcmp
63 else
64 wcsmbs-benchset :=
65 endif
66
67 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
68
69 ifeq (no,$(cross-compiling))
70 # We have to generate locales
71 LOCALES := en_US.UTF-8 tr_TR.UTF-8 cs_CZ.UTF-8 fa_IR.UTF-8 fr_FR.UTF-8 \
72            ja_JP.UTF-8 si_LK.UTF-8 en_GB.UTF-8 vi_VN.UTF-8 ar_SA.UTF-8 \
73            da_DK.UTF-8 pl_PL.UTF-8 pt_PT.UTF-8 el_GR.UTF-8 ru_RU.UTF-8 \
74            he_IL.UTF-8 is_IS.UTF-8 es_ES.UTF-8 hi_IN.UTF-8 sv_SE.UTF-8 \
75            hu_HU.UTF-8 it_IT.UTF-8 sr_RS.UTF-8 zh_CN.UTF-8
76 include ../gen-locales.mk
77 endif
78
79 stdlib-benchset := strtod
80
81 stdio-common-benchset := sprintf
82
83 math-benchset := math-inlines
84
85 ifeq (${BENCHSET},)
86 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
87             $(math-benchset)
88 else
89 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
90 endif
91
92 CFLAGS-bench-ffs.c += -fno-builtin
93 CFLAGS-bench-ffsll.c += -fno-builtin
94 CFLAGS-bench-sqrt.c += -fno-builtin
95 CFLAGS-bench-fmin.c += -fno-builtin
96 CFLAGS-bench-fminf.c += -fno-builtin
97 CFLAGS-bench-fmax.c += -fno-builtin
98 CFLAGS-bench-fmaxf.c += -fno-builtin
99 CFLAGS-bench-trunc.c += -fno-builtin
100 CFLAGS-bench-truncf.c += -fno-builtin
101 CFLAGS-bench-roundeven.c += -fno-builtin
102 CFLAGS-bench-roundevenf.c += -fno-builtin
103 CFLAGS-bench-isnan.c += -fsignaling-nans
104 CFLAGS-bench-isinf.c += -fsignaling-nans
105 CFLAGS-bench-isfinite.c += -fsignaling-nans
106
107 ifeq (${BENCHSET},)
108 bench-malloc := malloc-thread malloc-simple
109 else
110 bench-malloc := $(filter malloc-%,${BENCHSET})
111 endif
112
113 ifeq (${STATIC-BENCHTESTS},yes)
114 +link-benchtests = $(+link-static-tests)
115 link-libc-benchtests = $(link-libc-static)
116 libm-benchtests = $(common-objpfx)math/libm.a
117 thread-library-benchtests = $(static-thread-library)
118 else
119 link-libc-benchtests = $(link-libc)
120 +link-benchtests = $(+link-tests)
121 thread-library-benchtests = $(shared-thread-library)
122 libm-benchtests = $(libm)
123 endif
124
125 $(addprefix $(objpfx)bench-,$(bench-math)): $(libm-benchtests)
126 $(addprefix $(objpfx)bench-,$(math-benchset)): $(libm-benchtests)
127 $(addprefix $(objpfx)bench-,$(bench-pthread)): $(thread-library-benchtests)
128 $(addprefix $(objpfx)bench-,$(bench-malloc)): $(thread-library-benchtests)
129 $(addprefix $(objpfx)bench-,pthread-locks): $(libm-benchtests)
130
131 \f
132
133 # Rules to build and execute the benchmarks.  Do not put any benchmark
134 # parameters beyond this point.
135
136 # We don't want the benchmark programs to run in parallel since that could
137 # affect their performance.
138 .NOTPARALLEL:
139
140 bench-extra-objs = json-lib.o
141
142 extra-objs += $(bench-extra-objs)
143 others-extras = $(bench-extra-objs)
144
145 include ../Rules
146
147 binaries-bench := $(addprefix $(objpfx)bench-,$(bench))
148 binaries-benchset := $(addprefix $(objpfx)bench-,$(benchset))
149 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
150
151 # The default duration: 1 seconds.
152 ifndef BENCH_DURATION
153 BENCH_DURATION := 1
154 endif
155
156 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
157
158 # Use clock_gettime to measure performance of functions.  The default is
159 # to use the architecture-specific high precision timing instructions.
160 ifdef USE_CLOCK_GETTIME
161 CPPFLAGS-nonlib += -DUSE_CLOCK_GETTIME
162 else
163 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
164 # of functions.  All x86 processors since 2010 support RDTSCP instruction.
165 ifdef USE_RDTSCP
166 CPPFLAGS-nonlib += -DUSE_RDTSCP
167 endif
168 endif
169
170 DETAILED_OPT :=
171
172 ifdef DETAILED
173 DETAILED_OPT := -d
174 endif
175
176 bench-deps := bench-skeleton.c bench-timing.h Makefile
177
178 run-bench = $(test-wrapper-env) \
179             $(run-program-env) \
180             $($*-ENV) $(test-via-rtld-prefix) $${run}
181
182 timing-type := $(objpfx)bench-timing-type
183
184 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
185 # for all these modules.
186 cpp-srcs-left := $(binaries-benchset:=.c) $(binaries-bench:=.c) \
187                  $(binaries-bench-malloc:=.c) $(timing-type:=.c)
188 lib := nonlib
189 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
190
191 bench-clean:
192         rm -f $(binaries-bench) $(addsuffix .o,$(binaries-bench))
193         rm -f $(binaries-benchset) $(addsuffix .o,$(binaries-benchset))
194         rm -f $(binaries-bench-malloc) $(addsuffix .o,$(binaries-bench-malloc))
195         rm -f $(timing-type) $(addsuffix .o,$(timing-type))
196         rm -f $(addprefix $(objpfx),$(bench-extra-objs))
197
198 # Validate the passed in BENCHSET
199 ifneq ($(strip ${BENCHSET}),)
200 VALIDBENCHSETNAMES := bench-pthread bench-math bench-string string-benchset \
201    wcsmbs-benchset stdlib-benchset stdio-common-benchset math-benchset \
202    malloc-thread malloc-simple
203 INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
204 ifneq (${INVALIDBENCHSETNAMES},)
205 $(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
206 $(info The valid ones are: ${VALIDBENCHSETNAMES})
207 $(error Invalid BENCHSET value)
208 endif
209 endif
210
211 bench: bench-build bench-set bench-func bench-malloc
212
213 # Target to only build the benchmark without running it.  We generate locales
214 # only if we're building natively.
215 ifeq (no,$(cross-compiling))
216 bench-build: $(gen-locales) $(timing-type) $(binaries-bench) \
217         $(binaries-benchset) $(binaries-bench-malloc)
218 else
219 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
220         $(binaries-bench-malloc)
221 endif
222
223 bench-set: $(binaries-benchset)
224         for run in $^; do \
225           echo "Running $${run}"; \
226           $(run-bench) > $${run}.out; \
227         done
228
229 bench-malloc: $(binaries-bench-malloc)
230         for run in $^; do \
231           echo "$${run}"; \
232           if [ `basename $${run}` = "bench-malloc-thread" ]; then \
233                 for thr in 1 8 16 32; do \
234                         echo "Running $${run} $${thr}"; \
235                         $(run-bench) $${thr} > $${run}-$${thr}.out; \
236                 done;\
237           else \
238                 for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
239                   echo "Running $${run} $${thr}"; \
240                   $(run-bench) $${thr} > $${run}-$${thr}.out; \
241                 done;\
242           fi;\
243         done
244
245 # Build and execute the benchmark functions.  This target generates JSON
246 # formatted bench.out.  Each of the programs produce independent JSON output,
247 # so one could even execute them individually and process it using any JSON
248 # capable language or tool.
249 bench-func: $(binaries-bench)
250         if [ -n '$^' ] ; then \
251         { timing_type=$$($(test-wrapper-env) \
252                          $(run-program-env) \
253                          $(test-via-rtld-prefix) \
254                          $(timing-type)); \
255           echo "{\"timing_type\": \"$${timing_type}\","; \
256           echo " \"functions\": {"; \
257           for run in $^; do \
258             if ! [ "x$${run}" = "x$<" ]; then \
259               echo ","; \
260             fi; \
261             echo "Running $${run}" >&2; \
262             $(run-bench) $(DETAILED_OPT); \
263           done; \
264           echo; \
265           echo " }"; \
266           echo "}"; \
267           } > $(objpfx)bench.out-tmp; \
268           if [ -f $(objpfx)bench.out ]; then \
269             mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
270           fi; \
271           mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
272           $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
273           scripts/benchout.schema.json; \
274         fi
275
276 ifeq ($(bind-now),yes)
277 link-bench-bind-now = -Wl,-z,now
278 endif
279
280 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
281         $(binaries-bench-malloc)
282
283 $(bench-link-targets): %: %.o $(objpfx)json-lib.o \
284         $(link-extra-libs-tests) \
285   $(sort $(filter $(common-objpfx)lib%,$(link-libc-benchtests))) \
286   $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
287         $(+link-benchtests)
288
289 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
290
291 $(objpfx)bench-%.c: %-inputs $(bench-deps)
292         { if [ -n "$($*-INCLUDE)" ]; then \
293           cat $($*-INCLUDE); \
294         fi; \
295         $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp
296         mv -f $@-tmp $@