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"
16 namespace trace_event
{
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
))
28 if (!value
->GetAsString(&str_value
))
30 *out_cstr
= str_value
->GetString().c_str();
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
),
49 // The |absolute_name| cannot be empty.
50 DCHECK(!absolute_name
.empty());
52 // The |absolute_name| can contain slash separator, but not leading or
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
,
69 MemoryAllocatorDumpGuid(StringPrintf(
71 TraceLog::GetInstance()->process_id(),
72 absolute_name
.c_str()))) {
75 MemoryAllocatorDump::~MemoryAllocatorDump() {
78 void MemoryAllocatorDump::Add(const std::string
& name
,
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
))
98 if (!GetDictionaryValueAsCStr(attribute
, "type", out_type
))
101 if (!GetDictionaryValueAsCStr(attribute
, "units", out_units
))
104 if (!attribute
->GetWithoutPathExpansion("value", out_value
))
110 void MemoryAllocatorDump::AddScalar(const std::string
& name
,
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
,
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