[tracing] Extend memory dump API with graphs
[chromium-blink-merge.git] / base / trace_event / memory_allocator_dump.cc
blob755d25d89fdee2892744e2b8143ab9f7829853c8
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.
5 #include "base/trace_event/memory_allocator_dump.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/trace_event/memory_dump_manager.h"
10 #include "base/trace_event/memory_dump_provider.h"
11 #include "base/trace_event/process_memory_dump.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "base/values.h"
15 namespace base {
16 namespace trace_event {
18 namespace {
19 // Returns the c-string pointer from a dictionary value without performing extra
20 // std::string copies. The ptr will be valid as long as the value exists.
21 bool GetDictionaryValueAsCStr(const DictionaryValue* dict_value,
22 const std::string& key,
23 const char** out_cstr) {
24 const Value* value = nullptr;
25 const StringValue* str_value = nullptr;
26 if (!dict_value->GetWithoutPathExpansion(key, &value))
27 return false;
28 if (!value->GetAsString(&str_value))
29 return false;
30 *out_cstr = str_value->GetString().c_str();
31 return true;
33 } // namespace
35 const char MemoryAllocatorDump::kNameOuterSize[] = "outer_size";
36 const char MemoryAllocatorDump::kNameInnerSize[] = "inner_size";
37 const char MemoryAllocatorDump::kNameObjectsCount[] = "objects_count";
38 const char MemoryAllocatorDump::kTypeScalar[] = "scalar";
39 const char MemoryAllocatorDump::kTypeString[] = "string";
40 const char MemoryAllocatorDump::kUnitsBytes[] = "bytes";
41 const char MemoryAllocatorDump::kUnitsObjects[] = "objects";
43 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name,
44 ProcessMemoryDump* process_memory_dump,
45 const MemoryAllocatorDumpGuid& guid)
46 : absolute_name_(absolute_name),
47 process_memory_dump_(process_memory_dump),
48 guid_(guid) {
49 // The |absolute_name| cannot be empty.
50 DCHECK(!absolute_name.empty());
52 // The |absolute_name| can contain slash separator, but not leading or
53 // trailing ones.
54 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/');
56 // Dots are not allowed anywhere as the underlying base::DictionaryValue
57 // would treat them magically and split in sub-nodes, which is not intended.
58 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.'));
61 // If the caller didn't provide a guid, make one up by hashing the
62 // absolute_name with the current PID.
63 // Rationale: |absolute_name| is already supposed to be unique within a
64 // process, the pid will make it unique among all processes.
65 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name,
66 ProcessMemoryDump* process_memory_dump)
67 : MemoryAllocatorDump(absolute_name,
68 process_memory_dump,
69 MemoryAllocatorDumpGuid(StringPrintf(
70 "%d:%s",
71 TraceLog::GetInstance()->process_id(),
72 absolute_name.c_str()))) {
75 MemoryAllocatorDump::~MemoryAllocatorDump() {
78 void MemoryAllocatorDump::Add(const std::string& name,
79 const char* type,
80 const char* units,
81 scoped_ptr<Value> value) {
82 scoped_ptr<DictionaryValue> attribute(new DictionaryValue());
83 DCHECK(!attributes_.HasKey(name));
84 attribute->SetStringWithoutPathExpansion("type", type);
85 attribute->SetStringWithoutPathExpansion("units", units);
86 attribute->SetWithoutPathExpansion("value", value.Pass());
87 attributes_.SetWithoutPathExpansion(name, attribute.Pass());
90 bool MemoryAllocatorDump::Get(const std::string& name,
91 const char** out_type,
92 const char** out_units,
93 const Value** out_value) const {
94 const DictionaryValue* attribute = nullptr;
95 if (!attributes_.GetDictionaryWithoutPathExpansion(name, &attribute))
96 return false;
98 if (!GetDictionaryValueAsCStr(attribute, "type", out_type))
99 return false;
101 if (!GetDictionaryValueAsCStr(attribute, "units", out_units))
102 return false;
104 if (!attribute->GetWithoutPathExpansion("value", out_value))
105 return false;
107 return true;
110 void MemoryAllocatorDump::AddScalar(const std::string& name,
111 const char* units,
112 uint64 value) {
113 scoped_ptr<Value> hex_value(new StringValue(StringPrintf("%" PRIx64, value)));
114 Add(name, kTypeScalar, units, hex_value.Pass());
117 void MemoryAllocatorDump::AddString(const std::string& name,
118 const char* units,
119 const std::string& value) {
120 scoped_ptr<Value> str_value(new StringValue(value));
121 Add(name, kTypeString, units, str_value.Pass());
124 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const {
125 value->BeginDictionary(absolute_name_.c_str());
126 value->SetString("guid", guid_.ToString());
128 value->BeginDictionary("attrs");
130 for (DictionaryValue::Iterator it(attributes_); !it.IsAtEnd(); it.Advance())
131 value->SetValue(it.key().c_str(), it.value().CreateDeepCopy());
133 value->EndDictionary(); // "attrs": { ... }
134 value->EndDictionary(); // "allocator_name/heap_subheap": { ... }
137 } // namespace trace_event
138 } // namespace base