]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-profile.c
Update.
[thirdparty/glibc.git] / elf / dl-profile.c
1 /* Profiling of shared libraries.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 Based on the BSD mcount implementation.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <ldsodefs.h>
31 #include <sys/gmon.h>
32 #include <sys/gmon_out.h>
33 #include <sys/mman.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <atomicity.h>
37
38 /* The LD_PROFILE feature has to be implemented different to the
39 normal profiling using the gmon/ functions. The problem is that an
40 arbitrary amount of processes simulataneously can be run using
41 profiling and all write the results in the same file. To provide
42 this mechanism one could implement a complicated mechanism to merge
43 the content of two profiling runs or one could extend the file
44 format to allow more than one data set. For the second solution we
45 would have the problem that the file can grow in size beyond any
46 limit and both solutions have the problem that the concurrency of
47 writing the results is a big problem.
48
49 Another much simpler method is to use mmap to map the same file in
50 all using programs and modify the data in the mmap'ed area and so
51 also automatically on the disk. Using the MAP_SHARED option of
52 mmap(2) this can be done without big problems in more than one
53 file.
54
55 This approach is very different from the normal profiling. We have
56 to use the profiling data in exactly the way they are expected to
57 be written to disk. But the normal format used by gprof is not usable
58 to do this. It is optimized for size. It writes the tags as single
59 bytes but this means that the following 32/64 bit values are
60 unaligned.
61
62 Therefore we use a new format. This will look like this
63
64 0 1 2 3 <- byte is 32 bit word
65 0000 g m o n
66 0004 *version* <- GMON_SHOBJ_VERSION
67 0008 00 00 00 00
68 000c 00 00 00 00
69 0010 00 00 00 00
70
71 0014 *tag* <- GMON_TAG_TIME_HIST
72 0018 ?? ?? ?? ??
73 ?? ?? ?? ?? <- 32/64 bit LowPC
74 0018+A ?? ?? ?? ??
75 ?? ?? ?? ?? <- 32/64 bit HighPC
76 0018+2*A *histsize*
77 001c+2*A *profrate*
78 0020+2*A s e c o
79 0024+2*A n d s \0
80 0028+2*A \0 \0 \0 \0
81 002c+2*A \0 \0 \0
82 002f+2*A s
83
84 0030+2*A ?? ?? ?? ?? <- Count data
85 ... ...
86 0030+2*A+K ?? ?? ?? ??
87
88 0030+2*A+K *tag* <- GMON_TAG_CG_ARC
89 0034+2*A+K *lastused*
90 0038+2*A+K ?? ?? ?? ??
91 ?? ?? ?? ?? <- FromPC#1
92 0038+3*A+K ?? ?? ?? ??
93 ?? ?? ?? ?? <- ToPC#1
94 0038+4*A+K ?? ?? ?? ?? <- Count#1
95 ... ... ...
96 0038+(2*(CN-1)+2)*A+(CN-1)*4+K ?? ?? ?? ??
97 ?? ?? ?? ?? <- FromPC#CGN
98 0038+(2*(CN-1)+3)*A+(CN-1)*4+K ?? ?? ?? ??
99 ?? ?? ?? ?? <- ToPC#CGN
100 0038+(2*CN+2)*A+(CN-1)*4+K ?? ?? ?? ?? <- Count#CGN
101
102 We put (for now?) no basic block information in the file since this would
103 introduce rase conditions among all the processes who want to write them.
104
105 `K' is the number of count entries which is computed as
106
107 textsize / HISTFRACTION
108
109 `CG' in the above table is the number of call graph arcs. Normally,
110 the table is sparse and the profiling code writes out only the those
111 entries which are really used in the program run. But since we must
112 not extend this table (the profiling file) we'll keep them all here.
113 So CN can be executed in advance as
114
115 MINARCS <= textsize*(ARCDENSITY/100) <= MAXARCS
116
117 Now the remaining question is: how to build the data structures we can
118 work with from this data. We need the from set and must associate the
119 froms with all the associated tos. We will do this by constructing this
120 data structures at the program start. To do this we'll simply visit all
121 entries in the call graph table and add it to the appropriate list. */
122
123 extern int __profile_frequency (void);
124
125 /* We define a special type to address the elements of the arc table.
126 This is basically the `gmon_cg_arc_record' format but it includes
127 the room for the tag and it uses real types. */
128 struct here_cg_arc_record
129 {
130 uintptr_t from_pc;
131 uintptr_t self_pc;
132 uint32_t count;
133 } __attribute__ ((packed));
134
135 static struct here_cg_arc_record *data;
136
137 /* Nonzero if profiling is under way. */
138 static int running;
139
140 /* This is the number of entry which have been incorporated in the toset. */
141 static uint32_t narcs;
142 /* This is a pointer to the object representing the number of entries
143 currently in the mmaped file. At no point of time this has to be the
144 same as NARCS. If it is equal all entries from the file are in our
145 lists. */
146 static volatile uint32_t *narcsp;
147
148 static volatile uint16_t *kcount;
149 static size_t kcountsize;
150
151 struct here_fromstruct
152 {
153 struct here_cg_arc_record volatile *here;
154 uint16_t link;
155 };
156
157 static volatile uint16_t *tos;
158
159 static struct here_fromstruct *froms;
160 static uint32_t fromlimit;
161 static volatile uint32_t fromidx;
162
163 static uintptr_t lowpc;
164 static size_t textsize;
165 static unsigned int hashfraction;
166 static unsigned int log_hashfraction;
167
168
169 \f
170 /* Set up profiling data to profile object desribed by MAP. The output
171 file is found (or created) in OUTPUT_DIR. */
172 void
173 internal_function
174 _dl_start_profile (struct link_map *map, const char *output_dir)
175 {
176 char *filename;
177 int fd;
178 struct stat64 st;
179 const ElfW(Phdr) *ph;
180 ElfW(Addr) mapstart = ~((ElfW(Addr)) 0);
181 ElfW(Addr) mapend = 0;
182 struct gmon_hdr gmon_hdr;
183 struct gmon_hist_hdr hist_hdr;
184 char *hist, *cp;
185 size_t idx;
186 size_t tossize;
187 size_t fromssize;
188 uintptr_t highpc;
189 struct gmon_hdr *addr = NULL;
190 off_t expected_size;
191 /* See profil(2) where this is described. */
192 int s_scale;
193 #define SCALE_1_TO_1 0x10000L
194
195 /* Compute the size of the sections which contain program code. */
196 for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph)
197 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_X))
198 {
199 ElfW(Addr) start = (ph->p_vaddr & ~(GL(dl_pagesize) - 1));
200 ElfW(Addr) end = ((ph->p_vaddr + ph->p_memsz + GL(dl_pagesize) - 1)
201 & ~(GL(dl_pagesize) - 1));
202
203 if (start < mapstart)
204 mapstart = start;
205 if (end > mapend)
206 mapend = end;
207 }
208
209 /* Now we can compute the size of the profiling data. This is done
210 with the same formulars as in `monstartup' (see gmon.c). */
211 running = 0;
212 lowpc = ROUNDDOWN (mapstart + map->l_addr,
213 HISTFRACTION * sizeof (HISTCOUNTER));
214 highpc = ROUNDUP (mapend + map->l_addr,
215 HISTFRACTION * sizeof (HISTCOUNTER));
216 textsize = highpc - lowpc;
217 kcountsize = textsize / HISTFRACTION;
218 hashfraction = HASHFRACTION;
219 if ((HASHFRACTION & (HASHFRACTION - 1)) == 0)
220 /* If HASHFRACTION is a power of two, mcount can use shifting
221 instead of integer division. Precompute shift amount. */
222 log_hashfraction = __ffs (hashfraction * sizeof (*froms)) - 1;
223 else
224 log_hashfraction = -1;
225 tossize = textsize / HASHFRACTION;
226 fromlimit = textsize * ARCDENSITY / 100;
227 if (fromlimit < MINARCS)
228 fromlimit = MINARCS;
229 if (fromlimit > MAXARCS)
230 fromlimit = MAXARCS;
231 fromssize = fromlimit * sizeof (struct here_fromstruct);
232
233 expected_size = (sizeof (struct gmon_hdr)
234 + 4 + sizeof (struct gmon_hist_hdr) + kcountsize
235 + 4 + 4 + fromssize * sizeof (struct here_cg_arc_record));
236
237 /* Create the gmon_hdr we expect or write. */
238 memset (&gmon_hdr, '\0', sizeof (struct gmon_hdr));
239 memcpy (&gmon_hdr.cookie[0], GMON_MAGIC, sizeof (gmon_hdr.cookie));
240 *(int32_t *) gmon_hdr.version = GMON_SHOBJ_VERSION;
241
242 /* Create the hist_hdr we expect or write. */
243 *(char **) hist_hdr.low_pc = (char *) mapstart;
244 *(char **) hist_hdr.high_pc = (char *) mapend;
245 *(int32_t *) hist_hdr.hist_size = kcountsize / sizeof (HISTCOUNTER);
246 *(int32_t *) hist_hdr.prof_rate = __profile_frequency ();
247 if (sizeof (hist_hdr.dimen) >= sizeof ("seconds"))
248 memcpy (hist_hdr.dimen, "seconds", sizeof ("seconds"));
249 else
250 strncpy (hist_hdr.dimen, "seconds", sizeof (hist_hdr.dimen));
251 hist_hdr.dimen_abbrev = 's';
252
253 /* First determine the output name. We write in the directory
254 OUTPUT_DIR and the name is composed from the shared objects
255 soname (or the file name) and the ending ".profile". */
256 filename = (char *) alloca (strlen (output_dir) + 1 + strlen (GL(dl_profile))
257 + sizeof ".profile");
258 cp = __stpcpy (filename, output_dir);
259 *cp++ = '/';
260 __stpcpy (__stpcpy (cp, GL(dl_profile)), ".profile");
261
262 #ifdef O_NOFOLLOW
263 # define EXTRA_FLAGS | O_NOFOLLOW
264 #else
265 # define EXTRA_FLAGS
266 #endif
267 fd = __open (filename, O_RDWR | O_CREAT EXTRA_FLAGS, DEFFILEMODE);
268 if (fd == -1)
269 {
270 /* We cannot write the profiling data so don't do anything. */
271 char buf[400];
272 _dl_error_printf ("%s: cannot open file: %s\n", filename,
273 __strerror_r (errno, buf, sizeof buf));
274 return;
275 }
276
277 if (__fxstat64 (_STAT_VER, fd, &st) < 0 || !S_ISREG (st.st_mode))
278 {
279 /* Not stat'able or not a regular file => don't use it. */
280 char buf[400];
281 int errnum = errno;
282 __close (fd);
283 _dl_error_printf ("%s: cannot stat file: %s\n", filename,
284 __strerror_r (errnum, buf, sizeof buf));
285 return;
286 }
287
288 /* Test the size. If it does not match what we expect from the size
289 values in the map MAP we don't use it and warn the user. */
290 if (st.st_size == 0)
291 {
292 /* We have to create the file. */
293 char buf[GL(dl_pagesize)];
294
295 memset (buf, '\0', GL(dl_pagesize));
296
297 if (__lseek (fd, expected_size & ~(GL(dl_pagesize) - 1), SEEK_SET) == -1)
298 {
299 char buf[400];
300 int errnum;
301 cannot_create:
302 errnum = errno;
303 __close (fd);
304 _dl_error_printf ("%s: cannot create file: %s\n", filename,
305 __strerror_r (errnum, buf, sizeof buf));
306 return;
307 }
308
309 if (TEMP_FAILURE_RETRY (__libc_write (fd, buf, (expected_size
310 & (GL(dl_pagesize)
311 - 1))))
312 < 0)
313 goto cannot_create;
314 }
315 else if (st.st_size != expected_size)
316 {
317 __close (fd);
318 wrong_format:
319
320 if (addr != NULL)
321 __munmap ((void *) addr, expected_size);
322
323 _dl_error_printf ("%s: file is no correct profile data file for `%s'\n",
324 filename, GL(dl_profile));
325 return;
326 }
327
328 addr = (struct gmon_hdr *) __mmap (NULL, expected_size, PROT_READ|PROT_WRITE,
329 MAP_SHARED|MAP_FILE, fd, 0);
330 if (addr == (struct gmon_hdr *) MAP_FAILED)
331 {
332 char buf[400];
333 int errnum = errno;
334 __close (fd);
335 _dl_error_printf ("%s: cannot map file: %s\n", filename,
336 __strerror_r (errnum, buf, sizeof buf));
337 return;
338 }
339
340 /* We don't need the file desriptor anymore. */
341 __close (fd);
342
343 /* Pointer to data after the header. */
344 hist = (char *) (addr + 1);
345 kcount = (uint16_t *) ((char *) hist + sizeof (uint32_t)
346 + sizeof (struct gmon_hist_hdr));
347
348 /* Compute pointer to array of the arc information. */
349 narcsp = (uint32_t *) ((char *) kcount + kcountsize + sizeof (uint32_t));
350 data = (struct here_cg_arc_record *) ((char *) narcsp + sizeof (uint32_t));
351
352 if (st.st_size == 0)
353 {
354 /* Create the signature. */
355 memcpy (addr, &gmon_hdr, sizeof (struct gmon_hdr));
356
357 *(uint32_t *) hist = GMON_TAG_TIME_HIST;
358 memcpy (hist + sizeof (uint32_t), &hist_hdr,
359 sizeof (struct gmon_hist_hdr));
360
361 narcsp[-1] = GMON_TAG_CG_ARC;
362 }
363 else
364 {
365 /* Test the signature in the file. */
366 if (memcmp (addr, &gmon_hdr, sizeof (struct gmon_hdr)) != 0
367 || *(uint32_t *) hist != GMON_TAG_TIME_HIST
368 || memcmp (hist + sizeof (uint32_t), &hist_hdr,
369 sizeof (struct gmon_hist_hdr)) != 0
370 || narcsp[-1] != GMON_TAG_CG_ARC)
371 goto wrong_format;
372 }
373
374 /* Allocate memory for the froms data and the pointer to the tos records. */
375 tos = (uint16_t *) calloc (tossize + fromssize, 1);
376 if (tos == NULL)
377 {
378 __munmap ((void *) addr, expected_size);
379 _dl_fatal_printf ("Out of memory while initializing profiler\n");
380 /* NOTREACHED */
381 }
382
383 froms = (struct here_fromstruct *) ((char *) tos + tossize);
384 fromidx = 0;
385
386 /* Now we have to process all the arc count entries. BTW: it is
387 not critical whether the *NARCSP value changes meanwhile. Before
388 we enter a new entry in to toset we will check that everything is
389 available in TOS. This happens in _dl_mcount.
390
391 Loading the entries in reverse order should help to get the most
392 frequently used entries at the front of the list. */
393 for (idx = narcs = MIN (*narcsp, fromlimit); idx > 0; )
394 {
395 size_t to_index;
396 size_t newfromidx;
397 --idx;
398 to_index = (data[idx].self_pc / (hashfraction * sizeof (*tos)));
399 newfromidx = fromidx++;
400 froms[newfromidx].here = &data[idx];
401 froms[newfromidx].link = tos[to_index];
402 tos[to_index] = newfromidx;
403 }
404
405 /* Setup counting data. */
406 if (kcountsize < highpc - lowpc)
407 {
408 #if 0
409 s_scale = ((double) kcountsize / (highpc - lowpc)) * SCALE_1_TO_1;
410 #else
411 size_t range = highpc - lowpc;
412 size_t quot = range / kcountsize;
413
414 if (quot >= SCALE_1_TO_1)
415 s_scale = 1;
416 else if (quot >= SCALE_1_TO_1 / 256)
417 s_scale = SCALE_1_TO_1 / quot;
418 else if (range > ULONG_MAX / 256)
419 s_scale = (SCALE_1_TO_1 * 256) / (range / (kcountsize / 256));
420 else
421 s_scale = (SCALE_1_TO_1 * 256) / ((range * 256) / kcountsize);
422 #endif
423 }
424 else
425 s_scale = SCALE_1_TO_1;
426
427 /* Start the profiler. */
428 __profil ((void *) kcount, kcountsize, lowpc, s_scale);
429
430 /* Turn on profiling. */
431 running = 1;
432 }
433
434
435 void
436 _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc)
437 {
438 volatile uint16_t *topcindex;
439 size_t i, fromindex;
440 struct here_fromstruct *fromp;
441
442 if (! running)
443 return;
444
445 /* Compute relative addresses. The shared object can be loaded at
446 any address. The value of frompc could be anything. We cannot
447 restrict it in any way, just set to a fixed value (0) in case it
448 is outside the allowed range. These calls show up as calls from
449 <external> in the gprof output. */
450 frompc -= lowpc;
451 if (frompc >= textsize)
452 frompc = 0;
453 selfpc -= lowpc;
454 if (selfpc >= textsize)
455 goto done;
456
457 /* Getting here we now have to find out whether the location was
458 already used. If yes we are lucky and only have to increment a
459 counter (this also has to be atomic). If the entry is new things
460 are getting complicated... */
461
462 /* Avoid integer divide if possible. */
463 if ((HASHFRACTION & (HASHFRACTION - 1)) == 0)
464 i = selfpc >> log_hashfraction;
465 else
466 i = selfpc / (hashfraction * sizeof (*tos));
467
468 topcindex = &tos[i];
469 fromindex = *topcindex;
470
471 if (fromindex == 0)
472 goto check_new_or_add;
473
474 fromp = &froms[fromindex];
475
476 /* We have to look through the chain of arcs whether there is already
477 an entry for our arc. */
478 while (fromp->here->from_pc != frompc)
479 {
480 if (fromp->link != 0)
481 do
482 fromp = &froms[fromp->link];
483 while (fromp->link != 0 && fromp->here->from_pc != frompc);
484
485 if (fromp->here->from_pc != frompc)
486 {
487 topcindex = &fromp->link;
488
489 check_new_or_add:
490 /* Our entry is not among the entries we read so far from the
491 data file. Now see whether we have to update the list. */
492 while (narcs != *narcsp && narcs < fromlimit)
493 {
494 size_t to_index;
495 size_t newfromidx;
496 to_index = (data[narcs].self_pc
497 / (hashfraction * sizeof (*tos)));
498 newfromidx = exchange_and_add (&fromidx, 1) + 1;
499 froms[newfromidx].here = &data[narcs];
500 froms[newfromidx].link = tos[to_index];
501 tos[to_index] = newfromidx;
502 atomic_add (&narcs, 1);
503 }
504
505 /* If we still have no entry stop searching and insert. */
506 if (*topcindex == 0)
507 {
508 uint_fast32_t newarc = exchange_and_add (narcsp, 1);
509
510 /* In rare cases it could happen that all entries in FROMS are
511 occupied. So we cannot count this anymore. */
512 if (newarc >= fromlimit)
513 goto done;
514
515 *topcindex = exchange_and_add (&fromidx, 1) + 1;
516 fromp = &froms[*topcindex];
517
518 fromp->here = &data[newarc];
519 data[newarc].from_pc = frompc;
520 data[newarc].self_pc = selfpc;
521 data[newarc].count = 0;
522 fromp->link = 0;
523 atomic_add (&narcs, 1);
524
525 break;
526 }
527
528 fromp = &froms[*topcindex];
529 }
530 else
531 /* Found in. */
532 break;
533 }
534
535 /* Increment the counter. */
536 atomic_add (&fromp->here->count, 1);
537
538 done:
539 ;
540 }