Backport r203445 from v17
[official-gcc.git] / gcc-4_8 / gcc / gcov-io.c
blob2a0fe78f6e37a5bc57c4a482a84a88b8740fbe2b
1 /* File format for coverage information
2 Copyright (C) 1996-2013 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
11 version.
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.
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. */
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
39 /* Optimum number of gcov_unsigned_t's read from or written to disk. */
40 #define GCOV_BLOCK_SIZE (1 << 10)
42 GCOV_LINKAGE struct gcov_var
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;
66 /* Save the current position in the gcov file. */
67 static inline gcov_position_t
68 gcov_position (void)
70 return gcov_var.start + gcov_var.offset;
73 /* Return nonzero if the error flag is set. */
74 static inline int
75 gcov_is_error (void)
77 return gcov_var.file ? gcov_var.error : 1;
80 /* Move to beginning of file and initialize for writing. */
81 static inline void
82 gcov_rewrite (void)
84 gcc_assert (gcov_var.mode > 0);
85 gcov_var.mode = -1;
86 gcov_var.start = 0;
87 gcov_var.offset = 0;
88 fseek (gcov_var.file, 0L, SEEK_SET);
91 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
93 #if !IN_LIBGCOV
94 if (gcov_var.endian)
96 value = (value >> 16) | (value << 16);
97 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
99 #endif
100 return value;
103 /* Open a gcov file. NAME is the name of the file to open and MODE
104 indicates whether a new file should be created, or an existing file
105 opened. If MODE is >= 0 an existing file will be opened, if
106 possible, and if MODE is <= 0, a new file will be created. Use
107 MODE=0 to attempt to reopen an existing file and then fall back on
108 creating a new one. If MODE < 0, the file will be opened in
109 read-only mode. Otherwise it will be opened for modification.
110 Return zero on failure, >0 on opening an existing file and <0 on
111 creating a new one. */
113 GCOV_LINKAGE int
114 #if IN_LIBGCOV
115 gcov_open (const char *name)
116 #else
117 gcov_open (const char *name, int mode)
118 #endif
120 #if IN_LIBGCOV
121 const int mode = 0;
122 #endif
123 #if GCOV_LOCKED
124 struct flock s_flock;
125 int fd;
127 s_flock.l_whence = SEEK_SET;
128 s_flock.l_start = 0;
129 s_flock.l_len = 0; /* Until EOF. */
130 s_flock.l_pid = getpid ();
131 #endif
133 gcc_assert (!gcov_var.file);
134 gcov_var.start = 0;
135 gcov_var.offset = gcov_var.length = 0;
136 gcov_var.overread = -1u;
137 gcov_var.error = 0;
138 #if !IN_LIBGCOV
139 gcov_var.endian = 0;
140 #endif
141 #if GCOV_LOCKED
142 if (mode > 0)
144 /* Read-only mode - acquire a read-lock. */
145 s_flock.l_type = F_RDLCK;
146 /* pass mode (ignored) for compatibility */
147 fd = open (name, O_RDONLY, S_IRUSR | S_IWUSR);
149 else
151 /* Write mode - acquire a write-lock. */
152 s_flock.l_type = F_WRLCK;
153 fd = open (name, O_RDWR | O_CREAT, 0666);
155 if (fd < 0)
156 return 0;
158 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
159 continue;
161 gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
163 if (!gcov_var.file)
165 close (fd);
166 return 0;
169 if (mode > 0)
170 gcov_var.mode = 1;
171 else if (mode == 0)
173 struct stat st;
175 if (fstat (fd, &st) < 0)
177 fclose (gcov_var.file);
178 gcov_var.file = 0;
179 return 0;
181 if (st.st_size != 0)
182 gcov_var.mode = 1;
183 else
184 gcov_var.mode = mode * 2 + 1;
186 else
187 gcov_var.mode = mode * 2 + 1;
188 #else
189 if (mode >= 0)
190 gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
192 if (gcov_var.file)
193 gcov_var.mode = 1;
194 else if (mode <= 0)
196 gcov_var.file = fopen (name, "w+b");
197 if (gcov_var.file)
198 gcov_var.mode = mode * 2 + 1;
200 if (!gcov_var.file)
201 return 0;
202 #endif
204 setbuf (gcov_var.file, (char *)0);
206 return 1;
209 /* Close the current gcov file. Flushes data to disk. Returns nonzero
210 on failure or error flag set. */
212 GCOV_LINKAGE int
213 gcov_close (void)
215 if (gcov_var.file)
217 #if !IN_GCOV
218 if (gcov_var.offset && gcov_var.mode < 0)
219 gcov_write_block (gcov_var.offset);
220 #endif
221 fclose (gcov_var.file);
222 gcov_var.file = 0;
223 gcov_var.length = 0;
225 #if !IN_LIBGCOV
226 free (gcov_var.buffer);
227 gcov_var.alloc = 0;
228 gcov_var.buffer = 0;
229 #endif
230 gcov_var.mode = 0;
231 return gcov_var.error;
234 #if !IN_LIBGCOV
235 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
236 file. Returns +1 for same endian, -1 for other endian and zero for
237 not EXPECTED. */
239 GCOV_LINKAGE int
240 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
242 if (magic == expected)
243 return 1;
244 magic = (magic >> 16) | (magic << 16);
245 magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
246 if (magic == expected)
248 gcov_var.endian = 1;
249 return -1;
251 return 0;
253 #endif
255 #if !IN_LIBGCOV
256 static void
257 gcov_allocate (unsigned length)
259 size_t new_size = gcov_var.alloc;
261 if (!new_size)
262 new_size = GCOV_BLOCK_SIZE;
263 new_size += length;
264 new_size *= 2;
266 gcov_var.alloc = new_size;
267 gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
269 #endif
271 #if !IN_GCOV
272 /* Write out the current block, if needs be. */
274 static void
275 gcov_write_block (unsigned size)
277 if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
278 gcov_var.error = 1;
279 gcov_var.start += size;
280 gcov_var.offset -= size;
283 /* Allocate space to write BYTES bytes to the gcov file. Return a
284 pointer to those bytes, or NULL on failure. */
286 static gcov_unsigned_t *
287 gcov_write_words (unsigned words)
289 gcov_unsigned_t *result;
291 gcc_assert (gcov_var.mode < 0);
292 #if IN_LIBGCOV
293 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
295 gcov_write_block (GCOV_BLOCK_SIZE);
296 if (gcov_var.offset)
298 gcc_assert (gcov_var.offset == 1);
299 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
302 #else
303 if (gcov_var.offset + words > gcov_var.alloc)
304 gcov_allocate (gcov_var.offset + words);
305 #endif
306 result = &gcov_var.buffer[gcov_var.offset];
307 gcov_var.offset += words;
309 return result;
312 /* Write unsigned VALUE to coverage file. Sets error flag
313 appropriately. */
315 GCOV_LINKAGE void
316 gcov_write_unsigned (gcov_unsigned_t value)
318 gcov_unsigned_t *buffer = gcov_write_words (1);
320 buffer[0] = value;
323 /* Write counter VALUE to coverage file. Sets error flag
324 appropriately. */
326 #if IN_LIBGCOV
327 GCOV_LINKAGE void
328 gcov_write_counter (gcov_type value)
330 gcov_unsigned_t *buffer = gcov_write_words (2);
332 buffer[0] = (gcov_unsigned_t) value;
333 if (sizeof (value) > sizeof (gcov_unsigned_t))
334 buffer[1] = (gcov_unsigned_t) (value >> 32);
335 else
336 buffer[1] = 0;
338 #endif /* IN_LIBGCOV */
340 #if !IN_LIBGCOV
341 /* Write STRING to coverage file. Sets error flag on file
342 error, overflow flag on overflow */
344 GCOV_LINKAGE void
345 gcov_write_string (const char *string)
347 unsigned length = 0;
348 unsigned alloc = 0;
349 gcov_unsigned_t *buffer;
351 if (string)
353 length = strlen (string);
354 alloc = (length + 4) >> 2;
357 buffer = gcov_write_words (1 + alloc);
359 buffer[0] = alloc;
360 buffer[alloc] = 0;
361 memcpy (&buffer[1], string, length);
363 #endif
365 #if !IN_LIBGCOV
366 /* Write a tag TAG and reserve space for the record length. Return a
367 value to be used for gcov_write_length. */
369 GCOV_LINKAGE gcov_position_t
370 gcov_write_tag (gcov_unsigned_t tag)
372 gcov_position_t result = gcov_var.start + gcov_var.offset;
373 gcov_unsigned_t *buffer = gcov_write_words (2);
375 buffer[0] = tag;
376 buffer[1] = 0;
378 return result;
381 /* Write a record length using POSITION, which was returned by
382 gcov_write_tag. The current file position is the end of the
383 record, and is restored before returning. Returns nonzero on
384 overflow. */
386 GCOV_LINKAGE void
387 gcov_write_length (gcov_position_t position)
389 unsigned offset;
390 gcov_unsigned_t length;
391 gcov_unsigned_t *buffer;
393 gcc_assert (gcov_var.mode < 0);
394 gcc_assert (position + 2 <= gcov_var.start + gcov_var.offset);
395 gcc_assert (position >= gcov_var.start);
396 offset = position - gcov_var.start;
397 length = gcov_var.offset - offset - 2;
398 buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
399 buffer[1] = length;
400 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
401 gcov_write_block (gcov_var.offset);
404 #else /* IN_LIBGCOV */
406 /* Write a tag TAG and length LENGTH. */
408 GCOV_LINKAGE void
409 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
411 gcov_unsigned_t *buffer = gcov_write_words (2);
413 buffer[0] = tag;
414 buffer[1] = length;
417 /* Write a summary structure to the gcov file. Return nonzero on
418 overflow. */
420 GCOV_LINKAGE void
421 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
423 unsigned ix, h_ix, bv_ix, h_cnt = 0;
424 const struct gcov_ctr_summary *csum;
425 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE];
427 /* Count number of non-zero histogram entries, and fill in a bit vector
428 of non-zero indices. The histogram is only currently computed for arc
429 counters. */
430 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
431 histo_bitvector[bv_ix] = 0;
432 csum = &summary->ctrs[GCOV_COUNTER_ARCS];
433 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
435 if (csum->histogram[h_ix].num_counters > 0)
437 histo_bitvector[h_ix / 32] |= 1 << (h_ix % 32);
438 h_cnt++;
441 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH(h_cnt));
442 gcov_write_unsigned (summary->checksum);
443 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
445 gcov_write_unsigned (csum->num);
446 gcov_write_unsigned (csum->runs);
447 gcov_write_counter (csum->sum_all);
448 gcov_write_counter (csum->run_max);
449 gcov_write_counter (csum->sum_max);
450 if (ix != GCOV_COUNTER_ARCS)
452 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
453 gcov_write_unsigned (0);
454 continue;
456 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
457 gcov_write_unsigned (histo_bitvector[bv_ix]);
458 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
460 if (!csum->histogram[h_ix].num_counters)
461 continue;
462 gcov_write_unsigned (csum->histogram[h_ix].num_counters);
463 gcov_write_counter (csum->histogram[h_ix].min_value);
464 gcov_write_counter (csum->histogram[h_ix].cum_value);
468 #endif /* IN_LIBGCOV */
470 #endif /*!IN_GCOV */
472 /* Return a pointer to read BYTES bytes from the gcov file. Returns
473 NULL on failure (read past EOF). */
475 static const gcov_unsigned_t *
476 gcov_read_words (unsigned words)
478 const gcov_unsigned_t *result;
479 unsigned excess = gcov_var.length - gcov_var.offset;
481 gcc_assert (gcov_var.mode > 0);
482 if (excess < words)
484 gcov_var.start += gcov_var.offset;
485 #if IN_LIBGCOV
486 if (excess)
488 gcc_assert (excess == 1);
489 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
491 #else
492 memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
493 #endif
494 gcov_var.offset = 0;
495 gcov_var.length = excess;
496 #if IN_LIBGCOV
497 gcc_assert (!gcov_var.length || gcov_var.length == 1);
498 excess = GCOV_BLOCK_SIZE;
499 #else
500 if (gcov_var.length + words > gcov_var.alloc)
501 gcov_allocate (gcov_var.length + words);
502 excess = gcov_var.alloc - gcov_var.length;
503 #endif
504 excess = fread (gcov_var.buffer + gcov_var.length,
505 1, excess << 2, gcov_var.file) >> 2;
506 gcov_var.length += excess;
507 if (gcov_var.length < words)
509 gcov_var.overread += words - gcov_var.length;
510 gcov_var.length = 0;
511 return 0;
514 result = &gcov_var.buffer[gcov_var.offset];
515 gcov_var.offset += words;
516 return result;
519 /* Read unsigned value from a coverage file. Sets error flag on file
520 error, overflow flag on overflow */
522 GCOV_LINKAGE gcov_unsigned_t
523 gcov_read_unsigned (void)
525 gcov_unsigned_t value;
526 const gcov_unsigned_t *buffer = gcov_read_words (1);
528 if (!buffer)
529 return 0;
530 value = from_file (buffer[0]);
531 return value;
534 /* Read counter value from a coverage file. Sets error flag on file
535 error, overflow flag on overflow */
537 GCOV_LINKAGE gcov_type
538 gcov_read_counter (void)
540 gcov_type value;
541 const gcov_unsigned_t *buffer = gcov_read_words (2);
543 if (!buffer)
544 return 0;
545 value = from_file (buffer[0]);
546 if (sizeof (value) > sizeof (gcov_unsigned_t))
547 value |= ((gcov_type) from_file (buffer[1])) << 32;
548 else if (buffer[1])
549 gcov_var.error = -1;
551 return value;
554 /* Read string from coverage file. Returns a pointer to a static
555 buffer, or NULL on empty string. You must copy the string before
556 calling another gcov function. */
558 #if !IN_LIBGCOV || IN_GCOV_TOOL
559 GCOV_LINKAGE const char *
560 gcov_read_string (void)
562 unsigned length = gcov_read_unsigned ();
564 if (!length)
565 return 0;
567 return (const char *) gcov_read_words (length);
569 #endif
571 GCOV_LINKAGE void
572 gcov_read_summary (struct gcov_summary *summary)
574 unsigned ix, h_ix, bv_ix, h_cnt = 0;
575 struct gcov_ctr_summary *csum;
576 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE];
577 unsigned cur_bitvector;
579 summary->checksum = gcov_read_unsigned ();
580 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
582 csum->num = gcov_read_unsigned ();
583 csum->runs = gcov_read_unsigned ();
584 csum->sum_all = gcov_read_counter ();
585 csum->run_max = gcov_read_counter ();
586 csum->sum_max = gcov_read_counter ();
587 memset (csum->histogram, 0,
588 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
589 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
591 histo_bitvector[bv_ix] = gcov_read_unsigned ();
592 #if IN_LIBGCOV
593 /* When building libgcov we don't include system.h, which includes
594 hwint.h (where popcount_hwi is declared). However, libgcov.a
595 is built by the bootstrapped compiler and therefore the builtins
596 are always available. */
597 h_cnt += __builtin_popcount (histo_bitvector[bv_ix]);
598 #else
599 h_cnt += popcount_hwi (histo_bitvector[bv_ix]);
600 #endif
602 bv_ix = 0;
603 h_ix = 0;
604 cur_bitvector = 0;
605 while (h_cnt--)
607 /* Find the index corresponding to the next entry we will read in.
608 First find the next non-zero bitvector and re-initialize
609 the histogram index accordingly, then right shift and increment
610 the index until we find a set bit. */
611 while (!cur_bitvector)
613 h_ix = bv_ix * 32;
614 gcc_assert(bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE);
615 cur_bitvector = histo_bitvector[bv_ix++];
617 while (!(cur_bitvector & 0x1))
619 h_ix++;
620 cur_bitvector >>= 1;
622 gcc_assert(h_ix < GCOV_HISTOGRAM_SIZE);
624 csum->histogram[h_ix].num_counters = gcov_read_unsigned ();
625 csum->histogram[h_ix].min_value = gcov_read_counter ();
626 csum->histogram[h_ix].cum_value = gcov_read_counter ();
627 /* Shift off the index we are done with and increment to the
628 corresponding next histogram entry. */
629 cur_bitvector >>= 1;
630 h_ix++;
635 #if IN_GCOV_TOOL || !IN_LIBGCOV && IN_GCOV != 1
636 /* Read LEN words (unsigned type) and construct MOD_INFO. */
638 GCOV_LINKAGE void
639 gcov_read_module_info (struct gcov_module_info *mod_info,
640 gcov_unsigned_t len)
642 gcov_unsigned_t src_filename_len, filename_len, i, j, num_strings;
643 mod_info->ident = gcov_read_unsigned ();
644 mod_info->is_primary = gcov_read_unsigned ();
645 mod_info->flags = gcov_read_unsigned ();
646 mod_info->lang = gcov_read_unsigned ();
647 mod_info->ggc_memory = gcov_read_unsigned ();
648 mod_info->num_quote_paths = gcov_read_unsigned ();
649 mod_info->num_bracket_paths = gcov_read_unsigned ();
650 mod_info->num_system_paths = gcov_read_unsigned ();
651 mod_info->num_cpp_defines = gcov_read_unsigned ();
652 mod_info->num_cpp_includes = gcov_read_unsigned ();
653 mod_info->num_cl_args = gcov_read_unsigned ();
654 len -= 11;
656 filename_len = gcov_read_unsigned ();
657 mod_info->da_filename = (char *) xmalloc (filename_len *
658 sizeof (gcov_unsigned_t));
659 for (i = 0; i < filename_len; i++)
660 ((gcov_unsigned_t *) mod_info->da_filename)[i] = gcov_read_unsigned ();
661 len -= (filename_len + 1);
663 src_filename_len = gcov_read_unsigned ();
664 mod_info->source_filename = (char *) xmalloc (src_filename_len *
665 sizeof (gcov_unsigned_t));
666 for (i = 0; i < src_filename_len; i++)
667 ((gcov_unsigned_t *) mod_info->source_filename)[i] = gcov_read_unsigned ();
668 len -= (src_filename_len + 1);
670 num_strings = mod_info->num_quote_paths + mod_info->num_bracket_paths
671 + mod_info->num_system_paths
672 + mod_info->num_cpp_defines + mod_info->num_cpp_includes
673 + mod_info->num_cl_args;
674 for (j = 0; j < num_strings; j++)
676 gcov_unsigned_t string_len = gcov_read_unsigned ();
677 mod_info->string_array[j] =
678 (char *) xmalloc (string_len * sizeof (gcov_unsigned_t));
679 for (i = 0; i < string_len; i++)
680 ((gcov_unsigned_t *) mod_info->string_array[j])[i] =
681 gcov_read_unsigned ();
682 len -= (string_len + 1);
684 gcc_assert (!len);
686 #endif
688 #if !IN_LIBGCOV || IN_GCOV_TOOL
689 /* Reset to a known position. BASE should have been obtained from
690 gcov_position, LENGTH should be a record length. */
692 GCOV_LINKAGE void
693 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
695 gcc_assert (gcov_var.mode > 0);
696 base += length;
697 if (base - gcov_var.start <= gcov_var.length)
698 gcov_var.offset = base - gcov_var.start;
699 else
701 gcov_var.offset = gcov_var.length = 0;
702 fseek (gcov_var.file, base << 2, SEEK_SET);
703 gcov_var.start = ftell (gcov_var.file) >> 2;
706 #endif
708 #if IN_LIBGCOV
709 /* Move to a given position in a gcov file. */
711 GCOV_LINKAGE void
712 gcov_seek (gcov_position_t base)
714 gcc_assert (gcov_var.mode < 0);
715 if (gcov_var.offset)
716 gcov_write_block (gcov_var.offset);
717 fseek (gcov_var.file, base << 2, SEEK_SET);
718 gcov_var.start = ftell (gcov_var.file) >> 2;
721 /* Truncate the gcov file at the current position. */
723 GCOV_LINKAGE void
724 gcov_truncate (void)
726 long offs;
727 int filenum;
728 gcc_assert (gcov_var.mode < 0);
729 if (gcov_var.offset)
730 gcov_write_block (gcov_var.offset);
731 offs = ftell (gcov_var.file);
732 filenum = fileno (gcov_var.file);
733 if (offs == -1 || filenum == -1 || ftruncate (filenum, offs))
734 gcov_var.error = 1;
736 #endif
738 #if IN_GCOV > 0
739 /* Return the modification time of the current gcov file. */
741 GCOV_LINKAGE time_t
742 gcov_time (void)
744 struct stat status;
746 if (fstat (fileno (gcov_var.file), &status))
747 return 0;
748 else
749 return status.st_mtime;
751 #endif /* IN_GCOV */
753 #if !IN_GCOV
754 /* Determine the index into histogram for VALUE. */
756 #if IN_LIBGCOV
757 static unsigned
758 #else
759 GCOV_LINKAGE unsigned
760 #endif
761 gcov_histo_index (gcov_type value)
763 gcov_type_unsigned v = (gcov_type_unsigned)value;
764 unsigned r = 0;
765 unsigned prev2bits = 0;
767 /* Find index into log2 scale histogram, where each of the log2
768 sized buckets is divided into 4 linear sub-buckets for better
769 focus in the higher buckets. */
771 /* Find the place of the most-significant bit set. */
772 if (v > 0)
774 #if IN_LIBGCOV
775 /* When building libgcov we don't include system.h, which includes
776 hwint.h (where floor_log2 is declared). However, libgcov.a
777 is built by the bootstrapped compiler and therefore the builtins
778 are always available. */
779 r = sizeof (long long) * __CHAR_BIT__ - 1 - __builtin_clzll (v);
780 #else
781 /* We use floor_log2 from hwint.c, which takes a HOST_WIDE_INT
782 that is either 32 or 64 bits, and gcov_type_unsigned may be 64 bits.
783 Need to check for the case where gcov_type_unsigned is 64 bits
784 and HOST_WIDE_INT is 32 bits and handle it specially. */
785 #if HOST_BITS_PER_WIDEST_INT == HOST_BITS_PER_WIDE_INT
786 r = floor_log2 (v);
787 #elif HOST_BITS_PER_WIDEST_INT == 2 * HOST_BITS_PER_WIDE_INT
788 HOST_WIDE_INT hwi_v = v >> HOST_BITS_PER_WIDE_INT;
789 if (hwi_v)
790 r = floor_log2 (hwi_v) + HOST_BITS_PER_WIDE_INT;
791 else
792 r = floor_log2 ((HOST_WIDE_INT)v);
793 #else
794 gcc_unreachable ();
795 #endif
796 #endif
799 /* If at most the 2 least significant bits are set (value is
800 0 - 3) then that value is our index into the lowest set of
801 four buckets. */
802 if (r < 2)
803 return (unsigned)value;
805 gcc_assert (r < 64);
807 /* Find the two next most significant bits to determine which
808 of the four linear sub-buckets to select. */
809 prev2bits = (v >> (r - 2)) & 0x3;
810 /* Finally, compose the final bucket index from the log2 index and
811 the next 2 bits. The minimum r value at this point is 2 since we
812 returned above if r was 2 or more, so the minimum bucket at this
813 point is 4. */
814 return (r - 1) * 4 + prev2bits;
817 /* Merge SRC_HISTO into TGT_HISTO. The counters are assumed to be in
818 the same relative order in both histograms, and are matched up
819 and merged in reverse order. Each counter is assigned an equal portion of
820 its entry's original cumulative counter value when computing the
821 new merged cum_value. */
823 static void gcov_histogram_merge (gcov_bucket_type *tgt_histo,
824 gcov_bucket_type *src_histo)
826 int src_i, tgt_i, tmp_i = 0;
827 unsigned src_num, tgt_num, merge_num;
828 gcov_type src_cum, tgt_cum, merge_src_cum, merge_tgt_cum, merge_cum;
829 gcov_type merge_min;
830 gcov_bucket_type tmp_histo[GCOV_HISTOGRAM_SIZE];
831 int src_done = 0;
833 memset(tmp_histo, 0, sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
835 /* Assume that the counters are in the same relative order in both
836 histograms. Walk the histograms from largest to smallest entry,
837 matching up and combining counters in order. */
838 src_num = 0;
839 src_cum = 0;
840 src_i = GCOV_HISTOGRAM_SIZE - 1;
841 for (tgt_i = GCOV_HISTOGRAM_SIZE - 1; tgt_i >= 0 && !src_done; tgt_i--)
843 tgt_num = tgt_histo[tgt_i].num_counters;
844 tgt_cum = tgt_histo[tgt_i].cum_value;
845 /* Keep going until all of the target histogram's counters at this
846 position have been matched and merged with counters from the
847 source histogram. */
848 while (tgt_num > 0 && !src_done)
850 /* If this is either the first time through this loop or we just
851 exhausted the previous non-zero source histogram entry, look
852 for the next non-zero source histogram entry. */
853 if (!src_num)
855 /* Locate the next non-zero entry. */
856 while (src_i >= 0 && !src_histo[src_i].num_counters)
857 src_i--;
858 /* If source histogram has fewer counters, then just copy over the
859 remaining target counters and quit. */
860 if (src_i < 0)
862 tmp_histo[tgt_i].num_counters += tgt_num;
863 tmp_histo[tgt_i].cum_value += tgt_cum;
864 if (!tmp_histo[tgt_i].min_value ||
865 tgt_histo[tgt_i].min_value < tmp_histo[tgt_i].min_value)
866 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
867 while (--tgt_i >= 0)
869 tmp_histo[tgt_i].num_counters
870 += tgt_histo[tgt_i].num_counters;
871 tmp_histo[tgt_i].cum_value += tgt_histo[tgt_i].cum_value;
872 if (!tmp_histo[tgt_i].min_value ||
873 tgt_histo[tgt_i].min_value
874 < tmp_histo[tgt_i].min_value)
875 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
878 src_done = 1;
879 break;
882 src_num = src_histo[src_i].num_counters;
883 src_cum = src_histo[src_i].cum_value;
886 /* The number of counters to merge on this pass is the minimum
887 of the remaining counters from the current target and source
888 histogram entries. */
889 merge_num = tgt_num;
890 if (src_num < merge_num)
891 merge_num = src_num;
893 /* The merged min_value is the sum of the min_values from target
894 and source. */
895 merge_min = tgt_histo[tgt_i].min_value + src_histo[src_i].min_value;
897 /* Compute the portion of source and target entries' cum_value
898 that will be apportioned to the counters being merged.
899 The total remaining cum_value from each entry is divided
900 equally among the counters from that histogram entry if we
901 are not merging all of them. */
902 merge_src_cum = src_cum;
903 if (merge_num < src_num)
904 merge_src_cum = merge_num * src_cum / src_num;
905 merge_tgt_cum = tgt_cum;
906 if (merge_num < tgt_num)
907 merge_tgt_cum = merge_num * tgt_cum / tgt_num;
908 /* The merged cum_value is the sum of the source and target
909 components. */
910 merge_cum = merge_src_cum + merge_tgt_cum;
912 /* Update the remaining number of counters and cum_value left
913 to be merged from this source and target entry. */
914 src_cum -= merge_src_cum;
915 tgt_cum -= merge_tgt_cum;
916 src_num -= merge_num;
917 tgt_num -= merge_num;
919 /* The merged counters get placed in the new merged histogram
920 at the entry for the merged min_value. */
921 tmp_i = gcov_histo_index(merge_min);
922 gcc_assert (tmp_i < GCOV_HISTOGRAM_SIZE);
923 tmp_histo[tmp_i].num_counters += merge_num;
924 tmp_histo[tmp_i].cum_value += merge_cum;
925 if (!tmp_histo[tmp_i].min_value ||
926 merge_min < tmp_histo[tmp_i].min_value)
927 tmp_histo[tmp_i].min_value = merge_min;
929 /* Ensure the search for the next non-zero src_histo entry starts
930 at the next smallest histogram bucket. */
931 if (!src_num)
932 src_i--;
936 gcc_assert (tgt_i < 0);
938 /* In the case where there were more counters in the source histogram,
939 accumulate the remaining unmerged cumulative counter values. Add
940 those to the smallest non-zero target histogram entry. Otherwise,
941 the total cumulative counter values in the histogram will be smaller
942 than the sum_all stored in the summary, which will complicate
943 computing the working set information from the histogram later on. */
944 if (src_num)
945 src_i--;
946 while (src_i >= 0)
948 src_cum += src_histo[src_i].cum_value;
949 src_i--;
951 /* At this point, tmp_i should be the smallest non-zero entry in the
952 tmp_histo. */
953 gcc_assert(tmp_i >= 0 && tmp_i < GCOV_HISTOGRAM_SIZE
954 && tmp_histo[tmp_i].num_counters > 0);
955 tmp_histo[tmp_i].cum_value += src_cum;
957 /* Finally, copy the merged histogram into tgt_histo. */
958 memcpy(tgt_histo, tmp_histo, sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
960 #endif /* !IN_GCOV */
962 /* This is used by gcov-dump (IN_GCOV == -1) and in the compiler
963 (!IN_GCOV && !IN_LIBGCOV). */
964 #if IN_GCOV <= 0 && !IN_LIBGCOV
965 /* Compute the working set information from the counter histogram in
966 the profile summary. This is an array of information corresponding to a
967 range of percentages of the total execution count (sum_all), and includes
968 the number of counters required to cover that working set percentage and
969 the minimum counter value in that working set. */
971 GCOV_LINKAGE void
972 compute_working_sets (const struct gcov_ctr_summary *summary,
973 gcov_working_set_t *gcov_working_sets)
975 gcov_type working_set_cum_values[NUM_GCOV_WORKING_SETS];
976 gcov_type ws_cum_hotness_incr;
977 gcov_type cum, tmp_cum;
978 const gcov_bucket_type *histo_bucket;
979 unsigned ws_ix, c_num, count;
980 int h_ix;
982 /* Compute the amount of sum_all that the cumulative hotness grows
983 by in each successive working set entry, which depends on the
984 number of working set entries. */
985 ws_cum_hotness_incr = summary->sum_all / NUM_GCOV_WORKING_SETS;
987 /* Next fill in an array of the cumulative hotness values corresponding
988 to each working set summary entry we are going to compute below.
989 Skip 0% statistics, which can be extrapolated from the
990 rest of the summary data. */
991 cum = ws_cum_hotness_incr;
992 for (ws_ix = 0; ws_ix < NUM_GCOV_WORKING_SETS;
993 ws_ix++, cum += ws_cum_hotness_incr)
994 working_set_cum_values[ws_ix] = cum;
995 /* The last summary entry is reserved for (roughly) 99.9% of the
996 working set. Divide by 1024 so it becomes a shift, which gives
997 almost exactly 99.9%. */
998 working_set_cum_values[NUM_GCOV_WORKING_SETS-1]
999 = summary->sum_all - summary->sum_all/1024;
1001 /* Next, walk through the histogram in decending order of hotness
1002 and compute the statistics for the working set summary array.
1003 As histogram entries are accumulated, we check to see which
1004 working set entries have had their expected cum_value reached
1005 and fill them in, walking the working set entries in increasing
1006 size of cum_value. */
1007 ws_ix = 0; /* The current entry into the working set array. */
1008 cum = 0; /* The current accumulated counter sum. */
1009 count = 0; /* The current accumulated count of block counters. */
1010 for (h_ix = GCOV_HISTOGRAM_SIZE - 1;
1011 h_ix >= 0 && ws_ix < NUM_GCOV_WORKING_SETS; h_ix--)
1013 histo_bucket = &summary->histogram[h_ix];
1015 /* If we haven't reached the required cumulative counter value for
1016 the current working set percentage, simply accumulate this histogram
1017 entry into the running sums and continue to the next histogram
1018 entry. */
1019 if (cum + histo_bucket->cum_value < working_set_cum_values[ws_ix])
1021 cum += histo_bucket->cum_value;
1022 count += histo_bucket->num_counters;
1023 continue;
1026 /* If adding the current histogram entry's cumulative counter value
1027 causes us to exceed the current working set size, then estimate
1028 how many of this histogram entry's counter values are required to
1029 reach the working set size, and fill in working set entries
1030 as we reach their expected cumulative value. */
1031 for (c_num = 0, tmp_cum = cum;
1032 c_num < histo_bucket->num_counters && ws_ix < NUM_GCOV_WORKING_SETS;
1033 c_num++)
1035 count++;
1036 /* If we haven't reached the last histogram entry counter, add
1037 in the minimum value again. This will underestimate the
1038 cumulative sum so far, because many of the counter values in this
1039 entry may have been larger than the minimum. We could add in the
1040 average value every time, but that would require an expensive
1041 divide operation. */
1042 if (c_num + 1 < histo_bucket->num_counters)
1043 tmp_cum += histo_bucket->min_value;
1044 /* If we have reached the last histogram entry counter, then add
1045 in the entire cumulative value. */
1046 else
1047 tmp_cum = cum + histo_bucket->cum_value;
1049 /* Next walk through successive working set entries and fill in
1050 the statistics for any whose size we have reached by accumulating
1051 this histogram counter. */
1052 while (ws_ix < NUM_GCOV_WORKING_SETS
1053 && tmp_cum >= working_set_cum_values[ws_ix])
1055 gcov_working_sets[ws_ix].num_counters = count;
1056 gcov_working_sets[ws_ix].min_counter
1057 = histo_bucket->min_value;
1058 ws_ix++;
1061 /* Finally, update the running cumulative value since we were
1062 using a temporary above. */
1063 cum += histo_bucket->cum_value;
1065 gcc_assert (ws_ix == NUM_GCOV_WORKING_SETS);
1067 #endif /* IN_GCOV <= 0 && !IN_LIBGCOV */