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