1 # Copyright (C) 2013-2023 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.
89 ifneq (,$(filter yes
,$(float96-fcts
)))
95 ifneq (,$(filter yes
,$(float128-fcts
) $(float128-alias-fcts
)))
107 pthread-mutex-trylock \
109 pthread-spin-trylock \
114 LDLIBS-bench-pthread-mutex-lock
+= -lm
115 LDLIBS-bench-pthread-mutex-trylock
+= -lm
116 LDLIBS-bench-pthread-spin-lock
+= -lm
117 LDLIBS-bench-pthread-spin-trylock
+= -lm
124 # String function benchmarks.
177 # Build and run locale-dependent benchmarks only if we're building natively.
178 ifeq (no
,$(cross-compiling
))
205 string-benchset-all
:= $(string-benchset
) ${wcsmbs-benchset
}
207 ifeq (no
,$(cross-compiling
))
208 # We have to generate locales
235 include ..
/gen-locales.mk
249 stdio-common-benchset
:= sprintf
251 math-benchset
:= math-inlines
254 benchset
:= $(string-benchset-all
) $(stdlib-benchset
) $(stdio-common-benchset
) \
255 $(math-benchset
) $(hash-benchset
)
257 benchset
:= $(foreach B
,$(filter %-benchset
,${BENCHSET}), ${${B}})
260 CFLAGS-bench-ffs.c
+= -fno-builtin
261 CFLAGS-bench-ffsll.c
+= -fno-builtin
262 CFLAGS-bench-sqrt.c
+= -fno-builtin
263 CFLAGS-bench-fmin.c
+= -fno-builtin
264 CFLAGS-bench-fminf.c
+= -fno-builtin
265 CFLAGS-bench-fmax.c
+= -fno-builtin
266 CFLAGS-bench-fmaxf.c
+= -fno-builtin
267 CFLAGS-bench-trunc.c
+= -fno-builtin
268 CFLAGS-bench-truncf.c
+= -fno-builtin
269 CFLAGS-bench-roundeven.c
+= -fno-builtin
270 CFLAGS-bench-roundevenf.c
+= -fno-builtin
271 CFLAGS-bench-isnan.c
+= $(config-cflags-signaling-nans
)
272 CFLAGS-bench-isinf.c
+= $(config-cflags-signaling-nans
)
273 CFLAGS-bench-isfinite.c
+= $(config-cflags-signaling-nans
)
276 bench-malloc
:= malloc-thread malloc-simple
278 bench-malloc
:= $(filter malloc-
%,${BENCHSET})
281 ifeq (${STATIC-BENCHTESTS
},yes
)
282 +link-benchtests
= $(+link-static-tests
)
283 link-libc-benchtests
= $(link-libc-static
)
284 libm-benchtests
= $(common-objpfx
)math
/libm.a
285 thread-library-benchtests
= $(static-thread-library
)
287 link-libc-benchtests
= $(link-libc
)
288 +link-benchtests
= $(+link-tests
)
289 thread-library-benchtests
= $(shared-thread-library
)
290 libm-benchtests
= $(libm
)
293 $(addprefix $(objpfx
)bench-
,$(bench-math
)): $(libm-benchtests
)
294 $(addprefix $(objpfx
)bench-
,$(math-benchset
)): $(libm-benchtests
)
295 $(addprefix $(objpfx
)bench-
,$(bench-pthread
)): $(thread-library-benchtests
)
296 $(addprefix $(objpfx
)bench-
,$(bench-malloc
)): $(thread-library-benchtests
)
297 $(addprefix $(objpfx
)bench-
,pthread-locks
): $(libm-benchtests
)
298 $(addprefix $(objpfx
)bench-
,pthread-mutex-locks
): $(libm-benchtests
)
302 # Rules to build and execute the benchmarks. Do not put any benchmark
303 # parameters beyond this point.
305 # We don't want the benchmark programs to run in parallel since that could
306 # affect their performance.
309 bench-extra-objs
= json-lib.o
311 extra-objs
+= $(bench-extra-objs
)
312 others-extras
= $(bench-extra-objs
)
314 # The default duration: 1 seconds.
315 ifndef BENCH_DURATION
319 CPPFLAGS-nonlib
+= -DDURATION
=$(BENCH_DURATION
) -D_ISOMAC
321 # Use clock_gettime to measure performance of functions. The default is
322 # to use the architecture-specific high precision timing instructions.
323 ifdef USE_CLOCK_GETTIME
324 CPPFLAGS-nonlib
+= -DUSE_CLOCK_GETTIME
326 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
327 # of functions. All x86 processors since 2010 support RDTSCP instruction.
329 CPPFLAGS-nonlib
+= -DUSE_RDTSCP
339 bench-deps
:= bench-skeleton.c bench-timing.h Makefile
341 run-bench
= $(test-wrapper-env
) \
343 $($*-ENV
) $(test-via-rtld-prefix
) $${run}
345 timing-type
:= $(objpfx
)bench-timing-type
346 extra-objs
+= bench-timing-type.o
350 bench-math
+= $(bench-libmvec
)
353 bench
:= $(bench-math
) $(bench-pthread
) $(bench-string
)
355 bench
:= $(foreach B
,$(filter bench-
%,${BENCHSET}), ${${B}})
358 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
360 binaries-bench
= $(addprefix $(objpfx
)bench-
,$(bench
))
361 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench
)))
362 binaries-benchset
= $(addprefix $(objpfx
)bench-
,$(benchset
))
363 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(benchset
)))
364 binaries-bench-malloc
:= $(addprefix $(objpfx
)bench-
,$(bench-malloc
))
365 extra-objs
+= $(addsuffix .o
,$(addprefix bench-
,$(bench-malloc
)))
367 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
368 # for all these modules.
369 cpp-srcs-left
:= $(binaries-benchset
:=.c
) $(binaries-bench
:=.c
) \
370 $(binaries-bench-malloc
:=.c
) $(timing-type
:=.c
)
372 include $(patsubst %,$(..
)libof-iterator.mk
,$(cpp-srcs-left
))
375 rm -f
$(binaries-bench
) $(addsuffix .o
,$(binaries-bench
))
376 rm -f
$(binaries-benchset
) $(addsuffix .o
,$(binaries-benchset
))
377 rm -f
$(binaries-bench-malloc
) $(addsuffix .o
,$(binaries-bench-malloc
))
378 rm -f
$(timing-type
) $(addsuffix .o
,$(timing-type
))
379 rm -f
$(addprefix $(objpfx
),$(bench-extra-objs
))
381 # Validate the passed in BENCHSET
382 ifneq ($(strip ${BENCHSET}),)
383 VALIDBENCHSETNAMES
:= \
391 stdio-common-benchset \
397 INVALIDBENCHSETNAMES
:= $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
398 ifneq (${INVALIDBENCHSETNAMES},)
399 $(info The following values in BENCHSET are invalid
: ${INVALIDBENCHSETNAMES})
400 $(info The valid ones are
: ${VALIDBENCHSETNAMES})
401 $(error Invalid BENCHSET value
)
405 bench
: bench-build bench-set bench-func bench-malloc
407 # Target to only build the benchmark without running it. We generate locales
408 # only if we're building natively.
409 ifeq (no
,$(cross-compiling
))
410 bench-build
: $(gen-locales
) $(timing-type
) $(binaries-bench
) \
411 $(binaries-benchset
) $(binaries-bench-malloc
)
413 bench-build
: $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
414 $(binaries-bench-malloc
)
417 bench-set
: $(binaries-benchset
)
419 echo
"Running $${run}"; \
420 $(run-bench
) > $${run}.out
; \
423 bench-malloc
: $(binaries-bench-malloc
)
426 if
[ `basename $${run}` = "bench-malloc-thread" ]; then \
427 for thr in
1 8 16 32; do \
428 echo
"Running $${run} $${thr}"; \
429 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
432 for thr in
8 16 32 64 128 256 512 1024 2048 4096; do \
433 echo
"Running $${run} $${thr}"; \
434 $(run-bench
) $${thr} > $${run}-$${thr}.out
; \
439 # Build and execute the benchmark functions. This target generates JSON
440 # formatted bench.out. Each of the programs produce independent JSON output,
441 # so one could even execute them individually and process it using any JSON
442 # capable language or tool.
443 bench-func
: $(binaries-bench
)
444 if
[ -n
'$^' ] ; then \
445 { timing_type
=$$($(test-wrapper-env
) \
447 $(test-via-rtld-prefix
) \
449 echo
"{\"timing_type\": \"$${timing_type}\","; \
450 echo
" \"functions\": {"; \
452 op
=$$($(run-bench
) $(DETAILED_OPT
)); \
456 echo
"UNSUPPORTED $${run}: $${op}" >&2; \
459 echo
"Running $${run}" >&2; \
460 if
[ "$${run}" != "$<" ]; then \
466 echo
"FAILED $${run}" >&2; \
473 } > $(objpfx
)bench.out-tmp
; \
474 if
[ -f
$(objpfx
)bench.out
]; then \
475 mv
-f
$(objpfx
)bench.out
$(objpfx
)bench.out.old
; \
477 mv
-f
$(objpfx
)bench.out-tmp
$(objpfx
)bench.out
; \
478 $(PYTHON
) scripts
/validate_benchout.py
$(objpfx
)bench.out \
479 scripts
/benchout.schema.json
; \
482 ifeq ($(bind-now
),yes
)
483 link-bench-bind-now
= -Wl
,-z
,now
486 bench-link-targets
= $(timing-type
) $(binaries-bench
) $(binaries-benchset
) \
487 $(binaries-bench-malloc
)
489 $(bench-link-targets
): %: %.o
$(objpfx
)json-lib.o \
490 $(link-extra-libs-tests
) \
491 $(sort $(filter $(common-objpfx
)lib
%,$(link-libc-benchtests
))) \
492 $(addprefix $(csu-objpfx
),start.o
) $(+preinit
) $(+postinit
)
495 $(bench-link-targets
): LDFLAGS
+= $(link-bench-bind-now
)
497 $(objpfx
)bench-
%.c
: %-inputs
$(bench-deps
)
498 { if
[ -n
"$($*-INCLUDE)" ]; then \
501 $(PYTHON
) scripts
/bench.py
$(patsubst %-inputs
,%,$<); } > $@
-tmp