profiler_trace.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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_trace.h
  22. * @brief Data structures to represent profiling traces.
  23. */
  24. // Written by Lixia Liu and Silvius Rus.
  25. #ifndef _GLIBCXX_PROFILE_PROFILER_TRACE_H
  26. #define _GLIBCXX_PROFILE_PROFILER_TRACE_H 1
  27. #include <cstdio> // fopen, fclose, fprintf, FILE
  28. #include <cerrno>
  29. #include <cstdlib> // atof, atoi, strtol, getenv, atexit, abort
  30. #if __cplusplus >= 201103L
  31. #include <unordered_map>
  32. #define _GLIBCXX_IMPL_UNORDERED_MAP std::_GLIBCXX_STD_C::unordered_map
  33. #else
  34. #include <tr1/unordered_map>
  35. #define _GLIBCXX_IMPL_UNORDERED_MAP std::tr1::unordered_map
  36. #endif
  37. #include <ext/concurrence.h>
  38. #include <fstream>
  39. #include <string>
  40. #include <utility>
  41. #include <vector>
  42. #include "profile/impl/profiler_algos.h"
  43. #include "profile/impl/profiler_state.h"
  44. #include "profile/impl/profiler_node.h"
  45. namespace __gnu_profile
  46. {
  47. /** @brief Internal environment. Values can be set one of two ways:
  48. 1. In config file "var = value". The default config file path is
  49. libstdcxx-profile.conf.
  50. 2. By setting process environment variables. For instance, in a Bash
  51. shell you can set the unit cost of iterating through a map like this:
  52. export __map_iterate_cost_factor=5.0.
  53. If a value is set both in the input file and through an environment
  54. variable, the environment value takes precedence. */
  55. typedef _GLIBCXX_IMPL_UNORDERED_MAP<std::string, std::string> __env_t;
  56. _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__env_t, __env);
  57. /** @brief Master lock. */
  58. _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__gnu_cxx::__mutex, __global_mutex);
  59. /** @brief Representation of a warning. */
  60. struct __warning_data
  61. {
  62. float __magnitude;
  63. __stack_t __context;
  64. const char* __warning_id;
  65. std::string __warning_message;
  66. __warning_data()
  67. : __magnitude(0.0), __context(0), __warning_id(0) { }
  68. __warning_data(float __m, __stack_t __c, const char* __id,
  69. const std::string& __msg)
  70. : __magnitude(__m), __context(__c), __warning_id(__id),
  71. __warning_message(__msg) { }
  72. bool
  73. operator<(const __warning_data& __other) const
  74. { return __magnitude < __other.__magnitude; }
  75. };
  76. typedef std::_GLIBCXX_STD_C::vector<__warning_data> __warning_vector_t;
  77. // Defined in profiler_<diagnostic name>.h.
  78. class __trace_hash_func;
  79. class __trace_hashtable_size;
  80. class __trace_map2umap;
  81. class __trace_vector_size;
  82. class __trace_vector_to_list;
  83. class __trace_list_to_slist;
  84. class __trace_list_to_vector;
  85. void __trace_vector_size_init();
  86. void __trace_hashtable_size_init();
  87. void __trace_hash_func_init();
  88. void __trace_vector_to_list_init();
  89. void __trace_list_to_slist_init();
  90. void __trace_list_to_vector_init();
  91. void __trace_map_to_unordered_map_init();
  92. void __trace_vector_size_report(FILE*, __warning_vector_t&);
  93. void __trace_hashtable_size_report(FILE*, __warning_vector_t&);
  94. void __trace_hash_func_report(FILE*, __warning_vector_t&);
  95. void __trace_vector_to_list_report(FILE*, __warning_vector_t&);
  96. void __trace_list_to_slist_report(FILE*, __warning_vector_t&);
  97. void __trace_list_to_vector_report(FILE*, __warning_vector_t&);
  98. void __trace_map_to_unordered_map_report(FILE*, __warning_vector_t&);
  99. void __trace_vector_size_free();
  100. void __trace_hashtable_size_free();
  101. void __trace_hash_func_free();
  102. void __trace_vector_to_list_free();
  103. void __trace_list_to_slist_free();
  104. void __trace_list_to_vector_free();
  105. void __trace_map_to_unordered_map_free();
  106. struct __cost_factor
  107. {
  108. const char* __env_var;
  109. float __value;
  110. };
  111. typedef std::_GLIBCXX_STD_C::vector<__cost_factor*> __cost_factor_vector;
  112. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_hash_func*, _S_hash_func, 0);
  113. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_hashtable_size*, _S_hashtable_size, 0);
  114. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_map2umap*, _S_map2umap, 0);
  115. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_vector_size*, _S_vector_size, 0);
  116. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_vector_to_list*, _S_vector_to_list, 0);
  117. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_list_to_slist*, _S_list_to_slist, 0);
  118. _GLIBCXX_PROFILE_DEFINE_DATA(__trace_list_to_vector*, _S_list_to_vector, 0);
  119. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __vector_shift_cost_factor,
  120. {"__vector_shift_cost_factor", 1.0});
  121. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __vector_iterate_cost_factor,
  122. {"__vector_iterate_cost_factor", 1.0});
  123. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __vector_resize_cost_factor,
  124. {"__vector_resize_cost_factor", 1.0});
  125. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __list_shift_cost_factor,
  126. {"__list_shift_cost_factor", 0.0});
  127. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __list_iterate_cost_factor,
  128. {"__list_iterate_cost_factor", 10.0});
  129. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __list_resize_cost_factor,
  130. {"__list_resize_cost_factor", 0.0});
  131. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __map_insert_cost_factor,
  132. {"__map_insert_cost_factor", 1.5});
  133. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __map_erase_cost_factor,
  134. {"__map_erase_cost_factor", 1.5});
  135. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __map_find_cost_factor,
  136. {"__map_find_cost_factor", 1});
  137. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __map_iterate_cost_factor,
  138. {"__map_iterate_cost_factor", 2.3});
  139. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __umap_insert_cost_factor,
  140. {"__umap_insert_cost_factor", 12.0});
  141. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __umap_erase_cost_factor,
  142. {"__umap_erase_cost_factor", 12.0});
  143. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __umap_find_cost_factor,
  144. {"__umap_find_cost_factor", 10.0});
  145. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor, __umap_iterate_cost_factor,
  146. {"__umap_iterate_cost_factor", 1.7});
  147. _GLIBCXX_PROFILE_DEFINE_DATA(__cost_factor_vector*, __cost_factors, 0);
  148. _GLIBCXX_PROFILE_DEFINE_DATA(const char*, _S_trace_file_name,
  149. _GLIBCXX_PROFILE_TRACE_PATH_ROOT);
  150. _GLIBCXX_PROFILE_DEFINE_DATA(std::size_t, _S_max_warn_count,
  151. _GLIBCXX_PROFILE_MAX_WARN_COUNT);
  152. _GLIBCXX_PROFILE_DEFINE_DATA(std::size_t, _S_max_stack_depth,
  153. _GLIBCXX_PROFILE_MAX_STACK_DEPTH);
  154. _GLIBCXX_PROFILE_DEFINE_DATA(std::size_t, _S_max_mem,
  155. _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC);
  156. inline std::size_t
  157. __stack_max_depth()
  158. { return _GLIBCXX_PROFILE_DATA(_S_max_stack_depth); }
  159. inline std::size_t
  160. __max_mem()
  161. { return _GLIBCXX_PROFILE_DATA(_S_max_mem); }
  162. /** @brief Base class for all trace producers. */
  163. template<typename __object_info, typename __stack_info>
  164. class __trace_base
  165. {
  166. public:
  167. // Do not pick the initial size too large, as we don't know which
  168. // diagnostics are more active.
  169. __trace_base()
  170. : __objects_byte_size(0), __stack_table(10000),
  171. __stack_table_byte_size(0), __id(0) { }
  172. ~__trace_base()
  173. {
  174. for (typename __stack_table_t::iterator __it
  175. = __stack_table.begin(); __it != __stack_table.end(); ++__it)
  176. delete __it->first;
  177. }
  178. __object_info* __add_object(__stack_t __stack);
  179. void __retire_object(__object_info* __info);
  180. void __write(FILE* __f);
  181. void __collect_warnings(__warning_vector_t& __warnings);
  182. void __free();
  183. private:
  184. __gnu_cxx::__mutex __trace_mutex;
  185. typedef _GLIBCXX_IMPL_UNORDERED_MAP<__stack_t, __stack_info,
  186. __stack_hash,
  187. __stack_hash> __stack_table_t;
  188. std::size_t __objects_byte_size;
  189. __stack_table_t __stack_table;
  190. std::size_t __stack_table_byte_size;
  191. protected:
  192. const char* __id;
  193. };
  194. template<typename __object_info, typename __stack_info>
  195. __object_info*
  196. __trace_base<__object_info, __stack_info>::
  197. __add_object(__stack_t __stack)
  198. {
  199. // If we have no backtrace information no need to collect data.
  200. if (!__stack)
  201. return 0;
  202. __gnu_cxx::__scoped_lock __lock(this->__trace_mutex);
  203. if (__max_mem() != 0 && __objects_byte_size >= __max_mem())
  204. {
  205. delete __stack;
  206. return 0;
  207. }
  208. __object_info* __ret = new(std::nothrow) __object_info(__stack);
  209. if (!__ret)
  210. {
  211. delete __stack;
  212. return 0;
  213. }
  214. __objects_byte_size += sizeof(__object_info);
  215. return __ret;
  216. }
  217. template<typename __object_info, typename __stack_info>
  218. void
  219. __trace_base<__object_info, __stack_info>::
  220. __retire_object(__object_info* __obj_info)
  221. {
  222. if (!__obj_info)
  223. return;
  224. __gnu_cxx::__scoped_lock __lock(this->__trace_mutex);
  225. const __object_info& __info = *__obj_info;
  226. __stack_t __stack = __info.__stack();
  227. typename __stack_table_t::iterator __stack_it
  228. = __stack_table.find(__stack);
  229. if (__stack_it == __stack_table.end())
  230. {
  231. // First occurrence of this call context.
  232. if (__max_mem() == 0 || __stack_table_byte_size < __max_mem())
  233. {
  234. __stack_table_byte_size
  235. += (sizeof(__instruction_address_t) * __size(__stack)
  236. + sizeof(__stack) + sizeof(__stack_info));
  237. __stack_table.insert(make_pair(__stack,
  238. __stack_info(__info)));
  239. }
  240. else
  241. delete __stack;
  242. }
  243. else
  244. {
  245. // Merge object info into info summary for this call context.
  246. __stack_it->second.__merge(__info);
  247. delete __stack;
  248. }
  249. delete __obj_info;
  250. __objects_byte_size -= sizeof(__object_info);
  251. }
  252. template<typename __object_info, typename __stack_info>
  253. void
  254. __trace_base<__object_info, __stack_info>::
  255. __write(FILE* __f)
  256. {
  257. for (typename __stack_table_t::iterator __it
  258. = __stack_table.begin(); __it != __stack_table.end(); ++__it)
  259. if (__it->second.__is_valid())
  260. {
  261. std::fprintf(__f, __id);
  262. std::fprintf(__f, "|");
  263. __gnu_profile::__write(__f, __it->first);
  264. std::fprintf(__f, "|");
  265. __it->second.__write(__f);
  266. }
  267. }
  268. template<typename __object_info, typename __stack_info>
  269. void
  270. __trace_base<__object_info, __stack_info>::
  271. __collect_warnings(__warning_vector_t& __warnings)
  272. {
  273. for (typename __stack_table_t::iterator __it
  274. = __stack_table.begin(); __it != __stack_table.end(); ++__it)
  275. __warnings.push_back(__warning_data(__it->second.__magnitude(),
  276. __it->first, __id,
  277. __it->second.__advice()));
  278. }
  279. template<typename __object_info, typename __stack_info>
  280. inline void
  281. __trace_report(__trace_base<__object_info, __stack_info>* __cont,
  282. FILE* __f, __warning_vector_t& __warnings)
  283. {
  284. if (__cont)
  285. {
  286. __cont->__collect_warnings(__warnings);
  287. __cont->__write(__f);
  288. }
  289. }
  290. inline std::size_t
  291. __env_to_size_t(const char* __env_var, std::size_t __default_value)
  292. {
  293. char* __env_value = std::getenv(__env_var);
  294. if (__env_value)
  295. {
  296. errno = 0;
  297. long __converted_value = std::strtol(__env_value, 0, 10);
  298. if (errno || __converted_value < 0)
  299. {
  300. std::fprintf(stderr,
  301. "Bad value for environment variable '%s'.\n",
  302. __env_var);
  303. std::abort();
  304. }
  305. else
  306. return static_cast<std::size_t>(__converted_value);
  307. }
  308. else
  309. return __default_value;
  310. }
  311. inline void
  312. __set_max_stack_trace_depth()
  313. {
  314. _GLIBCXX_PROFILE_DATA(_S_max_stack_depth)
  315. = __env_to_size_t(_GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR,
  316. _GLIBCXX_PROFILE_DATA(_S_max_stack_depth));
  317. }
  318. inline void
  319. __set_max_mem()
  320. {
  321. _GLIBCXX_PROFILE_DATA(_S_max_mem)
  322. = __env_to_size_t(_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR,
  323. _GLIBCXX_PROFILE_DATA(_S_max_mem));
  324. }
  325. inline int
  326. __log_magnitude(float __f)
  327. {
  328. const float __log_base = 10.0;
  329. int __result = 0;
  330. int __sign = 1;
  331. if (__f < 0)
  332. {
  333. __f = -__f;
  334. __sign = -1;
  335. }
  336. while (__f > __log_base)
  337. {
  338. ++__result;
  339. __f /= 10.0;
  340. }
  341. return __sign * __result;
  342. }
  343. inline FILE*
  344. __open_output_file(const char* __extension)
  345. {
  346. // The path is made of _S_trace_file_name + "." + extension.
  347. std::size_t __root_len
  348. = __builtin_strlen(_GLIBCXX_PROFILE_DATA(_S_trace_file_name));
  349. std::size_t __ext_len = __builtin_strlen(__extension);
  350. char* __file_name = new char[__root_len + 1 + __ext_len + 1];
  351. __builtin_memcpy(__file_name,
  352. _GLIBCXX_PROFILE_DATA(_S_trace_file_name),
  353. __root_len);
  354. *(__file_name + __root_len) = '.';
  355. __builtin_memcpy(__file_name + __root_len + 1,
  356. __extension, __ext_len + 1);
  357. FILE* __out_file = std::fopen(__file_name, "w");
  358. if (!__out_file)
  359. {
  360. std::fprintf(stderr, "Could not open trace file '%s'.\n",
  361. __file_name);
  362. std::abort();
  363. }
  364. delete[] __file_name;
  365. return __out_file;
  366. }
  367. struct __warn
  368. {
  369. FILE* __file;
  370. __warn(FILE* __f)
  371. { __file = __f; }
  372. void
  373. operator()(const __warning_data& __info)
  374. {
  375. std::fprintf(__file, __info.__warning_id);
  376. std::fprintf(__file, ": improvement = %d",
  377. __log_magnitude(__info.__magnitude));
  378. std::fprintf(__file, ": call stack = ");
  379. __gnu_profile::__write(__file, __info.__context);
  380. std::fprintf(__file, ": advice = %s\n",
  381. __info.__warning_message.c_str());
  382. }
  383. };
  384. /** @brief Final report method, registered with @b atexit.
  385. *
  386. * This can also be called directly by user code, including signal handlers.
  387. * It is protected against deadlocks by the reentrance guard in profiler.h.
  388. * However, when called from a signal handler that triggers while within
  389. * __gnu_profile (under the guarded zone), no output will be produced.
  390. */
  391. inline void
  392. __report()
  393. {
  394. __gnu_cxx::__scoped_lock __lock(_GLIBCXX_PROFILE_DATA(__global_mutex));
  395. __warning_vector_t __warnings, __top_warnings;
  396. FILE* __raw_file = __open_output_file("raw");
  397. __trace_vector_size_report(__raw_file, __warnings);
  398. __trace_hashtable_size_report(__raw_file, __warnings);
  399. __trace_hash_func_report(__raw_file, __warnings);
  400. __trace_vector_to_list_report(__raw_file, __warnings);
  401. __trace_list_to_slist_report(__raw_file, __warnings);
  402. __trace_list_to_vector_report(__raw_file, __warnings);
  403. __trace_map_to_unordered_map_report(__raw_file, __warnings);
  404. std::fclose(__raw_file);
  405. // Sort data by magnitude, keeping just top N.
  406. std::size_t __cutoff = std::min(_GLIBCXX_PROFILE_DATA(_S_max_warn_count),
  407. __warnings.size());
  408. __top_n(__warnings, __top_warnings, __cutoff);
  409. FILE* __warn_file = __open_output_file("txt");
  410. __for_each(__top_warnings.begin(), __top_warnings.end(),
  411. __warn(__warn_file));
  412. std::fclose(__warn_file);
  413. }
  414. inline void
  415. __report_and_free()
  416. {
  417. __report();
  418. __trace_map_to_unordered_map_free();
  419. __trace_list_to_vector_free();
  420. __trace_list_to_slist_free();
  421. __trace_vector_to_list_free();
  422. __trace_hash_func_free();
  423. __trace_hashtable_size_free();
  424. __trace_vector_size_free();
  425. delete _GLIBCXX_PROFILE_DATA(__cost_factors);
  426. }
  427. inline void
  428. __set_trace_path()
  429. {
  430. char* __env_trace_file_name = std::getenv(_GLIBCXX_PROFILE_TRACE_ENV_VAR);
  431. if (__env_trace_file_name)
  432. _GLIBCXX_PROFILE_DATA(_S_trace_file_name) = __env_trace_file_name;
  433. // Make sure early that we can create the trace file.
  434. std::fclose(__open_output_file("txt"));
  435. }
  436. inline void
  437. __set_max_warn_count()
  438. {
  439. char* __env_max_warn_count_str
  440. = std::getenv(_GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR);
  441. if (__env_max_warn_count_str)
  442. _GLIBCXX_PROFILE_DATA(_S_max_warn_count)
  443. = static_cast<std::size_t>(std::atoi(__env_max_warn_count_str));
  444. }
  445. inline void
  446. __read_cost_factors()
  447. {
  448. std::string __conf_file_name(_GLIBCXX_PROFILE_DATA(_S_trace_file_name));
  449. __conf_file_name += ".conf";
  450. std::ifstream __conf_file(__conf_file_name.c_str());
  451. if (__conf_file.is_open())
  452. {
  453. std::string __line;
  454. while (std::getline(__conf_file, __line))
  455. {
  456. std::string::size_type __i = __line.find_first_not_of(" \t\n\v");
  457. if (__line.length() <= 0 || __line[__i] == '#')
  458. // Skip empty lines or comments.
  459. continue;
  460. }
  461. // Trim.
  462. __line.erase(__remove(__line.begin(), __line.end(), ' '),
  463. __line.end());
  464. std::string::size_type __pos = __line.find("=");
  465. std::string __factor_name = __line.substr(0, __pos);
  466. std::string::size_type __end = __line.find_first_of(";\n");
  467. std::string __factor_value = __line.substr(__pos + 1, __end - __pos);
  468. _GLIBCXX_PROFILE_DATA(__env)[__factor_name] = __factor_value;
  469. }
  470. }
  471. struct __cost_factor_writer
  472. {
  473. FILE* __file;
  474. __cost_factor_writer(FILE* __f)
  475. : __file(__f) { }
  476. void
  477. operator() (const __cost_factor* __factor)
  478. { std::fprintf(__file, "%s = %f\n", __factor->__env_var,
  479. __factor->__value); }
  480. };
  481. inline void
  482. __write_cost_factors()
  483. {
  484. FILE* __file = __open_output_file("conf.out");
  485. __for_each(_GLIBCXX_PROFILE_DATA(__cost_factors)->begin(),
  486. _GLIBCXX_PROFILE_DATA(__cost_factors)->end(),
  487. __cost_factor_writer(__file));
  488. std::fclose(__file);
  489. }
  490. struct __cost_factor_setter
  491. {
  492. void
  493. operator()(__cost_factor* __factor)
  494. {
  495. // Look it up in the process environment first.
  496. const char* __env_value = std::getenv(__factor->__env_var);
  497. if (!__env_value)
  498. {
  499. // Look it up in the config file.
  500. __env_t::iterator __it
  501. = _GLIBCXX_PROFILE_DATA(__env).find(__factor->__env_var);
  502. if (__it != _GLIBCXX_PROFILE_DATA(__env).end())
  503. __env_value = __it->second.c_str();
  504. }
  505. if (__env_value)
  506. __factor->__value = std::atof(__env_value);
  507. }
  508. };
  509. inline void
  510. __set_cost_factors()
  511. {
  512. __cost_factor_vector* __factors = new __cost_factor_vector;
  513. _GLIBCXX_PROFILE_DATA(__cost_factors) = __factors;
  514. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor));
  515. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor));
  516. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__vector_resize_cost_factor));
  517. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__list_shift_cost_factor));
  518. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor));
  519. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__list_resize_cost_factor));
  520. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__map_insert_cost_factor));
  521. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__map_erase_cost_factor));
  522. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__map_find_cost_factor));
  523. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__map_iterate_cost_factor));
  524. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__umap_insert_cost_factor));
  525. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__umap_erase_cost_factor));
  526. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__umap_find_cost_factor));
  527. __factors->push_back(&_GLIBCXX_PROFILE_DATA(__umap_iterate_cost_factor));
  528. __for_each(__factors->begin(), __factors->end(), __cost_factor_setter());
  529. }
  530. inline void
  531. __profcxx_init_unconditional()
  532. {
  533. __gnu_cxx::__scoped_lock __lock(_GLIBCXX_PROFILE_DATA(__global_mutex));
  534. if (__is_invalid())
  535. {
  536. __set_max_warn_count();
  537. if (_GLIBCXX_PROFILE_DATA(_S_max_warn_count) == 0)
  538. __turn_off();
  539. else
  540. {
  541. __set_max_stack_trace_depth();
  542. __set_max_mem();
  543. __set_trace_path();
  544. __read_cost_factors();
  545. __set_cost_factors();
  546. __write_cost_factors();
  547. __trace_vector_size_init();
  548. __trace_hashtable_size_init();
  549. __trace_hash_func_init();
  550. __trace_vector_to_list_init();
  551. __trace_list_to_slist_init();
  552. __trace_list_to_vector_init();
  553. __trace_map_to_unordered_map_init();
  554. std::atexit(__report_and_free);
  555. __turn_on();
  556. }
  557. }
  558. }
  559. /** @brief This function must be called by each instrumentation point.
  560. *
  561. * The common path is inlined fully.
  562. */
  563. inline bool
  564. __profcxx_init()
  565. {
  566. if (__is_invalid())
  567. __profcxx_init_unconditional();
  568. return __is_on();
  569. }
  570. } // namespace __gnu_profile
  571. #endif /* _GLIBCXX_PROFILE_PROFILER_TRACE_H */