memory_allocator_dump.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_MEMORY_ALLOCATOR_DUMP_H_
  5. #define BASE_TRACE_EVENT_MEMORY_ALLOCATOR_DUMP_H_
  6. #include <stdint.h>
  7. #include <iosfwd>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/base_export.h"
  12. #include "base/trace_event/memory_allocator_dump_guid.h"
  13. #include "base/trace_event/memory_dump_request_args.h"
  14. #include "base/unguessable_token.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace perfetto {
  17. namespace protos {
  18. namespace pbzero {
  19. class MemoryTrackerSnapshot_ProcessSnapshot_MemoryNode;
  20. }
  21. } // namespace protos
  22. } // namespace perfetto
  23. namespace base {
  24. namespace trace_event {
  25. class ProcessMemoryDump;
  26. class TracedValue;
  27. // Data model for user-land memory allocator dumps.
  28. class BASE_EXPORT MemoryAllocatorDump {
  29. public:
  30. enum Flags {
  31. DEFAULT = 0,
  32. // A dump marked weak will be discarded by TraceViewer.
  33. WEAK = 1 << 0,
  34. };
  35. // In the TraceViewer UI table each MemoryAllocatorDump becomes
  36. // a row and each Entry generates a column (if it doesn't already
  37. // exist).
  38. struct BASE_EXPORT Entry {
  39. enum EntryType {
  40. kUint64,
  41. kString,
  42. };
  43. // By design name, units and value_string are always coming from
  44. // indefinitely lived const char* strings, the only reason we copy
  45. // them into a std::string is to handle Mojo (de)serialization.
  46. // TODO(hjd): Investigate optimization (e.g. using StringPiece).
  47. Entry(); // Only for deserialization.
  48. Entry(std::string name, std::string units, uint64_t value);
  49. Entry(std::string name, std::string units, std::string value);
  50. Entry(Entry&& other) noexcept;
  51. Entry(const Entry&) = delete;
  52. Entry& operator=(const Entry&) = delete;
  53. Entry& operator=(Entry&& other);
  54. bool operator==(const Entry& rhs) const;
  55. std::string name;
  56. std::string units;
  57. EntryType entry_type;
  58. uint64_t value_uint64;
  59. std::string value_string;
  60. };
  61. MemoryAllocatorDump(const std::string& absolute_name,
  62. MemoryDumpLevelOfDetail,
  63. const MemoryAllocatorDumpGuid&);
  64. MemoryAllocatorDump(const MemoryAllocatorDump&) = delete;
  65. MemoryAllocatorDump& operator=(const MemoryAllocatorDump&) = delete;
  66. ~MemoryAllocatorDump();
  67. // Standard attribute |name|s for the AddScalar and AddString() methods.
  68. static const char kNameSize[]; // To represent allocated space.
  69. static const char kNameObjectCount[]; // To represent number of objects.
  70. // Standard attribute |unit|s for the AddScalar and AddString() methods.
  71. static const char kUnitsBytes[]; // Unit name to represent bytes.
  72. static const char kUnitsObjects[]; // Unit name to represent #objects.
  73. // Constants used only internally and by tests.
  74. static const char kTypeScalar[]; // Type name for scalar attributes.
  75. static const char kTypeString[]; // Type name for string attributes.
  76. // Setters for scalar attributes. Some examples:
  77. // - "size" column (all dumps are expected to have at least this one):
  78. // AddScalar(kNameSize, kUnitsBytes, 1234);
  79. // - Some extra-column reporting internal details of the subsystem:
  80. // AddScalar("number_of_freelist_entries", kUnitsObjects, 42)
  81. // - Other informational column:
  82. // AddString("kitten", "name", "shadow");
  83. void AddScalar(const char* name, const char* units, uint64_t value);
  84. void AddString(const char* name, const char* units, const std::string& value);
  85. // Absolute name, unique within the scope of an entire ProcessMemoryDump.
  86. const std::string& absolute_name() const { return absolute_name_; }
  87. // Called at trace generation time to populate the TracedValue.
  88. void AsValueInto(TracedValue* value) const;
  89. void AsProtoInto(
  90. perfetto::protos::pbzero::
  91. MemoryTrackerSnapshot_ProcessSnapshot_MemoryNode* memory_node) const;
  92. // Get the size for this dump.
  93. // The size is the value set with AddScalar(kNameSize, kUnitsBytes, size);
  94. // TODO(hjd): this should return an optional<uint64_t>.
  95. uint64_t GetSizeInternal() const;
  96. MemoryDumpLevelOfDetail level_of_detail() const { return level_of_detail_; }
  97. // Use enum Flags to set values.
  98. void set_flags(int flags) { flags_ |= flags; }
  99. void clear_flags(int flags) { flags_ &= ~flags; }
  100. int flags() const { return flags_; }
  101. // |guid| is an optional global dump identifier, unique across all processes
  102. // within the scope of a global dump. It is only required when using the
  103. // graph APIs (see TODO_method_name) to express retention / suballocation or
  104. // cross process sharing. See crbug.com/492102 for design docs.
  105. // Subsequent MemoryAllocatorDump(s) with the same |absolute_name| are
  106. // expected to have the same guid.
  107. const MemoryAllocatorDumpGuid& guid() const { return guid_; }
  108. const std::vector<Entry>& entries() const { return entries_; }
  109. // Only for mojo serialization, which can mutate the collection.
  110. std::vector<Entry>* mutable_entries_for_serialization() const {
  111. cached_size_.reset(); // The caller can mutate the collection.
  112. // Mojo takes a const input argument even for move-only types that can be
  113. // mutate while serializing (like this one). Hence the const_cast.
  114. return const_cast<std::vector<Entry>*>(&entries_);
  115. }
  116. private:
  117. const std::string absolute_name_;
  118. MemoryAllocatorDumpGuid guid_;
  119. MemoryDumpLevelOfDetail level_of_detail_;
  120. int flags_; // See enum Flags.
  121. mutable absl::optional<uint64_t>
  122. cached_size_; // Lazy, for GetSizeInternal().
  123. std::vector<Entry> entries_;
  124. };
  125. // This is required by gtest to print a readable output on test failures.
  126. void BASE_EXPORT PrintTo(const MemoryAllocatorDump::Entry&, std::ostream*);
  127. } // namespace trace_event
  128. } // namespace base
  129. #endif // BASE_TRACE_EVENT_MEMORY_ALLOCATOR_DUMP_H_