Blame gmon/tst-sprofil.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
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 <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/profil.h>
Packit 6c4009
Packit 6c4009
#include <bits/wordsize.h>
Packit 6c4009
Packit 6c4009
#define NELEMS(a)	(sizeof (a)/sizeof ((a)[0]))
Packit 6c4009
Packit 6c4009
size_t taddr[] =
Packit 6c4009
  {
Packit 6c4009
    0x00001000,		/* elf32/hppa */
Packit 6c4009
    0x08048000,		/* Linux elf32/x86 */
Packit 6c4009
    0x80000000,		/* Linux elf32/m68k */
Packit 6c4009
    0x00400000,		/* Linux elf32/mips */
Packit 6c4009
    0x01800000,		/* Linux elf32/ppc */
Packit 6c4009
    0x00010000		/* Linux elf32/sparc */
Packit 6c4009
#if __WORDSIZE > 32
Packit 6c4009
    ,
Packit 6c4009
    0x4000000000000000,	/* Linux elf64/ia64 */
Packit 6c4009
    0x0000000120000000,	/* Linux elf64/alpha */
Packit 6c4009
    0x4000000000001000,	/* elf64/hppa */
Packit 6c4009
    0x0000000100000000	/* Linux elf64/sparc */
Packit 6c4009
#endif
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
unsigned int buf[NELEMS (taddr)][0x10000 / sizeof (int)];
Packit 6c4009
unsigned int bshort[5][0x100 / sizeof (int)];
Packit 6c4009
unsigned int blong[1][0x1000 / sizeof (int)];
Packit 6c4009
unsigned int vlong[1][0x2000 / sizeof (int)];
Packit 6c4009
Packit 6c4009
static long int
Packit 6c4009
fac (long int n)
Packit 6c4009
{
Packit 6c4009
  if (n == 0)
Packit 6c4009
    return 1;
Packit 6c4009
  return n * fac (n - 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  unsigned int ovfl = 0, profcnt = 0;
Packit 6c4009
  struct timeval tv, start;
Packit 6c4009
  struct prof prof[32];
Packit 6c4009
  double t_tick, delta;
Packit 6c4009
  long int sum = 0;
Packit 6c4009
  int i, j;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < NELEMS (taddr); ++i)
Packit 6c4009
    {
Packit 6c4009
      prof[profcnt].pr_base = buf[i];
Packit 6c4009
      prof[profcnt].pr_size = sizeof (buf[i]);
Packit 6c4009
      prof[profcnt].pr_off = taddr[i];
Packit 6c4009
      prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
      ++profcnt;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = blong[0];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (blong[0]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80001000;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = bshort[0];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (bshort[0]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80000080;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = bshort[1];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (bshort[1]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80000f80;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = bshort[2];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (bshort[2]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80001080;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = bshort[3];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (bshort[3]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80001f80;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = bshort[4];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (bshort[4]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80002080;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  prof[profcnt].pr_base = vlong[0];
Packit 6c4009
  prof[profcnt].pr_size = sizeof (vlong[0]);
Packit 6c4009
  prof[profcnt].pr_off = 0x80000080;
Packit 6c4009
  prof[profcnt].pr_scale = 0x10000;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  /* Set up overflow counter (must be last on Irix).  */
Packit 6c4009
  prof[profcnt].pr_base = &ovf;;
Packit 6c4009
  prof[profcnt].pr_size = sizeof (ovfl);
Packit 6c4009
  prof[profcnt].pr_off = 0;
Packit 6c4009
  prof[profcnt].pr_scale = 2;
Packit 6c4009
  ++profcnt;
Packit 6c4009
Packit 6c4009
  /* Turn it on.  */
Packit 6c4009
  if (sprofil (prof, profcnt, &tv, PROF_UINT) < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	exit (0);
Packit 6c4009
      perror ("sprofil");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  t_tick = tv.tv_sec + 1e-6 * tv.tv_usec;
Packit 6c4009
  printf ("profiling period = %g ms\n", 1e3 * t_tick);
Packit 6c4009
Packit 6c4009
  gettimeofday (&start, NULL);
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      for (i = 0; i < 21; ++i)
Packit 6c4009
	sum += fac (i);
Packit 6c4009
Packit 6c4009
      gettimeofday (&tv, NULL);
Packit 6c4009
      timersub (&tv, &start, &tv;;
Packit 6c4009
      delta = tv.tv_sec + 1e-6 * tv.tv_usec;
Packit 6c4009
    }
Packit 6c4009
  while (delta < 1000 * t_tick);
Packit 6c4009
Packit 6c4009
  printf ("sum = 0x%lx\n", sum);
Packit 6c4009
Packit 6c4009
  /* Turn it off.  */
Packit 6c4009
  if (sprofil (NULL, 0, NULL, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	exit (0);
Packit 6c4009
      perror ("sprofil");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("overflow = %u\n", ovfl);
Packit 6c4009
  for (i = 0; i < NELEMS (taddr); ++i)
Packit 6c4009
    for (j = 0; j < 0x10000 / sizeof (int); ++j)
Packit 6c4009
      if (buf[i][j] != 0)
Packit 6c4009
	printf ("%0*Zx\t%u\t(buffer %d)\n",
Packit 6c4009
		(int) (sizeof (size_t) * 2),
Packit 6c4009
		(taddr[i] + ((char *) &buf[i][j] - (char *) &buf[i][0])),
Packit 6c4009
		buf[i][j], i);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}