1 # Copyright (C) 2013-2021 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
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.
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.
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/>.
18 # Makefile for benchmark tests. The only useful target here is `bench`.
19 # Add benchmark functions in alphabetical order.
87 ifneq (,$(filter yes,$(float96-fcts)))
91 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
92 bench-math += expf128 powf128 sinf128 ilogbf128
95 bench-pthread := pthread_once thread_create pthread-locks
97 bench-string := ffs ffsll
100 bench := $(bench-math) $(bench-pthread) $(bench-string)
102 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
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
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
123 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
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
135 stdlib-benchset := strtod
137 stdio-common-benchset := sprintf
139 math-benchset := math-inlines
142 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
145 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
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
164 bench-malloc := malloc-thread malloc-simple
166 bench-malloc := $(filter malloc-%,${BENCHSET})
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)
175 link-libc-benchtests = $(link-libc)
176 +link-benchtests = $(+link-tests)
177 thread-library-benchtests = $(shared-thread-library)
178 libm-benchtests = $(libm)
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)
189 # Rules to build and execute the benchmarks. Do not put any benchmark
190 # parameters beyond this point.
192 # We don't want the benchmark programs to run in parallel since that could
193 # affect their performance.
196 bench-extra-objs = json-lib.o
198 extra-objs += $(bench-extra-objs)
199 others-extras = $(bench-extra-objs)
203 binaries-bench := $(addprefix $(objpfx)bench-,$(bench))
204 binaries-benchset := $(addprefix $(objpfx)bench-,$(benchset))
205 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
207 # The default duration: 1 seconds.
208 ifndef BENCH_DURATION
212 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
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
219 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
220 # of functions. All x86 processors since 2010 support RDTSCP instruction.
222 CPPFLAGS-nonlib += -DUSE_RDTSCP
232 bench-deps := bench-skeleton.c bench-timing.h Makefile
234 run-bench = $(test-wrapper-env) \
236 $($*-ENV) $(test-via-rtld-prefix) $${run}
238 timing-type := $(objpfx)bench-timing-type
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)
245 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
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))
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)
267 bench: bench-build bench-set bench-func bench-malloc
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)
275 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
276 $(binaries-bench-malloc)
279 bench-set: $(binaries-benchset)
281 echo "Running $${run}"; \
282 $(run-bench) > $${run}.out; \
285 bench-malloc: $(binaries-bench-malloc)
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; \
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; \
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) \
309 $(test-via-rtld-prefix) \
311 echo "{\"timing_type\": \"$${timing_type}\","; \
312 echo " \"functions\": {"; \
314 if ! [ "x$${run}" = "x$<" ]; then \
317 echo "Running $${run}" >&2; \
318 $(run-bench) $(DETAILED_OPT); \
323 } > $(objpfx)bench.out-tmp; \
324 if [ -f $(objpfx)bench.out ]; then \
325 mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
327 mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
328 $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
329 scripts/benchout.schema.json; \
332 ifeq ($(bind-now),yes)
333 link-bench-bind-now = -Wl,-z,now
336 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
337 $(binaries-bench-malloc)
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)
345 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
347 $(objpfx)bench-%.c: %-inputs $(bench-deps)
348 { if [ -n "$($*-INCLUDE)" ]; then \
351 $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp