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