profiler_node.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_node.h
  22. * @brief Data structures to represent a single profiling event.
  23. */
  24. // Written by Lixia Liu and Silvius Rus.
  25. #ifndef _GLIBCXX_PROFILE_PROFILER_NODE_H
  26. #define _GLIBCXX_PROFILE_PROFILER_NODE_H 1
  27. #include <cstdio> // FILE, fprintf
  28. #include <vector>
  29. #if defined _GLIBCXX_HAVE_EXECINFO_H
  30. #include <execinfo.h>
  31. #endif
  32. namespace __gnu_profile
  33. {
  34. typedef void* __instruction_address_t;
  35. typedef std::_GLIBCXX_STD_C::vector<__instruction_address_t> __stack_npt;
  36. typedef __stack_npt* __stack_t;
  37. std::size_t __stack_max_depth();
  38. inline __stack_t
  39. __get_stack()
  40. {
  41. #if defined _GLIBCXX_HAVE_EXECINFO_H
  42. __try
  43. {
  44. std::size_t __max_depth = __stack_max_depth();
  45. if (__max_depth == 0)
  46. return 0;
  47. __stack_npt __buffer(__max_depth);
  48. int __depth = backtrace(&__buffer[0], __max_depth);
  49. return new(std::nothrow) __stack_npt(__buffer.begin(),
  50. __buffer.begin() + __depth);
  51. }
  52. __catch(...)
  53. {
  54. return 0;
  55. }
  56. #else
  57. return 0;
  58. #endif
  59. }
  60. inline std::size_t
  61. __size(__stack_t __stack)
  62. {
  63. if (!__stack)
  64. return 0;
  65. else
  66. return __stack->size();
  67. }
  68. // XXX
  69. inline void
  70. __write(FILE* __f, __stack_t __stack)
  71. {
  72. if (!__stack)
  73. return;
  74. __stack_npt::const_iterator __it;
  75. for (__it = __stack->begin(); __it != __stack->end(); ++__it)
  76. std::fprintf(__f, "%p ", *__it);
  77. }
  78. /** @brief Hash function for summary trace using call stack as index. */
  79. class __stack_hash
  80. {
  81. public:
  82. std::size_t
  83. operator()(__stack_t __s) const
  84. {
  85. if (!__s)
  86. return 0;
  87. std::size_t __index = 0;
  88. __stack_npt::const_iterator __it;
  89. for (__it = __s->begin(); __it != __s->end(); ++__it)
  90. __index += reinterpret_cast<std::size_t>(*__it);
  91. return __index;
  92. }
  93. bool operator() (__stack_t __stack1, __stack_t __stack2) const
  94. {
  95. if (!__stack1 && !__stack2)
  96. return true;
  97. if (!__stack1 || !__stack2)
  98. return false;
  99. if (__stack1->size() != __stack2->size())
  100. return false;
  101. std::size_t __byte_size
  102. = __stack1->size() * sizeof(__stack_npt::value_type);
  103. return __builtin_memcmp(&(*__stack1)[0], &(*__stack2)[0],
  104. __byte_size) == 0;
  105. }
  106. };
  107. /** @brief Base class for a line in the object table. */
  108. class __object_info_base
  109. {
  110. public:
  111. __object_info_base(__stack_t __stack)
  112. : _M_stack(__stack), _M_valid(true) { }
  113. bool
  114. __is_valid() const
  115. { return _M_valid; }
  116. void
  117. __set_invalid()
  118. { _M_valid = false; }
  119. void
  120. __merge(const __object_info_base& __o)
  121. { _M_valid &= __o._M_valid; }
  122. __stack_t
  123. __stack() const
  124. { return _M_stack; }
  125. protected:
  126. __stack_t _M_stack;
  127. bool _M_valid;
  128. };
  129. } // namespace __gnu_profile
  130. #endif /* _GLIBCXX_PROFILE_PROFILER_NODE_H */