* ChangeLog: Fix whitespace.
[gcc.git] / gcc / gcov-io.c
1 /* File format for coverage information
2 Copyright (C) 1996-2014 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 /* Routines declared in gcov-io.h. This file should be #included by
28 another source file, after having #included gcov-io.h. */
29
30 #if !IN_GCOV
31 static void gcov_write_block (unsigned);
32 static gcov_unsigned_t *gcov_write_words (unsigned);
33 #endif
34 static const gcov_unsigned_t *gcov_read_words (unsigned);
35 #if !IN_LIBGCOV
36 static void gcov_allocate (unsigned);
37 #endif
38
39 /* Optimum number of gcov_unsigned_t's read from or written to disk. */
40 #define GCOV_BLOCK_SIZE (1 << 10)
41
42 GCOV_LINKAGE struct gcov_var
43 {
44 FILE *file;
45 gcov_position_t start; /* Position of first byte of block */
46 unsigned offset; /* Read/write position within the block. */
47 unsigned length; /* Read limit in the block. */
48 unsigned overread; /* Number of words overread. */
49 int error; /* < 0 overflow, > 0 disk error. */
50 int mode; /* < 0 writing, > 0 reading */
51 #if IN_LIBGCOV
52 /* Holds one block plus 4 bytes, thus all coverage reads & writes
53 fit within this buffer and we always can transfer GCOV_BLOCK_SIZE
54 to and from the disk. libgcov never backtracks and only writes 4
55 or 8 byte objects. */
56 gcov_unsigned_t buffer[GCOV_BLOCK_SIZE + 1];
57 #else
58 int endian; /* Swap endianness. */
59 /* Holds a variable length block, as the compiler can write
60 strings and needs to backtrack. */
61 size_t alloc;
62 gcov_unsigned_t *buffer;
63 #endif
64 } gcov_var;
65
66 /* Save the current position in the gcov file. */
67 static inline gcov_position_t
68 gcov_position (void)
69 {
70 gcov_nonruntime_assert (gcov_var.mode > 0);
71 return gcov_var.start + gcov_var.offset;
72 }
73
74 /* Return nonzero if the error flag is set. */
75 static inline int
76 gcov_is_error (void)
77 {
78 return gcov_var.file ? gcov_var.error : 1;
79 }
80
81 #if IN_LIBGCOV
82 /* Move to beginning of file and initialize for writing. */
83 GCOV_LINKAGE inline void
84 gcov_rewrite (void)
85 {
86 gcov_var.mode = -1;
87 gcov_var.start = 0;
88 gcov_var.offset = 0;
89 fseek (gcov_var.file, 0L, SEEK_SET);
90 }
91 #endif
92
93 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
94 {
95 #if !IN_LIBGCOV
96 if (gcov_var.endian)
97 {
98 value = (value >> 16) | (value << 16);
99 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
100 }
101 #endif
102 return value;
103 }
104
105 /* Open a gcov file. NAME is the name of the file to open and MODE
106 indicates whether a new file should be created, or an existing file
107 opened. If MODE is >= 0 an existing file will be opened, if
108 possible, and if MODE is <= 0, a new file will be created. Use
109 MODE=0 to attempt to reopen an existing file and then fall back on
110 creating a new one. If MODE < 0, the file will be opened in
111 read-only mode. Otherwise it will be opened for modification.
112 Return zero on failure, >0 on opening an existing file and <0 on
113 creating a new one. */
114
115 GCOV_LINKAGE int
116 #if IN_LIBGCOV
117 gcov_open (const char *name)
118 #else
119 gcov_open (const char *name, int mode)
120 #endif
121 {
122 #if IN_LIBGCOV
123 const int mode = 0;
124 #endif
125 #if GCOV_LOCKED
126 struct flock s_flock;
127 int fd;
128
129 s_flock.l_whence = SEEK_SET;
130 s_flock.l_start = 0;
131 s_flock.l_len = 0; /* Until EOF. */
132 s_flock.l_pid = getpid ();
133 #endif
134
135 gcov_nonruntime_assert (!gcov_var.file);
136 gcov_var.start = 0;
137 gcov_var.offset = gcov_var.length = 0;
138 gcov_var.overread = -1u;
139 gcov_var.error = 0;
140 #if !IN_LIBGCOV
141 gcov_var.endian = 0;
142 #endif
143 #if GCOV_LOCKED
144 if (mode > 0)
145 {
146 /* Read-only mode - acquire a read-lock. */
147 s_flock.l_type = F_RDLCK;
148 /* pass mode (ignored) for compatibility */
149 fd = open (name, O_RDONLY, S_IRUSR | S_IWUSR);
150 }
151 else if (mode < 0)
152 {
153 /* Write mode - acquire a write-lock. */
154 s_flock.l_type = F_WRLCK;
155 fd = open (name, O_RDWR | O_CREAT | O_TRUNC, 0666);
156 }
157 else /* mode == 0 */
158 {
159 /* Read-Write mode - acquire a write-lock. */
160 s_flock.l_type = F_WRLCK;
161 fd = open (name, O_RDWR | O_CREAT, 0666);
162 }
163 if (fd < 0)
164 return 0;
165
166 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
167 continue;
168
169 gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
170
171 if (!gcov_var.file)
172 {
173 close (fd);
174 return 0;
175 }
176
177 if (mode > 0)
178 gcov_var.mode = 1;
179 else if (mode == 0)
180 {
181 struct stat st;
182
183 if (fstat (fd, &st) < 0)
184 {
185 fclose (gcov_var.file);
186 gcov_var.file = 0;
187 return 0;
188 }
189 if (st.st_size != 0)
190 gcov_var.mode = 1;
191 else
192 gcov_var.mode = mode * 2 + 1;
193 }
194 else
195 gcov_var.mode = mode * 2 + 1;
196 #else
197 if (mode >= 0)
198 gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
199
200 if (gcov_var.file)
201 gcov_var.mode = 1;
202 else if (mode <= 0)
203 {
204 gcov_var.file = fopen (name, "w+b");
205 if (gcov_var.file)
206 gcov_var.mode = mode * 2 + 1;
207 }
208 if (!gcov_var.file)
209 return 0;
210 #endif
211
212 setbuf (gcov_var.file, (char *)0);
213
214 return 1;
215 }
216
217 /* Close the current gcov file. Flushes data to disk. Returns nonzero
218 on failure or error flag set. */
219
220 GCOV_LINKAGE int
221 gcov_close (void)
222 {
223 if (gcov_var.file)
224 {
225 #if !IN_GCOV
226 if (gcov_var.offset && gcov_var.mode < 0)
227 gcov_write_block (gcov_var.offset);
228 #endif
229 fclose (gcov_var.file);
230 gcov_var.file = 0;
231 gcov_var.length = 0;
232 }
233 #if !IN_LIBGCOV
234 free (gcov_var.buffer);
235 gcov_var.alloc = 0;
236 gcov_var.buffer = 0;
237 #endif
238 gcov_var.mode = 0;
239 return gcov_var.error;
240 }
241
242 #if !IN_LIBGCOV
243 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
244 file. Returns +1 for same endian, -1 for other endian and zero for
245 not EXPECTED. */
246
247 GCOV_LINKAGE int
248 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
249 {
250 if (magic == expected)
251 return 1;
252 magic = (magic >> 16) | (magic << 16);
253 magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
254 if (magic == expected)
255 {
256 gcov_var.endian = 1;
257 return -1;
258 }
259 return 0;
260 }
261 #endif
262
263 #if !IN_LIBGCOV
264 static void
265 gcov_allocate (unsigned length)
266 {
267 size_t new_size = gcov_var.alloc;
268
269 if (!new_size)
270 new_size = GCOV_BLOCK_SIZE;
271 new_size += length;
272 new_size *= 2;
273
274 gcov_var.alloc = new_size;
275 gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
276 }
277 #endif
278
279 #if !IN_GCOV
280 /* Write out the current block, if needs be. */
281
282 static void
283 gcov_write_block (unsigned size)
284 {
285 if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
286 gcov_var.error = 1;
287 gcov_var.start += size;
288 gcov_var.offset -= size;
289 }
290
291 /* Allocate space to write BYTES bytes to the gcov file. Return a
292 pointer to those bytes, or NULL on failure. */
293
294 static gcov_unsigned_t *
295 gcov_write_words (unsigned words)
296 {
297 gcov_unsigned_t *result;
298
299 gcov_nonruntime_assert (gcov_var.mode < 0);
300 #if IN_LIBGCOV
301 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
302 {
303 gcov_write_block (GCOV_BLOCK_SIZE);
304 if (gcov_var.offset)
305 {
306 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
307 }
308 }
309 #else
310 if (gcov_var.offset + words > gcov_var.alloc)
311 gcov_allocate (gcov_var.offset + words);
312 #endif
313 result = &gcov_var.buffer[gcov_var.offset];
314 gcov_var.offset += words;
315
316 return result;
317 }
318
319 /* Write unsigned VALUE to coverage file. Sets error flag
320 appropriately. */
321
322 GCOV_LINKAGE void
323 gcov_write_unsigned (gcov_unsigned_t value)
324 {
325 gcov_unsigned_t *buffer = gcov_write_words (1);
326
327 buffer[0] = value;
328 }
329
330 /* Write counter VALUE to coverage file. Sets error flag
331 appropriately. */
332
333 #if IN_LIBGCOV
334 GCOV_LINKAGE void
335 gcov_write_counter (gcov_type value)
336 {
337 gcov_unsigned_t *buffer = gcov_write_words (2);
338
339 buffer[0] = (gcov_unsigned_t) value;
340 if (sizeof (value) > sizeof (gcov_unsigned_t))
341 buffer[1] = (gcov_unsigned_t) (value >> 32);
342 else
343 buffer[1] = 0;
344 }
345 #endif /* IN_LIBGCOV */
346
347 #if !IN_LIBGCOV
348 /* Write STRING to coverage file. Sets error flag on file
349 error, overflow flag on overflow */
350
351 GCOV_LINKAGE void
352 gcov_write_string (const char *string)
353 {
354 unsigned length = 0;
355 unsigned alloc = 0;
356 gcov_unsigned_t *buffer;
357
358 if (string)
359 {
360 length = strlen (string);
361 alloc = (length + 4) >> 2;
362 }
363
364 buffer = gcov_write_words (1 + alloc);
365
366 buffer[0] = alloc;
367 buffer[alloc] = 0;
368 memcpy (&buffer[1], string, length);
369 }
370 #endif
371
372 #if !IN_LIBGCOV
373 /* Write a tag TAG and reserve space for the record length. Return a
374 value to be used for gcov_write_length. */
375
376 GCOV_LINKAGE gcov_position_t
377 gcov_write_tag (gcov_unsigned_t tag)
378 {
379 gcov_position_t result = gcov_var.start + gcov_var.offset;
380 gcov_unsigned_t *buffer = gcov_write_words (2);
381
382 buffer[0] = tag;
383 buffer[1] = 0;
384
385 return result;
386 }
387
388 /* Write a record length using POSITION, which was returned by
389 gcov_write_tag. The current file position is the end of the
390 record, and is restored before returning. Returns nonzero on
391 overflow. */
392
393 GCOV_LINKAGE void
394 gcov_write_length (gcov_position_t position)
395 {
396 unsigned offset;
397 gcov_unsigned_t length;
398 gcov_unsigned_t *buffer;
399
400 gcov_nonruntime_assert (gcov_var.mode < 0);
401 gcov_nonruntime_assert (position + 2 <= gcov_var.start + gcov_var.offset);
402 gcov_nonruntime_assert (position >= gcov_var.start);
403 offset = position - gcov_var.start;
404 length = gcov_var.offset - offset - 2;
405 buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
406 buffer[1] = length;
407 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
408 gcov_write_block (gcov_var.offset);
409 }
410
411 #else /* IN_LIBGCOV */
412
413 /* Write a tag TAG and length LENGTH. */
414
415 GCOV_LINKAGE void
416 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
417 {
418 gcov_unsigned_t *buffer = gcov_write_words (2);
419
420 buffer[0] = tag;
421 buffer[1] = length;
422 }
423
424 /* Write a summary structure to the gcov file. Return nonzero on
425 overflow. */
426
427 GCOV_LINKAGE void
428 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
429 {
430 unsigned ix, h_ix, bv_ix, h_cnt = 0;
431 const struct gcov_ctr_summary *csum;
432 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE];
433
434 /* Count number of non-zero histogram entries, and fill in a bit vector
435 of non-zero indices. The histogram is only currently computed for arc
436 counters. */
437 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
438 histo_bitvector[bv_ix] = 0;
439 csum = &summary->ctrs[GCOV_COUNTER_ARCS];
440 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
441 {
442 if (csum->histogram[h_ix].num_counters > 0)
443 {
444 histo_bitvector[h_ix / 32] |= 1 << (h_ix % 32);
445 h_cnt++;
446 }
447 }
448 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH (h_cnt));
449 gcov_write_unsigned (summary->checksum);
450 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
451 {
452 gcov_write_unsigned (csum->num);
453 gcov_write_unsigned (csum->runs);
454 gcov_write_counter (csum->sum_all);
455 gcov_write_counter (csum->run_max);
456 gcov_write_counter (csum->sum_max);
457 if (ix != GCOV_COUNTER_ARCS)
458 {
459 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
460 gcov_write_unsigned (0);
461 continue;
462 }
463 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
464 gcov_write_unsigned (histo_bitvector[bv_ix]);
465 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
466 {
467 if (!csum->histogram[h_ix].num_counters)
468 continue;
469 gcov_write_unsigned (csum->histogram[h_ix].num_counters);
470 gcov_write_counter (csum->histogram[h_ix].min_value);
471 gcov_write_counter (csum->histogram[h_ix].cum_value);
472 }
473 }
474 }
475 #endif /* IN_LIBGCOV */
476
477 #endif /*!IN_GCOV */
478
479 /* Return a pointer to read BYTES bytes from the gcov file. Returns
480 NULL on failure (read past EOF). */
481
482 static const gcov_unsigned_t *
483 gcov_read_words (unsigned words)
484 {
485 const gcov_unsigned_t *result;
486 unsigned excess = gcov_var.length - gcov_var.offset;
487
488 gcov_nonruntime_assert (gcov_var.mode > 0);
489 if (excess < words)
490 {
491 gcov_var.start += gcov_var.offset;
492 if (excess)
493 {
494 #if IN_LIBGCOV
495 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
496 #else
497 memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset,
498 excess * 4);
499 #endif
500 }
501 gcov_var.offset = 0;
502 gcov_var.length = excess;
503 #if IN_LIBGCOV
504 excess = GCOV_BLOCK_SIZE;
505 #else
506 if (gcov_var.length + words > gcov_var.alloc)
507 gcov_allocate (gcov_var.length + words);
508 excess = gcov_var.alloc - gcov_var.length;
509 #endif
510 excess = fread (gcov_var.buffer + gcov_var.length,
511 1, excess << 2, gcov_var.file) >> 2;
512 gcov_var.length += excess;
513 if (gcov_var.length < words)
514 {
515 gcov_var.overread += words - gcov_var.length;
516 gcov_var.length = 0;
517 return 0;
518 }
519 }
520 result = &gcov_var.buffer[gcov_var.offset];
521 gcov_var.offset += words;
522 return result;
523 }
524
525 /* Read unsigned value from a coverage file. Sets error flag on file
526 error, overflow flag on overflow */
527
528 GCOV_LINKAGE gcov_unsigned_t
529 gcov_read_unsigned (void)
530 {
531 gcov_unsigned_t value;
532 const gcov_unsigned_t *buffer = gcov_read_words (1);
533
534 if (!buffer)
535 return 0;
536 value = from_file (buffer[0]);
537 return value;
538 }
539
540 /* Read counter value from a coverage file. Sets error flag on file
541 error, overflow flag on overflow */
542
543 GCOV_LINKAGE gcov_type
544 gcov_read_counter (void)
545 {
546 gcov_type value;
547 const gcov_unsigned_t *buffer = gcov_read_words (2);
548
549 if (!buffer)
550 return 0;
551 value = from_file (buffer[0]);
552 if (sizeof (value) > sizeof (gcov_unsigned_t))
553 value |= ((gcov_type) from_file (buffer[1])) << 32;
554 else if (buffer[1])
555 gcov_var.error = -1;
556
557 return value;
558 }
559
560 /* Read string from coverage file. Returns a pointer to a static
561 buffer, or NULL on empty string. You must copy the string before
562 calling another gcov function. */
563
564 #if !IN_LIBGCOV
565 GCOV_LINKAGE const char *
566 gcov_read_string (void)
567 {
568 unsigned length = gcov_read_unsigned ();
569
570 if (!length)
571 return 0;
572
573 return (const char *) gcov_read_words (length);
574 }
575 #endif
576
577 GCOV_LINKAGE void
578 gcov_read_summary (struct gcov_summary *summary)
579 {
580 unsigned ix, h_ix, bv_ix, h_cnt = 0;
581 struct gcov_ctr_summary *csum;
582 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE];
583 unsigned cur_bitvector;
584
585 summary->checksum = gcov_read_unsigned ();
586 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
587 {
588 csum->num = gcov_read_unsigned ();
589 csum->runs = gcov_read_unsigned ();
590 csum->sum_all = gcov_read_counter ();
591 csum->run_max = gcov_read_counter ();
592 csum->sum_max = gcov_read_counter ();
593 memset (csum->histogram, 0,
594 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
595 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
596 {
597 histo_bitvector[bv_ix] = gcov_read_unsigned ();
598 #if IN_LIBGCOV
599 /* When building libgcov we don't include system.h, which includes
600 hwint.h (where popcount_hwi is declared). However, libgcov.a
601 is built by the bootstrapped compiler and therefore the builtins
602 are always available. */
603 h_cnt += __builtin_popcount (histo_bitvector[bv_ix]);
604 #else
605 h_cnt += popcount_hwi (histo_bitvector[bv_ix]);
606 #endif
607 }
608 bv_ix = 0;
609 h_ix = 0;
610 cur_bitvector = 0;
611 while (h_cnt--)
612 {
613 /* Find the index corresponding to the next entry we will read in.
614 First find the next non-zero bitvector and re-initialize
615 the histogram index accordingly, then right shift and increment
616 the index until we find a set bit. */
617 while (!cur_bitvector)
618 {
619 h_ix = bv_ix * 32;
620 if (bv_ix >= GCOV_HISTOGRAM_BITVECTOR_SIZE)
621 gcov_error ("corrupted profile info: summary histogram "
622 "bitvector is corrupt");
623 cur_bitvector = histo_bitvector[bv_ix++];
624 }
625 while (!(cur_bitvector & 0x1))
626 {
627 h_ix++;
628 cur_bitvector >>= 1;
629 }
630 if (h_ix >= GCOV_HISTOGRAM_SIZE)
631 gcov_error ("corrupted profile info: summary histogram "
632 "index is corrupt");
633
634 csum->histogram[h_ix].num_counters = gcov_read_unsigned ();
635 csum->histogram[h_ix].min_value = gcov_read_counter ();
636 csum->histogram[h_ix].cum_value = gcov_read_counter ();
637 /* Shift off the index we are done with and increment to the
638 corresponding next histogram entry. */
639 cur_bitvector >>= 1;
640 h_ix++;
641 }
642 }
643 }
644
645 #if !IN_LIBGCOV
646 /* Reset to a known position. BASE should have been obtained from
647 gcov_position, LENGTH should be a record length. */
648
649 GCOV_LINKAGE void
650 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
651 {
652 gcov_nonruntime_assert (gcov_var.mode > 0);
653 base += length;
654 if (base - gcov_var.start <= gcov_var.length)
655 gcov_var.offset = base - gcov_var.start;
656 else
657 {
658 gcov_var.offset = gcov_var.length = 0;
659 fseek (gcov_var.file, base << 2, SEEK_SET);
660 gcov_var.start = ftell (gcov_var.file) >> 2;
661 }
662 }
663 #endif
664
665 #if IN_LIBGCOV
666 /* Move to a given position in a gcov file. */
667
668 GCOV_LINKAGE void
669 gcov_seek (gcov_position_t base)
670 {
671 if (gcov_var.offset)
672 gcov_write_block (gcov_var.offset);
673 fseek (gcov_var.file, base << 2, SEEK_SET);
674 gcov_var.start = ftell (gcov_var.file) >> 2;
675 }
676 #endif
677
678 #if IN_GCOV > 0
679 /* Return the modification time of the current gcov file. */
680
681 GCOV_LINKAGE time_t
682 gcov_time (void)
683 {
684 struct stat status;
685
686 if (fstat (fileno (gcov_var.file), &status))
687 return 0;
688 else
689 return status.st_mtime;
690 }
691 #endif /* IN_GCOV */
692
693 #if !IN_GCOV
694 /* Determine the index into histogram for VALUE. */
695
696 #if IN_LIBGCOV
697 static unsigned
698 #else
699 GCOV_LINKAGE unsigned
700 #endif
701 gcov_histo_index (gcov_type value)
702 {
703 gcov_type_unsigned v = (gcov_type_unsigned)value;
704 unsigned r = 0;
705 unsigned prev2bits = 0;
706
707 /* Find index into log2 scale histogram, where each of the log2
708 sized buckets is divided into 4 linear sub-buckets for better
709 focus in the higher buckets. */
710
711 /* Find the place of the most-significant bit set. */
712 if (v > 0)
713 {
714 #if IN_LIBGCOV
715 /* When building libgcov we don't include system.h, which includes
716 hwint.h (where floor_log2 is declared). However, libgcov.a
717 is built by the bootstrapped compiler and therefore the builtins
718 are always available. */
719 r = sizeof (long long) * __CHAR_BIT__ - 1 - __builtin_clzll (v);
720 #else
721 /* We use floor_log2 from hwint.c, which takes a HOST_WIDE_INT
722 that is 64 bits and gcov_type_unsigned is 64 bits. */
723 r = floor_log2 (v);
724 #endif
725 }
726
727 /* If at most the 2 least significant bits are set (value is
728 0 - 3) then that value is our index into the lowest set of
729 four buckets. */
730 if (r < 2)
731 return (unsigned)value;
732
733 gcov_nonruntime_assert (r < 64);
734
735 /* Find the two next most significant bits to determine which
736 of the four linear sub-buckets to select. */
737 prev2bits = (v >> (r - 2)) & 0x3;
738 /* Finally, compose the final bucket index from the log2 index and
739 the next 2 bits. The minimum r value at this point is 2 since we
740 returned above if r was 2 or more, so the minimum bucket at this
741 point is 4. */
742 return (r - 1) * 4 + prev2bits;
743 }
744
745 /* Merge SRC_HISTO into TGT_HISTO. The counters are assumed to be in
746 the same relative order in both histograms, and are matched up
747 and merged in reverse order. Each counter is assigned an equal portion of
748 its entry's original cumulative counter value when computing the
749 new merged cum_value. */
750
751 static void gcov_histogram_merge (gcov_bucket_type *tgt_histo,
752 gcov_bucket_type *src_histo)
753 {
754 int src_i, tgt_i, tmp_i = 0;
755 unsigned src_num, tgt_num, merge_num;
756 gcov_type src_cum, tgt_cum, merge_src_cum, merge_tgt_cum, merge_cum;
757 gcov_type merge_min;
758 gcov_bucket_type tmp_histo[GCOV_HISTOGRAM_SIZE];
759 int src_done = 0;
760
761 memset (tmp_histo, 0, sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
762
763 /* Assume that the counters are in the same relative order in both
764 histograms. Walk the histograms from largest to smallest entry,
765 matching up and combining counters in order. */
766 src_num = 0;
767 src_cum = 0;
768 src_i = GCOV_HISTOGRAM_SIZE - 1;
769 for (tgt_i = GCOV_HISTOGRAM_SIZE - 1; tgt_i >= 0 && !src_done; tgt_i--)
770 {
771 tgt_num = tgt_histo[tgt_i].num_counters;
772 tgt_cum = tgt_histo[tgt_i].cum_value;
773 /* Keep going until all of the target histogram's counters at this
774 position have been matched and merged with counters from the
775 source histogram. */
776 while (tgt_num > 0 && !src_done)
777 {
778 /* If this is either the first time through this loop or we just
779 exhausted the previous non-zero source histogram entry, look
780 for the next non-zero source histogram entry. */
781 if (!src_num)
782 {
783 /* Locate the next non-zero entry. */
784 while (src_i >= 0 && !src_histo[src_i].num_counters)
785 src_i--;
786 /* If source histogram has fewer counters, then just copy over the
787 remaining target counters and quit. */
788 if (src_i < 0)
789 {
790 tmp_histo[tgt_i].num_counters += tgt_num;
791 tmp_histo[tgt_i].cum_value += tgt_cum;
792 if (!tmp_histo[tgt_i].min_value ||
793 tgt_histo[tgt_i].min_value < tmp_histo[tgt_i].min_value)
794 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
795 while (--tgt_i >= 0)
796 {
797 tmp_histo[tgt_i].num_counters
798 += tgt_histo[tgt_i].num_counters;
799 tmp_histo[tgt_i].cum_value += tgt_histo[tgt_i].cum_value;
800 if (!tmp_histo[tgt_i].min_value ||
801 tgt_histo[tgt_i].min_value
802 < tmp_histo[tgt_i].min_value)
803 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
804 }
805
806 src_done = 1;
807 break;
808 }
809
810 src_num = src_histo[src_i].num_counters;
811 src_cum = src_histo[src_i].cum_value;
812 }
813
814 /* The number of counters to merge on this pass is the minimum
815 of the remaining counters from the current target and source
816 histogram entries. */
817 merge_num = tgt_num;
818 if (src_num < merge_num)
819 merge_num = src_num;
820
821 /* The merged min_value is the sum of the min_values from target
822 and source. */
823 merge_min = tgt_histo[tgt_i].min_value + src_histo[src_i].min_value;
824
825 /* Compute the portion of source and target entries' cum_value
826 that will be apportioned to the counters being merged.
827 The total remaining cum_value from each entry is divided
828 equally among the counters from that histogram entry if we
829 are not merging all of them. */
830 merge_src_cum = src_cum;
831 if (merge_num < src_num)
832 merge_src_cum = merge_num * src_cum / src_num;
833 merge_tgt_cum = tgt_cum;
834 if (merge_num < tgt_num)
835 merge_tgt_cum = merge_num * tgt_cum / tgt_num;
836 /* The merged cum_value is the sum of the source and target
837 components. */
838 merge_cum = merge_src_cum + merge_tgt_cum;
839
840 /* Update the remaining number of counters and cum_value left
841 to be merged from this source and target entry. */
842 src_cum -= merge_src_cum;
843 tgt_cum -= merge_tgt_cum;
844 src_num -= merge_num;
845 tgt_num -= merge_num;
846
847 /* The merged counters get placed in the new merged histogram
848 at the entry for the merged min_value. */
849 tmp_i = gcov_histo_index (merge_min);
850 gcov_nonruntime_assert (tmp_i < GCOV_HISTOGRAM_SIZE);
851 tmp_histo[tmp_i].num_counters += merge_num;
852 tmp_histo[tmp_i].cum_value += merge_cum;
853 if (!tmp_histo[tmp_i].min_value ||
854 merge_min < tmp_histo[tmp_i].min_value)
855 tmp_histo[tmp_i].min_value = merge_min;
856
857 /* Ensure the search for the next non-zero src_histo entry starts
858 at the next smallest histogram bucket. */
859 if (!src_num)
860 src_i--;
861 }
862 }
863
864 gcov_nonruntime_assert (tgt_i < 0);
865
866 /* In the case where there were more counters in the source histogram,
867 accumulate the remaining unmerged cumulative counter values. Add
868 those to the smallest non-zero target histogram entry. Otherwise,
869 the total cumulative counter values in the histogram will be smaller
870 than the sum_all stored in the summary, which will complicate
871 computing the working set information from the histogram later on. */
872 if (src_num)
873 src_i--;
874 while (src_i >= 0)
875 {
876 src_cum += src_histo[src_i].cum_value;
877 src_i--;
878 }
879 /* At this point, tmp_i should be the smallest non-zero entry in the
880 tmp_histo. */
881 gcov_nonruntime_assert (tmp_i >= 0 && tmp_i < GCOV_HISTOGRAM_SIZE
882 && tmp_histo[tmp_i].num_counters > 0);
883 tmp_histo[tmp_i].cum_value += src_cum;
884
885 /* Finally, copy the merged histogram into tgt_histo. */
886 memcpy (tgt_histo, tmp_histo,
887 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
888 }
889 #endif /* !IN_GCOV */
890
891 /* This is used by gcov-dump (IN_GCOV == -1) and in the compiler
892 (!IN_GCOV && !IN_LIBGCOV). */
893 #if IN_GCOV <= 0 && !IN_LIBGCOV
894 /* Compute the working set information from the counter histogram in
895 the profile summary. This is an array of information corresponding to a
896 range of percentages of the total execution count (sum_all), and includes
897 the number of counters required to cover that working set percentage and
898 the minimum counter value in that working set. */
899
900 GCOV_LINKAGE void
901 compute_working_sets (const struct gcov_ctr_summary *summary,
902 gcov_working_set_t *gcov_working_sets)
903 {
904 gcov_type working_set_cum_values[NUM_GCOV_WORKING_SETS];
905 gcov_type ws_cum_hotness_incr;
906 gcov_type cum, tmp_cum;
907 const gcov_bucket_type *histo_bucket;
908 unsigned ws_ix, c_num, count;
909 int h_ix;
910
911 /* Compute the amount of sum_all that the cumulative hotness grows
912 by in each successive working set entry, which depends on the
913 number of working set entries. */
914 ws_cum_hotness_incr = summary->sum_all / NUM_GCOV_WORKING_SETS;
915
916 /* Next fill in an array of the cumulative hotness values corresponding
917 to each working set summary entry we are going to compute below.
918 Skip 0% statistics, which can be extrapolated from the
919 rest of the summary data. */
920 cum = ws_cum_hotness_incr;
921 for (ws_ix = 0; ws_ix < NUM_GCOV_WORKING_SETS;
922 ws_ix++, cum += ws_cum_hotness_incr)
923 working_set_cum_values[ws_ix] = cum;
924 /* The last summary entry is reserved for (roughly) 99.9% of the
925 working set. Divide by 1024 so it becomes a shift, which gives
926 almost exactly 99.9%. */
927 working_set_cum_values[NUM_GCOV_WORKING_SETS-1]
928 = summary->sum_all - summary->sum_all/1024;
929
930 /* Next, walk through the histogram in decending order of hotness
931 and compute the statistics for the working set summary array.
932 As histogram entries are accumulated, we check to see which
933 working set entries have had their expected cum_value reached
934 and fill them in, walking the working set entries in increasing
935 size of cum_value. */
936 ws_ix = 0; /* The current entry into the working set array. */
937 cum = 0; /* The current accumulated counter sum. */
938 count = 0; /* The current accumulated count of block counters. */
939 for (h_ix = GCOV_HISTOGRAM_SIZE - 1;
940 h_ix >= 0 && ws_ix < NUM_GCOV_WORKING_SETS; h_ix--)
941 {
942 histo_bucket = &summary->histogram[h_ix];
943
944 /* If we haven't reached the required cumulative counter value for
945 the current working set percentage, simply accumulate this histogram
946 entry into the running sums and continue to the next histogram
947 entry. */
948 if (cum + histo_bucket->cum_value < working_set_cum_values[ws_ix])
949 {
950 cum += histo_bucket->cum_value;
951 count += histo_bucket->num_counters;
952 continue;
953 }
954
955 /* If adding the current histogram entry's cumulative counter value
956 causes us to exceed the current working set size, then estimate
957 how many of this histogram entry's counter values are required to
958 reach the working set size, and fill in working set entries
959 as we reach their expected cumulative value. */
960 for (c_num = 0, tmp_cum = cum;
961 c_num < histo_bucket->num_counters && ws_ix < NUM_GCOV_WORKING_SETS;
962 c_num++)
963 {
964 count++;
965 /* If we haven't reached the last histogram entry counter, add
966 in the minimum value again. This will underestimate the
967 cumulative sum so far, because many of the counter values in this
968 entry may have been larger than the minimum. We could add in the
969 average value every time, but that would require an expensive
970 divide operation. */
971 if (c_num + 1 < histo_bucket->num_counters)
972 tmp_cum += histo_bucket->min_value;
973 /* If we have reached the last histogram entry counter, then add
974 in the entire cumulative value. */
975 else
976 tmp_cum = cum + histo_bucket->cum_value;
977
978 /* Next walk through successive working set entries and fill in
979 the statistics for any whose size we have reached by accumulating
980 this histogram counter. */
981 while (ws_ix < NUM_GCOV_WORKING_SETS
982 && tmp_cum >= working_set_cum_values[ws_ix])
983 {
984 gcov_working_sets[ws_ix].num_counters = count;
985 gcov_working_sets[ws_ix].min_counter
986 = histo_bucket->min_value;
987 ws_ix++;
988 }
989 }
990 /* Finally, update the running cumulative value since we were
991 using a temporary above. */
992 cum += histo_bucket->cum_value;
993 }
994 gcov_nonruntime_assert (ws_ix == NUM_GCOV_WORKING_SETS);
995 }
996 #endif /* IN_GCOV <= 0 && !IN_LIBGCOV */