OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/memory_allocator_dump.h" | 5 #include "base/trace_event/memory_allocator_dump.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/trace_event/memory_dump_manager.h" | 9 #include "base/trace_event/memory_dump_manager.h" |
10 #include "base/trace_event/memory_dump_provider.h" | 10 #include "base/trace_event/memory_dump_provider.h" |
11 #include "base/trace_event/process_memory_dump.h" | 11 #include "base/trace_event/process_memory_dump.h" |
12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 namespace trace_event { | 16 namespace trace_event { |
17 | 17 |
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; | |
32 } | |
33 } // namespace | |
34 | |
35 // TODO(primiano): remove kName{Inner,Outer}Size below after all the existing | |
36 // dump providers have been rewritten. | |
37 const char MemoryAllocatorDump::kNameSize[] = "size"; | 18 const char MemoryAllocatorDump::kNameSize[] = "size"; |
38 const char MemoryAllocatorDump::kNameInnerSize[] = "inner_size"; | |
39 const char MemoryAllocatorDump::kNameOuterSize[] = "outer_size"; | |
40 const char MemoryAllocatorDump::kNameObjectsCount[] = "objects_count"; | 19 const char MemoryAllocatorDump::kNameObjectsCount[] = "objects_count"; |
41 const char MemoryAllocatorDump::kTypeScalar[] = "scalar"; | 20 const char MemoryAllocatorDump::kTypeScalar[] = "scalar"; |
42 const char MemoryAllocatorDump::kTypeString[] = "string"; | 21 const char MemoryAllocatorDump::kTypeString[] = "string"; |
43 const char MemoryAllocatorDump::kUnitsBytes[] = "bytes"; | 22 const char MemoryAllocatorDump::kUnitsBytes[] = "bytes"; |
44 const char MemoryAllocatorDump::kUnitsObjects[] = "objects"; | 23 const char MemoryAllocatorDump::kUnitsObjects[] = "objects"; |
45 | 24 |
46 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name, | 25 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name, |
47 ProcessMemoryDump* process_memory_dump, | 26 ProcessMemoryDump* process_memory_dump, |
48 const MemoryAllocatorDumpGuid& guid) | 27 const MemoryAllocatorDumpGuid& guid) |
49 : absolute_name_(absolute_name), | 28 : absolute_name_(absolute_name), |
50 process_memory_dump_(process_memory_dump), | 29 process_memory_dump_(process_memory_dump), |
| 30 attributes_(new TracedValue), |
51 guid_(guid) { | 31 guid_(guid) { |
52 // The |absolute_name| cannot be empty. | 32 // The |absolute_name| cannot be empty. |
53 DCHECK(!absolute_name.empty()); | 33 DCHECK(!absolute_name.empty()); |
54 | 34 |
55 // The |absolute_name| can contain slash separator, but not leading or | 35 // The |absolute_name| can contain slash separator, but not leading or |
56 // trailing ones. | 36 // trailing ones. |
57 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/'); | 37 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/'); |
58 | 38 |
59 // Dots are not allowed anywhere as the underlying base::DictionaryValue | 39 // Dots are not allowed anywhere as the underlying base::DictionaryValue |
60 // would treat them magically and split in sub-nodes, which is not intended. | 40 // would treat them magically and split in sub-nodes, which is not intended. |
61 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.')); | 41 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.')); |
62 } | 42 } |
63 | 43 |
64 // If the caller didn't provide a guid, make one up by hashing the | 44 // If the caller didn't provide a guid, make one up by hashing the |
65 // absolute_name with the current PID. | 45 // absolute_name with the current PID. |
66 // Rationale: |absolute_name| is already supposed to be unique within a | 46 // Rationale: |absolute_name| is already supposed to be unique within a |
67 // process, the pid will make it unique among all processes. | 47 // process, the pid will make it unique among all processes. |
68 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name, | 48 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name, |
69 ProcessMemoryDump* process_memory_dump) | 49 ProcessMemoryDump* process_memory_dump) |
70 : MemoryAllocatorDump(absolute_name, | 50 : MemoryAllocatorDump(absolute_name, |
71 process_memory_dump, | 51 process_memory_dump, |
72 MemoryAllocatorDumpGuid(StringPrintf( | 52 MemoryAllocatorDumpGuid(StringPrintf( |
73 "%d:%s", | 53 "%d:%s", |
74 TraceLog::GetInstance()->process_id(), | 54 TraceLog::GetInstance()->process_id(), |
75 absolute_name.c_str()))) { | 55 absolute_name.c_str()))) { |
| 56 string_conversion_buffer_.reserve(16); |
76 } | 57 } |
77 | 58 |
78 MemoryAllocatorDump::~MemoryAllocatorDump() { | 59 MemoryAllocatorDump::~MemoryAllocatorDump() { |
79 } | 60 } |
80 | 61 |
81 void MemoryAllocatorDump::Add(const std::string& name, | 62 void MemoryAllocatorDump::AddScalar(const char* name, |
82 const char* type, | 63 const char* units, |
83 const char* units, | 64 uint64 value) { |
84 scoped_ptr<Value> value) { | 65 SStringPrintf(&string_conversion_buffer_, "%" PRIx64, value); |
85 scoped_ptr<DictionaryValue> attribute(new DictionaryValue()); | 66 attributes_->BeginDictionary(name); |
86 DCHECK(!attributes_.HasKey(name)); | 67 attributes_->SetString("type", kTypeScalar); |
87 attribute->SetStringWithoutPathExpansion("type", type); | 68 attributes_->SetString("units", units); |
88 attribute->SetStringWithoutPathExpansion("units", units); | 69 attributes_->SetString("value", string_conversion_buffer_); |
89 attribute->SetWithoutPathExpansion("value", value.Pass()); | 70 attributes_->EndDictionary(); |
90 attributes_.SetWithoutPathExpansion(name, attribute.Pass()); | |
91 } | 71 } |
92 | 72 |
93 bool MemoryAllocatorDump::Get(const std::string& name, | 73 void MemoryAllocatorDump::AddScalarF(const char* name, |
94 const char** out_type, | 74 const char* units, |
95 const char** out_units, | 75 double value) { |
96 const Value** out_value) const { | 76 attributes_->BeginDictionary(name); |
97 const DictionaryValue* attribute = nullptr; | 77 attributes_->SetString("type", kTypeScalar); |
98 if (!attributes_.GetDictionaryWithoutPathExpansion(name, &attribute)) | 78 attributes_->SetString("units", units); |
99 return false; | 79 attributes_->SetDouble("value", value); |
100 | 80 attributes_->EndDictionary(); |
101 if (!GetDictionaryValueAsCStr(attribute, "type", out_type)) | |
102 return false; | |
103 | |
104 if (!GetDictionaryValueAsCStr(attribute, "units", out_units)) | |
105 return false; | |
106 | |
107 if (!attribute->GetWithoutPathExpansion("value", out_value)) | |
108 return false; | |
109 | |
110 return true; | |
111 } | 81 } |
112 | 82 |
113 void MemoryAllocatorDump::AddScalar(const std::string& name, | 83 void MemoryAllocatorDump::AddString(const char* name, |
114 const char* units, | |
115 uint64 value) { | |
116 scoped_ptr<Value> hex_value(new StringValue(StringPrintf("%" PRIx64, value))); | |
117 Add(name, kTypeScalar, units, hex_value.Pass()); | |
118 } | |
119 | |
120 void MemoryAllocatorDump::AddScalarF(const std::string& name, | |
121 const char* units, | |
122 double value) { | |
123 Add(name, kTypeScalar, units, make_scoped_ptr(new FundamentalValue(value))); | |
124 } | |
125 | |
126 void MemoryAllocatorDump::AddString(const std::string& name, | |
127 const char* units, | 84 const char* units, |
128 const std::string& value) { | 85 const std::string& value) { |
129 scoped_ptr<Value> str_value(new StringValue(value)); | 86 attributes_->BeginDictionary(name); |
130 Add(name, kTypeString, units, str_value.Pass()); | 87 attributes_->SetString("type", kTypeString); |
| 88 attributes_->SetString("units", units); |
| 89 attributes_->SetString("value", value); |
| 90 attributes_->EndDictionary(); |
131 } | 91 } |
132 | 92 |
133 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const { | 93 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const { |
134 value->BeginDictionary(absolute_name_.c_str()); | 94 value->BeginDictionaryWithCopiedName(absolute_name_); |
135 value->SetString("guid", guid_.ToString()); | 95 value->SetString("guid", guid_.ToString()); |
136 | 96 value->SetValue("attrs", *attributes_); |
137 value->BeginDictionary("attrs"); | |
138 | |
139 for (DictionaryValue::Iterator it(attributes_); !it.IsAtEnd(); it.Advance()) | |
140 value->SetValue(it.key().c_str(), it.value().CreateDeepCopy()); | |
141 | |
142 value->EndDictionary(); // "attrs": { ... } | |
143 value->EndDictionary(); // "allocator_name/heap_subheap": { ... } | 97 value->EndDictionary(); // "allocator_name/heap_subheap": { ... } |
144 } | 98 } |
145 | 99 |
146 } // namespace trace_event | 100 } // namespace trace_event |
147 } // namespace base | 101 } // namespace base |
OLD | NEW |