heap_profiler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TRACE_EVENT_HEAP_PROFILER_H_
  5. #define BASE_TRACE_EVENT_HEAP_PROFILER_H_
  6. #include "base/base_export.h"
  7. #include "base/compiler_specific.h"
  8. #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
  9. // This header file defines the set of macros that are used to track memory
  10. // usage in the heap profiler. This is in addition to the macros defined in
  11. // trace_event.h and are specific to heap profiler. This file also defines
  12. // implementation details of these macros.
  13. // Implementation detail: heap profiler macros create temporary variables to
  14. // keep instrumentation overhead low. These macros give each temporary variable
  15. // a unique name based on the line number to prevent name collisions.
  16. #define INTERNAL_HEAP_PROFILER_UID3(a, b) heap_profiler_unique_##a##b
  17. #define INTERNAL_HEAP_PROFILER_UID2(a, b) INTERNAL_HEAP_PROFILER_UID3(a, b)
  18. #define INTERNAL_HEAP_PROFILER_UID(name_prefix) \
  19. INTERNAL_HEAP_PROFILER_UID2(name_prefix, __LINE__)
  20. // Scoped tracker for task execution context in the heap profiler.
  21. #define TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION \
  22. trace_event_internal::HeapProfilerScopedTaskExecutionTracker
  23. // Returns the current task context (c-string) tracked by heap profiler. This is
  24. // useful along with TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION if a async
  25. // system needs to track client's allocation context across post tasks. Use this
  26. // macro to get the current context and use
  27. // TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION in the posted task which
  28. // allocates memory for a client.
  29. #define TRACE_HEAP_PROFILER_API_GET_CURRENT_TASK_CONTEXT \
  30. trace_event_internal::HeapProfilerCurrentTaskContext
  31. // A scoped ignore event used to tell heap profiler to ignore all the
  32. // allocations in the scope. It is useful to exclude allocations made for
  33. // tracing from the heap profiler dumps.
  34. #define HEAP_PROFILER_SCOPED_IGNORE \
  35. trace_event_internal::HeapProfilerScopedIgnore INTERNAL_HEAP_PROFILER_UID( \
  36. scoped_ignore)
  37. namespace trace_event_internal {
  38. // HeapProfilerScopedTaskExecutionTracker records the current task's context in
  39. // the heap profiler.
  40. class HeapProfilerScopedTaskExecutionTracker {
  41. public:
  42. inline explicit HeapProfilerScopedTaskExecutionTracker(
  43. const char* task_context)
  44. : context_(task_context) {
  45. using base::trace_event::AllocationContextTracker;
  46. if (UNLIKELY(AllocationContextTracker::capture_mode() !=
  47. AllocationContextTracker::CaptureMode::DISABLED)) {
  48. AllocationContextTracker::GetInstanceForCurrentThread()
  49. ->PushCurrentTaskContext(context_);
  50. }
  51. }
  52. inline ~HeapProfilerScopedTaskExecutionTracker() {
  53. using base::trace_event::AllocationContextTracker;
  54. if (UNLIKELY(AllocationContextTracker::capture_mode() !=
  55. AllocationContextTracker::CaptureMode::DISABLED)) {
  56. AllocationContextTracker::GetInstanceForCurrentThread()
  57. ->PopCurrentTaskContext(context_);
  58. }
  59. }
  60. private:
  61. const char* context_;
  62. };
  63. inline const char* HeapProfilerCurrentTaskContext() {
  64. return base::trace_event::AllocationContextTracker::
  65. GetInstanceForCurrentThread()
  66. ->TaskContext();
  67. }
  68. class BASE_EXPORT HeapProfilerScopedIgnore {
  69. public:
  70. inline HeapProfilerScopedIgnore() {
  71. using base::trace_event::AllocationContextTracker;
  72. if (UNLIKELY(
  73. AllocationContextTracker::capture_mode() !=
  74. AllocationContextTracker::CaptureMode::DISABLED)) {
  75. AllocationContextTracker::GetInstanceForCurrentThread()
  76. ->begin_ignore_scope();
  77. }
  78. }
  79. inline ~HeapProfilerScopedIgnore() {
  80. using base::trace_event::AllocationContextTracker;
  81. if (UNLIKELY(
  82. AllocationContextTracker::capture_mode() !=
  83. AllocationContextTracker::CaptureMode::DISABLED)) {
  84. AllocationContextTracker::GetInstanceForCurrentThread()
  85. ->end_ignore_scope();
  86. }
  87. }
  88. };
  89. } // namespace trace_event_internal
  90. #endif // BASE_TRACE_EVENT_HEAP_PROFILER_H_