Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / posix / sprofil.c
blobabd59d5d0a5d8d29d4fbe4c763d05b74913c98e1
1 /* Copyright (C) 2001-2015 Free Software Foundation, Inc.
2 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <assert.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <sys/time.h>
26 #include <sys/profil.h>
28 #ifndef SIGPROF
29 # include <gmon/sprofil.c>
30 #else
32 #include <libc-internal.h>
34 struct region
36 size_t offset;
37 size_t nsamples;
38 unsigned int scale;
39 union
41 void *vp;
42 unsigned short *us;
43 unsigned int *ui;
45 sample;
46 size_t start;
47 size_t end;
50 struct prof_info
52 unsigned int num_regions;
53 struct region *region;
54 struct region *last, *overflow;
55 struct itimerval saved_timer;
56 struct sigaction saved_action;
59 static unsigned int overflow_counter;
61 static struct region default_overflow_region =
63 .offset = 0,
64 .nsamples = 1,
65 .scale = 2,
66 .sample = { &overflow_counter },
67 .start = 0,
68 .end = ~(size_t) 0
71 static struct prof_info prof_info;
73 static unsigned long int
74 pc_to_index (size_t pc, size_t offset, unsigned int scale, int prof_uint)
76 size_t i = (pc - offset) / (prof_uint ? sizeof (int) : sizeof (short));
78 if (sizeof (unsigned long long int) > sizeof (size_t))
79 return (unsigned long long int) i * scale / 65536;
80 else
81 return i / 65536 * scale + i % 65536 * scale / 65536;
84 static inline size_t
85 index_to_pc (unsigned long int n, size_t offset, unsigned int scale,
86 int prof_uint)
88 size_t pc, bin_size = (prof_uint ? sizeof (int) : sizeof (short));
90 if (sizeof (unsigned long long int) > sizeof (size_t))
91 pc = offset + (unsigned long long int) n * bin_size * 65536ull / scale;
92 else
93 pc = (offset + n * bin_size / scale * 65536
94 + n * bin_size % scale * 65536 / scale);
96 if (pc_to_index (pc, offset, scale, prof_uint) < n)
97 /* Adjust for rounding error. */
98 ++pc;
100 assert (pc_to_index (pc - 1, offset, scale, prof_uint) < n
101 && pc_to_index (pc, offset, scale, prof_uint) >= n);
103 return pc;
106 static void
107 profil_count (void *pcp, int prof_uint)
109 struct region *region, *r = prof_info.last;
110 size_t lo, hi, mid, pc = (unsigned long int) pcp;
111 unsigned long int i;
113 /* Fast path: pc is in same region as before. */
114 if (pc >= r->start && pc < r->end)
115 region = r;
116 else
118 /* Slow path: do a binary search for the right region. */
119 lo = 0; hi = prof_info.num_regions - 1;
120 while (lo <= hi)
122 mid = (lo + hi) / 2;
124 r = prof_info.region + mid;
125 if (pc >= r->start && pc < r->end)
127 prof_info.last = r;
128 region = r;
129 break;
132 if (pc < r->start)
133 hi = mid - 1;
134 else
135 lo = mid + 1;
138 /* No matching region: increment overflow count. There is no point
139 in updating the cache here, as it won't hit anyhow. */
140 region = prof_info.overflow;
143 i = pc_to_index (pc, region->offset, region->scale, prof_uint);
144 if (i < r->nsamples)
146 if (prof_uint)
148 if (r->sample.ui[i] < (unsigned int) ~0)
149 ++r->sample.ui[i];
151 else
153 if (r->sample.us[i] < (unsigned short) ~0)
154 ++r->sample.us[i];
157 else
159 if (prof_uint)
160 ++prof_info.overflow->sample.ui[0];
161 else
162 ++prof_info.overflow->sample.us[0];
166 static inline void
167 profil_count_ushort (void *pcp)
169 profil_count (pcp, 0);
172 static inline void
173 profil_count_uint (void *pcp)
175 profil_count (pcp, 1);
178 /* Get the machine-dependent definition of `__profil_counter', the signal
179 handler for SIGPROF. It calls `profil_count' (above) with the PC of the
180 interrupted code. */
181 #define __profil_counter __profil_counter_ushort
182 #define profil_count(pc) profil_count (pc, 0)
183 #include <profil-counter.h>
185 #undef __profil_counter
186 #undef profil_count
188 #define __profil_counter __profil_counter_uint
189 #define profil_count(pc) profil_count (pc, 1)
190 #include <profil-counter.h>
192 static int
193 insert (int i, unsigned long int start, unsigned long int end, struct prof *p,
194 int prof_uint)
196 struct region *r;
197 size_t to_copy;
199 if (start >= end)
200 return 0; /* don't bother with empty regions */
202 if (prof_info.num_regions == 0)
203 r = malloc (sizeof (*r));
204 else
205 r = realloc (prof_info.region, (prof_info.num_regions + 1) * sizeof (*r));
206 if (r == NULL)
207 return -1;
209 to_copy = prof_info.num_regions - i;
210 if (to_copy > 0)
211 memmove (r + i + 1, r + i, to_copy * sizeof (*r));
213 r[i].offset = p->pr_off;
214 r[i].nsamples = p->pr_size / (prof_uint ? sizeof (int) : sizeof (short));
215 r[i].scale = p->pr_scale;
216 r[i].sample.vp = p->pr_base;
217 r[i].start = start;
218 r[i].end = end;
220 prof_info.region = r;
221 ++prof_info.num_regions;
223 if (p->pr_off == 0 && p->pr_scale == 2)
224 prof_info.overflow = r;
226 return 0;
229 /* Add a new profiling region. If the new region overlaps with
230 existing ones, this may add multiple subregions so that the final
231 data structure is free of overlaps. The absence of overlaps makes
232 it possible to use a binary search in profil_count(). Note that
233 this function depends on new regions being presented in DECREASING
234 ORDER of starting address. */
236 static int
237 add_region (struct prof *p, int prof_uint)
239 unsigned long int nsamples;
240 size_t start, end;
241 unsigned int i;
243 if (p->pr_scale < 2)
244 return 0;
246 nsamples = p->pr_size / (prof_uint ? sizeof (int) : sizeof (short));
248 start = p->pr_off;
249 end = index_to_pc (nsamples, p->pr_off, p->pr_scale, prof_uint);
251 /* Merge with existing regions. */
252 for (i = 0; i < prof_info.num_regions; ++i)
254 if (start < prof_info.region[i].start)
256 if (end < prof_info.region[i].start)
257 break;
258 else if (insert (i, start, prof_info.region[i].start, p, prof_uint)
259 < 0)
260 return -1;
262 start = prof_info.region[i].end;
264 return insert (i, start, end, p, prof_uint);
267 static int
268 pcmp (const void *left, const void *right)
270 struct prof *l = *(struct prof **) left;
271 struct prof *r = *(struct prof **) right;
273 if (l->pr_off < r->pr_off)
274 return 1;
275 else if (l->pr_off > r->pr_off)
276 return -1;
277 return 0;
281 __sprofil (struct prof *profp, int profcnt, struct timeval *tvp,
282 unsigned int flags)
284 struct prof *p[profcnt];
285 struct itimerval timer;
286 struct sigaction act;
287 int i;
289 if (tvp != NULL)
291 /* Return profiling period. */
292 unsigned long int t = 1000000 / __profile_frequency ();
293 tvp->tv_sec = t / 1000000;
294 tvp->tv_usec = t % 1000000;
297 if (prof_info.num_regions > 0)
299 /* Disable profiling. */
300 if (__setitimer (ITIMER_PROF, &prof_info.saved_timer, NULL) < 0)
301 return -1;
303 if (__sigaction (SIGPROF, &prof_info.saved_action, NULL) < 0)
304 return -1;
306 free (prof_info.region);
307 return 0;
310 prof_info.num_regions = 0;
311 prof_info.region = NULL;
312 prof_info.overflow = &default_overflow_region;
314 for (i = 0; i < profcnt; ++i)
315 p[i] = profp + i;
317 /* Sort in order of decreasing starting address: */
318 qsort (p, profcnt, sizeof (p[0]), pcmp);
320 /* Add regions in order of decreasing starting address: */
321 for (i = 0; i < profcnt; ++i)
322 if (add_region (p[i], (flags & PROF_UINT) != 0) < 0)
324 free (prof_info.region);
325 prof_info.num_regions = 0;
326 prof_info.region = NULL;
327 return -1;
330 if (prof_info.num_regions == 0)
331 return 0;
333 prof_info.last = prof_info.region;
335 /* Install SIGPROF handler. */
336 if (flags & PROF_UINT)
337 act.sa_handler = (sighandler_t) &__profil_counter_uint;
338 else
339 act.sa_handler = (sighandler_t) &__profil_counter_ushort;
340 act.sa_flags = SA_RESTART;
341 __sigfillset (&act.sa_mask);
342 if (__sigaction (SIGPROF, &act, &prof_info.saved_action) < 0)
343 return -1;
345 /* Setup profiling timer. */
346 timer.it_value.tv_sec = 0;
347 timer.it_value.tv_usec = 1;
348 timer.it_interval = timer.it_value;
349 return __setitimer (ITIMER_PROF, &timer, &prof_info.saved_timer);
352 weak_alias (__sprofil, sprofil)
354 #endif /* SIGPROF */