libstdc++

profiler_node.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Copyright (C) 2009-2013 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
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
23
24 /** @file profile/impl/profiler_node.h
25 * @brief Data structures to represent a single profiling event.
26 */
27
28 // Written by Lixia Liu and Silvius Rus.
29
30 #ifndef _GLIBCXX_PROFILE_PROFILER_NODE_H
31 #define _GLIBCXX_PROFILE_PROFILER_NODE_H 1
32
33 #include <cstdio> // FILE, fprintf
34
35 #include <vector>
36 #if defined _GLIBCXX_HAVE_EXECINFO_H
37 #include <execinfo.h>
38 #endif
39
40 namespace __gnu_profile
41 {
42 typedef const void* __object_t;
43 typedef void* __instruction_address_t;
44 typedef std::_GLIBCXX_STD_C::vector<__instruction_address_t> __stack_npt;
45 typedef __stack_npt* __stack_t;
46
47 std::size_t __stack_max_depth();
48
49 inline __stack_t
50 __get_stack()
51 {
52 #if defined _GLIBCXX_HAVE_EXECINFO_H
53 std::size_t __max_depth = __stack_max_depth();
54 if (__max_depth == 0)
55 return 0;
56 __stack_npt __buffer(__max_depth);
57 int __depth = backtrace(&__buffer[0], __max_depth);
58 __stack_t __stack = new __stack_npt(__depth);
59 __builtin_memcpy(&(*__stack)[0], &__buffer[0],
60 __depth * sizeof(__object_t));
61 return __stack;
62 #else
63 return 0;
64 #endif
65 }
66
67 inline std::size_t
68 __size(__stack_t __stack)
69 {
70 if (!__stack)
71 return 0;
72 else
73 return __stack->size();
74 }
75
76 // XXX
77 inline void
78 __write(FILE* __f, __stack_t __stack)
79 {
80 if (!__stack)
81 return;
82
83 __stack_npt::const_iterator __it;
84 for (__it = __stack->begin(); __it != __stack->end(); ++__it)
85 std::fprintf(__f, "%p ", *__it);
86 }
87
88 /** @brief Hash function for summary trace using call stack as index. */
89 class __stack_hash
90 {
91 public:
92 std::size_t
93 operator()(__stack_t __s) const
94 {
95 if (!__s)
96 return 0;
97
98 std::size_t __index = 0;
99 __stack_npt::const_iterator __it;
100 for (__it = __s->begin(); __it != __s->end(); ++__it)
101 __index += reinterpret_cast<std::size_t>(*__it);
102 return __index;
103 }
104
105 bool operator() (__stack_t __stack1, __stack_t __stack2) const
106 {
107 if (!__stack1 && !__stack2)
108 return true;
109 if (!__stack1 || !__stack2)
110 return false;
111 if (__stack1->size() != __stack2->size())
112 return false;
113
114 std::size_t __byte_size
115 = __stack1->size() * sizeof(__stack_npt::value_type);
116 return __builtin_memcmp(&(*__stack1)[0], &(*__stack2)[0],
117 __byte_size) == 0;
118 }
119 };
120
121
122 /** @brief Base class for a line in the object table. */
123 class __object_info_base
124 {
125 public:
126 __object_info_base() { }
127
128 __object_info_base(__stack_t __stack)
129 : _M_stack(__stack), _M_valid(true) { }
130
131 __object_info_base(const __object_info_base& __o)
132 : _M_stack(__o._M_stack), _M_valid(__o._M_valid) { }
133
134 virtual ~__object_info_base() { }
135
136 bool
137 __is_valid() const
138 { return _M_valid; }
139
140 __stack_t
141 __stack() const
142 { return _M_stack; }
143
144 virtual void __write(FILE* __f) const = 0;
145
146 protected:
147 __stack_t _M_stack;
148 bool _M_valid;
149 };
150
151
152 /** @brief Base class for a line in the stack table. */
153 template<typename __object_info>
154 class __stack_info_base
155 {
156 public:
157 __stack_info_base() { }
158 __stack_info_base(const __object_info& __info) = 0;
159 virtual ~__stack_info_base() {}
160 void __merge(const __object_info& __info) = 0;
161 virtual float __magnitude() const = 0;
162 virtual const char* __get_id() const = 0;
163 };
164
165 } // namespace __gnu_profile
166 #endif /* _GLIBCXX_PROFILE_PROFILER_NODE_H */