libgcov-merge.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2022 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgcov.h"
  21. #if defined(inhibit_libc)
  22. /* If libc and its header files are not available, provide dummy functions. */
  23. #ifdef L_gcov_merge_add
  24. void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
  25. unsigned n_counters __attribute__ ((unused))) {}
  26. #endif
  27. #ifdef L_gcov_merge_topn
  28. void __gcov_merge_topn (gcov_type *counters __attribute__ ((unused)),
  29. unsigned n_counters __attribute__ ((unused))) {}
  30. #endif
  31. #else
  32. #ifdef L_gcov_merge_add
  33. /* The profile merging function that just adds the counters. It is given
  34. an array COUNTERS of N_COUNTERS old counters and it reads the same number
  35. of counters from the gcov file. */
  36. void
  37. __gcov_merge_add (gcov_type *counters, unsigned n_counters)
  38. {
  39. for (; n_counters; counters++, n_counters--)
  40. *counters += gcov_get_counter ();
  41. }
  42. #endif /* L_gcov_merge_add */
  43. #ifdef L_gcov_merge_ior
  44. /* The profile merging function that just adds the counters. It is given
  45. an array COUNTERS of N_COUNTERS old counters and it reads the same number
  46. of counters from the gcov file. */
  47. void
  48. __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
  49. {
  50. for (; n_counters; counters++, n_counters--)
  51. *counters |= gcov_get_counter_target ();
  52. }
  53. #endif
  54. #ifdef L_gcov_merge_time_profile
  55. /* Time profiles are merged so that minimum from all valid (greater than zero)
  56. is stored. There could be a fork that creates new counters. To have
  57. the profile stable, we chosen to pick the smallest function visit time. */
  58. void
  59. __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
  60. {
  61. unsigned int i;
  62. gcov_type value;
  63. for (i = 0; i < n_counters; i++)
  64. {
  65. value = gcov_get_counter_target ();
  66. if (value && (!counters[i] || value < counters[i]))
  67. counters[i] = value;
  68. }
  69. }
  70. #endif /* L_gcov_merge_time_profile */
  71. #ifdef L_gcov_merge_topn
  72. /* The profile merging function for choosing the most common value.
  73. It is given an array COUNTERS of N_COUNTERS old counters and it
  74. reads the same number of counters from the gcov file. The counters
  75. are split into pairs where the members of the tuple have
  76. meanings:
  77. -- the stored candidate on the most common value of the measured entity
  78. -- counter
  79. We use -TOTAL for situation when merging dropped some values.
  80. The information is used for -fprofile-reproducible flag.
  81. */
  82. void
  83. __gcov_merge_topn (gcov_type *counters, unsigned n_counters)
  84. {
  85. gcc_assert (!(n_counters % GCOV_TOPN_MEM_COUNTERS));
  86. for (unsigned i = 0; i < (n_counters / GCOV_TOPN_MEM_COUNTERS); i++)
  87. {
  88. /* First value is number of total executions of the profiler. */
  89. gcov_type all = gcov_get_counter_ignore_scaling (-1);
  90. gcov_type n = gcov_get_counter_ignore_scaling (-1);
  91. unsigned full = all < 0;
  92. gcov_type *total = &counters[GCOV_TOPN_MEM_COUNTERS * i];
  93. *total += full ? -all : all;
  94. for (unsigned j = 0; j < n; j++)
  95. {
  96. gcov_type value = gcov_get_counter_target ();
  97. gcov_type count = gcov_get_counter_ignore_scaling (-1);
  98. // TODO: we should use atomic here
  99. full |= gcov_topn_add_value (counters + GCOV_TOPN_MEM_COUNTERS * i,
  100. value, count, 0, 0);
  101. }
  102. if (full)
  103. *total = -(*total);
  104. }
  105. }
  106. #endif /* L_gcov_merge_topn */
  107. #endif /* inhibit_libc */