heap_profiler_allocation_context.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 native call
  17. // stack.
  18. // Represents a stack frame. Used in Backtrace class below.
  19. //
  20. // Conceptually stack frame is identified by its value, and type is used
  21. // mostly to properly format the value. Value is expected to be a valid
  22. // pointer from process' address space.
  23. struct BASE_EXPORT StackFrame {
  24. enum class Type {
  25. THREAD_NAME, // const char* thread name
  26. PROGRAM_COUNTER, // as returned by stack tracing (e.g. by StackTrace)
  27. };
  28. static StackFrame FromThreadName(const char* name) {
  29. return {Type::THREAD_NAME, name};
  30. }
  31. static StackFrame FromProgramCounter(const void* pc) {
  32. return {Type::PROGRAM_COUNTER, pc};
  33. }
  34. Type type;
  35. const void* value;
  36. };
  37. bool BASE_EXPORT operator < (const StackFrame& lhs, const StackFrame& rhs);
  38. bool BASE_EXPORT operator == (const StackFrame& lhs, const StackFrame& rhs);
  39. bool BASE_EXPORT operator != (const StackFrame& lhs, const StackFrame& rhs);
  40. struct BASE_EXPORT Backtrace {
  41. Backtrace();
  42. // If the stack is higher than what can be stored here, the top frames
  43. // (the ones further from main()) are stored. Depth of 12 is enough for most
  44. // pseudo traces (see above), but not for native traces, where we need more.
  45. enum { kMaxFrameCount = 48 };
  46. StackFrame frames[kMaxFrameCount];
  47. size_t frame_count = 0;
  48. };
  49. bool BASE_EXPORT operator==(const Backtrace& lhs, const Backtrace& rhs);
  50. bool BASE_EXPORT operator!=(const Backtrace& lhs, const Backtrace& rhs);
  51. // The |AllocationContext| is context metadata that is kept for every allocation
  52. // when heap profiling is enabled. To simplify memory management for book-
  53. // keeping, this struct has a fixed size.
  54. struct BASE_EXPORT AllocationContext {
  55. AllocationContext();
  56. AllocationContext(const Backtrace& backtrace, const char* type_name);
  57. Backtrace backtrace;
  58. // Type name of the type stored in the allocated memory. A null pointer
  59. // indicates "unknown type". Grouping is done by comparing pointers, not by
  60. // deep string comparison. In a component build, where a type name can have a
  61. // string literal in several dynamic libraries, this may distort grouping.
  62. const char* type_name;
  63. };
  64. bool BASE_EXPORT operator==(const AllocationContext& lhs,
  65. const AllocationContext& rhs);
  66. bool BASE_EXPORT operator!=(const AllocationContext& lhs,
  67. const AllocationContext& rhs);
  68. // Struct to store the size and count of the allocations.
  69. struct AllocationMetrics {
  70. size_t size;
  71. size_t count;
  72. };
  73. } // namespace trace_event
  74. } // namespace base
  75. namespace std {
  76. template <>
  77. struct BASE_EXPORT hash<base::trace_event::StackFrame> {
  78. size_t operator()(const base::trace_event::StackFrame& frame) const;
  79. };
  80. template <>
  81. struct BASE_EXPORT hash<base::trace_event::Backtrace> {
  82. size_t operator()(const base::trace_event::Backtrace& backtrace) const;
  83. };
  84. template <>
  85. struct BASE_EXPORT hash<base::trace_event::AllocationContext> {
  86. size_t operator()(const base::trace_event::AllocationContext& context) const;
  87. };
  88. } // namespace std
  89. #endif // BASE_TRACE_EVENT_HEAP_PROFILER_ALLOCATION_CONTEXT_H_