446cf2
commit 1e372ded4f83362509c8672ff501cba871bb1edc
446cf2
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
446cf2
Date:   Thu Jan 24 12:46:59 2019 +0000
446cf2
446cf2
    Refactor hp-timing rtld usage
446cf2
    
446cf2
    This patch refactor how hp-timing is used on loader code for statistics
446cf2
    report.  The HP_TIMING_AVAIL and HP_SMALL_TIMING_AVAIL are removed and
446cf2
    HP_TIMING_INLINE is used instead to check for hp-timing avaliability.
446cf2
    For alpha, which only defines HP_SMALL_TIMING_AVAIL, the HP_TIMING_INLINE
446cf2
    is set iff for IS_IN(rtld).
446cf2
    
446cf2
    Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. I also
446cf2
    checked the builds for all afected ABIs.
446cf2
    
446cf2
            * benchtests/bench-timing.h: Replace HP_TIMING_AVAIL with
446cf2
            HP_TIMING_INLINE.
446cf2
            * nptl/descr.h: Likewise.
446cf2
            * elf/rtld.c (RLTD_TIMING_DECLARE, RTLD_TIMING_NOW, RTLD_TIMING_DIFF,
446cf2
            RTLD_TIMING_ACCUM_NT, RTLD_TIMING_SET): Define.
446cf2
            (dl_start_final_info, _dl_start_final, dl_main, print_statistics):
446cf2
            Abstract hp-timing usage with RTLD_* macros.
446cf2
            * sysdeps/alpha/hp-timing.h (HP_TIMING_INLINE): Define iff IS_IN(rtld).
446cf2
            (HP_TIMING_AVAIL, HP_SMALL_TIMING_AVAIL): Remove.
446cf2
            * sysdeps/generic/hp-timing.h (HP_TIMING_AVAIL, HP_SMALL_TIMING_AVAIL,
446cf2
            HP_TIMING_NONAVAIL): Likewise.
446cf2
            * sysdeps/ia64/hp-timing.h (HP_TIMING_AVAIL, HP_SMALL_TIMING_AVAIL):
446cf2
            Likewise.
446cf2
            * sysdeps/powerpc/powerpc32/power4/hp-timing.h (HP_TIMING_AVAIL,
446cf2
            HP_SMALL_TIMING_AVAIL): Likewise.
446cf2
            * sysdeps/powerpc/powerpc64/hp-timing.h (HP_TIMING_AVAIL,
446cf2
            HP_SMALL_TIMING_AVAIL): Likewise.
446cf2
            * sysdeps/sparc/sparc32/sparcv9/hp-timing.h (HP_TIMING_AVAIL,
446cf2
            HP_SMALL_TIMING_AVAIL): Likewise.
446cf2
            * sysdeps/sparc/sparc64/hp-timing.h (HP_TIMING_AVAIL,
446cf2
            HP_SMALL_TIMING_AVAIL): Likewise.
446cf2
            * sysdeps/x86/hp-timing.h (HP_TIMING_AVAIL, HP_SMALL_TIMING_AVAIL):
446cf2
            Likewise.
446cf2
            * sysdeps/generic/hp-timing-common.h: Update comment with
446cf2
            HP_TIMING_AVAIL removal.
446cf2
446cf2
diff --git a/benchtests/bench-timing.h b/benchtests/bench-timing.h
446cf2
index 96cde1e8be2e0c2f..8ba6be51d5b6d4e1 100644
446cf2
--- a/benchtests/bench-timing.h
446cf2
+++ b/benchtests/bench-timing.h
446cf2
@@ -21,7 +21,7 @@
446cf2
 #include <hp-timing.h>
446cf2
 #include <stdint.h>
446cf2
 
446cf2
-#if HP_TIMING_AVAIL && !defined USE_CLOCK_GETTIME
446cf2
+#if HP_TIMING_INLINE && !defined USE_CLOCK_GETTIME
446cf2
 # define GL(x) _##x
446cf2
 # define GLRO(x) _##x
446cf2
 typedef hp_timing_t timing_t;
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index 375e0de8fa2e049e..ffbd8f4553bb3425 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -46,6 +46,49 @@
446cf2
 
446cf2
 #include <assert.h>
446cf2
 
446cf2
+/* Only enables rtld profiling for architectures which provides non generic
446cf2
+   hp-timing support.  The generic support requires either syscall
446cf2
+   (clock_gettime), which will incur in extra overhead on loading time.
446cf2
+   Using vDSO is also an option, but it will require extra support on loader
446cf2
+   to setup the vDSO pointer before its usage.  */
446cf2
+#if HP_TIMING_INLINE
446cf2
+# define RLTD_TIMING_DECLARE(var, classifier,...) \
446cf2
+  classifier hp_timing_t var __VA_ARGS__
446cf2
+# define RTLD_TIMING_VAR(var)        RLTD_TIMING_DECLARE (var, )
446cf2
+# define RTLD_TIMING_SET(var, value) (var) = (value)
446cf2
+# define RTLD_TIMING_REF(var)        &(var)
446cf2
+
446cf2
+static inline void
446cf2
+rtld_timer_start (hp_timing_t *var)
446cf2
+{
446cf2
+  HP_TIMING_NOW (*var);
446cf2
+}
446cf2
+
446cf2
+static inline void
446cf2
+rtld_timer_stop (hp_timing_t *var, hp_timing_t start)
446cf2
+{
446cf2
+  hp_timing_t stop;
446cf2
+  HP_TIMING_NOW (stop);
446cf2
+  HP_TIMING_DIFF (*var, start, stop);
446cf2
+}
446cf2
+
446cf2
+static inline void
446cf2
+rtld_timer_accum (hp_timing_t *sum, hp_timing_t start)
446cf2
+{
446cf2
+  hp_timing_t stop;
446cf2
+  rtld_timer_stop (&stop, start);
446cf2
+  HP_TIMING_ACCUM_NT(*sum, stop);
446cf2
+}
446cf2
+#else
446cf2
+# define RLTD_TIMING_DECLARE(var, classifier...)
446cf2
+# define RTLD_TIMING_SET(var, value)
446cf2
+# define RTLD_TIMING_VAR(var)
446cf2
+# define RTLD_TIMING_REF(var)			 0
446cf2
+# define rtld_timer_start(var)
446cf2
+# define rtld_timer_stop(var, start)
446cf2
+# define rtld_timer_accum(sum, start)
446cf2
+#endif
446cf2
+
446cf2
 /* Avoid PLT use for our local calls at startup.  */
446cf2
 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
446cf2
 
446cf2
@@ -62,7 +105,7 @@ static void print_missing_version (int errcode, const char *objname,
446cf2
 				   const char *errsting);
446cf2
 
446cf2
 /* Print the various times we collected.  */
446cf2
-static void print_statistics (hp_timing_t *total_timep);
446cf2
+static void print_statistics (const hp_timing_t *total_timep);
446cf2
 
446cf2
 /* Add audit objects.  */
446cf2
 static void process_dl_audit (char *str);
446cf2
@@ -303,11 +346,9 @@ static struct libname_list _dl_rtld_libname;
446cf2
 static struct libname_list _dl_rtld_libname2;
446cf2
 
446cf2
 /* Variable for statistics.  */
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-static hp_timing_t relocate_time;
446cf2
-static hp_timing_t load_time attribute_relro;
446cf2
-static hp_timing_t start_time attribute_relro;
446cf2
-#endif
446cf2
+RLTD_TIMING_DECLARE (relocate_time, static);
446cf2
+RLTD_TIMING_DECLARE (load_time,     static, attribute_relro);
446cf2
+RLTD_TIMING_DECLARE (start_time,    static, attribute_relro);
446cf2
 
446cf2
 /* Additional definitions needed by TLS initialization.  */
446cf2
 #ifdef TLS_INIT_HELPER
446cf2
@@ -335,9 +376,7 @@ static ElfW(Addr) _dl_start_final (void *arg);
446cf2
 struct dl_start_final_info
446cf2
 {
446cf2
   struct link_map l;
446cf2
-#if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
446cf2
-  hp_timing_t start_time;
446cf2
-#endif
446cf2
+  RTLD_TIMING_VAR (start_time);
446cf2
 };
446cf2
 static ElfW(Addr) _dl_start_final (void *arg,
446cf2
 				   struct dl_start_final_info *info);
446cf2
@@ -371,16 +410,11 @@ _dl_start_final (void *arg, struct dl_start_final_info *info)
446cf2
 {
446cf2
   ElfW(Addr) start_addr;
446cf2
 
446cf2
-  if (HP_SMALL_TIMING_AVAIL)
446cf2
-    {
446cf2
-      /* If it hasn't happen yet record the startup time.  */
446cf2
-      if (! HP_TIMING_INLINE)
446cf2
-	HP_TIMING_NOW (start_time);
446cf2
-#if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
446cf2
-      else
446cf2
-	start_time = info->start_time;
446cf2
+  /* If it hasn't happen yet record the startup time.  */
446cf2
+  rtld_timer_start (&start_time);
446cf2
+#if !defined DONT_USE_BOOTSTRAP_MAP
446cf2
+  RTLD_TIMING_SET (start_time, info->start_time);
446cf2
 #endif
446cf2
-    }
446cf2
 
446cf2
   /* Transfer data about ourselves to the permanent link_map structure.  */
446cf2
 #ifndef DONT_USE_BOOTSTRAP_MAP
446cf2
@@ -412,27 +446,11 @@ _dl_start_final (void *arg, struct dl_start_final_info *info)
446cf2
      entry point on the same stack we entered on.  */
446cf2
   start_addr = _dl_sysdep_start (arg, &dl_main);
446cf2
 
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-  hp_timing_t rtld_total_time;
446cf2
-  if (HP_SMALL_TIMING_AVAIL)
446cf2
-    {
446cf2
-      hp_timing_t end_time;
446cf2
-
446cf2
-      /* Get the current time.  */
446cf2
-      HP_TIMING_NOW (end_time);
446cf2
-
446cf2
-      /* Compute the difference.  */
446cf2
-      HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
446cf2
-    }
446cf2
-#endif
446cf2
-
446cf2
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
446cf2
     {
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-      print_statistics (&rtld_total_time);
446cf2
-#else
446cf2
-      print_statistics (NULL);
446cf2
-#endif
446cf2
+      RTLD_TIMING_VAR (rtld_total_time);
446cf2
+      rtld_timer_stop (&rtld_total_time, start_time);
446cf2
+      print_statistics (RTLD_TIMING_REF(rtld_total_time));
446cf2
     }
446cf2
 
446cf2
   return start_addr;
446cf2
@@ -457,11 +475,10 @@ _dl_start (void *arg)
446cf2
 #define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP
446cf2
 #include "dynamic-link.h"
446cf2
 
446cf2
-  if (HP_TIMING_INLINE && HP_SMALL_TIMING_AVAIL)
446cf2
 #ifdef DONT_USE_BOOTSTRAP_MAP
446cf2
-    HP_TIMING_NOW (start_time);
446cf2
+  rtld_timer_start (&start_time);
446cf2
 #else
446cf2
-    HP_TIMING_NOW (info.start_time);
446cf2
+  rtld_timer_start (&info.start_time);
446cf2
 #endif
446cf2
 
446cf2
   /* Partly clean the `bootstrap_map' structure up.  Don't use
446cf2
@@ -1078,11 +1095,6 @@ dl_main (const ElfW(Phdr) *phdr,
446cf2
   unsigned int i;
446cf2
   bool prelinked = false;
446cf2
   bool rtld_is_main = false;
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-  hp_timing_t start;
446cf2
-  hp_timing_t stop;
446cf2
-  hp_timing_t diff;
446cf2
-#endif
446cf2
   void *tcbp = NULL;
446cf2
 
446cf2
   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
446cf2
@@ -1256,12 +1268,11 @@ of this helper program; chances are you did not intend to run this program.\n\
446cf2
 	}
446cf2
       else
446cf2
 	{
446cf2
-	  HP_TIMING_NOW (start);
446cf2
+	  RTLD_TIMING_VAR (start);
446cf2
+	  rtld_timer_start (&start;;
446cf2
 	  _dl_map_object (NULL, rtld_progname, lt_executable, 0,
446cf2
 			  __RTLD_OPENEXEC, LM_ID_BASE);
446cf2
-	  HP_TIMING_NOW (stop);
446cf2
-
446cf2
-	  HP_TIMING_DIFF (load_time, start, stop);
446cf2
+	  rtld_timer_stop (&load_time, start);
446cf2
 	}
446cf2
 
446cf2
       /* Now the map for the main executable is available.  */
446cf2
@@ -1664,20 +1675,18 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 
446cf2
   if (__glibc_unlikely (preloadlist != NULL))
446cf2
     {
446cf2
-      HP_TIMING_NOW (start);
446cf2
+      RTLD_TIMING_VAR (start);
446cf2
+      rtld_timer_start (&start;;
446cf2
       npreloads += handle_preload_list (preloadlist, main_map, "LD_PRELOAD");
446cf2
-      HP_TIMING_NOW (stop);
446cf2
-      HP_TIMING_DIFF (diff, start, stop);
446cf2
-      HP_TIMING_ACCUM_NT (load_time, diff);
446cf2
+      rtld_timer_accum (&load_time, start);
446cf2
     }
446cf2
 
446cf2
   if (__glibc_unlikely (preloadarg != NULL))
446cf2
     {
446cf2
-      HP_TIMING_NOW (start);
446cf2
+      RTLD_TIMING_VAR (start);
446cf2
+      rtld_timer_start (&start;;
446cf2
       npreloads += handle_preload_list (preloadarg, main_map, "--preload");
446cf2
-      HP_TIMING_NOW (stop);
446cf2
-      HP_TIMING_DIFF (diff, start, stop);
446cf2
-      HP_TIMING_ACCUM_NT (load_time, diff);
446cf2
+      rtld_timer_accum (&load_time, start);
446cf2
     }
446cf2
 
446cf2
   /* There usually is no ld.so.preload file, it should only be used
446cf2
@@ -1737,7 +1746,8 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	      file[file_size - 1] = '\0';
446cf2
 	    }
446cf2
 
446cf2
-	  HP_TIMING_NOW (start);
446cf2
+	  RTLD_TIMING_VAR (start);
446cf2
+	  rtld_timer_start (&start;;
446cf2
 
446cf2
 	  if (file != problem)
446cf2
 	    {
446cf2
@@ -1755,9 +1765,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	      npreloads += do_preload (p, main_map, preload_file);
446cf2
 	    }
446cf2
 
446cf2
-	  HP_TIMING_NOW (stop);
446cf2
-	  HP_TIMING_DIFF (diff, start, stop);
446cf2
-	  HP_TIMING_ACCUM_NT (load_time, diff);
446cf2
+	  rtld_timer_accum (&load_time, start);
446cf2
 
446cf2
 	  /* We don't need the file anymore.  */
446cf2
 	  __munmap (file, file_size);
446cf2
@@ -1781,11 +1789,12 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
446cf2
      specified some libraries to load, these are inserted before the actual
446cf2
      dependencies in the executable's searchlist for symbol resolution.  */
446cf2
-  HP_TIMING_NOW (start);
446cf2
-  _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
446cf2
-  HP_TIMING_NOW (stop);
446cf2
-  HP_TIMING_DIFF (diff, start, stop);
446cf2
-  HP_TIMING_ACCUM_NT (load_time, diff);
446cf2
+  {
446cf2
+    RTLD_TIMING_VAR (start);
446cf2
+    rtld_timer_start (&start;;
446cf2
+    _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
446cf2
+    rtld_timer_accum (&load_time, start);
446cf2
+  }
446cf2
 
446cf2
   /* Mark all objects as being in the global scope.  */
446cf2
   for (i = main_map->l_searchlist.r_nlist; i > 0; )
446cf2
@@ -2178,12 +2187,10 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
       if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
446cf2
 	{
446cf2
 	  ElfW(Rela) *conflict, *conflictend;
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-	  hp_timing_t start;
446cf2
-	  hp_timing_t stop;
446cf2
-#endif
446cf2
 
446cf2
-	  HP_TIMING_NOW (start);
446cf2
+	  RTLD_TIMING_VAR (start);
446cf2
+	  rtld_timer_start (&start;;
446cf2
+
446cf2
 	  assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
446cf2
 	  conflict = (ElfW(Rela) *)
446cf2
 	    main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
446cf2
@@ -2191,8 +2198,8 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	    ((char *) conflict
446cf2
 	     + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
446cf2
 	  _dl_resolve_conflicts (main_map, conflict, conflictend);
446cf2
-	  HP_TIMING_NOW (stop);
446cf2
-	  HP_TIMING_DIFF (relocate_time, start, stop);
446cf2
+
446cf2
+	  rtld_timer_stop (&relocate_time, start);
446cf2
 	}
446cf2
 
446cf2
 
446cf2
@@ -2220,15 +2227,12 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	 know that because it is self-contained).  */
446cf2
 
446cf2
       int consider_profiling = GLRO(dl_profile) != NULL;
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-      hp_timing_t start;
446cf2
-      hp_timing_t stop;
446cf2
-#endif
446cf2
 
446cf2
       /* If we are profiling we also must do lazy reloaction.  */
446cf2
       GLRO(dl_lazy) |= consider_profiling;
446cf2
 
446cf2
-      HP_TIMING_NOW (start);
446cf2
+      RTLD_TIMING_VAR (start);
446cf2
+      rtld_timer_start (&start;;
446cf2
       unsigned i = main_map->l_searchlist.r_nlist;
446cf2
       while (i-- > 0)
446cf2
 	{
446cf2
@@ -2255,9 +2259,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
446cf2
 	    _dl_add_to_slotinfo (l, true);
446cf2
 	}
446cf2
-      HP_TIMING_NOW (stop);
446cf2
-
446cf2
-      HP_TIMING_DIFF (relocate_time, start, stop);
446cf2
+      rtld_timer_stop (&relocate_time, start);
446cf2
 
446cf2
       /* Now enable profiling if needed.  Like the previous call,
446cf2
 	 this has to go here because the calls it makes should use the
446cf2
@@ -2300,19 +2302,14 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
 	 re-relocation, we might call a user-supplied function
446cf2
 	 (e.g. calloc from _dl_relocate_object) that uses TLS data.  */
446cf2
 
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-      hp_timing_t start;
446cf2
-      hp_timing_t stop;
446cf2
-      hp_timing_t add;
446cf2
-#endif
446cf2
+      RTLD_TIMING_VAR (start);
446cf2
+      rtld_timer_start (&start;;
446cf2
 
446cf2
-      HP_TIMING_NOW (start);
446cf2
       /* Mark the link map as not yet relocated again.  */
446cf2
       GL(dl_rtld_map).l_relocated = 0;
446cf2
       _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
446cf2
-      HP_TIMING_NOW (stop);
446cf2
-      HP_TIMING_DIFF (add, start, stop);
446cf2
-      HP_TIMING_ACCUM_NT (relocate_time, add);
446cf2
+
446cf2
+      rtld_timer_accum (&relocate_time, start);
446cf2
     }
446cf2
 
446cf2
   /* Do any necessary cleanups for the startup OS interface code.
446cf2
@@ -2744,46 +2741,51 @@ process_envvars (enum mode *modep)
446cf2
     }
446cf2
 }
446cf2
 
446cf2
+#if HP_TIMING_INLINE
446cf2
+static void
446cf2
+print_statistics_item (const char *title, hp_timing_t time,
446cf2
+		       hp_timing_t total)
446cf2
+{
446cf2
+  char cycles[HP_TIMING_PRINT_SIZE];
446cf2
+  HP_TIMING_PRINT (cycles, sizeof (cycles), time);
446cf2
+
446cf2
+  char relative[3 * sizeof (hp_timing_t) + 2];
446cf2
+  char *cp = _itoa ((1000ULL * time) / total, relative + sizeof (relative),
446cf2
+		    10, 0);
446cf2
+  /* Sets the decimal point.  */
446cf2
+  char *wp = relative;
446cf2
+  switch (relative + sizeof (relative) - cp)
446cf2
+    {
446cf2
+    case 3:
446cf2
+      *wp++ = *cp++;
446cf2
+      /* Fall through.  */
446cf2
+    case 2:
446cf2
+      *wp++ = *cp++;
446cf2
+      /* Fall through.  */
446cf2
+    case 1:
446cf2
+      *wp++ = '.';
446cf2
+      *wp++ = *cp++;
446cf2
+    }
446cf2
+  *wp = '\0';
446cf2
+  _dl_debug_printf ("%s: %s cycles (%s%%)\n", title, cycles, relative);
446cf2
+}
446cf2
+#endif
446cf2
 
446cf2
 /* Print the various times we collected.  */
446cf2
 static void
446cf2
 __attribute ((noinline))
446cf2
-print_statistics (hp_timing_t *rtld_total_timep)
446cf2
+print_statistics (const hp_timing_t *rtld_total_timep)
446cf2
 {
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-  char buf[200];
446cf2
-  char *cp;
446cf2
-  char *wp;
446cf2
-
446cf2
-  /* Total time rtld used.  */
446cf2
-  if (HP_SMALL_TIMING_AVAIL)
446cf2
-    {
446cf2
-      HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
446cf2
-      _dl_debug_printf ("\nruntime linker statistics:\n"
446cf2
-			"  total startup time in dynamic loader: %s\n", buf);
446cf2
-
446cf2
-      /* Print relocation statistics.  */
446cf2
-      char pbuf[30];
446cf2
-      HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
446cf2
-      cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
446cf2
-		  pbuf + sizeof (pbuf), 10, 0);
446cf2
-      wp = pbuf;
446cf2
-      switch (pbuf + sizeof (pbuf) - cp)
446cf2
-	{
446cf2
-	case 3:
446cf2
-	  *wp++ = *cp++;
446cf2
-	  /* Fall through.  */
446cf2
-	case 2:
446cf2
-	  *wp++ = *cp++;
446cf2
-	  /* Fall through.  */
446cf2
-	case 1:
446cf2
-	  *wp++ = '.';
446cf2
-	  *wp++ = *cp++;
446cf2
-	}
446cf2
-      *wp = '\0';
446cf2
-      _dl_debug_printf ("\
446cf2
-	    time needed for relocation: %s (%s%%)\n", buf, pbuf);
446cf2
-    }
446cf2
+#if HP_TIMING_INLINE
446cf2
+  {
446cf2
+    char cycles[HP_TIMING_PRINT_SIZE];
446cf2
+    HP_TIMING_PRINT (cycles, sizeof (cycles), *rtld_total_timep);
446cf2
+    _dl_debug_printf ("\nruntime linker statistics:\n"
446cf2
+		      "  total startup time in dynamic loader: %s cycles\n",
446cf2
+		      cycles);
446cf2
+    print_statistics_item ("            time needed for relocation",
446cf2
+			   relocate_time, *rtld_total_timep);
446cf2
+  }
446cf2
 #endif
446cf2
 
446cf2
   unsigned long int num_relative_relocations = 0;
446cf2
@@ -2824,31 +2826,8 @@ print_statistics (hp_timing_t *rtld_total_timep)
446cf2
 		    GL(dl_num_cache_relocations),
446cf2
 		    num_relative_relocations);
446cf2
 
446cf2
-#ifndef HP_TIMING_NONAVAIL
446cf2
-  /* Time spend while loading the object and the dependencies.  */
446cf2
-  if (HP_SMALL_TIMING_AVAIL)
446cf2
-    {
446cf2
-      char pbuf[30];
446cf2
-      HP_TIMING_PRINT (buf, sizeof (buf), load_time);
446cf2
-      cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
446cf2
-		  pbuf + sizeof (pbuf), 10, 0);
446cf2
-      wp = pbuf;
446cf2
-      switch (pbuf + sizeof (pbuf) - cp)
446cf2
-	{
446cf2
-	case 3:
446cf2
-	  *wp++ = *cp++;
446cf2
-	  /* Fall through.  */
446cf2
-	case 2:
446cf2
-	  *wp++ = *cp++;
446cf2
-	  /* Fall through.  */
446cf2
-	case 1:
446cf2
-	  *wp++ = '.';
446cf2
-	  *wp++ = *cp++;
446cf2
-	}
446cf2
-      *wp = '\0';
446cf2
-      _dl_debug_printf ("\
446cf2
-	   time needed to load objects: %s (%s%%)\n",
446cf2
-				buf, pbuf);
446cf2
-    }
446cf2
+#if HP_TIMING_INLINE
446cf2
+  print_statistics_item ("           time needed to load objects",
446cf2
+			 load_time, *rtld_total_timep);
446cf2
 #endif
446cf2
 }
446cf2
diff --git a/nptl/descr.h b/nptl/descr.h
446cf2
index c3b81d8b27839502..98ba730bfeb7e4dd 100644
446cf2
--- a/nptl/descr.h
446cf2
+++ b/nptl/descr.h
446cf2
@@ -342,7 +342,7 @@ struct pthread
446cf2
   /* Lock for synchronizing setxid calls.  */
446cf2
   unsigned int setxid_futex;
446cf2
 
446cf2
-#if HP_TIMING_AVAIL
446cf2
+#if HP_TIMING_INLINE
446cf2
   hp_timing_t cpuclock_offset_ununsed;
446cf2
 #endif
446cf2
 
446cf2
diff --git a/sysdeps/alpha/hp-timing.h b/sysdeps/alpha/hp-timing.h
446cf2
index 62284e003acbca64..d6b603e2c51d1688 100644
446cf2
--- a/sysdeps/alpha/hp-timing.h
446cf2
+++ b/sysdeps/alpha/hp-timing.h
446cf2
@@ -17,16 +17,13 @@
446cf2
    License along with the GNU C Library.  If not, see
446cf2
    <http://www.gnu.org/licenses/>.  */
446cf2
 
446cf2
-#ifndef _HP_TIMING_H
446cf2
-#define _HP_TIMING_H	1
446cf2
+#ifndef _HP_TIMING_ALPHA_H
446cf2
+#define _HP_TIMING_ALPHA_H	1
446cf2
 
446cf2
+#if IS_IN(rtld)
446cf2
 /* We always have the timestamp register, but it's got only a 4 second
446cf2
    range.  Use it for ld.so profiling only.  */
446cf2
-#define HP_TIMING_AVAIL		(0)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
-
446cf2
-/* We indeed have inlined functions.  */
446cf2
-#define HP_TIMING_INLINE	(1)
446cf2
+# define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
 /* We use 32 bit values for the times.  */
446cf2
 typedef unsigned int hp_timing_t;
446cf2
@@ -34,13 +31,16 @@ typedef unsigned int hp_timing_t;
446cf2
 /* The "rpcc" instruction returns a 32-bit counting half and a 32-bit
446cf2
    "virtual cycle counter displacement".  Subtracting the two gives us
446cf2
    a virtual cycle count.  */
446cf2
-#define HP_TIMING_NOW(VAR) \
446cf2
+# define HP_TIMING_NOW(VAR) \
446cf2
   do {									      \
446cf2
     unsigned long int x_;						      \
446cf2
     asm volatile ("rpcc %0" : "=r"(x_));				      \
446cf2
     (VAR) = (int) (x_) - (int) (x_ >> 32);				      \
446cf2
   } while (0)
446cf2
+# include <hp-timing-common.h>
446cf2
 
446cf2
-#include <hp-timing-common.h>
446cf2
+#else
446cf2
+# include <sysdeps/generic/hp-timing.h>
446cf2
+#endif /* IS_IN(rtld)  */
446cf2
 
446cf2
 #endif	/* hp-timing.h */
446cf2
diff --git a/sysdeps/generic/hp-timing-common.h b/sysdeps/generic/hp-timing-common.h
446cf2
index 505c6bf5d2ee9395..ce338c990bd9fccd 100644
446cf2
--- a/sysdeps/generic/hp-timing-common.h
446cf2
+++ b/sysdeps/generic/hp-timing-common.h
446cf2
@@ -20,8 +20,6 @@
446cf2
 /* In case a platform supports timers in the hardware the following macros
446cf2
    and types must be defined:
446cf2
 
446cf2
-   - HP_TIMING_AVAIL: test for availability.
446cf2
-
446cf2
    - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
446cf2
      implemented using function calls but instead uses some inlined code
446cf2
      which might simply consist of a few assembler instructions.  We have to
446cf2
@@ -47,16 +45,16 @@
446cf2
 /* Accumulate ADD into SUM.  No attempt is made to be thread-safe.  */
446cf2
 #define HP_TIMING_ACCUM_NT(Sum, Diff)		((Sum) += (Diff))
446cf2
 
446cf2
+#define HP_TIMING_PRINT_SIZE (3 * sizeof (hp_timing_t) + 1)
446cf2
+
446cf2
 /* Write a decimal representation of the timing value into the given string.  */
446cf2
 #define HP_TIMING_PRINT(Dest, Len, Val) 				\
446cf2
   do {									\
446cf2
-    char __buf[20];							\
446cf2
+    char __buf[HP_TIMING_PRINT_SIZE];					\
446cf2
     char *__dest = (Dest);						\
446cf2
     size_t __len = (Len);						\
446cf2
     char *__cp = _itoa ((Val), __buf + sizeof (__buf), 10, 0);		\
446cf2
     size_t __cp_len = MIN (__buf + sizeof (__buf) - __cp, __len);	\
446cf2
     memcpy (__dest, __cp, __cp_len);					\
446cf2
-    memcpy (__dest + __cp_len, " cycles",				\
446cf2
-	    MIN (__len - __cp_len, sizeof (" cycles")));		\
446cf2
     __dest[__len - 1] = '\0';						\
446cf2
   } while (0)
446cf2
diff --git a/sysdeps/generic/hp-timing.h b/sysdeps/generic/hp-timing.h
446cf2
index e2c02c2bc0fd1564..97598099db29d69d 100644
446cf2
--- a/sysdeps/generic/hp-timing.h
446cf2
+++ b/sysdeps/generic/hp-timing.h
446cf2
@@ -25,8 +25,6 @@
446cf2
    the system call might be too high.  */
446cf2
 
446cf2
 /* Provide dummy definitions.  */
446cf2
-#define HP_TIMING_AVAIL		(0)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(0)
446cf2
 #define HP_TIMING_INLINE	(0)
446cf2
 typedef int hp_timing_t;
446cf2
 #define HP_TIMING_NOW(var)
446cf2
@@ -34,7 +32,4 @@ typedef int hp_timing_t;
446cf2
 #define HP_TIMING_ACCUM_NT(Sum, Diff)
446cf2
 #define HP_TIMING_PRINT(Buf, Len, Val)
446cf2
 
446cf2
-/* Since this implementation is not available we tell the user about it.  */
446cf2
-#define HP_TIMING_NONAVAIL	1
446cf2
-
446cf2
 #endif	/* hp-timing.h */
446cf2
diff --git a/sysdeps/ia64/hp-timing.h b/sysdeps/ia64/hp-timing.h
446cf2
index d8d1d7bf2c21f6e6..5ebbbc45746d5cf2 100644
446cf2
--- a/sysdeps/ia64/hp-timing.h
446cf2
+++ b/sysdeps/ia64/hp-timing.h
446cf2
@@ -20,10 +20,6 @@
446cf2
 #ifndef _HP_TIMING_H
446cf2
 #define _HP_TIMING_H	1
446cf2
 
446cf2
-/* We always assume having the timestamp register.  */
446cf2
-#define HP_TIMING_AVAIL		(1)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
-
446cf2
 /* We indeed have inlined functions.  */
446cf2
 #define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
diff --git a/sysdeps/powerpc/powerpc32/power4/hp-timing.h b/sysdeps/powerpc/powerpc32/power4/hp-timing.h
446cf2
index 10efcac481349ee3..0e81f4fe6a46ab86 100644
446cf2
--- a/sysdeps/powerpc/powerpc32/power4/hp-timing.h
446cf2
+++ b/sysdeps/powerpc/powerpc32/power4/hp-timing.h
446cf2
@@ -20,10 +20,6 @@
446cf2
 #ifndef _HP_TIMING_H
446cf2
 #define _HP_TIMING_H	1
446cf2
 
446cf2
-/* We always assume having the timestamp register.  */
446cf2
-#define HP_TIMING_AVAIL		(1)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
-
446cf2
 /* We indeed have inlined functions.  */
446cf2
 #define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
diff --git a/sysdeps/powerpc/powerpc64/hp-timing.h b/sysdeps/powerpc/powerpc64/hp-timing.h
446cf2
index c0aa3642f6ff1a42..77fe5e85bb32c163 100644
446cf2
--- a/sysdeps/powerpc/powerpc64/hp-timing.h
446cf2
+++ b/sysdeps/powerpc/powerpc64/hp-timing.h
446cf2
@@ -20,10 +20,6 @@
446cf2
 #ifndef _HP_TIMING_H
446cf2
 #define _HP_TIMING_H	1
446cf2
 
446cf2
-/* We always assume having the timestamp register.  */
446cf2
-#define HP_TIMING_AVAIL		(1)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
-
446cf2
 /* We indeed have inlined functions.  */
446cf2
 #define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
diff --git a/sysdeps/sparc/sparc32/sparcv9/hp-timing.h b/sysdeps/sparc/sparc32/sparcv9/hp-timing.h
446cf2
index 42451966f6192bcb..aedf9c031a0daad9 100644
446cf2
--- a/sysdeps/sparc/sparc32/sparcv9/hp-timing.h
446cf2
+++ b/sysdeps/sparc/sparc32/sparcv9/hp-timing.h
446cf2
@@ -20,8 +20,6 @@
446cf2
 #ifndef _HP_TIMING_H
446cf2
 #define _HP_TIMING_H	1
446cf2
 
446cf2
-#define HP_TIMING_AVAIL		(1)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
 #define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
 typedef unsigned long long int hp_timing_t;
446cf2
diff --git a/sysdeps/sparc/sparc64/hp-timing.h b/sysdeps/sparc/sparc64/hp-timing.h
446cf2
index 66325641067e1198..ee22729063745944 100644
446cf2
--- a/sysdeps/sparc/sparc64/hp-timing.h
446cf2
+++ b/sysdeps/sparc/sparc64/hp-timing.h
446cf2
@@ -20,8 +20,6 @@
446cf2
 #ifndef _HP_TIMING_H
446cf2
 #define _HP_TIMING_H	1
446cf2
 
446cf2
-#define HP_TIMING_AVAIL		(1)
446cf2
-#define HP_SMALL_TIMING_AVAIL	(1)
446cf2
 #define HP_TIMING_INLINE	(1)
446cf2
 
446cf2
 typedef unsigned long int hp_timing_t;
446cf2
diff --git a/sysdeps/x86/hp-timing.h b/sysdeps/x86/hp-timing.h
446cf2
index 0aa6f5e3f83e0d34..4dbd2aa8af69f95e 100644
446cf2
--- a/sysdeps/x86/hp-timing.h
446cf2
+++ b/sysdeps/x86/hp-timing.h
446cf2
@@ -22,10 +22,6 @@
446cf2
 #include <isa.h>
446cf2
 
446cf2
 #if MINIMUM_ISA == 686 || MINIMUM_ISA == 8664
446cf2
-/* We always assume having the timestamp register.  */
446cf2
-# define HP_TIMING_AVAIL	(1)
446cf2
-# define HP_SMALL_TIMING_AVAIL	(1)
446cf2
-
446cf2
 /* We indeed have inlined functions.  */
446cf2
 # define HP_TIMING_INLINE	(1)
446cf2