Blame benchtests/bench-string.h

Packit 6c4009
/* Measure string and memory functions.
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <getopt.h>
Packit 6c4009
#include <sys/cdefs.h>
Packit 6c4009
Packit 6c4009
/* We are compiled under _ISOMAC, so libc-symbols.h does not do this
Packit 6c4009
   for us.  */
Packit 6c4009
#include "config.h"
Packit 6c4009
#ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
Packit 6c4009
# define inhibit_loop_to_libcall \
Packit 6c4009
    __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
Packit 6c4009
#else
Packit 6c4009
# define inhibit_loop_to_libcall
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  void (*fn) (void);
Packit 6c4009
  long test;
Packit 6c4009
} impl_t;
Packit 6c4009
extern impl_t __start_impls[], __stop_impls[];
Packit 6c4009
Packit 6c4009
#define IMPL(name, test) \
Packit 6c4009
  impl_t tst_ ## name							\
Packit 6c4009
  __attribute__ ((section ("impls"), aligned (sizeof (void *))))	\
Packit 6c4009
       = { __STRING (name), (void (*) (void))name, test };
Packit 6c4009
Packit 6c4009
#ifdef TEST_MAIN
Packit 6c4009
Packit 6c4009
# ifndef _GNU_SOURCE
Packit 6c4009
#  define _GNU_SOURCE
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# undef __USE_STRING_INLINES
Packit 6c4009
Packit 6c4009
# include <stdio.h>
Packit 6c4009
# include <stdlib.h>
Packit 6c4009
# include <string.h>
Packit 6c4009
# include <sys/mman.h>
Packit 6c4009
# include <sys/param.h>
Packit 6c4009
# include <unistd.h>
Packit 6c4009
# include <fcntl.h>
Packit 6c4009
# include <error.h>
Packit 6c4009
# include <errno.h>
Packit 6c4009
# include <time.h>
Packit 6c4009
# include <ifunc-impl-list.h>
Packit 6c4009
# define GL(x) _##x
Packit 6c4009
# define GLRO(x) _##x
Packit 6c4009
# include "bench-timing.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
# define TEST_FUNCTION test_main
Packit 6c4009
# ifndef TIMEOUT
Packit 6c4009
#  define TIMEOUT (4 * 60)
Packit 6c4009
# endif
Packit 6c4009
# define OPT_ITERATIONS 10000
Packit 6c4009
# define OPT_RANDOM 10001
Packit 6c4009
# define OPT_SEED 10002
Packit 6c4009
Packit 6c4009
# define INNER_LOOP_ITERS 64
Packit 6c4009
Packit 6c4009
unsigned char *buf1, *buf2;
Packit 6c4009
int ret, do_srandom;
Packit 6c4009
unsigned int seed;
Packit 6c4009
size_t page_size;
Packit 6c4009
Packit 6c4009
# ifndef ITERATIONS
Packit 6c4009
size_t iterations = 100000;
Packit 6c4009
#  define ITERATIONS_OPTIONS \
Packit 6c4009
     { "iterations", required_argument, NULL, OPT_ITERATIONS },
Packit 6c4009
#  define ITERATIONS_PROCESS \
Packit 6c4009
     case OPT_ITERATIONS:						      \
Packit 6c4009
       iterations = strtoul (optarg, NULL, 0);				      \
Packit 6c4009
       break;
Packit 6c4009
#  define ITERATIONS iterations
Packit 6c4009
# else
Packit 6c4009
#  define ITERATIONS_OPTIONS
Packit 6c4009
#  define ITERATIONS_PROCESS
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
Packit 6c4009
    { "random", no_argument, NULL, OPT_RANDOM },			      \
Packit 6c4009
    { "seed", required_argument, NULL, OPT_SEED },
Packit 6c4009
Packit 6c4009
static void __attribute__ ((used))
Packit 6c4009
cmdline_process_function (int c)
Packit 6c4009
{
Packit 6c4009
  switch (c)
Packit 6c4009
    {
Packit 6c4009
      ITERATIONS_PROCESS
Packit 6c4009
      case OPT_RANDOM:
Packit 6c4009
	{
Packit 6c4009
	  int fdr = open ("/dev/urandom", O_RDONLY);
Packit 6c4009
	  if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed))
Packit 6c4009
	    seed = time (NULL);
Packit 6c4009
	  if (fdr >= 0)
Packit 6c4009
	    close (fdr);
Packit 6c4009
	  do_srandom = 1;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      case OPT_SEED:
Packit 6c4009
	seed = strtoul (optarg, NULL, 0);
Packit 6c4009
	do_srandom = 1;
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
# define CMDLINE_PROCESS cmdline_process_function
Packit 6c4009
# define CALL(impl, ...)	\
Packit 6c4009
    (* (proto_t) (impl)->fn) (__VA_ARGS__)
Packit 6c4009
Packit 6c4009
# ifdef TEST_NAME
Packit 6c4009
/* Increase size of FUNC_LIST if assert is triggered at run-time.  */
Packit 6c4009
static struct libc_ifunc_impl func_list[32];
Packit 6c4009
static int func_count;
Packit 6c4009
static int impl_count = -1;
Packit 6c4009
static impl_t *impl_array;
Packit 6c4009
Packit 6c4009
#  define FOR_EACH_IMPL(impl, notall) \
Packit 6c4009
     impl_t *impl;							      \
Packit 6c4009
     int count;								      \
Packit 6c4009
     if (impl_count == -1)						      \
Packit 6c4009
       {								      \
Packit 6c4009
	 impl_count = 0;						      \
Packit 6c4009
	 if (func_count != 0)						      \
Packit 6c4009
	   {								      \
Packit 6c4009
	     int f;							      \
Packit 6c4009
	     impl_t *skip = NULL, *a;					      \
Packit 6c4009
	     for (impl = __start_impls; impl < __stop_impls; ++impl)	      \
Packit 6c4009
	       if (strcmp (impl->name, TEST_NAME) == 0)			      \
Packit 6c4009
		 skip = impl;						      \
Packit 6c4009
	       else							      \
Packit 6c4009
		 impl_count++;						      \
Packit 6c4009
	     a = impl_array = malloc ((impl_count + func_count) *	      \
Packit 6c4009
				   sizeof (impl_t));			      \
Packit 6c4009
	     for (impl = __start_impls; impl < __stop_impls; ++impl)	      \
Packit 6c4009
	       if (impl != skip)					      \
Packit 6c4009
		 *a++ = *impl;						      \
Packit 6c4009
	     for (f = 0; f < func_count; f++)				      \
Packit 6c4009
	       if (func_list[f].usable)					      \
Packit 6c4009
		 {							      \
Packit 6c4009
		   a->name = func_list[f].name;				      \
Packit 6c4009
		   a->fn = func_list[f].fn;				      \
Packit 6c4009
		   a->test = 1;						      \
Packit 6c4009
		   a++;							      \
Packit 6c4009
		 }							      \
Packit 6c4009
	     impl_count = a - impl_array;				      \
Packit 6c4009
	   }								      \
Packit 6c4009
	 else								      \
Packit 6c4009
	   {								      \
Packit 6c4009
	     impl_count = __stop_impls - __start_impls;			      \
Packit 6c4009
	     impl_array = __start_impls;				      \
Packit 6c4009
	   }								      \
Packit 6c4009
       }								      \
Packit 6c4009
     impl = impl_array;							      \
Packit 6c4009
     for (count = 0; count < impl_count; ++count, ++impl)		      \
Packit 6c4009
       if (!notall || impl->test)
Packit 6c4009
# else /* !TEST_NAME */
Packit 6c4009
#  define FOR_EACH_IMPL(impl, notall) \
Packit 6c4009
     for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl)	      \
Packit 6c4009
       if (!notall || impl->test)
Packit 6c4009
# endif /* !TEST_NAME */
Packit 6c4009
Packit 6c4009
# ifndef BUF1PAGES
Packit 6c4009
#  define BUF1PAGES 1
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
alloc_bufs (void)
Packit 6c4009
{
Packit 6c4009
  page_size = 2 * getpagesize ();
Packit 6c4009
# ifdef MIN_PAGE_SIZE
Packit 6c4009
  if (page_size < MIN_PAGE_SIZE)
Packit 6c4009
    page_size = MIN_PAGE_SIZE;
Packit 6c4009
# endif
Packit 6c4009
  buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
Packit 6c4009
	       MAP_PRIVATE | MAP_ANON, -1, 0);
Packit 6c4009
  if (buf1 == MAP_FAILED)
Packit 6c4009
    error (EXIT_FAILURE, errno, "mmap failed for buf1");
Packit 6c4009
  if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
Packit 6c4009
    error (EXIT_FAILURE, errno, "mprotect failed for buf1");
Packit 6c4009
  buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
Packit 6c4009
	       MAP_PRIVATE | MAP_ANON, -1, 0);
Packit 6c4009
  if (buf2 == MAP_FAILED)
Packit 6c4009
    error (EXIT_FAILURE, errno, "mmap failed for buf2");
Packit 6c4009
  if (mprotect (buf2 + page_size, page_size, PROT_NONE))
Packit 6c4009
    error (EXIT_FAILURE, errno, "mprotect failed for buf2");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((unused))
Packit 6c4009
realloc_bufs (void)
Packit 6c4009
{
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  if (buf1)
Packit 6c4009
    ret = munmap (buf1, (BUF1PAGES + 1) * page_size);
Packit 6c4009
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    error (EXIT_FAILURE, errno, "munmap failed for buf1");
Packit 6c4009
Packit 6c4009
  if (buf2)
Packit 6c4009
    ret = munmap (buf2, 2 * page_size);
Packit 6c4009
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    error (EXIT_FAILURE, errno, "munmap failed for buf2");
Packit 6c4009
Packit 6c4009
  alloc_bufs ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_init (void)
Packit 6c4009
{
Packit 6c4009
# ifdef TEST_NAME
Packit 6c4009
  func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
Packit 6c4009
				       (sizeof func_list
Packit 6c4009
					/ sizeof func_list[0]));
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
  alloc_bufs ();
Packit 6c4009
Packit 6c4009
  if (do_srandom)
Packit 6c4009
    {
Packit 6c4009
      printf ("Setting seed to 0x%x\n", seed);
Packit 6c4009
      srandom (seed);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* TEST_MAIN */