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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/memory_allocator_dump.h"
#include <string.h>
#include "base/format_macros.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_dump_provider.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/traced_value.h"
#include "base/values.h"
#include "third_party/perfetto/protos/perfetto/trace/memory_graph.pbzero.h"
#include "third_party/perfetto/protos/perfetto/trace/trace_packet.pbzero.h"
namespace base {
namespace trace_event {
const char MemoryAllocatorDump::kNameSize[] = "size";
const char MemoryAllocatorDump::kNameObjectCount[] = "object_count";
const char MemoryAllocatorDump::kTypeScalar[] = "scalar";
const char MemoryAllocatorDump::kTypeString[] = "string";
const char MemoryAllocatorDump::kUnitsBytes[] = "bytes";
const char MemoryAllocatorDump::kUnitsObjects[] = "objects";
MemoryAllocatorDump::MemoryAllocatorDump(
const std::string& absolute_name,
MemoryDumpLevelOfDetail level_of_detail,
const MemoryAllocatorDumpGuid& guid)
: absolute_name_(absolute_name),
guid_(guid),
level_of_detail_(level_of_detail),
flags_(Flags::DEFAULT) {
// The |absolute_name| cannot be empty.
DCHECK(!absolute_name.empty());
// The |absolute_name| can contain slash separator, but not leading or
// trailing ones.
DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/');
}
MemoryAllocatorDump::~MemoryAllocatorDump() = default;
void MemoryAllocatorDump::AddScalar(const char* name,
const char* units,
uint64_t value) {
entries_.emplace_back(name, units, value);
}
void MemoryAllocatorDump::AddString(const char* name,
const char* units,
const std::string& value) {
// String attributes are disabled in background mode.
if (level_of_detail_ == MemoryDumpLevelOfDetail::kBackground) {
NOTREACHED();
return;
}
entries_.emplace_back(name, units, value);
}
void MemoryAllocatorDump::AsValueInto(TracedValue* value) const {
value->BeginDictionaryWithCopiedName(absolute_name_);
value->SetString("guid", guid_.ToString());
value->BeginDictionary("attrs");
for (const Entry& entry : entries_) {
value->BeginDictionaryWithCopiedName(entry.name);
switch (entry.entry_type) {
case Entry::kUint64:
value->SetString("type", kTypeScalar);
value->SetString("units", entry.units);
value->SetString("value", StringPrintf("%" PRIx64, entry.value_uint64));
break;
case Entry::kString:
value->SetString("type", kTypeString);
value->SetString("units", entry.units);
value->SetString("value", entry.value_string);
break;
}
value->EndDictionary();
}
value->EndDictionary(); // "attrs": { ... }
if (flags_)
value->SetInteger("flags", flags_);
value->EndDictionary(); // "allocator_name/heap_subheap": { ... }
}
void MemoryAllocatorDump::AsProtoInto(
perfetto::protos::pbzero::MemoryTrackerSnapshot::ProcessSnapshot::
MemoryNode* memory_node) const {
memory_node->set_id(guid_.ToUint64());
memory_node->set_absolute_name(absolute_name_);
if (flags() & WEAK) {
memory_node->set_weak(true);
}
for (const Entry& entry : entries_) {
if (entry.name == "size") {
DCHECK_EQ(entry.entry_type, Entry::EntryType::kUint64);
DCHECK_EQ(entry.units, kUnitsBytes);
memory_node->set_size_bytes(entry.value_uint64);
continue;
}
perfetto::protos::pbzero::MemoryTrackerSnapshot_ProcessSnapshot::
MemoryNode::MemoryNodeEntry* proto_memory_node_entry =
memory_node->add_entries();
proto_memory_node_entry->set_name(entry.name);
switch (entry.entry_type) {
case Entry::EntryType::kUint64:
proto_memory_node_entry->set_value_uint64(entry.value_uint64);
break;
case Entry::EntryType::kString:
proto_memory_node_entry->set_value_string(entry.value_string);
break;
}
if (entry.units == kUnitsBytes) {
proto_memory_node_entry->set_units(
perfetto::protos::pbzero::MemoryTrackerSnapshot::ProcessSnapshot::
MemoryNode::MemoryNodeEntry::BYTES);
} else if (entry.units == kUnitsObjects) {
proto_memory_node_entry->set_units(
perfetto::protos::pbzero::MemoryTrackerSnapshot::ProcessSnapshot::
MemoryNode::MemoryNodeEntry::COUNT);
} else {
proto_memory_node_entry->set_units(
perfetto::protos::pbzero::MemoryTrackerSnapshot::ProcessSnapshot::
MemoryNode::MemoryNodeEntry::UNSPECIFIED);
}
}
}
uint64_t MemoryAllocatorDump::GetSizeInternal() const {
if (cached_size_.has_value())
return *cached_size_;
for (const auto& entry : entries_) {
if (entry.entry_type == Entry::kUint64 && entry.units == kUnitsBytes &&
strcmp(entry.name.c_str(), kNameSize) == 0) {
cached_size_ = entry.value_uint64;
return entry.value_uint64;
}
}
return 0;
}
MemoryAllocatorDump::Entry::Entry() : entry_type(kString), value_uint64() {}
MemoryAllocatorDump::Entry::Entry(MemoryAllocatorDump::Entry&&) noexcept =
default;
MemoryAllocatorDump::Entry& MemoryAllocatorDump::Entry::operator=(
MemoryAllocatorDump::Entry&&) = default;
MemoryAllocatorDump::Entry::Entry(std::string name,
std::string units,
uint64_t value)
: name(name), units(units), entry_type(kUint64), value_uint64(value) {}
MemoryAllocatorDump::Entry::Entry(std::string name,
std::string units,
std::string value)
: name(name), units(units), entry_type(kString), value_string(value) {}
bool MemoryAllocatorDump::Entry::operator==(const Entry& rhs) const {
if (!(name == rhs.name && units == rhs.units && entry_type == rhs.entry_type))
return false;
switch (entry_type) {
case EntryType::kUint64:
return value_uint64 == rhs.value_uint64;
case EntryType::kString:
return value_string == rhs.value_string;
}
NOTREACHED();
return false;
}
void PrintTo(const MemoryAllocatorDump::Entry& entry, std::ostream* out) {
switch (entry.entry_type) {
case MemoryAllocatorDump::Entry::EntryType::kUint64:
*out << "<Entry(\"" << entry.name << "\", \"" << entry.units << "\", "
<< entry.value_uint64 << ")>";
return;
case MemoryAllocatorDump::Entry::EntryType::kString:
*out << "<Entry(\"" << entry.name << "\", \"" << entry.units << "\", \""
<< entry.value_string << "\")>";
return;
}
NOTREACHED();
}
} // namespace trace_event
} // namespace base
|