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