lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2001-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com>. |
| 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, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/profil.h> |
| 23 | |
| 24 | #include <bits/wordsize.h> |
| 25 | |
| 26 | #define NELEMS(a) (sizeof (a)/sizeof ((a)[0])) |
| 27 | |
| 28 | size_t taddr[] = |
| 29 | { |
| 30 | 0x00001000, /* elf32/hppa */ |
| 31 | 0x08048000, /* Linux elf32/x86 */ |
| 32 | 0x80000000, /* Linux elf32/m68k */ |
| 33 | 0x00400000, /* Linux elf32/mips */ |
| 34 | 0x01800000, /* Linux elf32/ppc */ |
| 35 | 0x00010000 /* Linux elf32/sparc */ |
| 36 | #if __WORDSIZE > 32 |
| 37 | , |
| 38 | 0x4000000000000000, /* Linux elf64/ia64 */ |
| 39 | 0x0000000120000000, /* Linux elf64/alpha */ |
| 40 | 0x4000000000001000, /* elf64/hppa */ |
| 41 | 0x0000000100000000 /* Linux elf64/sparc */ |
| 42 | #endif |
| 43 | }; |
| 44 | |
| 45 | unsigned int buf[NELEMS (taddr)][0x10000 / sizeof (int)]; |
| 46 | unsigned int bshort[5][0x100 / sizeof (int)]; |
| 47 | unsigned int blong[1][0x1000 / sizeof (int)]; |
| 48 | unsigned int vlong[1][0x2000 / sizeof (int)]; |
| 49 | |
| 50 | static long int |
| 51 | fac (long int n) |
| 52 | { |
| 53 | if (n == 0) |
| 54 | return 1; |
| 55 | return n * fac (n - 1); |
| 56 | } |
| 57 | |
| 58 | int |
| 59 | main (int argc, char **argv) |
| 60 | { |
| 61 | unsigned int ovfl = 0, profcnt = 0; |
| 62 | struct timeval tv, start; |
| 63 | struct prof prof[32]; |
| 64 | double t_tick, delta; |
| 65 | long int sum = 0; |
| 66 | int i, j; |
| 67 | |
| 68 | for (i = 0; i < NELEMS (taddr); ++i) |
| 69 | { |
| 70 | prof[profcnt].pr_base = buf[i]; |
| 71 | prof[profcnt].pr_size = sizeof (buf[i]); |
| 72 | prof[profcnt].pr_off = taddr[i]; |
| 73 | prof[profcnt].pr_scale = 0x10000; |
| 74 | ++profcnt; |
| 75 | } |
| 76 | |
| 77 | prof[profcnt].pr_base = blong[0]; |
| 78 | prof[profcnt].pr_size = sizeof (blong[0]); |
| 79 | prof[profcnt].pr_off = 0x80001000; |
| 80 | prof[profcnt].pr_scale = 0x10000; |
| 81 | ++profcnt; |
| 82 | |
| 83 | prof[profcnt].pr_base = bshort[0]; |
| 84 | prof[profcnt].pr_size = sizeof (bshort[0]); |
| 85 | prof[profcnt].pr_off = 0x80000080; |
| 86 | prof[profcnt].pr_scale = 0x10000; |
| 87 | ++profcnt; |
| 88 | |
| 89 | prof[profcnt].pr_base = bshort[1]; |
| 90 | prof[profcnt].pr_size = sizeof (bshort[1]); |
| 91 | prof[profcnt].pr_off = 0x80000f80; |
| 92 | prof[profcnt].pr_scale = 0x10000; |
| 93 | ++profcnt; |
| 94 | |
| 95 | prof[profcnt].pr_base = bshort[2]; |
| 96 | prof[profcnt].pr_size = sizeof (bshort[2]); |
| 97 | prof[profcnt].pr_off = 0x80001080; |
| 98 | prof[profcnt].pr_scale = 0x10000; |
| 99 | ++profcnt; |
| 100 | |
| 101 | prof[profcnt].pr_base = bshort[3]; |
| 102 | prof[profcnt].pr_size = sizeof (bshort[3]); |
| 103 | prof[profcnt].pr_off = 0x80001f80; |
| 104 | prof[profcnt].pr_scale = 0x10000; |
| 105 | ++profcnt; |
| 106 | |
| 107 | prof[profcnt].pr_base = bshort[4]; |
| 108 | prof[profcnt].pr_size = sizeof (bshort[4]); |
| 109 | prof[profcnt].pr_off = 0x80002080; |
| 110 | prof[profcnt].pr_scale = 0x10000; |
| 111 | ++profcnt; |
| 112 | |
| 113 | prof[profcnt].pr_base = vlong[0]; |
| 114 | prof[profcnt].pr_size = sizeof (vlong[0]); |
| 115 | prof[profcnt].pr_off = 0x80000080; |
| 116 | prof[profcnt].pr_scale = 0x10000; |
| 117 | ++profcnt; |
| 118 | |
| 119 | /* Set up overflow counter (must be last on Irix). */ |
| 120 | prof[profcnt].pr_base = &ovfl; |
| 121 | prof[profcnt].pr_size = sizeof (ovfl); |
| 122 | prof[profcnt].pr_off = 0; |
| 123 | prof[profcnt].pr_scale = 2; |
| 124 | ++profcnt; |
| 125 | |
| 126 | /* Turn it on. */ |
| 127 | if (sprofil (prof, profcnt, &tv, PROF_UINT) < 0) |
| 128 | { |
| 129 | if (errno == ENOSYS) |
| 130 | exit (0); |
| 131 | perror ("sprofil"); |
| 132 | exit (1); |
| 133 | } |
| 134 | |
| 135 | t_tick = tv.tv_sec + 1e-6 * tv.tv_usec; |
| 136 | printf ("profiling period = %g ms\n", 1e3 * t_tick); |
| 137 | |
| 138 | gettimeofday (&start, NULL); |
| 139 | do |
| 140 | { |
| 141 | for (i = 0; i < 21; ++i) |
| 142 | sum += fac (i); |
| 143 | |
| 144 | gettimeofday (&tv, NULL); |
| 145 | timersub (&tv, &start, &tv); |
| 146 | delta = tv.tv_sec + 1e-6 * tv.tv_usec; |
| 147 | } |
| 148 | while (delta < 1000 * t_tick); |
| 149 | |
| 150 | printf ("sum = 0x%lx\n", sum); |
| 151 | |
| 152 | /* Turn it off. */ |
| 153 | if (sprofil (NULL, 0, NULL, 0) < 0) |
| 154 | { |
| 155 | if (errno == ENOSYS) |
| 156 | exit (0); |
| 157 | perror ("sprofil"); |
| 158 | exit (1); |
| 159 | } |
| 160 | |
| 161 | printf ("overflow = %u\n", ovfl); |
| 162 | for (i = 0; i < NELEMS (taddr); ++i) |
| 163 | for (j = 0; j < 0x10000 / sizeof (int); ++j) |
| 164 | if (buf[i][j] != 0) |
| 165 | printf ("%0*Zx\t%u\t(buffer %d)\n", |
| 166 | (int) (sizeof (size_t) * 2), |
| 167 | (taddr[i] + ((char *) &buf[i][j] - (char *) &buf[i][0])), |
| 168 | buf[i][j], i); |
| 169 | |
| 170 | return 0; |
| 171 | } |