libgcov-merge.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2015 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_single
  28. void __gcov_merge_single (gcov_type *counters __attribute__ ((unused)),
  29. unsigned n_counters __attribute__ ((unused))) {}
  30. #endif
  31. #ifdef L_gcov_merge_delta
  32. void __gcov_merge_delta (gcov_type *counters __attribute__ ((unused)),
  33. unsigned n_counters __attribute__ ((unused))) {}
  34. #endif
  35. #else
  36. #ifdef L_gcov_merge_add
  37. /* The profile merging function that just adds the counters. It is given
  38. an array COUNTERS of N_COUNTERS old counters and it reads the same number
  39. of counters from the gcov file. */
  40. void
  41. __gcov_merge_add (gcov_type *counters, unsigned n_counters)
  42. {
  43. for (; n_counters; counters++, n_counters--)
  44. *counters += gcov_get_counter ();
  45. }
  46. #endif /* L_gcov_merge_add */
  47. #ifdef L_gcov_merge_ior
  48. /* The profile merging function that just adds the counters. It is given
  49. an array COUNTERS of N_COUNTERS old counters and it reads the same number
  50. of counters from the gcov file. */
  51. void
  52. __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
  53. {
  54. for (; n_counters; counters++, n_counters--)
  55. *counters |= gcov_get_counter_target ();
  56. }
  57. #endif
  58. #ifdef L_gcov_merge_time_profile
  59. /* Time profiles are merged so that minimum from all valid (greater than zero)
  60. is stored. There could be a fork that creates new counters. To have
  61. the profile stable, we chosen to pick the smallest function visit time. */
  62. void
  63. __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
  64. {
  65. unsigned int i;
  66. gcov_type value;
  67. for (i = 0; i < n_counters; i++)
  68. {
  69. value = gcov_get_counter_target ();
  70. if (value && (!counters[i] || value < counters[i]))
  71. counters[i] = value;
  72. }
  73. }
  74. #endif /* L_gcov_merge_time_profile */
  75. #ifdef L_gcov_merge_single
  76. /* The profile merging function for choosing the most common value.
  77. It is given an array COUNTERS of N_COUNTERS old counters and it
  78. reads the same number of counters from the gcov file. The counters
  79. are split into 3-tuples where the members of the tuple have
  80. meanings:
  81. -- the stored candidate on the most common value of the measured entity
  82. -- counter
  83. -- total number of evaluations of the value */
  84. void
  85. __gcov_merge_single (gcov_type *counters, unsigned n_counters)
  86. {
  87. unsigned i, n_measures;
  88. gcov_type value, counter, all;
  89. gcc_assert (!(n_counters % 3));
  90. n_measures = n_counters / 3;
  91. for (i = 0; i < n_measures; i++, counters += 3)
  92. {
  93. value = gcov_get_counter_target ();
  94. counter = gcov_get_counter ();
  95. all = gcov_get_counter ();
  96. if (counters[0] == value)
  97. counters[1] += counter;
  98. else if (counter > counters[1])
  99. {
  100. counters[0] = value;
  101. counters[1] = counter - counters[1];
  102. }
  103. else
  104. counters[1] -= counter;
  105. counters[2] += all;
  106. }
  107. }
  108. #endif /* L_gcov_merge_single */
  109. #ifdef L_gcov_merge_delta
  110. /* The profile merging function for choosing the most common
  111. difference between two consecutive evaluations of the value. It is
  112. given an array COUNTERS of N_COUNTERS old counters and it reads the
  113. same number of counters from the gcov file. The counters are split
  114. into 4-tuples where the members of the tuple have meanings:
  115. -- the last value of the measured entity
  116. -- the stored candidate on the most common difference
  117. -- counter
  118. -- total number of evaluations of the value */
  119. void
  120. __gcov_merge_delta (gcov_type *counters, unsigned n_counters)
  121. {
  122. unsigned i, n_measures;
  123. gcov_type value, counter, all;
  124. gcc_assert (!(n_counters % 4));
  125. n_measures = n_counters / 4;
  126. for (i = 0; i < n_measures; i++, counters += 4)
  127. {
  128. /* last = */ gcov_get_counter ();
  129. value = gcov_get_counter_target ();
  130. counter = gcov_get_counter ();
  131. all = gcov_get_counter ();
  132. if (counters[1] == value)
  133. counters[2] += counter;
  134. else if (counter > counters[2])
  135. {
  136. counters[1] = value;
  137. counters[2] = counter - counters[2];
  138. }
  139. else
  140. counters[2] -= counter;
  141. counters[3] += all;
  142. }
  143. }
  144. #endif /* L_gcov_merge_delta */
  145. #ifdef L_gcov_merge_icall_topn
  146. /* The profile merging function used for merging indirect call counts
  147. This function is given array COUNTERS of N_COUNTERS old counters and it
  148. reads the same number of counters from the gcov file. */
  149. void
  150. __gcov_merge_icall_topn (gcov_type *counters, unsigned n_counters)
  151. {
  152. unsigned i, j, k, m;
  153. gcc_assert (!(n_counters % GCOV_ICALL_TOPN_NCOUNTS));
  154. for (i = 0; i < n_counters; i += GCOV_ICALL_TOPN_NCOUNTS)
  155. {
  156. gcov_type *value_array = &counters[i + 1];
  157. unsigned tmp_size = 2 * (GCOV_ICALL_TOPN_NCOUNTS - 1);
  158. gcov_type *tmp_array
  159. = (gcov_type *) alloca (tmp_size * sizeof (gcov_type));
  160. for (j = 0; j < tmp_size; j++)
  161. tmp_array[j] = 0;
  162. for (j = 0; j < GCOV_ICALL_TOPN_NCOUNTS - 1; j += 2)
  163. {
  164. tmp_array[j] = value_array[j];
  165. tmp_array[j + 1] = value_array [j + 1];
  166. }
  167. /* Skip the number_of_eviction entry. */
  168. gcov_get_counter ();
  169. for (k = 0; k < GCOV_ICALL_TOPN_NCOUNTS - 1; k += 2)
  170. {
  171. int found = 0;
  172. gcov_type global_id = gcov_get_counter_target ();
  173. gcov_type call_count = gcov_get_counter ();
  174. for (m = 0; m < j; m += 2)
  175. {
  176. if (tmp_array[m] == global_id)
  177. {
  178. found = 1;
  179. tmp_array[m + 1] += call_count;
  180. break;
  181. }
  182. }
  183. if (!found)
  184. {
  185. tmp_array[j] = global_id;
  186. tmp_array[j + 1] = call_count;
  187. j += 2;
  188. }
  189. }
  190. /* Now sort the temp array */
  191. gcov_sort_n_vals (tmp_array, j);
  192. /* Now copy back the top half of the temp array */
  193. for (k = 0; k < GCOV_ICALL_TOPN_NCOUNTS - 1; k += 2)
  194. {
  195. value_array[k] = tmp_array[k];
  196. value_array[k + 1] = tmp_array[k + 1];
  197. }
  198. }
  199. }
  200. #endif /* L_gcov_merge_icall_topn */
  201. #endif /* inhibit_libc */