| 1 | /* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
|---|
| 2 | This file is part of the GNU C Library.
|
|---|
| 3 | Contributed by David Mosberger-Tang <[email protected]>.
|
|---|
| 4 |
|
|---|
| 5 | The GNU C Library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | The GNU C Library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with the GNU C Library; if not, write to the Free
|
|---|
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA. */
|
|---|
| 19 |
|
|---|
| 20 | #include <errno.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <stdlib.h>
|
|---|
| 23 | #include <sys/profil.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <bits/wordsize.h>
|
|---|
| 26 |
|
|---|
| 27 | #define NELEMS(a) (sizeof (a)/sizeof ((a)[0]))
|
|---|
| 28 |
|
|---|
| 29 | size_t taddr[] =
|
|---|
| 30 | {
|
|---|
| 31 | 0x00001000, /* elf32/hppa */
|
|---|
| 32 | 0x08048000, /* Linux elf32/x86 */
|
|---|
| 33 | 0x80000000, /* Linux elf32/m68k */
|
|---|
| 34 | 0x00400000, /* Linux elf32/mips */
|
|---|
| 35 | 0x01800000, /* Linux elf32/ppc */
|
|---|
| 36 | 0x00010000 /* Linux elf32/sparc */
|
|---|
| 37 | #if __WORDSIZE > 32
|
|---|
| 38 | ,
|
|---|
| 39 | 0x4000000000000000, /* Linux elf64/ia64 */
|
|---|
| 40 | 0x0000000120000000, /* Linux elf64/alpha */
|
|---|
| 41 | 0x4000000000001000, /* elf64/hppa */
|
|---|
| 42 | 0x0000000100000000 /* Linux elf64/sparc */
|
|---|
| 43 | #endif
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | unsigned int buf[NELEMS (taddr)][0x10000 / sizeof (int)];
|
|---|
| 47 | unsigned int bshort[5][0x100 / sizeof (int)];
|
|---|
| 48 | unsigned int blong[1][0x1000 / sizeof (int)];
|
|---|
| 49 | unsigned int vlong[1][0x2000 / sizeof (int)];
|
|---|
| 50 |
|
|---|
| 51 | static long int
|
|---|
| 52 | fac (long int n)
|
|---|
| 53 | {
|
|---|
| 54 | if (n == 0)
|
|---|
| 55 | return 1;
|
|---|
| 56 | return n * fac (n - 1);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | int
|
|---|
| 60 | main (int argc, char **argv)
|
|---|
| 61 | {
|
|---|
| 62 | unsigned int ovfl = 0, profcnt = 0;
|
|---|
| 63 | struct timeval tv, start;
|
|---|
| 64 | struct prof prof[32];
|
|---|
| 65 | double t_tick, delta;
|
|---|
| 66 | long int sum = 0;
|
|---|
| 67 | int i, j;
|
|---|
| 68 |
|
|---|
| 69 | for (i = 0; i < NELEMS (taddr); ++i)
|
|---|
| 70 | {
|
|---|
| 71 | prof[profcnt].pr_base = buf[i];
|
|---|
| 72 | prof[profcnt].pr_size = sizeof (buf[i]);
|
|---|
| 73 | prof[profcnt].pr_off = taddr[i];
|
|---|
| 74 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 75 | ++profcnt;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | prof[profcnt].pr_base = blong[0];
|
|---|
| 79 | prof[profcnt].pr_size = sizeof (blong[0]);
|
|---|
| 80 | prof[profcnt].pr_off = 0x80001000;
|
|---|
| 81 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 82 | ++profcnt;
|
|---|
| 83 |
|
|---|
| 84 | prof[profcnt].pr_base = bshort[0];
|
|---|
| 85 | prof[profcnt].pr_size = sizeof (bshort[0]);
|
|---|
| 86 | prof[profcnt].pr_off = 0x80000080;
|
|---|
| 87 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 88 | ++profcnt;
|
|---|
| 89 |
|
|---|
| 90 | prof[profcnt].pr_base = bshort[1];
|
|---|
| 91 | prof[profcnt].pr_size = sizeof (bshort[1]);
|
|---|
| 92 | prof[profcnt].pr_off = 0x80000f80;
|
|---|
| 93 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 94 | ++profcnt;
|
|---|
| 95 |
|
|---|
| 96 | prof[profcnt].pr_base = bshort[2];
|
|---|
| 97 | prof[profcnt].pr_size = sizeof (bshort[2]);
|
|---|
| 98 | prof[profcnt].pr_off = 0x80001080;
|
|---|
| 99 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 100 | ++profcnt;
|
|---|
| 101 |
|
|---|
| 102 | prof[profcnt].pr_base = bshort[3];
|
|---|
| 103 | prof[profcnt].pr_size = sizeof (bshort[3]);
|
|---|
| 104 | prof[profcnt].pr_off = 0x80001f80;
|
|---|
| 105 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 106 | ++profcnt;
|
|---|
| 107 |
|
|---|
| 108 | prof[profcnt].pr_base = bshort[4];
|
|---|
| 109 | prof[profcnt].pr_size = sizeof (bshort[4]);
|
|---|
| 110 | prof[profcnt].pr_off = 0x80002080;
|
|---|
| 111 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 112 | ++profcnt;
|
|---|
| 113 |
|
|---|
| 114 | prof[profcnt].pr_base = vlong[0];
|
|---|
| 115 | prof[profcnt].pr_size = sizeof (vlong[0]);
|
|---|
| 116 | prof[profcnt].pr_off = 0x80000080;
|
|---|
| 117 | prof[profcnt].pr_scale = 0x10000;
|
|---|
| 118 | ++profcnt;
|
|---|
| 119 |
|
|---|
| 120 | /* Set up overflow counter (must be last on Irix). */
|
|---|
| 121 | prof[profcnt].pr_base = &ovfl;
|
|---|
| 122 | prof[profcnt].pr_size = sizeof (ovfl);
|
|---|
| 123 | prof[profcnt].pr_off = 0;
|
|---|
| 124 | prof[profcnt].pr_scale = 2;
|
|---|
| 125 | ++profcnt;
|
|---|
| 126 |
|
|---|
| 127 | /* Turn it on. */
|
|---|
| 128 | if (sprofil (prof, profcnt, &tv, PROF_UINT) < 0)
|
|---|
| 129 | {
|
|---|
| 130 | if (errno == ENOSYS)
|
|---|
| 131 | exit (0);
|
|---|
| 132 | perror ("sprofil");
|
|---|
| 133 | exit (1);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | t_tick = tv.tv_sec + 1e-6 * tv.tv_usec;
|
|---|
| 137 | printf ("profiling period = %g ms\n", 1e3 * t_tick);
|
|---|
| 138 |
|
|---|
| 139 | gettimeofday (&start, NULL);
|
|---|
| 140 | do
|
|---|
| 141 | {
|
|---|
| 142 | for (i = 0; i < 21; ++i)
|
|---|
| 143 | sum += fac (i);
|
|---|
| 144 |
|
|---|
| 145 | gettimeofday (&tv, NULL);
|
|---|
| 146 | timersub (&tv, &start, &tv);
|
|---|
| 147 | delta = tv.tv_sec + 1e-6 * tv.tv_usec;
|
|---|
| 148 | }
|
|---|
| 149 | while (delta < 1000 * t_tick);
|
|---|
| 150 |
|
|---|
| 151 | printf ("sum = 0x%lx\n", sum);
|
|---|
| 152 |
|
|---|
| 153 | /* Turn it off. */
|
|---|
| 154 | if (sprofil (NULL, 0, NULL, 0) < 0)
|
|---|
| 155 | {
|
|---|
| 156 | if (errno == ENOSYS)
|
|---|
| 157 | exit (0);
|
|---|
| 158 | perror ("sprofil");
|
|---|
| 159 | exit (1);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | printf ("overflow = %u\n", ovfl);
|
|---|
| 163 | for (i = 0; i < NELEMS (taddr); ++i)
|
|---|
| 164 | for (j = 0; j < 0x10000 / sizeof (int); ++j)
|
|---|
| 165 | if (buf[i][j] != 0)
|
|---|
| 166 | printf ("%0*Zx\t%u\t(buffer %d)\n",
|
|---|
| 167 | (int) (sizeof (size_t) * 2),
|
|---|
| 168 | (taddr[i] + ((char *) &buf[i][j] - (char *) &buf[i][0])),
|
|---|
| 169 | buf[i][j], i);
|
|---|
| 170 |
|
|---|
| 171 | return 0;
|
|---|
| 172 | }
|
|---|