summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/profile/impl/profiler.h
blob: 49785bbee8f0460844abedf7f89bbb26edd3ff8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// -*- C++ -*-
//
// Copyright (C) 2009, 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

/** @file profile/impl/profiler.h
 *  @brief Interface of the profiling runtime library.
 */

// Written by Lixia Liu and Silvius Rus.

#ifndef _GLIBCXX_PROFILE_PROFILER_H
#define _GLIBCXX_PROFILE_PROFILER_H 1

#include <bits/c++config.h>

// Mechanism to define data with inline linkage.
#define _GLIBCXX_PROFILE_DEFINE_UNINIT_DATA(__type, __name)             \
  inline __type&                                                        \
  __get_##__name()                                                      \
  {                                                                     \
    static __type __name;                                               \
    return __name;                                                      \
  }
#define _GLIBCXX_PROFILE_DEFINE_DATA(__type, __name, __initial_value...) \
  inline __type& __get_##__name() {                                      \
    static __type __name(__initial_value);                               \
    return __name;                                                       \
  }
#define _GLIBCXX_PROFILE_DATA(__name) \
  __get_##__name()

namespace __gnu_profile
{
  /** @brief Reentrance guard.
   *
   * Mechanism to protect all __gnu_profile operations against recursion,
   * multithreaded and exception reentrance.
   */
  struct __reentrance_guard
  {
    static bool
    __get_in()
    {
      if (__inside() == true)
	return false;
      else
	{
	  __inside() = true;
	  return true;
	}
    }

    static bool&
    __inside()
    {
      static __thread bool _S_inside(false);
      return _S_inside;
    }

    __reentrance_guard() { }
    ~__reentrance_guard() { __inside() = false; }
  };

#define _GLIBCXX_PROFILE_REENTRANCE_GUARD(__x...)	      	\
  {                                                             \
    if (__gnu_profile::__reentrance_guard::__get_in())          \
    {                                                           \
      __gnu_profile::__reentrance_guard __get_out; 		\
      __x;                                                      \
    }                                                           \
  }

  // Forward declarations of implementation functions.
  // Don't use any __gnu_profile:: in user code.
  // Instead, use the __profcxx... macros, which offer guarded access.
  bool __turn_on();
  bool __turn_off();
  bool __is_invalid();
  bool __is_on();
  bool __is_off();
  void __report(void);
  void __trace_hashtable_size_resize(const void*, std::size_t, std::size_t);
  void __trace_hashtable_size_destruct(const void*, std::size_t, std::size_t);
  void __trace_hashtable_size_construct(const void*, std::size_t);
  void __trace_vector_size_resize(const void*, std::size_t, std::size_t);
  void __trace_vector_size_destruct(const void*, std::size_t, std::size_t);
  void __trace_vector_size_construct(const void*, std::size_t);
  void __trace_hash_func_destruct(const void*, std::size_t, std::size_t,
				  std::size_t);
  void __trace_hash_func_construct(const void*);
  void __trace_vector_to_list_destruct(const void*);
  void __trace_vector_to_list_construct(const void*);
  void __trace_vector_to_list_insert(const void*, std::size_t, std::size_t);
  void __trace_vector_to_list_iterate(const void*, std::size_t);
  void __trace_vector_to_list_invalid_operator(const void*);
  void __trace_vector_to_list_resize(const void*, std::size_t, std::size_t);
  void __trace_vector_to_list_find(const void*, std::size_t);

  void __trace_list_to_slist_destruct(const void*);
  void __trace_list_to_slist_construct(const void*);
  void __trace_list_to_slist_rewind(const void*);
  void __trace_list_to_slist_operation(const void*);

  void __trace_list_to_vector_destruct(const void*);
  void __trace_list_to_vector_construct(const void*);
  void __trace_list_to_vector_insert(const void*, std::size_t, std::size_t);
  void __trace_list_to_vector_iterate(const void*, std::size_t);
  void __trace_list_to_vector_invalid_operator(const void*);
  void __trace_list_to_vector_resize(const void*, std::size_t, std::size_t);

  void __trace_list_to_set_destruct(const void*);
  void __trace_list_to_set_construct(const void*);
  void __trace_list_to_set_insert(const void*, std::size_t, std::size_t); 
  void __trace_list_to_set_iterate(const void*, std::size_t);
  void __trace_list_to_set_invalid_operator(const void*);
  void __trace_list_to_set_find(const void*, std::size_t); 

  void __trace_map_to_unordered_map_construct(const void*);
  void __trace_map_to_unordered_map_invalidate(const void*);
  void __trace_map_to_unordered_map_insert(const void*, std::size_t,
					   std::size_t);
  void __trace_map_to_unordered_map_erase(const void*, std::size_t,
					  std::size_t);
  void __trace_map_to_unordered_map_iterate(const void*, std::size_t);
  void __trace_map_to_unordered_map_find(const void*, std::size_t);
  void __trace_map_to_unordered_map_destruct(const void*);
} // namespace __gnu_profile

// Master switch turns on all diagnostics that are not explicitly turned off.
#ifdef _GLIBCXX_PROFILE
#ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_SMALL
#define _GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL
#endif
#ifndef _GLIBCXX_PROFILE_NO_HASHTABLE_TOO_LARGE
#define _GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE
#endif
#ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_SMALL
#define _GLIBCXX_PROFILE_VECTOR_TOO_SMALL
#endif
#ifndef _GLIBCXX_PROFILE_NO_VECTOR_TOO_LARGE
#define _GLIBCXX_PROFILE_VECTOR_TOO_LARGE
#endif
#ifndef _GLIBCXX_PROFILE_NO_INEFFICIENT_HASH
#define _GLIBCXX_PROFILE_INEFFICIENT_HASH
#endif
#ifndef _GLIBCXX_PROFILE_NO_VECTOR_TO_LIST
#define _GLIBCXX_PROFILE_VECTOR_TO_LIST
#endif
#ifndef _GLIBCXX_PROFILE_NO_LIST_TO_SLIST
#define _GLIBCXX_PROFILE_LIST_TO_SLIST
#endif
#ifndef _GLIBCXX_PROFILE_NO_LIST_TO_VECTOR
#define _GLIBCXX_PROFILE_LIST_TO_VECTOR
#endif
#ifndef _GLIBCXX_PROFILE_NO_MAP_TO_UNORDERED_MAP
#define _GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP
#endif
#endif

// Expose global management routines to user code.
#ifdef _GLIBCXX_PROFILE
#define __profcxx_report() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__report())
#define __profcxx_turn_on() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_on())
#define __profcxx_turn_off() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__turn_off())
#define __profcxx_is_invalid() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_invalid())
#define __profcxx_is_on() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_on())
#define __profcxx__is_off() \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD(__gnu_profile::__is_off())
#else
#define __profcxx_report()
#define __profcxx_turn_on()
#define __profcxx_turn_off()
#define __profcxx_is_invalid()
#define __profcxx_is_on()
#define __profcxx_is_off()
#endif

// Turn on/off instrumentation for HASHTABLE_TOO_SMALL and HASHTABLE_TOO_LARGE.
#if (defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_SMALL) \
     || defined(_GLIBCXX_PROFILE_HASHTABLE_TOO_LARGE))
#define __profcxx_hashtable_resize(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_hashtable_size_resize(__x))
#define __profcxx_hashtable_destruct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_hashtable_size_destruct(__x))
#define __profcxx_hashtable_construct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_hashtable_size_construct(__x))
#else
#define __profcxx_hashtable_resize(__x...)  
#define __profcxx_hashtable_destruct(__x...) 
#define __profcxx_hashtable_construct(__x...)  
#endif

// Turn on/off instrumentation for VECTOR_TOO_SMALL and VECTOR_TOO_LARGE.
#if (defined(_GLIBCXX_PROFILE_VECTOR_TOO_SMALL) \
     || defined(_GLIBCXX_PROFILE_VECTOR_TOO_LARGE))
#define __profcxx_vector_resize(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_size_resize(__x))
#define __profcxx_vector_destruct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_size_destruct(__x))
#define __profcxx_vector_construct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_size_construct(__x))
#else
#define __profcxx_vector_resize(__x...)  
#define __profcxx_vector_destruct(__x...) 
#define __profcxx_vector_construct(__x...)  
#endif 

// Turn on/off instrumentation for INEFFICIENT_HASH.
#if defined(_GLIBCXX_PROFILE_INEFFICIENT_HASH)
#define __profcxx_hashtable_construct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_hash_func_construct(__x))
#define __profcxx_hashtable_destruct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_hash_func_destruct(__x))
#else
#define __profcxx_hashtable_destruct2(__x...) 
#define __profcxx_hashtable_construct2(__x...)  
#endif

// Turn on/off instrumentation for VECTOR_TO_LIST.
#if defined(_GLIBCXX_PROFILE_VECTOR_TO_LIST)
#define __profcxx_vector_construct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_construct(__x))
#define __profcxx_vector_destruct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_destruct(__x))
#define __profcxx_vector_insert(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_insert(__x))
#define __profcxx_vector_iterate(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_iterate(__x))
#define __profcxx_vector_invalid_operator(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_invalid_operator(__x))
#define __profcxx_vector_resize2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_resize(__x))
#define __profcxx_vector_find(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_vector_to_list_find(__x))
#else
#define __profcxx_vector_destruct2(__x...)
#define __profcxx_vector_construct2(__x...)
#define __profcxx_vector_insert(__x...)
#define __profcxx_vector_iterate(__x...)
#define __profcxx_vector_invalid_operator(__x...)
#define __profcxx_vector_resize2(__x...)
#define __profcxx_vector_find(__x...)
#endif

// Turn on/off instrumentation for LIST_TO_VECTOR. 
#if defined(_GLIBCXX_PROFILE_LIST_TO_VECTOR)
#define __profcxx_list_construct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_vector_construct(__x))
#define __profcxx_list_destruct2(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_vector_destruct(__x))
#define __profcxx_list_insert(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_vector_insert(__x))
#define __profcxx_list_iterate(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_vector_iterate(__x))
#define __profcxx_list_invalid_operator(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_vector_invalid_operator(__x))
#else
#define __profcxx_list_destruct2(__x...)
#define __profcxx_list_construct2(__x...)
#define __profcxx_list_insert(__x...)
#define __profcxx_list_iterate(__x...)
#define __profcxx_list_invalid_operator(__x...)
#endif

// Turn on/off instrumentation for LIST_TO_SLIST.  
#if defined(_GLIBCXX_PROFILE_LIST_TO_SLIST)
#define __profcxx_list_rewind(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_slist_rewind(__x))
#define __profcxx_list_operation(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_slist_operation(__x))
#define __profcxx_list_destruct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_slist_destruct(__x))
#define __profcxx_list_construct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_list_to_slist_construct(__x))
#else
#define __profcxx_list_rewind(__x...)  
#define __profcxx_list_operation(__x...)
#define __profcxx_list_destruct(__x...) 
#define __profcxx_list_construct(__x...)  
#endif 

// Turn on/off instrumentation for MAP_TO_UNORDERED_MAP.
#if defined(_GLIBCXX_PROFILE_MAP_TO_UNORDERED_MAP)
#define __profcxx_map_to_unordered_map_construct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_construct(__x))
#define __profcxx_map_to_unordered_map_destruct(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_destruct(__x))
#define __profcxx_map_to_unordered_map_insert(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_insert(__x))
#define __profcxx_map_to_unordered_map_erase(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_erase(__x))
#define __profcxx_map_to_unordered_map_iterate(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_iterate(__x))
#define __profcxx_map_to_unordered_map_invalidate(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_invalidate(__x))
#define __profcxx_map_to_unordered_map_find(__x...) \
  _GLIBCXX_PROFILE_REENTRANCE_GUARD( \
      __gnu_profile::__trace_map_to_unordered_map_find(__x))
#else
#define __profcxx_map_to_unordered_map_construct(__x...) \
  
#define __profcxx_map_to_unordered_map_destruct(__x...)
#define __profcxx_map_to_unordered_map_insert(__x...)
#define __profcxx_map_to_unordered_map_erase(__x...)
#define __profcxx_map_to_unordered_map_iterate(__x...)
#define __profcxx_map_to_unordered_map_invalidate(__x...)
#define __profcxx_map_to_unordered_map_find(__x...)
#endif

// Set default values for compile-time customizable variables.
#ifndef _GLIBCXX_PROFILE_TRACE_PATH_ROOT
#define _GLIBCXX_PROFILE_TRACE_PATH_ROOT "libstdcxx-profile"
#endif
#ifndef _GLIBCXX_PROFILE_TRACE_ENV_VAR
#define _GLIBCXX_PROFILE_TRACE_ENV_VAR "_GLIBCXX_PROFILE_TRACE_PATH_ROOT"
#endif
#ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR
#define _GLIBCXX_PROFILE_MAX_WARN_COUNT_ENV_VAR \
  "_GLIBCXX_PROFILE_MAX_WARN_COUNT"
#endif
#ifndef _GLIBCXX_PROFILE_MAX_WARN_COUNT
#define _GLIBCXX_PROFILE_MAX_WARN_COUNT 10
#endif
#ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH
#define _GLIBCXX_PROFILE_MAX_STACK_DEPTH 32
#endif
#ifndef _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR
#define _GLIBCXX_PROFILE_MAX_STACK_DEPTH_ENV_VAR \
  "_GLIBCXX_PROFILE_MAX_STACK_DEPTH"
#endif
#ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC
#define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC (1 << 28)
#endif
#ifndef _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR
#define _GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC_ENV_VAR \
  "_GLIBCXX_PROFILE_MEM_PER_DIAGNOSTIC"
#endif

// Instrumentation hook implementations.
#include "profile/impl/profiler_hash_func.h"
#include "profile/impl/profiler_hashtable_size.h"
#include "profile/impl/profiler_map_to_unordered_map.h"
#include "profile/impl/profiler_vector_size.h"
#include "profile/impl/profiler_vector_to_list.h"
#include "profile/impl/profiler_list_to_slist.h"
#include "profile/impl/profiler_list_to_vector.h"

#endif // _GLIBCXX_PROFILE_PROFILER_H