1 /* File format for coverage information
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
6 This file is part of GCC.
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
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
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.
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/>. */
27 /* Routines declared in gcov-io.h. This file should be #included by
28 another source file, after having #included gcov-io.h. */
31 static void gcov_write_block (unsigned);
32 static gcov_unsigned_t
*gcov_write_words (unsigned);
34 static const gcov_unsigned_t
*gcov_read_words (unsigned);
36 static void gcov_allocate (unsigned);
39 /* Optimum number of gcov_unsigned_t's read from or written to disk. */
40 #define GCOV_BLOCK_SIZE (1 << 10)
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 */
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
56 gcov_unsigned_t buffer
[GCOV_BLOCK_SIZE
+ 1];
58 int endian
; /* Swap endianness. */
59 /* Holds a variable length block, as the compiler can write
60 strings and needs to backtrack. */
62 gcov_unsigned_t
*buffer
;
66 /* Save the current position in the gcov file. */
67 /* We need to expose this function when compiling for gcov-tool. */
74 gcov_nonruntime_assert (gcov_var
.mode
> 0);
75 return gcov_var
.start
+ gcov_var
.offset
;
78 /* Return nonzero if the error flag is set. */
79 /* We need to expose this function when compiling for gcov-tool. */
86 return gcov_var
.file
? gcov_var
.error
: 1;
90 /* Move to beginning of file and initialize for writing. */
91 GCOV_LINKAGE
inline void
97 fseek (gcov_var
.file
, 0L, SEEK_SET
);
101 static inline gcov_unsigned_t
from_file (gcov_unsigned_t value
)
106 value
= (value
>> 16) | (value
<< 16);
107 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
113 /* Open a gcov file. NAME is the name of the file to open and MODE
114 indicates whether a new file should be created, or an existing file
115 opened. If MODE is >= 0 an existing file will be opened, if
116 possible, and if MODE is <= 0, a new file will be created. Use
117 MODE=0 to attempt to reopen an existing file and then fall back on
118 creating a new one. If MODE > 0, the file will be opened in
119 read-only mode. Otherwise it will be opened for modification.
120 Return zero on failure, non-zero on success. */
124 gcov_open (const char *name
)
126 gcov_open (const char *name
, int mode
)
133 struct flock s_flock
;
136 s_flock
.l_whence
= SEEK_SET
;
138 s_flock
.l_len
= 0; /* Until EOF. */
139 s_flock
.l_pid
= getpid ();
142 gcov_nonruntime_assert (!gcov_var
.file
);
144 gcov_var
.offset
= gcov_var
.length
= 0;
145 gcov_var
.overread
= -1u;
153 /* Read-only mode - acquire a read-lock. */
154 s_flock
.l_type
= F_RDLCK
;
155 /* pass mode (ignored) for compatibility */
156 fd
= open (name
, O_RDONLY
, S_IRUSR
| S_IWUSR
);
160 /* Write mode - acquire a write-lock. */
161 s_flock
.l_type
= F_WRLCK
;
162 /* Truncate if force new mode. */
163 fd
= open (name
, O_RDWR
| O_CREAT
| (mode
< 0 ? O_TRUNC
: 0), 0666);
168 while (fcntl (fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
171 gcov_var
.file
= fdopen (fd
, (mode
> 0) ? "rb" : "r+b");
180 /* Open an existing file. */
181 gcov_var
.file
= fopen (name
, (mode
> 0) ? "rb" : "r+b");
186 /* Create a new file. */
187 gcov_var
.file
= fopen (name
, "w+b");
193 gcov_var
.mode
= mode
? mode
: 1;
195 setbuf (gcov_var
.file
, (char *)0);
200 /* Close the current gcov file. Flushes data to disk. Returns nonzero
201 on failure or error flag set. */
209 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
210 gcov_write_block (gcov_var
.offset
);
212 fclose (gcov_var
.file
);
217 free (gcov_var
.buffer
);
222 return gcov_var
.error
;
226 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
227 file. Returns +1 for same endian, -1 for other endian and zero for
231 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
233 if (magic
== expected
)
235 magic
= (magic
>> 16) | (magic
<< 16);
236 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
237 if (magic
== expected
)
248 gcov_allocate (unsigned length
)
250 size_t new_size
= gcov_var
.alloc
;
253 new_size
= GCOV_BLOCK_SIZE
;
257 gcov_var
.alloc
= new_size
;
258 gcov_var
.buffer
= XRESIZEVAR (gcov_unsigned_t
, gcov_var
.buffer
, new_size
<< 2);
263 /* Write out the current block, if needs be. */
266 gcov_write_block (unsigned size
)
268 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
270 gcov_var
.start
+= size
;
271 gcov_var
.offset
-= size
;
274 /* Allocate space to write BYTES bytes to the gcov file. Return a
275 pointer to those bytes, or NULL on failure. */
277 static gcov_unsigned_t
*
278 gcov_write_words (unsigned words
)
280 gcov_unsigned_t
*result
;
282 gcov_nonruntime_assert (gcov_var
.mode
< 0);
284 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
286 gcov_write_block (GCOV_BLOCK_SIZE
);
289 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
293 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
294 gcov_allocate (gcov_var
.offset
+ words
);
296 result
= &gcov_var
.buffer
[gcov_var
.offset
];
297 gcov_var
.offset
+= words
;
302 /* Write unsigned VALUE to coverage file. Sets error flag
306 gcov_write_unsigned (gcov_unsigned_t value
)
308 gcov_unsigned_t
*buffer
= gcov_write_words (1);
313 /* Write counter VALUE to coverage file. Sets error flag
318 gcov_write_counter (gcov_type value
)
320 gcov_unsigned_t
*buffer
= gcov_write_words (2);
322 buffer
[0] = (gcov_unsigned_t
) value
;
323 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
324 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
328 #endif /* IN_LIBGCOV */
331 /* Write STRING to coverage file. Sets error flag on file
332 error, overflow flag on overflow */
335 gcov_write_string (const char *string
)
339 gcov_unsigned_t
*buffer
;
343 length
= strlen (string
);
344 alloc
= (length
+ 4) >> 2;
347 buffer
= gcov_write_words (1 + alloc
);
353 buffer
[alloc
] = 0; /* place nul terminators. */
354 memcpy (&buffer
[1], string
, length
);
360 /* Write FILENAME to coverage file. Sets error flag on file
361 error, overflow flag on overflow */
364 gcov_write_filename (const char *filename
)
366 if (profile_abs_path_flag
&& filename
&& filename
[0]
367 && !(IS_DIR_SEPARATOR (filename
[0])
368 #if HAVE_DOS_BASED_FILE_SYSTEM
369 || filename
[1] == ':'
373 char *buf
= getcwd (NULL
, 0);
374 if (buf
!= NULL
&& buf
[0])
376 size_t len
= strlen (buf
);
377 buf
= (char*)xrealloc (buf
, len
+ strlen (filename
) + 2);
378 if (!IS_DIR_SEPARATOR (buf
[len
- 1]))
380 strcat (buf
, filename
);
381 gcov_write_string (buf
);
387 gcov_write_string (filename
);
392 /* Write a tag TAG and reserve space for the record length. Return a
393 value to be used for gcov_write_length. */
395 GCOV_LINKAGE gcov_position_t
396 gcov_write_tag (gcov_unsigned_t tag
)
398 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
399 gcov_unsigned_t
*buffer
= gcov_write_words (2);
407 /* Write a record length using POSITION, which was returned by
408 gcov_write_tag. The current file position is the end of the
409 record, and is restored before returning. Returns nonzero on
413 gcov_write_length (gcov_position_t position
)
416 gcov_unsigned_t length
;
417 gcov_unsigned_t
*buffer
;
419 gcov_nonruntime_assert (gcov_var
.mode
< 0);
420 gcov_nonruntime_assert (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
421 gcov_nonruntime_assert (position
>= gcov_var
.start
);
422 offset
= position
- gcov_var
.start
;
423 length
= gcov_var
.offset
- offset
- 2;
424 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
426 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
427 gcov_write_block (gcov_var
.offset
);
430 #else /* IN_LIBGCOV */
432 /* Write a tag TAG and length LENGTH. */
435 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
437 gcov_unsigned_t
*buffer
= gcov_write_words (2);
443 /* Write a summary structure to the gcov file. Return nonzero on
447 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
449 unsigned h_ix
, bv_ix
, h_cnt
= 0;
450 unsigned histo_bitvector
[GCOV_HISTOGRAM_BITVECTOR_SIZE
];
452 /* Count number of non-zero histogram entries, and fill in a bit vector
453 of non-zero indices. The histogram is only currently computed for arc
455 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
456 histo_bitvector
[bv_ix
] = 0;
457 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
458 if (summary
->histogram
[h_ix
].num_counters
)
460 histo_bitvector
[h_ix
/ 32] |= 1 << (h_ix
% 32);
463 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH (h_cnt
));
464 gcov_write_unsigned (summary
->checksum
);
466 gcov_write_unsigned (summary
->num
);
467 gcov_write_unsigned (summary
->runs
);
468 gcov_write_counter (summary
->sum_all
);
469 gcov_write_counter (summary
->run_max
);
470 gcov_write_counter (summary
->sum_max
);
471 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
472 gcov_write_unsigned (histo_bitvector
[bv_ix
]);
473 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
475 if (!summary
->histogram
[h_ix
].num_counters
)
477 gcov_write_unsigned (summary
->histogram
[h_ix
].num_counters
);
478 gcov_write_counter (summary
->histogram
[h_ix
].min_value
);
479 gcov_write_counter (summary
->histogram
[h_ix
].cum_value
);
482 #endif /* IN_LIBGCOV */
486 /* Return a pointer to read BYTES bytes from the gcov file. Returns
487 NULL on failure (read past EOF). */
489 static const gcov_unsigned_t
*
490 gcov_read_words (unsigned words
)
492 const gcov_unsigned_t
*result
;
493 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
495 if (gcov_var
.mode
<= 0)
500 gcov_var
.start
+= gcov_var
.offset
;
504 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
506 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
,
511 gcov_var
.length
= excess
;
513 excess
= GCOV_BLOCK_SIZE
;
515 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
516 gcov_allocate (gcov_var
.length
+ words
);
517 excess
= gcov_var
.alloc
- gcov_var
.length
;
519 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
520 1, excess
<< 2, gcov_var
.file
) >> 2;
521 gcov_var
.length
+= excess
;
522 if (gcov_var
.length
< words
)
524 gcov_var
.overread
+= words
- gcov_var
.length
;
529 result
= &gcov_var
.buffer
[gcov_var
.offset
];
530 gcov_var
.offset
+= words
;
534 /* Read unsigned value from a coverage file. Sets error flag on file
535 error, overflow flag on overflow */
537 GCOV_LINKAGE gcov_unsigned_t
538 gcov_read_unsigned (void)
540 gcov_unsigned_t value
;
541 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
545 value
= from_file (buffer
[0]);
549 /* Read counter value from a coverage file. Sets error flag on file
550 error, overflow flag on overflow */
552 GCOV_LINKAGE gcov_type
553 gcov_read_counter (void)
556 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
560 value
= from_file (buffer
[0]);
561 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
562 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
569 /* Mangle filename path of BASE and output new allocated pointer with
573 mangle_path (char const *base
)
575 /* Convert '/' to '#', convert '..' to '^',
576 convert ':' to '~' on DOS based file system. */
578 char *buffer
= (char *)xmalloc (strlen (base
) + 10);
581 #if HAVE_DOS_BASED_FILE_SYSTEM
582 if (base
[0] && base
[1] == ':')
590 for (; *base
; base
= probe
)
594 for (probe
= base
; *probe
; probe
++)
598 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
602 memcpy (ptr
, base
, len
);
612 /* Terminate the string. */
618 /* We need to expose the below function when compiling for gcov-tool. */
620 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
621 /* Read string from coverage file. Returns a pointer to a static
622 buffer, or NULL on empty string. You must copy the string before
623 calling another gcov function. */
625 GCOV_LINKAGE
const char *
626 gcov_read_string (void)
628 unsigned length
= gcov_read_unsigned ();
633 return (const char *) gcov_read_words (length
);
638 gcov_read_summary (struct gcov_summary
*summary
)
640 unsigned h_ix
, bv_ix
, h_cnt
= 0;
641 unsigned histo_bitvector
[GCOV_HISTOGRAM_BITVECTOR_SIZE
];
642 unsigned cur_bitvector
;
644 summary
->checksum
= gcov_read_unsigned ();
645 summary
->num
= gcov_read_unsigned ();
646 summary
->runs
= gcov_read_unsigned ();
647 summary
->sum_all
= gcov_read_counter ();
648 summary
->run_max
= gcov_read_counter ();
649 summary
->sum_max
= gcov_read_counter ();
650 memset (summary
->histogram
, 0,
651 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
652 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
654 histo_bitvector
[bv_ix
] = gcov_read_unsigned ();
656 /* When building libgcov we don't include system.h, which includes
657 hwint.h (where popcount_hwi is declared). However, libgcov.a
658 is built by the bootstrapped compiler and therefore the builtins
659 are always available. */
660 h_cnt
+= __builtin_popcount (histo_bitvector
[bv_ix
]);
662 h_cnt
+= popcount_hwi (histo_bitvector
[bv_ix
]);
670 /* Find the index corresponding to the next entry we will read in.
671 First find the next non-zero bitvector and re-initialize
672 the histogram index accordingly, then right shift and increment
673 the index until we find a set bit. */
674 while (!cur_bitvector
)
677 if (bv_ix
>= GCOV_HISTOGRAM_BITVECTOR_SIZE
)
678 gcov_error ("corrupted profile info: summary histogram "
679 "bitvector is corrupt");
680 cur_bitvector
= histo_bitvector
[bv_ix
++];
682 while (!(cur_bitvector
& 0x1))
687 if (h_ix
>= GCOV_HISTOGRAM_SIZE
)
688 gcov_error ("corrupted profile info: summary histogram "
691 summary
->histogram
[h_ix
].num_counters
= gcov_read_unsigned ();
692 summary
->histogram
[h_ix
].min_value
= gcov_read_counter ();
693 summary
->histogram
[h_ix
].cum_value
= gcov_read_counter ();
694 /* Shift off the index we are done with and increment to the
695 corresponding next histogram entry. */
701 /* We need to expose the below function when compiling for gcov-tool. */
703 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
704 /* Reset to a known position. BASE should have been obtained from
705 gcov_position, LENGTH should be a record length. */
708 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
710 gcov_nonruntime_assert (gcov_var
.mode
> 0);
712 if (base
- gcov_var
.start
<= gcov_var
.length
)
713 gcov_var
.offset
= base
- gcov_var
.start
;
716 gcov_var
.offset
= gcov_var
.length
= 0;
717 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
718 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
724 /* Move to a given position in a gcov file. */
727 gcov_seek (gcov_position_t base
)
730 gcov_write_block (gcov_var
.offset
);
731 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
732 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
737 /* Return the modification time of the current gcov file. */
744 if (fstat (fileno (gcov_var
.file
), &status
))
747 return status
.st_mtime
;
752 /* Determine the index into histogram for VALUE. */
757 GCOV_LINKAGE
unsigned
759 gcov_histo_index (gcov_type value
)
761 gcov_type_unsigned v
= (gcov_type_unsigned
)value
;
763 unsigned prev2bits
= 0;
765 /* Find index into log2 scale histogram, where each of the log2
766 sized buckets is divided into 4 linear sub-buckets for better
767 focus in the higher buckets. */
769 /* Find the place of the most-significant bit set. */
773 /* When building libgcov we don't include system.h, which includes
774 hwint.h (where floor_log2 is declared). However, libgcov.a
775 is built by the bootstrapped compiler and therefore the builtins
776 are always available. */
777 r
= sizeof (long long) * __CHAR_BIT__
- 1 - __builtin_clzll (v
);
779 /* We use floor_log2 from hwint.c, which takes a HOST_WIDE_INT
780 that is 64 bits and gcov_type_unsigned is 64 bits. */
785 /* If at most the 2 least significant bits are set (value is
786 0 - 3) then that value is our index into the lowest set of
789 return (unsigned)value
;
791 gcov_nonruntime_assert (r
< 64);
793 /* Find the two next most significant bits to determine which
794 of the four linear sub-buckets to select. */
795 prev2bits
= (v
>> (r
- 2)) & 0x3;
796 /* Finally, compose the final bucket index from the log2 index and
797 the next 2 bits. The minimum r value at this point is 2 since we
798 returned above if r was 2 or more, so the minimum bucket at this
800 return (r
- 1) * 4 + prev2bits
;
803 /* Merge SRC_HISTO into TGT_HISTO. The counters are assumed to be in
804 the same relative order in both histograms, and are matched up
805 and merged in reverse order. Each counter is assigned an equal portion of
806 its entry's original cumulative counter value when computing the
807 new merged cum_value. */
809 static void gcov_histogram_merge (gcov_bucket_type
*tgt_histo
,
810 gcov_bucket_type
*src_histo
)
812 int src_i
, tgt_i
, tmp_i
= 0;
813 unsigned src_num
, tgt_num
, merge_num
;
814 gcov_type src_cum
, tgt_cum
, merge_src_cum
, merge_tgt_cum
, merge_cum
;
816 gcov_bucket_type tmp_histo
[GCOV_HISTOGRAM_SIZE
];
819 memset (tmp_histo
, 0, sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
821 /* Assume that the counters are in the same relative order in both
822 histograms. Walk the histograms from largest to smallest entry,
823 matching up and combining counters in order. */
826 src_i
= GCOV_HISTOGRAM_SIZE
- 1;
827 for (tgt_i
= GCOV_HISTOGRAM_SIZE
- 1; tgt_i
>= 0 && !src_done
; tgt_i
--)
829 tgt_num
= tgt_histo
[tgt_i
].num_counters
;
830 tgt_cum
= tgt_histo
[tgt_i
].cum_value
;
831 /* Keep going until all of the target histogram's counters at this
832 position have been matched and merged with counters from the
834 while (tgt_num
> 0 && !src_done
)
836 /* If this is either the first time through this loop or we just
837 exhausted the previous non-zero source histogram entry, look
838 for the next non-zero source histogram entry. */
841 /* Locate the next non-zero entry. */
842 while (src_i
>= 0 && !src_histo
[src_i
].num_counters
)
844 /* If source histogram has fewer counters, then just copy over the
845 remaining target counters and quit. */
848 tmp_histo
[tgt_i
].num_counters
+= tgt_num
;
849 tmp_histo
[tgt_i
].cum_value
+= tgt_cum
;
850 if (!tmp_histo
[tgt_i
].min_value
||
851 tgt_histo
[tgt_i
].min_value
< tmp_histo
[tgt_i
].min_value
)
852 tmp_histo
[tgt_i
].min_value
= tgt_histo
[tgt_i
].min_value
;
855 tmp_histo
[tgt_i
].num_counters
856 += tgt_histo
[tgt_i
].num_counters
;
857 tmp_histo
[tgt_i
].cum_value
+= tgt_histo
[tgt_i
].cum_value
;
858 if (!tmp_histo
[tgt_i
].min_value
||
859 tgt_histo
[tgt_i
].min_value
860 < tmp_histo
[tgt_i
].min_value
)
861 tmp_histo
[tgt_i
].min_value
= tgt_histo
[tgt_i
].min_value
;
868 src_num
= src_histo
[src_i
].num_counters
;
869 src_cum
= src_histo
[src_i
].cum_value
;
872 /* The number of counters to merge on this pass is the minimum
873 of the remaining counters from the current target and source
874 histogram entries. */
876 if (src_num
< merge_num
)
879 /* The merged min_value is the sum of the min_values from target
881 merge_min
= tgt_histo
[tgt_i
].min_value
+ src_histo
[src_i
].min_value
;
883 /* Compute the portion of source and target entries' cum_value
884 that will be apportioned to the counters being merged.
885 The total remaining cum_value from each entry is divided
886 equally among the counters from that histogram entry if we
887 are not merging all of them. */
888 merge_src_cum
= src_cum
;
889 if (merge_num
< src_num
)
890 merge_src_cum
= merge_num
* src_cum
/ src_num
;
891 merge_tgt_cum
= tgt_cum
;
892 if (merge_num
< tgt_num
)
893 merge_tgt_cum
= merge_num
* tgt_cum
/ tgt_num
;
894 /* The merged cum_value is the sum of the source and target
896 merge_cum
= merge_src_cum
+ merge_tgt_cum
;
898 /* Update the remaining number of counters and cum_value left
899 to be merged from this source and target entry. */
900 src_cum
-= merge_src_cum
;
901 tgt_cum
-= merge_tgt_cum
;
902 src_num
-= merge_num
;
903 tgt_num
-= merge_num
;
905 /* The merged counters get placed in the new merged histogram
906 at the entry for the merged min_value. */
907 tmp_i
= gcov_histo_index (merge_min
);
908 gcov_nonruntime_assert (tmp_i
< GCOV_HISTOGRAM_SIZE
);
909 tmp_histo
[tmp_i
].num_counters
+= merge_num
;
910 tmp_histo
[tmp_i
].cum_value
+= merge_cum
;
911 if (!tmp_histo
[tmp_i
].min_value
||
912 merge_min
< tmp_histo
[tmp_i
].min_value
)
913 tmp_histo
[tmp_i
].min_value
= merge_min
;
915 /* Ensure the search for the next non-zero src_histo entry starts
916 at the next smallest histogram bucket. */
922 gcov_nonruntime_assert (tgt_i
< 0);
924 /* In the case where there were more counters in the source histogram,
925 accumulate the remaining unmerged cumulative counter values. Add
926 those to the smallest non-zero target histogram entry. Otherwise,
927 the total cumulative counter values in the histogram will be smaller
928 than the sum_all stored in the summary, which will complicate
929 computing the working set information from the histogram later on. */
934 src_cum
+= src_histo
[src_i
].cum_value
;
937 /* At this point, tmp_i should be the smallest non-zero entry in the
939 gcov_nonruntime_assert (tmp_i
>= 0 && tmp_i
< GCOV_HISTOGRAM_SIZE
940 && tmp_histo
[tmp_i
].num_counters
> 0);
941 tmp_histo
[tmp_i
].cum_value
+= src_cum
;
943 /* Finally, copy the merged histogram into tgt_histo. */
944 memcpy (tgt_histo
, tmp_histo
,
945 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
947 #endif /* !IN_GCOV */
949 /* This is used by gcov-dump (IN_GCOV == -1) and in the compiler
950 (!IN_GCOV && !IN_LIBGCOV). */
951 #if IN_GCOV <= 0 && !IN_LIBGCOV
952 /* Compute the working set information from the counter histogram in
953 the profile summary. This is an array of information corresponding to a
954 range of percentages of the total execution count (sum_all), and includes
955 the number of counters required to cover that working set percentage and
956 the minimum counter value in that working set. */
959 compute_working_sets (const gcov_summary
*summary
,
960 gcov_working_set_t
*gcov_working_sets
)
962 gcov_type working_set_cum_values
[NUM_GCOV_WORKING_SETS
];
963 gcov_type ws_cum_hotness_incr
;
964 gcov_type cum
, tmp_cum
;
965 const gcov_bucket_type
*histo_bucket
;
966 unsigned ws_ix
, c_num
, count
;
969 /* Compute the amount of sum_all that the cumulative hotness grows
970 by in each successive working set entry, which depends on the
971 number of working set entries. */
972 ws_cum_hotness_incr
= summary
->sum_all
/ NUM_GCOV_WORKING_SETS
;
974 /* Next fill in an array of the cumulative hotness values corresponding
975 to each working set summary entry we are going to compute below.
976 Skip 0% statistics, which can be extrapolated from the
977 rest of the summary data. */
978 cum
= ws_cum_hotness_incr
;
979 for (ws_ix
= 0; ws_ix
< NUM_GCOV_WORKING_SETS
;
980 ws_ix
++, cum
+= ws_cum_hotness_incr
)
981 working_set_cum_values
[ws_ix
] = cum
;
982 /* The last summary entry is reserved for (roughly) 99.9% of the
983 working set. Divide by 1024 so it becomes a shift, which gives
984 almost exactly 99.9%. */
985 working_set_cum_values
[NUM_GCOV_WORKING_SETS
-1]
986 = summary
->sum_all
- summary
->sum_all
/1024;
988 /* Next, walk through the histogram in decending order of hotness
989 and compute the statistics for the working set summary array.
990 As histogram entries are accumulated, we check to see which
991 working set entries have had their expected cum_value reached
992 and fill them in, walking the working set entries in increasing
993 size of cum_value. */
994 ws_ix
= 0; /* The current entry into the working set array. */
995 cum
= 0; /* The current accumulated counter sum. */
996 count
= 0; /* The current accumulated count of block counters. */
997 for (h_ix
= GCOV_HISTOGRAM_SIZE
- 1;
998 h_ix
>= 0 && ws_ix
< NUM_GCOV_WORKING_SETS
; h_ix
--)
1000 histo_bucket
= &summary
->histogram
[h_ix
];
1002 /* If we haven't reached the required cumulative counter value for
1003 the current working set percentage, simply accumulate this histogram
1004 entry into the running sums and continue to the next histogram
1006 if (cum
+ histo_bucket
->cum_value
< working_set_cum_values
[ws_ix
])
1008 cum
+= histo_bucket
->cum_value
;
1009 count
+= histo_bucket
->num_counters
;
1013 /* If adding the current histogram entry's cumulative counter value
1014 causes us to exceed the current working set size, then estimate
1015 how many of this histogram entry's counter values are required to
1016 reach the working set size, and fill in working set entries
1017 as we reach their expected cumulative value. */
1018 for (c_num
= 0, tmp_cum
= cum
;
1019 c_num
< histo_bucket
->num_counters
&& ws_ix
< NUM_GCOV_WORKING_SETS
;
1023 /* If we haven't reached the last histogram entry counter, add
1024 in the minimum value again. This will underestimate the
1025 cumulative sum so far, because many of the counter values in this
1026 entry may have been larger than the minimum. We could add in the
1027 average value every time, but that would require an expensive
1028 divide operation. */
1029 if (c_num
+ 1 < histo_bucket
->num_counters
)
1030 tmp_cum
+= histo_bucket
->min_value
;
1031 /* If we have reached the last histogram entry counter, then add
1032 in the entire cumulative value. */
1034 tmp_cum
= cum
+ histo_bucket
->cum_value
;
1036 /* Next walk through successive working set entries and fill in
1037 the statistics for any whose size we have reached by accumulating
1038 this histogram counter. */
1039 while (ws_ix
< NUM_GCOV_WORKING_SETS
1040 && tmp_cum
>= working_set_cum_values
[ws_ix
])
1042 gcov_working_sets
[ws_ix
].num_counters
= count
;
1043 gcov_working_sets
[ws_ix
].min_counter
1044 = histo_bucket
->min_value
;
1048 /* Finally, update the running cumulative value since we were
1049 using a temporary above. */
1050 cum
+= histo_bucket
->cum_value
;
1052 gcov_nonruntime_assert (ws_ix
== NUM_GCOV_WORKING_SETS
);
1054 #endif /* IN_GCOV <= 0 && !IN_LIBGCOV */