profiler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // -*- C++ -*-
  2. //
  3. // Copyright (C) 2009-2018 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License along
  19. // with this library; see the file COPYING3. If not see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file profile/impl/profiler.h
  22. * @brief Interface of the profiling runtime library.
  23. */
  24. // Written by Lixia Liu and Silvius Rus.
  25. #ifndef _GLIBCXX_PROFILE_PROFILER_H
  26. #define _GLIBCXX_PROFILE_PROFILER_H 1
  27. #include <bits/c++config.h>
  28. // Mechanism to define data with inline linkage.
  29. #define _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__type, __name) \
  30. inline __type& \
  31. __get_##__name() \
  32. { \
  33. static __type __name; \
  34. return __name; \
  35. }
  36. #define _GLIBCXX_PROFILE_DEFINE_DATA(__type, __name, __initial_value...) \
  37. inline __type& __get_##__name() { \
  38. static __type __name(__initial_value); \
  39. return __name; \
  40. }
  41. #define _GLIBCXX_PROFILE_DATA(__name) \
  42. __get_##__name()
  43. namespace __gnu_profile
  44. {
  45. /** @brief Reentrance guard.
  46. *
  47. * Mechanism to protect all __gnu_profile operations against recursion,
  48. * multithreaded and exception reentrance.
  49. */
  50. struct __reentrance_guard
  51. {
  52. static bool
  53. __get_in()
  54. {
  55. if (__inside() == true)
  56. return false;
  57. else
  58. {
  59. __inside() = true;
  60. return true;
  61. }
  62. }
  63. static bool&
  64. __inside()
  65. {
  66. static __thread bool _S_inside(false);
  67. return _S_inside;
  68. }
  69. __reentrance_guard() { }
  70. ~__reentrance_guard() { __inside() = false; }
  71. };
  72. // Forward declarations of implementation functions.
  73. // Don't use any __gnu_profile:: in user code.
  74. // Instead, use the __profcxx... macros, which offer guarded access.
  75. class __container_size_info;
  76. class __hashfunc_info;
  77. class __map2umap_info;
  78. class __vector2list_info;
  79. class __list2slist_info;
  80. class __list2vector_info;
  81. bool __turn_on();
  82. bool __turn_off();
  83. bool __is_invalid();
  84. bool __is_on();
  85. bool __is_off();
  86. void __report();
  87. __container_size_info*
  88. __trace_hashtable_size_construct(std::size_t);
  89. void __trace_hashtable_size_resize(__container_size_info*,
  90. std::size_t, std::size_t);
  91. void __trace_hashtable_size_destruct(__container_size_info*,
  92. std::size_t, std::size_t);
  93. __hashfunc_info*
  94. __trace_hash_func_construct();
  95. void __trace_hash_func_destruct(__hashfunc_info*,
  96. std::size_t, std::size_t, std::size_t);
  97. __container_size_info*
  98. __trace_vector_size_construct(std::size_t);
  99. void __trace_vector_size_resize(__container_size_info*,
  100. std::size_t, std::size_t);
  101. void __trace_vector_size_destruct(__container_size_info*,
  102. std::size_t, std::size_t);
  103. __vector2list_info*
  104. __trace_vector_to_list_construct();
  105. void __trace_vector_to_list_insert(__vector2list_info*,
  106. std::size_t, std::size_t);
  107. void __trace_vector_to_list_iterate(__vector2list_info*, int);
  108. void __trace_vector_to_list_invalid_operator(__vector2list_info*);
  109. void __trace_vector_to_list_resize(__vector2list_info*,
  110. std::size_t, std::size_t);
  111. void __trace_vector_to_list_destruct(__vector2list_info*);
  112. __list2slist_info*
  113. __trace_list_to_slist_construct();
  114. void __trace_list_to_slist_rewind(__list2slist_info*);
  115. void __trace_list_to_slist_operation(__list2slist_info*);
  116. void __trace_list_to_slist_destruct(__list2slist_info*);
  117. __list2vector_info*
  118. __trace_list_to_vector_construct();
  119. void __trace_list_to_vector_insert(__list2vector_info*,
  120. std::size_t, std::size_t);
  121. void __trace_list_to_vector_iterate(__list2vector_info*, int);
  122. void __trace_list_to_vector_invalid_operator(__list2vector_info*);
  123. void __trace_list_to_vector_resize(__list2vector_info*,
  124. std::size_t, std::size_t);
  125. void __trace_list_to_vector_destruct(__list2vector_info*);
  126. __map2umap_info*
  127. __trace_map_to_unordered_map_construct();
  128. void __trace_map_to_unordered_map_invalidate(__map2umap_info*);
  129. void __trace_map_to_unordered_map_insert(__map2umap_info*, std::size_t,
  130. std::size_t);
  131. void __trace_map_to_unordered_map_erase(__map2umap_info*, std::size_t,
  132. std::size_t);
  133. void __trace_map_to_unordered_map_iterate(__map2umap_info*, std::size_t);
  134. void __trace_map_to_unordered_map_find(__map2umap_info*, std::size_t);
  135. void __trace_map_to_unordered_map_destruct(__map2umap_info*);
  136. } // namespace __gnu_profile
  137. // Master switch turns on all diagnostics that are not explicitly turned off.
  138. #ifdef _GLIBCXX_PROFILE
  139. #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
  140. #define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
  141. #endif
  142. #ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
  143. #define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
  144. #endif
  145. #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
  146. #define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
  147. #endif
  148. #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
  149. #define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
  150. #endif
  151. #ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
  152. #define _GLIBCXX_PROFILE_INEFFICIENT_HASH
  153. #endif
  154. #ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
  155. #define _GLIBCXX_PROFILE_VECTOR_TO_LIST
  156. #endif
  157. #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
  158. #define _GLIBCXX_PROFILE_LIST_TO_SLIST
  159. #endif
  160. #ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
  161. #define _GLIBCXX_PROFILE_LIST_TO_VECTOR
  162. #endif
  163. #ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
  164. #define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
  165. #endif
  166. #endif
  167. // Expose global management routines to user code.
  168. #ifdef _GLIBCXX_PROFILE
  169. #define __profcxx_report() __gnu_profile::__report()
  170. #define __profcxx_turn_on() __gnu_profile::__turn_on()
  171. #define __profcxx_turn_off() __gnu_profile::__turn_off()
  172. #define __profcxx_is_invalid() __gnu_profile::__is_invalid()
  173. #define __profcxx_is_on() __gnu_profile::__is_on()
  174. #define __profcxx_is_off() __gnu_profile::__is_off()
  175. #else
  176. #define __profcxx_report()
  177. #define __profcxx_turn_on()
  178. #define __profcxx_turn_off()
  179. #define __profcxx_is_invalid()
  180. #define __profcxx_is_on()
  181. #define __profcxx_is_off()
  182. #endif
  183. // Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
  184. #if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
  185. || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
  186. #define __profcxx_hashtable_size_construct(__x...) \
  187. __gnu_profile::__trace_hashtable_size_construct(__x)
  188. #define __profcxx_hashtable_size_resize(__x...) \
  189. __gnu_profile::__trace_hashtable_size_resize(__x)
  190. #define __profcxx_hashtable_size_destruct(__x...) \
  191. __gnu_profile::__trace_hashtable_size_destruct(__x)
  192. #else
  193. #define __profcxx_hashtable_size_construct(__x...) 0
  194. #define __profcxx_hashtable_size_resize(__x...)
  195. #define __profcxx_hashtable_size_destruct(__x...)
  196. #endif
  197. // Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
  198. #if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
  199. || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
  200. #define __profcxx_vector_size_construct(__x...) \
  201. __gnu_profile::__trace_vector_size_construct(__x)
  202. #define __profcxx_vector_size_resize(__x...) \
  203. __gnu_profile::__trace_vector_size_resize(__x)
  204. #define __profcxx_vector_size_destruct(__x...) \
  205. __gnu_profile::__trace_vector_size_destruct(__x)
  206. #else
  207. #define __profcxx_vector_size_construct(__x...) 0
  208. #define __profcxx_vector_size_resize(__x...)
  209. #define __profcxx_vector_size_destruct(__x...)
  210. #endif
  211. // Turn on/off instrumentation for INEFFICIENT_HASH.
  212. #if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
  213. #define __profcxx_hash_func_construct(__x...) \
  214. __gnu_profile::__trace_hash_func_construct(__x)
  215. #define __profcxx_hash_func_destruct(__x...) \
  216. __gnu_profile::__trace_hash_func_destruct(__x)
  217. #else
  218. #define __profcxx_hash_func_construct(__x...) 0
  219. #define __profcxx_hash_func_destruct(__x...)
  220. #endif
  221. // Turn on/off instrumentation for VECTOR_TO_LIST.
  222. #if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
  223. #define __profcxx_vector2list_construct(__x...) \
  224. __gnu_profile::__trace_vector_to_list_construct(__x)
  225. #define __profcxx_vector2list_insert(__x...) \
  226. __gnu_profile::__trace_vector_to_list_insert(__x)
  227. #define __profcxx_vector2list_iterate(__x...) \
  228. __gnu_profile::__trace_vector_to_list_iterate(__x)
  229. #define __profcxx_vector2list_invalid_operator(__x...) \
  230. __gnu_profile::__trace_vector_to_list_invalid_operator(__x)
  231. #define __profcxx_vector2list_resize(__x...) \
  232. __gnu_profile::__trace_vector_to_list_resize(__x)
  233. #define __profcxx_vector2list_destruct(__x...) \
  234. __gnu_profile::__trace_vector_to_list_destruct(__x)
  235. #else
  236. #define __profcxx_vector2list_construct(__x...) 0
  237. #define __profcxx_vector2list_insert(__x...)
  238. #define __profcxx_vector2list_iterate(__x...)
  239. #define __profcxx_vector2list_invalid_operator(__x...)
  240. #define __profcxx_vector2list_resize(__x...)
  241. #define __profcxx_vector2list_destruct(__x...)
  242. #endif
  243. // Turn on/off instrumentation for LIST_TO_VECTOR.
  244. #if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
  245. #define __profcxx_list2vector_construct(__x...) \
  246. __gnu_profile::__trace_list_to_vector_construct(__x)
  247. #define __profcxx_list2vector_insert(__x...) \
  248. __gnu_profile::__trace_list_to_vector_insert(__x)
  249. #define __profcxx_list2vector_iterate(__x...) \
  250. __gnu_profile::__trace_list_to_vector_iterate(__x)
  251. #define __profcxx_list2vector_invalid_operator(__x...) \
  252. __gnu_profile::__trace_list_to_vector_invalid_operator(__x)
  253. #define __profcxx_list2vector_destruct(__x...) \
  254. __gnu_profile::__trace_list_to_vector_destruct(__x)
  255. #else
  256. #define __profcxx_list2vector_construct(__x...) 0
  257. #define __profcxx_list2vector_insert(__x...)
  258. #define __profcxx_list2vector_iterate(__x...)
  259. #define __profcxx_list2vector_invalid_operator(__x...)
  260. #define __profcxx_list2vector_destruct(__x...)
  261. #endif
  262. // Turn on/off instrumentation for LIST_TO_SLIST.
  263. #if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
  264. #define __profcxx_list2slist_construct(__x...) \
  265. __gnu_profile::__trace_list_to_slist_construct(__x)
  266. #define __profcxx_list2slist_rewind(__x...) \
  267. __gnu_profile::__trace_list_to_slist_rewind(__x)
  268. #define __profcxx_list2slist_operation(__x...) \
  269. __gnu_profile::__trace_list_to_slist_operation(__x)
  270. #define __profcxx_list2slist_destruct(__x...) \
  271. __gnu_profile::__trace_list_to_slist_destruct(__x)
  272. #else
  273. #define __profcxx_list2slist_construct(__x...) 0
  274. #define __profcxx_list2slist_rewind(__x...)
  275. #define __profcxx_list2slist_operation(__x...)
  276. #define __profcxx_list2slist_destruct(__x...)
  277. #endif
  278. // Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
  279. #if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
  280. #define __profcxx_map2umap_construct(__x...) \
  281. __gnu_profile::__trace_map_to_unordered_map_construct(__x)
  282. #define __profcxx_map2umap_insert(__x...) \
  283. __gnu_profile::__trace_map_to_unordered_map_insert(__x)
  284. #define __profcxx_map2umap_erase(__x...) \
  285. __gnu_profile::__trace_map_to_unordered_map_erase(__x)
  286. #define __profcxx_map2umap_iterate(__x...) \
  287. __gnu_profile::__trace_map_to_unordered_map_iterate(__x)
  288. #define __profcxx_map2umap_invalidate(__x...) \
  289. __gnu_profile::__trace_map_to_unordered_map_invalidate(__x)
  290. #define __profcxx_map2umap_find(__x...) \
  291. __gnu_profile::__trace_map_to_unordered_map_find(__x)
  292. #define __profcxx_map2umap_destruct(__x...) \
  293. __gnu_profile::__trace_map_to_unordered_map_destruct(__x)
  294. #else
  295. #define __profcxx_map2umap_construct(__x...) 0
  296. #define __profcxx_map2umap_insert(__x...)
  297. #define __profcxx_map2umap_erase(__x...)
  298. #define __profcxx_map2umap_iterate(__x...)
  299. #define __profcxx_map2umap_invalidate(__x...)
  300. #define __profcxx_map2umap_find(__x...)
  301. #define __profcxx_map2umap_destruct(__x...)
  302. #endif
  303. // Set default values for compile-time customizable variables.
  304. #ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
  305. #define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
  306. #endif
  307. #ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
  308. #define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
  309. #endif
  310. #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
  311. #define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
  312. "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
  313. #endif
  314. #ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
  315. #define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
  316. #endif
  317. #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
  318. #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
  319. #endif
  320. #ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
  321. #define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
  322. "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
  323. #endif
  324. #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
  325. #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC (1 << 28)
  326. #endif
  327. #ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
  328. #define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
  329. "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
  330. #endif
  331. // Instrumentation hook implementations.
  332. #include "profile/impl/profiler_hash_func.h"
  333. #include "profile/impl/profiler_hashtable_size.h"
  334. #include "profile/impl/profiler_map_to_unordered_map.h"
  335. #include "profile/impl/profiler_vector_size.h"
  336. #include "profile/impl/profiler_vector_to_list.h"
  337. #include "profile/impl/profiler_list_to_slist.h"
  338. #include "profile/impl/profiler_list_to_vector.h"
  339. #endif // _GLIBCXX_PROFILE_PROFILER_H