comparison gcc/gcov-io.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* File format for coverage information 1 /* File format for coverage information
2 Copyright (C) 1996-2017 Free Software Foundation, Inc. 2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>. 3 Contributed by Bob Manson <manson@cygnus.com>.
4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>. 4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
444 overflow. */ 444 overflow. */
445 445
446 GCOV_LINKAGE void 446 GCOV_LINKAGE void
447 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary) 447 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
448 { 448 {
449 unsigned ix, h_ix, bv_ix, h_cnt = 0; 449 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
450 const struct gcov_ctr_summary *csum; 450 gcov_write_unsigned (summary->runs);
451 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE]; 451 gcov_write_unsigned (summary->sum_max);
452 452 }
453 /* Count number of non-zero histogram entries, and fill in a bit vector 453
454 of non-zero indices. The histogram is only currently computed for arc
455 counters. */
456 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
457 histo_bitvector[bv_ix] = 0;
458 csum = &summary->ctrs[GCOV_COUNTER_ARCS];
459 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
460 if (csum->histogram[h_ix].num_counters)
461 {
462 histo_bitvector[h_ix / 32] |= 1 << (h_ix % 32);
463 h_cnt++;
464 }
465 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH (h_cnt));
466 gcov_write_unsigned (summary->checksum);
467 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
468 {
469 gcov_write_unsigned (csum->num);
470 gcov_write_unsigned (csum->runs);
471 gcov_write_counter (csum->sum_all);
472 gcov_write_counter (csum->run_max);
473 gcov_write_counter (csum->sum_max);
474 if (ix != GCOV_COUNTER_ARCS)
475 {
476 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
477 gcov_write_unsigned (0);
478 continue;
479 }
480 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
481 gcov_write_unsigned (histo_bitvector[bv_ix]);
482 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
483 {
484 if (!csum->histogram[h_ix].num_counters)
485 continue;
486 gcov_write_unsigned (csum->histogram[h_ix].num_counters);
487 gcov_write_counter (csum->histogram[h_ix].min_value);
488 gcov_write_counter (csum->histogram[h_ix].cum_value);
489 }
490 }
491 }
492 #endif /* IN_LIBGCOV */ 454 #endif /* IN_LIBGCOV */
493 455
494 #endif /*!IN_GCOV */ 456 #endif /*!IN_GCOV */
495 457
496 /* Return a pointer to read BYTES bytes from the gcov file. Returns 458 /* Return a pointer to read BYTES bytes from the gcov file. Returns
574 gcov_var.error = -1; 536 gcov_var.error = -1;
575 537
576 return value; 538 return value;
577 } 539 }
578 540
541 /* Mangle filename path of BASE and output new allocated pointer with
542 mangled path. */
543
544 char *
545 mangle_path (char const *base)
546 {
547 /* Convert '/' to '#', convert '..' to '^',
548 convert ':' to '~' on DOS based file system. */
549 const char *probe;
550 char *buffer = (char *)xmalloc (strlen (base) + 10);
551 char *ptr = buffer;
552
553 #if HAVE_DOS_BASED_FILE_SYSTEM
554 if (base[0] && base[1] == ':')
555 {
556 ptr[0] = base[0];
557 ptr[1] = '~';
558 ptr += 2;
559 base += 2;
560 }
561 #endif
562 for (; *base; base = probe)
563 {
564 size_t len;
565
566 for (probe = base; *probe; probe++)
567 if (*probe == '/')
568 break;
569 len = probe - base;
570 if (len == 2 && base[0] == '.' && base[1] == '.')
571 *ptr++ = '^';
572 else
573 {
574 memcpy (ptr, base, len);
575 ptr += len;
576 }
577 if (*probe)
578 {
579 *ptr++ = '#';
580 probe++;
581 }
582 }
583
584 /* Terminate the string. */
585 *ptr = '\0';
586
587 return buffer;
588 }
589
579 /* We need to expose the below function when compiling for gcov-tool. */ 590 /* We need to expose the below function when compiling for gcov-tool. */
580 591
581 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL) 592 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
582 /* Read string from coverage file. Returns a pointer to a static 593 /* Read string from coverage file. Returns a pointer to a static
583 buffer, or NULL on empty string. You must copy the string before 594 buffer, or NULL on empty string. You must copy the string before
596 #endif 607 #endif
597 608
598 GCOV_LINKAGE void 609 GCOV_LINKAGE void
599 gcov_read_summary (struct gcov_summary *summary) 610 gcov_read_summary (struct gcov_summary *summary)
600 { 611 {
601 unsigned ix, h_ix, bv_ix, h_cnt = 0; 612 summary->runs = gcov_read_unsigned ();
602 struct gcov_ctr_summary *csum; 613 summary->sum_max = gcov_read_unsigned ();
603 unsigned histo_bitvector[GCOV_HISTOGRAM_BITVECTOR_SIZE];
604 unsigned cur_bitvector;
605
606 summary->checksum = gcov_read_unsigned ();
607 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
608 {
609 csum->num = gcov_read_unsigned ();
610 csum->runs = gcov_read_unsigned ();
611 csum->sum_all = gcov_read_counter ();
612 csum->run_max = gcov_read_counter ();
613 csum->sum_max = gcov_read_counter ();
614 memset (csum->histogram, 0,
615 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
616 for (bv_ix = 0; bv_ix < GCOV_HISTOGRAM_BITVECTOR_SIZE; bv_ix++)
617 {
618 histo_bitvector[bv_ix] = gcov_read_unsigned ();
619 #if IN_LIBGCOV
620 /* When building libgcov we don't include system.h, which includes
621 hwint.h (where popcount_hwi is declared). However, libgcov.a
622 is built by the bootstrapped compiler and therefore the builtins
623 are always available. */
624 h_cnt += __builtin_popcount (histo_bitvector[bv_ix]);
625 #else
626 h_cnt += popcount_hwi (histo_bitvector[bv_ix]);
627 #endif
628 }
629 bv_ix = 0;
630 h_ix = 0;
631 cur_bitvector = 0;
632 while (h_cnt--)
633 {
634 /* Find the index corresponding to the next entry we will read in.
635 First find the next non-zero bitvector and re-initialize
636 the histogram index accordingly, then right shift and increment
637 the index until we find a set bit. */
638 while (!cur_bitvector)
639 {
640 h_ix = bv_ix * 32;
641 if (bv_ix >= GCOV_HISTOGRAM_BITVECTOR_SIZE)
642 gcov_error ("corrupted profile info: summary histogram "
643 "bitvector is corrupt");
644 cur_bitvector = histo_bitvector[bv_ix++];
645 }
646 while (!(cur_bitvector & 0x1))
647 {
648 h_ix++;
649 cur_bitvector >>= 1;
650 }
651 if (h_ix >= GCOV_HISTOGRAM_SIZE)
652 gcov_error ("corrupted profile info: summary histogram "
653 "index is corrupt");
654
655 csum->histogram[h_ix].num_counters = gcov_read_unsigned ();
656 csum->histogram[h_ix].min_value = gcov_read_counter ();
657 csum->histogram[h_ix].cum_value = gcov_read_counter ();
658 /* Shift off the index we are done with and increment to the
659 corresponding next histogram entry. */
660 cur_bitvector >>= 1;
661 h_ix++;
662 }
663 }
664 } 614 }
665 615
666 /* We need to expose the below function when compiling for gcov-tool. */ 616 /* We need to expose the below function when compiling for gcov-tool. */
667 617
668 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL) 618 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
710 return 0; 660 return 0;
711 else 661 else
712 return status.st_mtime; 662 return status.st_mtime;
713 } 663 }
714 #endif /* IN_GCOV */ 664 #endif /* IN_GCOV */
715
716 #if !IN_GCOV
717 /* Determine the index into histogram for VALUE. */
718
719 #if IN_LIBGCOV
720 static unsigned
721 #else
722 GCOV_LINKAGE unsigned
723 #endif
724 gcov_histo_index (gcov_type value)
725 {
726 gcov_type_unsigned v = (gcov_type_unsigned)value;
727 unsigned r = 0;
728 unsigned prev2bits = 0;
729
730 /* Find index into log2 scale histogram, where each of the log2
731 sized buckets is divided into 4 linear sub-buckets for better
732 focus in the higher buckets. */
733
734 /* Find the place of the most-significant bit set. */
735 if (v > 0)
736 {
737 #if IN_LIBGCOV
738 /* When building libgcov we don't include system.h, which includes
739 hwint.h (where floor_log2 is declared). However, libgcov.a
740 is built by the bootstrapped compiler and therefore the builtins
741 are always available. */
742 r = sizeof (long long) * __CHAR_BIT__ - 1 - __builtin_clzll (v);
743 #else
744 /* We use floor_log2 from hwint.c, which takes a HOST_WIDE_INT
745 that is 64 bits and gcov_type_unsigned is 64 bits. */
746 r = floor_log2 (v);
747 #endif
748 }
749
750 /* If at most the 2 least significant bits are set (value is
751 0 - 3) then that value is our index into the lowest set of
752 four buckets. */
753 if (r < 2)
754 return (unsigned)value;
755
756 gcov_nonruntime_assert (r < 64);
757
758 /* Find the two next most significant bits to determine which
759 of the four linear sub-buckets to select. */
760 prev2bits = (v >> (r - 2)) & 0x3;
761 /* Finally, compose the final bucket index from the log2 index and
762 the next 2 bits. The minimum r value at this point is 2 since we
763 returned above if r was 2 or more, so the minimum bucket at this
764 point is 4. */
765 return (r - 1) * 4 + prev2bits;
766 }
767
768 /* Merge SRC_HISTO into TGT_HISTO. The counters are assumed to be in
769 the same relative order in both histograms, and are matched up
770 and merged in reverse order. Each counter is assigned an equal portion of
771 its entry's original cumulative counter value when computing the
772 new merged cum_value. */
773
774 static void gcov_histogram_merge (gcov_bucket_type *tgt_histo,
775 gcov_bucket_type *src_histo)
776 {
777 int src_i, tgt_i, tmp_i = 0;
778 unsigned src_num, tgt_num, merge_num;
779 gcov_type src_cum, tgt_cum, merge_src_cum, merge_tgt_cum, merge_cum;
780 gcov_type merge_min;
781 gcov_bucket_type tmp_histo[GCOV_HISTOGRAM_SIZE];
782 int src_done = 0;
783
784 memset (tmp_histo, 0, sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
785
786 /* Assume that the counters are in the same relative order in both
787 histograms. Walk the histograms from largest to smallest entry,
788 matching up and combining counters in order. */
789 src_num = 0;
790 src_cum = 0;
791 src_i = GCOV_HISTOGRAM_SIZE - 1;
792 for (tgt_i = GCOV_HISTOGRAM_SIZE - 1; tgt_i >= 0 && !src_done; tgt_i--)
793 {
794 tgt_num = tgt_histo[tgt_i].num_counters;
795 tgt_cum = tgt_histo[tgt_i].cum_value;
796 /* Keep going until all of the target histogram's counters at this
797 position have been matched and merged with counters from the
798 source histogram. */
799 while (tgt_num > 0 && !src_done)
800 {
801 /* If this is either the first time through this loop or we just
802 exhausted the previous non-zero source histogram entry, look
803 for the next non-zero source histogram entry. */
804 if (!src_num)
805 {
806 /* Locate the next non-zero entry. */
807 while (src_i >= 0 && !src_histo[src_i].num_counters)
808 src_i--;
809 /* If source histogram has fewer counters, then just copy over the
810 remaining target counters and quit. */
811 if (src_i < 0)
812 {
813 tmp_histo[tgt_i].num_counters += tgt_num;
814 tmp_histo[tgt_i].cum_value += tgt_cum;
815 if (!tmp_histo[tgt_i].min_value ||
816 tgt_histo[tgt_i].min_value < tmp_histo[tgt_i].min_value)
817 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
818 while (--tgt_i >= 0)
819 {
820 tmp_histo[tgt_i].num_counters
821 += tgt_histo[tgt_i].num_counters;
822 tmp_histo[tgt_i].cum_value += tgt_histo[tgt_i].cum_value;
823 if (!tmp_histo[tgt_i].min_value ||
824 tgt_histo[tgt_i].min_value
825 < tmp_histo[tgt_i].min_value)
826 tmp_histo[tgt_i].min_value = tgt_histo[tgt_i].min_value;
827 }
828
829 src_done = 1;
830 break;
831 }
832
833 src_num = src_histo[src_i].num_counters;
834 src_cum = src_histo[src_i].cum_value;
835 }
836
837 /* The number of counters to merge on this pass is the minimum
838 of the remaining counters from the current target and source
839 histogram entries. */
840 merge_num = tgt_num;
841 if (src_num < merge_num)
842 merge_num = src_num;
843
844 /* The merged min_value is the sum of the min_values from target
845 and source. */
846 merge_min = tgt_histo[tgt_i].min_value + src_histo[src_i].min_value;
847
848 /* Compute the portion of source and target entries' cum_value
849 that will be apportioned to the counters being merged.
850 The total remaining cum_value from each entry is divided
851 equally among the counters from that histogram entry if we
852 are not merging all of them. */
853 merge_src_cum = src_cum;
854 if (merge_num < src_num)
855 merge_src_cum = merge_num * src_cum / src_num;
856 merge_tgt_cum = tgt_cum;
857 if (merge_num < tgt_num)
858 merge_tgt_cum = merge_num * tgt_cum / tgt_num;
859 /* The merged cum_value is the sum of the source and target
860 components. */
861 merge_cum = merge_src_cum + merge_tgt_cum;
862
863 /* Update the remaining number of counters and cum_value left
864 to be merged from this source and target entry. */
865 src_cum -= merge_src_cum;
866 tgt_cum -= merge_tgt_cum;
867 src_num -= merge_num;
868 tgt_num -= merge_num;
869
870 /* The merged counters get placed in the new merged histogram
871 at the entry for the merged min_value. */
872 tmp_i = gcov_histo_index (merge_min);
873 gcov_nonruntime_assert (tmp_i < GCOV_HISTOGRAM_SIZE);
874 tmp_histo[tmp_i].num_counters += merge_num;
875 tmp_histo[tmp_i].cum_value += merge_cum;
876 if (!tmp_histo[tmp_i].min_value ||
877 merge_min < tmp_histo[tmp_i].min_value)
878 tmp_histo[tmp_i].min_value = merge_min;
879
880 /* Ensure the search for the next non-zero src_histo entry starts
881 at the next smallest histogram bucket. */
882 if (!src_num)
883 src_i--;
884 }
885 }
886
887 gcov_nonruntime_assert (tgt_i < 0);
888
889 /* In the case where there were more counters in the source histogram,
890 accumulate the remaining unmerged cumulative counter values. Add
891 those to the smallest non-zero target histogram entry. Otherwise,
892 the total cumulative counter values in the histogram will be smaller
893 than the sum_all stored in the summary, which will complicate
894 computing the working set information from the histogram later on. */
895 if (src_num)
896 src_i--;
897 while (src_i >= 0)
898 {
899 src_cum += src_histo[src_i].cum_value;
900 src_i--;
901 }
902 /* At this point, tmp_i should be the smallest non-zero entry in the
903 tmp_histo. */
904 gcov_nonruntime_assert (tmp_i >= 0 && tmp_i < GCOV_HISTOGRAM_SIZE
905 && tmp_histo[tmp_i].num_counters > 0);
906 tmp_histo[tmp_i].cum_value += src_cum;
907
908 /* Finally, copy the merged histogram into tgt_histo. */
909 memcpy (tgt_histo, tmp_histo,
910 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
911 }
912 #endif /* !IN_GCOV */
913
914 /* This is used by gcov-dump (IN_GCOV == -1) and in the compiler
915 (!IN_GCOV && !IN_LIBGCOV). */
916 #if IN_GCOV <= 0 && !IN_LIBGCOV
917 /* Compute the working set information from the counter histogram in
918 the profile summary. This is an array of information corresponding to a
919 range of percentages of the total execution count (sum_all), and includes
920 the number of counters required to cover that working set percentage and
921 the minimum counter value in that working set. */
922
923 GCOV_LINKAGE void
924 compute_working_sets (const struct gcov_ctr_summary *summary,
925 gcov_working_set_t *gcov_working_sets)
926 {
927 gcov_type working_set_cum_values[NUM_GCOV_WORKING_SETS];
928 gcov_type ws_cum_hotness_incr;
929 gcov_type cum, tmp_cum;
930 const gcov_bucket_type *histo_bucket;
931 unsigned ws_ix, c_num, count;
932 int h_ix;
933
934 /* Compute the amount of sum_all that the cumulative hotness grows
935 by in each successive working set entry, which depends on the
936 number of working set entries. */
937 ws_cum_hotness_incr = summary->sum_all / NUM_GCOV_WORKING_SETS;
938
939 /* Next fill in an array of the cumulative hotness values corresponding
940 to each working set summary entry we are going to compute below.
941 Skip 0% statistics, which can be extrapolated from the
942 rest of the summary data. */
943 cum = ws_cum_hotness_incr;
944 for (ws_ix = 0; ws_ix < NUM_GCOV_WORKING_SETS;
945 ws_ix++, cum += ws_cum_hotness_incr)
946 working_set_cum_values[ws_ix] = cum;
947 /* The last summary entry is reserved for (roughly) 99.9% of the
948 working set. Divide by 1024 so it becomes a shift, which gives
949 almost exactly 99.9%. */
950 working_set_cum_values[NUM_GCOV_WORKING_SETS-1]
951 = summary->sum_all - summary->sum_all/1024;
952
953 /* Next, walk through the histogram in decending order of hotness
954 and compute the statistics for the working set summary array.
955 As histogram entries are accumulated, we check to see which
956 working set entries have had their expected cum_value reached
957 and fill them in, walking the working set entries in increasing
958 size of cum_value. */
959 ws_ix = 0; /* The current entry into the working set array. */
960 cum = 0; /* The current accumulated counter sum. */
961 count = 0; /* The current accumulated count of block counters. */
962 for (h_ix = GCOV_HISTOGRAM_SIZE - 1;
963 h_ix >= 0 && ws_ix < NUM_GCOV_WORKING_SETS; h_ix--)
964 {
965 histo_bucket = &summary->histogram[h_ix];
966
967 /* If we haven't reached the required cumulative counter value for
968 the current working set percentage, simply accumulate this histogram
969 entry into the running sums and continue to the next histogram
970 entry. */
971 if (cum + histo_bucket->cum_value < working_set_cum_values[ws_ix])
972 {
973 cum += histo_bucket->cum_value;
974 count += histo_bucket->num_counters;
975 continue;
976 }
977
978 /* If adding the current histogram entry's cumulative counter value
979 causes us to exceed the current working set size, then estimate
980 how many of this histogram entry's counter values are required to
981 reach the working set size, and fill in working set entries
982 as we reach their expected cumulative value. */
983 for (c_num = 0, tmp_cum = cum;
984 c_num < histo_bucket->num_counters && ws_ix < NUM_GCOV_WORKING_SETS;
985 c_num++)
986 {
987 count++;
988 /* If we haven't reached the last histogram entry counter, add
989 in the minimum value again. This will underestimate the
990 cumulative sum so far, because many of the counter values in this
991 entry may have been larger than the minimum. We could add in the
992 average value every time, but that would require an expensive
993 divide operation. */
994 if (c_num + 1 < histo_bucket->num_counters)
995 tmp_cum += histo_bucket->min_value;
996 /* If we have reached the last histogram entry counter, then add
997 in the entire cumulative value. */
998 else
999 tmp_cum = cum + histo_bucket->cum_value;
1000
1001 /* Next walk through successive working set entries and fill in
1002 the statistics for any whose size we have reached by accumulating
1003 this histogram counter. */
1004 while (ws_ix < NUM_GCOV_WORKING_SETS
1005 && tmp_cum >= working_set_cum_values[ws_ix])
1006 {
1007 gcov_working_sets[ws_ix].num_counters = count;
1008 gcov_working_sets[ws_ix].min_counter
1009 = histo_bucket->min_value;
1010 ws_ix++;
1011 }
1012 }
1013 /* Finally, update the running cumulative value since we were
1014 using a temporary above. */
1015 cum += histo_bucket->cum_value;
1016 }
1017 gcov_nonruntime_assert (ws_ix == NUM_GCOV_WORKING_SETS);
1018 }
1019 #endif /* IN_GCOV <= 0 && !IN_LIBGCOV */