benchtests: Add pthread-mutex-locks bench
[platform/upstream/glibc.git] / benchtests / Makefile
1 # Copyright (C) 2013-2022 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
3
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.
8
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.
13
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/>.
17
18 # Makefile for benchmark tests.  The only useful target here is `bench`.
19 # Add benchmark functions in alphabetical order.
20
21 subdir := benchtests
22
23 include ../Makeconfig
24
25 bench-math := \
26   acos \
27   acosh \
28   asin \
29   asinh \
30   atan \
31   atan2 \
32   atanh \
33   cbrt \
34   cos \
35   cosf \
36   cosh \
37   erf \
38   erfc \
39   exp \
40   exp10 \
41   exp10f \
42   exp2 \
43   exp2f \
44   expf \
45   expm1 \
46   fmax \
47   fmaxf \
48   fmin \
49   fminf \
50   hypot \
51   hypotf \
52   ilogb \
53   ilogbf \
54   isfinite \
55   isinf \
56   isnan \
57   j0 \
58   j1 \
59   lgamma \
60   log \
61   log10 \
62   log1p \
63   log2 \
64   log2f \
65   logb \
66   logbf \
67   logf \
68   modf \
69   pow \
70   powf \
71   rint \
72   roundeven \
73   roundevenf \
74   sin \
75   sincos \
76   sincosf \
77   sinf \
78   sinh \
79   sqrt \
80   tan \
81   tanh \
82   tgamma \
83   trunc \
84   truncf \
85   y0 \
86   y1 \
87
88 ifneq (,$(filter yes,$(float96-fcts)))
89 bench-math += \
90   cbrtl \
91 # bench-math
92 endif
93
94 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
95 bench-math += \
96   expf128 \
97   ilogbf128 \
98   powf128 \
99   sinf128 \
100 # bench-math
101 endif
102
103 bench-pthread := \
104   pthread-locks \
105   pthread-mutex-locks \
106   pthread_once \
107   thread_create \
108 # bench-pthread
109
110 bench-string := \
111   ffs \
112   ffsll \
113 # bench-string
114
115 ifeq (${BENCHSET},)
116 bench := $(bench-math) $(bench-pthread) $(bench-string)
117 else
118 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
119 endif
120
121 # String function benchmarks.
122 string-benchset := \
123   bzero \
124   bzero-large \
125   bzero-walk \
126   memccpy \
127   memchr \
128   memcmp \
129   memcmpeq \
130   memcpy \
131   memcpy-large \
132   memcpy-random \
133   memcpy-walk \
134   memmem \
135   memmove \
136   memmove-large \
137   memmove-walk \
138   mempcpy \
139   memrchr \
140   memset \
141   memset-large \
142   memset-walk \
143   memset-zero \
144   memset-zero-large \
145   memset-zero-walk \
146   rawmemchr \
147   stpcpy \
148   stpcpy_chk \
149   stpncpy \
150   strcasecmp \
151   strcasestr \
152   strcat \
153   strchr \
154   strchrnul \
155   strcmp \
156   strcoll \
157   strcpy \
158   strcpy_chk \
159   strcspn \
160   strlen \
161   strncasecmp \
162   strncat \
163   strncmp \
164   strncpy \
165   strnlen \
166   strpbrk \
167   strrchr \
168   strsep \
169   strspn \
170   strstr \
171   strtok \
172 # string-benchset
173
174 # Build and run locale-dependent benchmarks only if we're building natively.
175 ifeq (no,$(cross-compiling))
176 wcsmbs-benchset := \
177   wcpcpy \
178   wcpncpy \
179   wcscat \
180   wcschr \
181   wcschrnul \
182   wcscmp \
183   wcscpy \
184   wcscspn \
185   wcslen \
186   wcsncat \
187   wcsncmp \
188   wcsncpy \
189   wcsnlen \
190   wcspbrk \
191   wcsrchr \
192   wcsspn \
193   wmemchr \
194   wmemcmp \
195   wmemset \
196 # wcsmbs-benchset
197 else
198 wcsmbs-benchset :=
199 endif
200
201 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
202
203 ifeq (no,$(cross-compiling))
204 # We have to generate locales
205 LOCALES := \
206   ar_SA.UTF-8 \
207   cs_CZ.UTF-8 \
208   da_DK.UTF-8 \
209   el_GR.UTF-8 \
210   en_GB.UTF-8 \
211   en_US.UTF-8 \
212   es_ES.UTF-8 \
213   fa_IR.UTF-8 \
214   fr_FR.UTF-8 \
215   he_IL.UTF-8 \
216   hi_IN.UTF-8 \
217   hu_HU.UTF-8 \
218   is_IS.UTF-8 \
219   it_IT.UTF-8 \
220   ja_JP.UTF-8 \
221   pl_PL.UTF-8 \
222   pt_PT.UTF-8 \
223   ru_RU.UTF-8 \
224   si_LK.UTF-8 \
225   sr_RS.UTF-8 \
226   sv_SE.UTF-8 \
227   tr_TR.UTF-8 \
228   vi_VN.UTF-8 \
229   zh_CN.UTF-8 \
230 # LOCALES
231 include ../gen-locales.mk
232 endif
233
234 stdlib-benchset := strtod
235
236 stdio-common-benchset := sprintf
237
238 math-benchset := math-inlines
239
240 ifeq (${BENCHSET},)
241 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
242             $(math-benchset)
243 else
244 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
245 endif
246
247 CFLAGS-bench-ffs.c += -fno-builtin
248 CFLAGS-bench-ffsll.c += -fno-builtin
249 CFLAGS-bench-sqrt.c += -fno-builtin
250 CFLAGS-bench-fmin.c += -fno-builtin
251 CFLAGS-bench-fminf.c += -fno-builtin
252 CFLAGS-bench-fmax.c += -fno-builtin
253 CFLAGS-bench-fmaxf.c += -fno-builtin
254 CFLAGS-bench-trunc.c += -fno-builtin
255 CFLAGS-bench-truncf.c += -fno-builtin
256 CFLAGS-bench-roundeven.c += -fno-builtin
257 CFLAGS-bench-roundevenf.c += -fno-builtin
258 CFLAGS-bench-isnan.c += -fsignaling-nans
259 CFLAGS-bench-isinf.c += -fsignaling-nans
260 CFLAGS-bench-isfinite.c += -fsignaling-nans
261
262 ifeq (${BENCHSET},)
263 bench-malloc := malloc-thread malloc-simple
264 else
265 bench-malloc := $(filter malloc-%,${BENCHSET})
266 endif
267
268 ifeq (${STATIC-BENCHTESTS},yes)
269 +link-benchtests = $(+link-static-tests)
270 link-libc-benchtests = $(link-libc-static)
271 libm-benchtests = $(common-objpfx)math/libm.a
272 thread-library-benchtests = $(static-thread-library)
273 else
274 link-libc-benchtests = $(link-libc)
275 +link-benchtests = $(+link-tests)
276 thread-library-benchtests = $(shared-thread-library)
277 libm-benchtests = $(libm)
278 endif
279
280 $(addprefix $(objpfx)bench-,$(bench-math)): $(libm-benchtests)
281 $(addprefix $(objpfx)bench-,$(math-benchset)): $(libm-benchtests)
282 $(addprefix $(objpfx)bench-,$(bench-pthread)): $(thread-library-benchtests)
283 $(addprefix $(objpfx)bench-,$(bench-malloc)): $(thread-library-benchtests)
284 $(addprefix $(objpfx)bench-,pthread-locks): $(libm-benchtests)
285 $(addprefix $(objpfx)bench-,pthread-mutex-locks): $(libm-benchtests)
286
287 \f
288
289 # Rules to build and execute the benchmarks.  Do not put any benchmark
290 # parameters beyond this point.
291
292 # We don't want the benchmark programs to run in parallel since that could
293 # affect their performance.
294 .NOTPARALLEL:
295
296 bench-extra-objs = json-lib.o
297
298 extra-objs += $(bench-extra-objs)
299 others-extras = $(bench-extra-objs)
300
301 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
302 # benches.
303 binaries-bench = $(addprefix $(objpfx)bench-,$(bench))
304 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench)))
305 binaries-benchset = $(addprefix $(objpfx)bench-,$(benchset))
306 extra-objs += $(addsuffix .o,$(addprefix bench-,$(benchset)))
307 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
308 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench-malloc)))
309
310 # The default duration: 1 seconds.
311 ifndef BENCH_DURATION
312 BENCH_DURATION := 1
313 endif
314
315 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
316
317 # Use clock_gettime to measure performance of functions.  The default is
318 # to use the architecture-specific high precision timing instructions.
319 ifdef USE_CLOCK_GETTIME
320 CPPFLAGS-nonlib += -DUSE_CLOCK_GETTIME
321 else
322 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
323 # of functions.  All x86 processors since 2010 support RDTSCP instruction.
324 ifdef USE_RDTSCP
325 CPPFLAGS-nonlib += -DUSE_RDTSCP
326 endif
327 endif
328
329 DETAILED_OPT :=
330
331 ifdef DETAILED
332 DETAILED_OPT := -d
333 endif
334
335 bench-deps := bench-skeleton.c bench-timing.h Makefile
336
337 run-bench = $(test-wrapper-env) \
338             $(run-program-env) \
339             $($*-ENV) $(test-via-rtld-prefix) $${run}
340
341 timing-type := $(objpfx)bench-timing-type
342 extra-objs += bench-timing-type.o
343
344 include ../Rules
345
346 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
347 # for all these modules.
348 cpp-srcs-left := $(binaries-benchset:=.c) $(binaries-bench:=.c) \
349                  $(binaries-bench-malloc:=.c) $(timing-type:=.c)
350 lib := nonlib
351 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
352
353 bench-clean:
354         rm -f $(binaries-bench) $(addsuffix .o,$(binaries-bench))
355         rm -f $(binaries-benchset) $(addsuffix .o,$(binaries-benchset))
356         rm -f $(binaries-bench-malloc) $(addsuffix .o,$(binaries-bench-malloc))
357         rm -f $(timing-type) $(addsuffix .o,$(timing-type))
358         rm -f $(addprefix $(objpfx),$(bench-extra-objs))
359
360 # Validate the passed in BENCHSET
361 ifneq ($(strip ${BENCHSET}),)
362 VALIDBENCHSETNAMES := bench-pthread bench-math bench-string string-benchset \
363    wcsmbs-benchset stdlib-benchset stdio-common-benchset math-benchset \
364    malloc-thread malloc-simple
365 INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
366 ifneq (${INVALIDBENCHSETNAMES},)
367 $(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
368 $(info The valid ones are: ${VALIDBENCHSETNAMES})
369 $(error Invalid BENCHSET value)
370 endif
371 endif
372
373 bench: bench-build bench-set bench-func bench-malloc
374
375 # Target to only build the benchmark without running it.  We generate locales
376 # only if we're building natively.
377 ifeq (no,$(cross-compiling))
378 bench-build: $(gen-locales) $(timing-type) $(binaries-bench) \
379         $(binaries-benchset) $(binaries-bench-malloc)
380 else
381 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
382         $(binaries-bench-malloc)
383 endif
384
385 bench-set: $(binaries-benchset)
386         for run in $^; do \
387           echo "Running $${run}"; \
388           $(run-bench) > $${run}.out; \
389         done
390
391 bench-malloc: $(binaries-bench-malloc)
392         for run in $^; do \
393           echo "$${run}"; \
394           if [ `basename $${run}` = "bench-malloc-thread" ]; then \
395                 for thr in 1 8 16 32; do \
396                         echo "Running $${run} $${thr}"; \
397                         $(run-bench) $${thr} > $${run}-$${thr}.out; \
398                 done;\
399           else \
400                 for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
401                   echo "Running $${run} $${thr}"; \
402                   $(run-bench) $${thr} > $${run}-$${thr}.out; \
403                 done;\
404           fi;\
405         done
406
407 # Build and execute the benchmark functions.  This target generates JSON
408 # formatted bench.out.  Each of the programs produce independent JSON output,
409 # so one could even execute them individually and process it using any JSON
410 # capable language or tool.
411 bench-func: $(binaries-bench)
412         if [ -n '$^' ] ; then \
413         { timing_type=$$($(test-wrapper-env) \
414                          $(run-program-env) \
415                          $(test-via-rtld-prefix) \
416                          $(timing-type)); \
417           echo "{\"timing_type\": \"$${timing_type}\","; \
418           echo " \"functions\": {"; \
419           for run in $^; do \
420             if ! [ "x$${run}" = "x$<" ]; then \
421               echo ","; \
422             fi; \
423             echo "Running $${run}" >&2; \
424             $(run-bench) $(DETAILED_OPT); \
425           done; \
426           echo; \
427           echo " }"; \
428           echo "}"; \
429           } > $(objpfx)bench.out-tmp; \
430           if [ -f $(objpfx)bench.out ]; then \
431             mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
432           fi; \
433           mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
434           $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
435           scripts/benchout.schema.json; \
436         fi
437
438 ifeq ($(bind-now),yes)
439 link-bench-bind-now = -Wl,-z,now
440 endif
441
442 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
443         $(binaries-bench-malloc)
444
445 $(bench-link-targets): %: %.o $(objpfx)json-lib.o \
446         $(link-extra-libs-tests) \
447   $(sort $(filter $(common-objpfx)lib%,$(link-libc-benchtests))) \
448   $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
449         $(+link-benchtests)
450
451 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
452
453 $(objpfx)bench-%.c: %-inputs $(bench-deps)
454         { if [ -n "$($*-INCLUDE)" ]; then \
455           cat $($*-INCLUDE); \
456         fi; \
457         $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp
458         mv -f $@-tmp $@