1 # Copyright (C) 2013-2022 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.
88 ifneq (,$(filter yes,$(float96-fcts)))
94 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
115 bench := $(bench-math) $(bench-pthread) $(bench-string)
117 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
120 # String function benchmarks.
173 # Build and run locale-dependent benchmarks only if we're building natively.
174 ifeq (no,$(cross-compiling))
200 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
202 ifeq (no,$(cross-compiling))
203 # We have to generate locales
230 include ../gen-locales.mk
233 stdlib-benchset := strtod
235 stdio-common-benchset := sprintf
237 math-benchset := math-inlines
240 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
243 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
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
262 bench-malloc := malloc-thread malloc-simple
264 bench-malloc := $(filter malloc-%,${BENCHSET})
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)
273 link-libc-benchtests = $(link-libc)
274 +link-benchtests = $(+link-tests)
275 thread-library-benchtests = $(shared-thread-library)
276 libm-benchtests = $(libm)
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)
287 # Rules to build and execute the benchmarks. Do not put any benchmark
288 # parameters beyond this point.
290 # We don't want the benchmark programs to run in parallel since that could
291 # affect their performance.
294 bench-extra-objs = json-lib.o
296 extra-objs += $(bench-extra-objs)
297 others-extras = $(bench-extra-objs)
299 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
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)))
308 # The default duration: 1 seconds.
309 ifndef BENCH_DURATION
313 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
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
320 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
321 # of functions. All x86 processors since 2010 support RDTSCP instruction.
323 CPPFLAGS-nonlib += -DUSE_RDTSCP
333 bench-deps := bench-skeleton.c bench-timing.h Makefile
335 run-bench = $(test-wrapper-env) \
337 $($*-ENV) $(test-via-rtld-prefix) $${run}
339 timing-type := $(objpfx)bench-timing-type
340 extra-objs += bench-timing-type.o
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)
349 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
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))
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)
371 bench: bench-build bench-set bench-func bench-malloc
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)
379 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
380 $(binaries-bench-malloc)
383 bench-set: $(binaries-benchset)
385 echo "Running $${run}"; \
386 $(run-bench) > $${run}.out; \
389 bench-malloc: $(binaries-bench-malloc)
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; \
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; \
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) \
413 $(test-via-rtld-prefix) \
415 echo "{\"timing_type\": \"$${timing_type}\","; \
416 echo " \"functions\": {"; \
418 if ! [ "x$${run}" = "x$<" ]; then \
421 echo "Running $${run}" >&2; \
422 $(run-bench) $(DETAILED_OPT); \
427 } > $(objpfx)bench.out-tmp; \
428 if [ -f $(objpfx)bench.out ]; then \
429 mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
431 mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
432 $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
433 scripts/benchout.schema.json; \
436 ifeq ($(bind-now),yes)
437 link-bench-bind-now = -Wl,-z,now
440 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
441 $(binaries-bench-malloc)
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)
449 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
451 $(objpfx)bench-%.c: %-inputs $(bench-deps)
452 { if [ -n "$($*-INCLUDE)" ]; then \
455 $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp