heap_profiler_allocation_context.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2015 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_ALLOCATION_CONTEXT_H_
  5. #define BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <functional>
  9. #include "base/base_export.h"
  10. namespace base {
  11. namespace trace_event {
  12. // When heap profiling is enabled, tracing keeps track of the allocation
  13. // context for each allocation intercepted. It is generated by the
  14. // |AllocationContextTracker| which keeps stacks of context in TLS.
  15. // The tracker is initialized lazily.
  16. // The backtrace in the allocation context is a snapshot of the stack. For now,
  17. // this is the pseudo stack where frames are created by trace event macros. In
  18. // the future, we might add the option to use the native call stack. In that
  19. // case, |Backtrace| and |AllocationContextTracker::GetContextSnapshot| might
  20. // have different implementations that can be selected by a compile time flag.
  21. // The number of stack frames stored in the backtrace is a trade off between
  22. // memory used for tracing and accuracy. Measurements done on a prototype
  23. // revealed that:
  24. //
  25. // - In 60 percent of the cases, pseudo stack depth <= 7.
  26. // - In 87 percent of the cases, pseudo stack depth <= 9.
  27. // - In 95 percent of the cases, pseudo stack depth <= 11.
  28. //
  29. // See the design doc (https://goo.gl/4s7v7b) for more details.
  30. // Represents (pseudo) stack frame. Used in Backtrace class below.
  31. //
  32. // Conceptually stack frame is identified by its value, and type is used
  33. // mostly to properly format the value. Value is expected to be a valid
  34. // pointer from process' address space.
  35. struct BASE_EXPORT StackFrame {
  36. enum class Type {
  37. TRACE_EVENT_NAME, // const char* string
  38. THREAD_NAME, // const char* thread name
  39. PROGRAM_COUNTER, // as returned by stack tracing (e.g. by StackTrace)
  40. };
  41. static StackFrame FromTraceEventName(const char* name) {
  42. return {Type::TRACE_EVENT_NAME, name};
  43. }
  44. static StackFrame FromThreadName(const char* name) {
  45. return {Type::THREAD_NAME, name};
  46. }
  47. static StackFrame FromProgramCounter(const void* pc) {
  48. return {Type::PROGRAM_COUNTER, pc};
  49. }
  50. Type type;
  51. const void* value;
  52. };
  53. bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs);
  54. bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs);
  55. bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs);
  56. struct BASE_EXPORT Backtrace {
  57. Backtrace();
  58. // If the stack is higher than what can be stored here, the top frames
  59. // (the ones further from main()) are stored. Depth of 12 is enough for most
  60. // pseudo traces (see above), but not for native traces, where we need more.
  61. enum { kMaxFrameCount = 48 };
  62. StackFrame frames[kMaxFrameCount];
  63. size_t frame_count = 0;
  64. };
  65. bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs);
  66. bool BASE_EXPORT operator!=(const Backtrace& lhs, const Backtrace& rhs);
  67. // The |AllocationContext| is context metadata that is kept for every allocation
  68. // when heap profiling is enabled. To simplify memory management for book-
  69. // keeping, this struct has a fixed size.
  70. struct BASE_EXPORT AllocationContext {
  71. AllocationContext();
  72. AllocationContext(const Backtrace& backtrace, const char* type_name);
  73. Backtrace backtrace;
  74. // Type name of the type stored in the allocated memory. A null pointer
  75. // indicates "unknown type". Grouping is done by comparing pointers, not by
  76. // deep string comparison. In a component build, where a type name can have a
  77. // string literal in several dynamic libraries, this may distort grouping.
  78. const char* type_name;
  79. };
  80. bool BASE_EXPORT operator==(const AllocationContext& lhs,
  81. const AllocationContext& rhs);
  82. bool BASE_EXPORT operator!=(const AllocationContext& lhs,
  83. const AllocationContext& rhs);
  84. // Struct to store the size and count of the allocations.
  85. struct AllocationMetrics {
  86. size_t size;
  87. size_t count;
  88. };
  89. } // namespace trace_event
  90. } // namespace base
  91. namespace std {
  92. template <>
  93. struct BASE_EXPORT hash<base::trace_event::StackFrame> {
  94. size_t operator()(const base::trace_event::StackFrame& frame) const;
  95. };
  96. template <>
  97. struct BASE_EXPORT hash<base::trace_event::Backtrace> {
  98. size_t operator()(const base::trace_event::Backtrace& backtrace) const;
  99. };
  100. template <>
  101. struct BASE_EXPORT hash<base::trace_event::AllocationContext> {
  102. size_t operator()(const base::trace_event::AllocationContext& context) const;
  103. };
  104. } // namespace std
  105. #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_