1 # Copyright (C) 2013-2024 Free Software Foundation, Inc.
2 # Copyright The GNU Toolchain Authors.
3 # This file is part of the GNU C Library.
5 # The GNU C Library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <https://www.gnu.org/licenses/>.
19 # Makefile for benchmark tests. The only useful target here is `bench`.
20 # Add benchmark functions in alphabetical order.
103 ifneq (,$(filter yes
,$(float96-fcts
)))
109 ifneq (,$(filter yes
,$(float128-fcts
) $(float128-alias-fcts
)))
121 pthread-mutex-trylock \
123 pthread-spin-trylock \
128 LDLIBS-bench-pthread-mutex-lock
+= -lm
129 LDLIBS-bench-pthread-mutex-trylock
+= -lm
130 LDLIBS-bench-pthread-spin-lock
+= -lm
131 LDLIBS-bench-pthread-spin-trylock
+= -lm
138 # String function benchmarks.
191 # Build and run locale-dependent benchmarks only if we're building natively.
192 ifeq (no
,$(cross-compiling
))
219 string-benchset-all
:= $(string-benchset
) ${wcsmbs-benchset
}
221 ifeq (no
,$(cross-compiling
))
222 # We have to generate locales
249 include ..
/gen-locales.mk
263 stdio-common-benchset
:= sprintf
265 math-benchset
:= math-inlines
271 $(stdio-common-benchset
) \
273 $(string-benchset-all
) \
276 benchset
:= $(foreach B
,$(filter %-benchset
,${BENCHSET}), ${${B}})
279 CFLAGS-bench-ffs.c
+= -fno-builtin
280 CFLAGS-bench-ffsll.c
+= -fno-builtin
281 CFLAGS-bench-sqrt.c
+= -fno-builtin
282 CFLAGS-bench-fmin.c
+= -fno-builtin
283 CFLAGS-bench-fminf.c
+= -fno-builtin
284 CFLAGS-bench-fmax.c
+= -fno-builtin
285 CFLAGS-bench-fmaxf.c
+= -fno-builtin
286 CFLAGS-bench-trunc.c
+= -fno-builtin
287 CFLAGS-bench-truncf.c
+= -fno-builtin
288 CFLAGS-bench-roundeven.c
+= -fno-builtin
289 CFLAGS-bench-roundevenf.c
+= -fno-builtin
290 CFLAGS-bench-isnan.c
+= $(config-cflags-signaling-nans
)
291 CFLAGS-bench-isinf.c
+= $(config-cflags-signaling-nans
)
292 CFLAGS-bench-isfinite.c
+= $(config-cflags-signaling-nans
)
300 bench-malloc
:= $(filter malloc-
%,${BENCHSET})
303 ifeq (${STATIC-BENCHTESTS
},yes
)
304 +link-benchtests
= $(+link-static-tests
)
305 link-libc-benchtests
= $(link-libc-static
)
306 libm-benchtests
= $(common-objpfx
)math
/libm.a
307 thread-library-benchtests
= $(static-thread-library
)
309 link-libc-benchtests
= $(link-libc
)
310 +link-benchtests
= $(+link-tests
)
311 thread-library-benchtests
= $(shared-thread-library
)
312 libm-benchtests
= $(libm
)
315 $(addprefix $(objpfx
)bench-
,$(bench-math
)): $(libm-benchtests
)
316 $(addprefix $(objpfx
)bench-
,$(math-benchset
)): $(libm-benchtests
)
317 $(addprefix $(objpfx
)bench-
,$(bench-pthread
)): $(thread-library-benchtests
)
318 $(addprefix $(objpfx
)bench-
,$(bench-malloc
)): $(thread-library-benchtests
)
319 $(addprefix $(objpfx
)bench-
,pthread-locks
): $(libm-benchtests
)
320 $(addprefix $(objpfx
)bench-
,pthread-mutex-locks
): $(libm-benchtests
)
324 # Rules to build and execute the benchmarks. Do not put any benchmark
325 # parameters beyond this point.
327 # We don't want the benchmark programs to run in parallel since that could
328 # affect their performance.
331 bench-extra-objs
= json-lib.o
333 extra-objs
+= $(bench-extra-objs
)
334 others-extras
= $(bench-extra-objs
)
336 # The default duration: 1 seconds.
337 ifndef BENCH_DURATION
341 CPPFLAGS-nonlib
+= -DDURATION
=$(BENCH_DURATION
) -D_ISOMAC
343 # Use clock_gettime to measure performance of functions. The default is
344 # to use the architecture-specific high precision timing instructions.
345 ifdef USE_CLOCK_GETTIME
346 CPPFLAGS-nonlib
+= -DUSE_CLOCK_GETTIME
348 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
349 # of functions. All x86 processors since 2010 support RDTSCP instruction.
351 CPPFLAGS-nonlib
+= -DUSE_RDTSCP
361 bench-deps
:= bench-skeleton.c bench-timing.h Makefile
363 run-bench
= $(test-wrapper-env
) \
365 $($*-ENV
) $(test-via-rtld-prefix
) $${run}
367 timing-type
:= $(objpfx
)bench-timing-type
368 extra-objs
+= bench-timing-type.o
372 bench-math
+= $(bench-libmvec
)
381 bench
:= $(foreach B
,$(filter bench-
%,${BENCHSET}), ${${B}})
384 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
386 binaries-bench
= $(addprefix $(objpfx
)bench-
,$(bench
))
387 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench
)))
388 binaries-benchset
= $(addprefix $(objpfx
)bench-
,$(benchset
))
389 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(benchset
)))
390 binaries-bench-malloc
:= $(addprefix $(objpfx
)bench-
,$(bench-malloc
))
391 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench-malloc
)))
393 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
394 # for all these modules.
396 $(binaries-bench-malloc
:=.c
) \
397 $(binaries-bench
:=.c
) \
398 $(binaries-benchset
:=.c
) \
402 include $(patsubst %,$(..
)libof-iterator.mk
,$(cpp-srcs-left
))
405 rm -f
$(binaries-bench
) $(addsuffix .o
,$(binaries-bench
))
406 rm -f
$(binaries-benchset
) $(addsuffix .o
,$(binaries-benchset
))
407 rm -f
$(binaries-bench-malloc
) $(addsuffix .o
,$(binaries-bench-malloc
))
408 rm -f
$(timing-type
) $(addsuffix .o
,$(timing-type
))
409 rm -f
$(addprefix $(objpfx
),$(bench-extra-objs
))
411 # Validate the passed in BENCHSET
412 ifneq ($(strip ${BENCHSET}),)
413 VALIDBENCHSETNAMES
:= \
421 stdio-common-benchset \
427 INVALIDBENCHSETNAMES
:= $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
428 ifneq (${INVALIDBENCHSETNAMES},)
429 $(info The following values in BENCHSET are invalid
: ${INVALIDBENCHSETNAMES})
430 $(info The valid ones are
: ${VALIDBENCHSETNAMES})
431 $(error Invalid BENCHSET value
)
435 bench
: bench-build bench-set bench-func bench-malloc
437 # Target to only build the benchmark without running it. We generate locales
438 # only if we're building natively.
439 ifeq (no
,$(cross-compiling
))
440 bench-build
: $(gen-locales
) $(timing-type
) $(binaries-bench
) \
441 $(binaries-benchset
) $(binaries-bench-malloc
)
443 bench-build
: $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
444 $(binaries-bench-malloc
)
447 bench-set
: $(binaries-benchset
)
449 echo
"Running $${run}"; \
450 $(run-bench
) > $${run}.out
; \
453 bench-malloc
: $(binaries-bench-malloc
)
456 if
[ `basename $${run}` = "bench-malloc-thread" ]; then \
457 for thr in
1 8 16 32; do \
458 echo
"Running $${run} $${thr}"; \
459 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
462 for thr in
8 16 32 64 128 256 512 1024 2048 4096; do \
463 echo
"Running $${run} $${thr}"; \
464 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
469 # Build and execute the benchmark functions. This target generates JSON
470 # formatted bench.out. Each of the programs produce independent JSON output,
471 # so one could even execute them individually and process it using any JSON
472 # capable language or tool.
473 bench-func
: $(binaries-bench
)
474 if
[ -n
'$^' ] ; then \
475 { timing_type
=$$($(test-wrapper-env
) \
477 $(test-via-rtld-prefix
) \
479 echo
"{\"timing_type\": \"$${timing_type}\","; \
480 echo
" \"functions\": {"; \
482 op
=$$($(run-bench
) $(DETAILED_OPT
)); \
486 echo
"UNSUPPORTED $${run}: $${op}" >&2; \
489 echo
"Running $${run}" >&2; \
490 if
[ "$${run}" != "$<" ]; then \
496 echo
"FAILED $${run}" >&2; \
503 } > $(objpfx
)bench.out-tmp
; \
504 if
[ -f
$(objpfx
)bench.out
]; then \
505 mv
-f
$(objpfx
)bench.out
$(objpfx
)bench.out.old
; \
507 mv
-f
$(objpfx
)bench.out-tmp
$(objpfx
)bench.out
; \
508 $(PYTHON
) scripts
/validate_benchout.py
$(objpfx
)bench.out \
509 scripts
/benchout.schema.json
; \
512 ifeq ($(bind-now
),yes
)
513 link-bench-bind-now
= -Wl
,-z
,now
516 bench-link-targets
= $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
517 $(binaries-bench-malloc
)
519 $(bench-link-targets
): %: %.o
$(objpfx
)json-lib.o \
520 $(link-extra-libs-tests
) \
521 $(sort $(filter $(common-objpfx
)lib
%,$(link-libc-benchtests
))) \
522 $(addprefix $(csu-objpfx
),start.o
) $(+preinit
) $(+postinit
)
525 $(bench-link-targets
): LDFLAGS
+= $(link-bench-bind-now
)
527 $(objpfx
)bench-
%.c
: %-inputs
$(bench-deps
)
528 { if
[ -n
"$($*-INCLUDE)" ]; then \
531 $(PYTHON
) scripts
/bench.py
$(patsubst %-inputs
,%,$<); } > $@
-tmp