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
264 stdio-common-benchset
:= sprintf
266 math-benchset
:= math-inlines
272 $(stdio-common-benchset
) \
274 $(string-benchset-all
) \
277 benchset
:= $(foreach B
,$(filter %-benchset
,${BENCHSET}), ${${B}})
280 CFLAGS-bench-ffs.c
+= -fno-builtin
281 CFLAGS-bench-ffsll.c
+= -fno-builtin
282 CFLAGS-bench-sqrt.c
+= -fno-builtin
283 CFLAGS-bench-fmin.c
+= -fno-builtin
284 CFLAGS-bench-fminf.c
+= -fno-builtin
285 CFLAGS-bench-fmax.c
+= -fno-builtin
286 CFLAGS-bench-fmaxf.c
+= -fno-builtin
287 CFLAGS-bench-trunc.c
+= -fno-builtin
288 CFLAGS-bench-truncf.c
+= -fno-builtin
289 CFLAGS-bench-roundeven.c
+= -fno-builtin
290 CFLAGS-bench-roundevenf.c
+= -fno-builtin
291 CFLAGS-bench-isnan.c
+= $(config-cflags-signaling-nans
)
292 CFLAGS-bench-isinf.c
+= $(config-cflags-signaling-nans
)
293 CFLAGS-bench-isfinite.c
+= $(config-cflags-signaling-nans
)
301 bench-malloc
:= $(filter malloc-
%,${BENCHSET})
304 ifeq (${STATIC-BENCHTESTS
},yes
)
305 +link-benchtests
= $(+link-static-tests
)
306 link-libc-benchtests
= $(link-libc-static
)
307 libm-benchtests
= $(common-objpfx
)math
/libm.a
308 thread-library-benchtests
= $(static-thread-library
)
310 link-libc-benchtests
= $(link-libc
)
311 +link-benchtests
= $(+link-tests
)
312 thread-library-benchtests
= $(shared-thread-library
)
313 libm-benchtests
= $(libm
)
316 $(addprefix $(objpfx
)bench-
,$(bench-math
)): $(libm-benchtests
)
317 $(addprefix $(objpfx
)bench-
,$(math-benchset
)): $(libm-benchtests
)
318 $(addprefix $(objpfx
)bench-
,$(bench-pthread
)): $(thread-library-benchtests
)
319 $(addprefix $(objpfx
)bench-
,$(bench-malloc
)): $(thread-library-benchtests
)
320 $(addprefix $(objpfx
)bench-
,pthread-locks
): $(libm-benchtests
)
321 $(addprefix $(objpfx
)bench-
,pthread-mutex-locks
): $(libm-benchtests
)
325 # Rules to build and execute the benchmarks. Do not put any benchmark
326 # parameters beyond this point.
328 # We don't want the benchmark programs to run in parallel since that could
329 # affect their performance.
332 bench-extra-objs
= json-lib.o
334 extra-objs
+= $(bench-extra-objs
)
335 others-extras
= $(bench-extra-objs
)
337 # The default duration: 1 seconds.
338 ifndef BENCH_DURATION
342 CPPFLAGS-nonlib
+= -DDURATION
=$(BENCH_DURATION
) -D_ISOMAC
344 # Use clock_gettime to measure performance of functions. The default is
345 # to use the architecture-specific high precision timing instructions.
346 ifdef USE_CLOCK_GETTIME
347 CPPFLAGS-nonlib
+= -DUSE_CLOCK_GETTIME
349 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
350 # of functions. All x86 processors since 2010 support RDTSCP instruction.
352 CPPFLAGS-nonlib
+= -DUSE_RDTSCP
362 bench-deps
:= bench-skeleton.c bench-timing.h Makefile
364 run-bench
= $(test-wrapper-env
) \
366 $($*-ENV
) $(test-via-rtld-prefix
) $${run}
368 timing-type
:= $(objpfx
)bench-timing-type
369 extra-objs
+= bench-timing-type.o
373 bench-math
+= $(bench-libmvec
)
382 bench
:= $(foreach B
,$(filter bench-
%,${BENCHSET}), ${${B}})
385 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
387 binaries-bench
= $(addprefix $(objpfx
)bench-
,$(bench
))
388 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench
)))
389 binaries-benchset
= $(addprefix $(objpfx
)bench-
,$(benchset
))
390 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(benchset
)))
391 binaries-bench-malloc
:= $(addprefix $(objpfx
)bench-
,$(bench-malloc
))
392 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench-malloc
)))
394 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
395 # for all these modules.
397 $(binaries-bench-malloc
:=.c
) \
398 $(binaries-bench
:=.c
) \
399 $(binaries-benchset
:=.c
) \
403 include $(patsubst %,$(..
)libof-iterator.mk
,$(cpp-srcs-left
))
406 rm -f
$(binaries-bench
) $(addsuffix .o
,$(binaries-bench
))
407 rm -f
$(binaries-benchset
) $(addsuffix .o
,$(binaries-benchset
))
408 rm -f
$(binaries-bench-malloc
) $(addsuffix .o
,$(binaries-bench-malloc
))
409 rm -f
$(timing-type
) $(addsuffix .o
,$(timing-type
))
410 rm -f
$(addprefix $(objpfx
),$(bench-extra-objs
))
412 # Validate the passed in BENCHSET
413 ifneq ($(strip ${BENCHSET}),)
414 VALIDBENCHSETNAMES
:= \
422 stdio-common-benchset \
428 INVALIDBENCHSETNAMES
:= $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
429 ifneq (${INVALIDBENCHSETNAMES},)
430 $(info The following values in BENCHSET are invalid
: ${INVALIDBENCHSETNAMES})
431 $(info The valid ones are
: ${VALIDBENCHSETNAMES})
432 $(error Invalid BENCHSET value
)
436 bench
: bench-build bench-set bench-func bench-malloc
438 # Target to only build the benchmark without running it. We generate locales
439 # only if we're building natively.
440 ifeq (no
,$(cross-compiling
))
441 bench-build
: $(gen-locales
) $(timing-type
) $(binaries-bench
) \
442 $(binaries-benchset
) $(binaries-bench-malloc
)
444 bench-build
: $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
445 $(binaries-bench-malloc
)
448 bench-set
: $(binaries-benchset
)
450 echo
"Running $${run}"; \
451 $(run-bench
) > $${run}.out
; \
454 bench-malloc
: $(binaries-bench-malloc
)
457 if
[ `basename $${run}` = "bench-malloc-thread" ]; then \
458 for thr in
1 8 16 32; do \
459 echo
"Running $${run} $${thr}"; \
460 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
463 for thr in
8 16 32 64 128 256 512 1024 2048 4096; do \
464 echo
"Running $${run} $${thr}"; \
465 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
470 # Build and execute the benchmark functions. This target generates JSON
471 # formatted bench.out. Each of the programs produce independent JSON output,
472 # so one could even execute them individually and process it using any JSON
473 # capable language or tool.
474 bench-func
: $(binaries-bench
)
475 if
[ -n
'$^' ] ; then \
476 { timing_type
=$$($(test-wrapper-env
) \
478 $(test-via-rtld-prefix
) \
480 echo
"{\"timing_type\": \"$${timing_type}\","; \
481 echo
" \"functions\": {"; \
483 op
=$$($(run-bench
) $(DETAILED_OPT
)); \
487 echo
"UNSUPPORTED $${run}: $${op}" >&2; \
490 echo
"Running $${run}" >&2; \
491 if
[ "$${run}" != "$<" ]; then \
497 echo
"FAILED $${run}" >&2; \
504 } > $(objpfx
)bench.out-tmp
; \
505 if
[ -f
$(objpfx
)bench.out
]; then \
506 mv
-f
$(objpfx
)bench.out
$(objpfx
)bench.out.old
; \
508 mv
-f
$(objpfx
)bench.out-tmp
$(objpfx
)bench.out
; \
509 $(PYTHON
) scripts
/validate_benchout.py
$(objpfx
)bench.out \
510 scripts
/benchout.schema.json
; \
513 ifeq ($(bind-now
),yes
)
514 link-bench-bind-now
= -Wl
,-z
,now
517 bench-link-targets
= $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
518 $(binaries-bench-malloc
)
520 $(bench-link-targets
): %: %.o
$(objpfx
)json-lib.o \
521 $(link-extra-libs-tests
) \
522 $(sort $(filter $(common-objpfx
)lib
%,$(link-libc-benchtests
))) \
523 $(addprefix $(csu-objpfx
),start.o
) $(+preinit
) $(+postinit
)
526 $(bench-link-targets
): LDFLAGS
+= $(link-bench-bind-now
)
528 $(objpfx
)bench-
%.c
: %-inputs
$(bench-deps
)
529 { if
[ -n
"$($*-INCLUDE)" ]; then \
532 $(PYTHON
) scripts
/bench.py
$(patsubst %-inputs
,%,$<); } > $@
-tmp