Back to index

glibc  2.9
tst-sprofil.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <sys/profil.h>
00024 
00025 #include <bits/wordsize.h>
00026 
00027 #define NELEMS(a)    (sizeof (a)/sizeof ((a)[0]))
00028 
00029 size_t taddr[] =
00030   {
00031     0x00001000,             /* elf32/hppa */
00032     0x08048000,             /* Linux elf32/x86 */
00033     0x80000000,             /* Linux elf32/m68k */
00034     0x00400000,             /* Linux elf32/mips */
00035     0x01800000,             /* Linux elf32/ppc */
00036     0x00010000              /* Linux elf32/sparc */
00037 #if __WORDSIZE > 32
00038     ,
00039     0x4000000000000000,     /* Linux elf64/ia64 */
00040     0x0000000120000000,     /* Linux elf64/alpha */
00041     0x4000000000001000,     /* elf64/hppa */
00042     0x0000000100000000      /* Linux elf64/sparc */
00043 #endif
00044   };
00045 
00046 unsigned int buf[NELEMS (taddr)][0x10000 / sizeof (int)];
00047 unsigned int bshort[5][0x100 / sizeof (int)];
00048 unsigned int blong[1][0x1000 / sizeof (int)];
00049 unsigned int vlong[1][0x2000 / sizeof (int)];
00050 
00051 static long int
00052 fac (long int n)
00053 {
00054   if (n == 0)
00055     return 1;
00056   return n * fac (n - 1);
00057 }
00058 
00059 int
00060 main (int argc, char **argv)
00061 {
00062   unsigned int ovfl = 0, profcnt = 0;
00063   struct timeval tv, start;
00064   struct prof prof[32];
00065   double t_tick, delta;
00066   long int sum = 0;
00067   int i, j;
00068 
00069   for (i = 0; i < NELEMS (taddr); ++i)
00070     {
00071       prof[profcnt].pr_base = buf[i];
00072       prof[profcnt].pr_size = sizeof (buf[i]);
00073       prof[profcnt].pr_off = taddr[i];
00074       prof[profcnt].pr_scale = 0x10000;
00075       ++profcnt;
00076     }
00077 
00078   prof[profcnt].pr_base = blong[0];
00079   prof[profcnt].pr_size = sizeof (blong[0]);
00080   prof[profcnt].pr_off = 0x80001000;
00081   prof[profcnt].pr_scale = 0x10000;
00082   ++profcnt;
00083 
00084   prof[profcnt].pr_base = bshort[0];
00085   prof[profcnt].pr_size = sizeof (bshort[0]);
00086   prof[profcnt].pr_off = 0x80000080;
00087   prof[profcnt].pr_scale = 0x10000;
00088   ++profcnt;
00089 
00090   prof[profcnt].pr_base = bshort[1];
00091   prof[profcnt].pr_size = sizeof (bshort[1]);
00092   prof[profcnt].pr_off = 0x80000f80;
00093   prof[profcnt].pr_scale = 0x10000;
00094   ++profcnt;
00095 
00096   prof[profcnt].pr_base = bshort[2];
00097   prof[profcnt].pr_size = sizeof (bshort[2]);
00098   prof[profcnt].pr_off = 0x80001080;
00099   prof[profcnt].pr_scale = 0x10000;
00100   ++profcnt;
00101 
00102   prof[profcnt].pr_base = bshort[3];
00103   prof[profcnt].pr_size = sizeof (bshort[3]);
00104   prof[profcnt].pr_off = 0x80001f80;
00105   prof[profcnt].pr_scale = 0x10000;
00106   ++profcnt;
00107 
00108   prof[profcnt].pr_base = bshort[4];
00109   prof[profcnt].pr_size = sizeof (bshort[4]);
00110   prof[profcnt].pr_off = 0x80002080;
00111   prof[profcnt].pr_scale = 0x10000;
00112   ++profcnt;
00113 
00114   prof[profcnt].pr_base = vlong[0];
00115   prof[profcnt].pr_size = sizeof (vlong[0]);
00116   prof[profcnt].pr_off = 0x80000080;
00117   prof[profcnt].pr_scale = 0x10000;
00118   ++profcnt;
00119 
00120   /* Set up overflow counter (must be last on Irix).  */
00121   prof[profcnt].pr_base = &ovfl;
00122   prof[profcnt].pr_size = sizeof (ovfl);
00123   prof[profcnt].pr_off = 0;
00124   prof[profcnt].pr_scale = 2;
00125   ++profcnt;
00126 
00127   /* Turn it on.  */
00128   if (sprofil (prof, profcnt, &tv, PROF_UINT) < 0)
00129     {
00130       if (errno == ENOSYS)
00131        exit (0);
00132       perror ("sprofil");
00133       exit (1);
00134     }
00135 
00136   t_tick = tv.tv_sec + 1e-6 * tv.tv_usec;
00137   printf ("profiling period = %g ms\n", 1e3 * t_tick);
00138 
00139   gettimeofday (&start, NULL);
00140   do
00141     {
00142       for (i = 0; i < 21; ++i)
00143        sum += fac (i);
00144 
00145       gettimeofday (&tv, NULL);
00146       timersub (&tv, &start, &tv);
00147       delta = tv.tv_sec + 1e-6 * tv.tv_usec;
00148     }
00149   while (delta < 1000 * t_tick);
00150 
00151   printf ("sum = 0x%lx\n", sum);
00152 
00153   /* Turn it off.  */
00154   if (sprofil (NULL, 0, NULL, 0) < 0)
00155     {
00156       if (errno == ENOSYS)
00157        exit (0);
00158       perror ("sprofil");
00159       exit (1);
00160     }
00161 
00162   printf ("overflow = %u\n", ovfl);
00163   for (i = 0; i < NELEMS (taddr); ++i)
00164     for (j = 0; j < 0x10000 / sizeof (int); ++j)
00165       if (buf[i][j] != 0)
00166        printf ("%0*Zx\t%u\t(buffer %d)\n",
00167               (int) (sizeof (size_t) * 2),
00168               (taddr[i] + ((char *) &buf[i][j] - (char *) &buf[i][0])),
00169               buf[i][j], i);
00170 
00171   return 0;
00172 }