| 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 // static | 18 namespace { |
| 19 const char MemoryAllocatorDump::kRootHeap[] = ""; | 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 |
| 20 | 34 |
| 21 // static | 35 const char MemoryAllocatorDump::kNameOuterSize[] = "outer_size"; |
| 22 std::string MemoryAllocatorDump::GetAbsoluteName( | 36 const char MemoryAllocatorDump::kNameInnerSize[] = "inner_size"; |
| 23 const std::string& allocator_name, | 37 const char MemoryAllocatorDump::kNameObjectsCount[] = "objects_count"; |
| 24 const std::string& heap_name) { | 38 const char MemoryAllocatorDump::kTypeScalar[] = "scalar"; |
| 25 return allocator_name + (heap_name == kRootHeap ? "" : "/" + heap_name); | 39 const char MemoryAllocatorDump::kTypeString[] = "string"; |
| 26 } | 40 const char MemoryAllocatorDump::kUnitsBytes[] = "bytes"; |
| 41 const char MemoryAllocatorDump::kUnitsObjects[] = "objects"; |
| 27 | 42 |
| 28 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& allocator_name, | 43 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name, |
| 29 const std::string& heap_name, | |
| 30 ProcessMemoryDump* process_memory_dump) | 44 ProcessMemoryDump* process_memory_dump) |
| 31 : allocator_name_(allocator_name), | 45 : absolute_name_(absolute_name), process_memory_dump_(process_memory_dump) { |
| 32 heap_name_(heap_name), | 46 // The |absolute_name| cannot be empty. |
| 33 process_memory_dump_(process_memory_dump), | 47 DCHECK(!absolute_name.empty()); |
| 34 physical_size_in_bytes_(0), | |
| 35 allocated_objects_count_(0), | |
| 36 allocated_objects_size_in_bytes_(0) { | |
| 37 // The allocator name cannot be empty or contain slash separators. | |
| 38 DCHECK(!allocator_name.empty()); | |
| 39 DCHECK_EQ(std::string::npos, allocator_name.find_first_of('/')); | |
| 40 | 48 |
| 41 // The heap_name can be empty and contain slash separator, but not | 49 // The |absolute_name| can contain slash separator, but not leading or |
| 42 // leading or trailing ones. | 50 // trailing ones. |
| 43 DCHECK(heap_name.empty() || | 51 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/'); |
| 44 (heap_name[0] != '/' && *heap_name.rbegin() != '/')); | |
| 45 | 52 |
| 46 // Dots are not allowed anywhere as the underlying base::DictionaryValue | 53 // Dots are not allowed anywhere as the underlying base::DictionaryValue |
| 47 // would treat them magically and split in sub-nodes, which is not intended. | 54 // would treat them magically and split in sub-nodes, which is not intended. |
| 48 DCHECK_EQ(std::string::npos, allocator_name.find_first_of('.')); | 55 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.')); |
| 49 DCHECK_EQ(std::string::npos, heap_name.find_first_of('.')); | |
| 50 } | 56 } |
| 51 | 57 |
| 52 MemoryAllocatorDump::~MemoryAllocatorDump() { | 58 MemoryAllocatorDump::~MemoryAllocatorDump() { |
| 53 } | 59 } |
| 54 | 60 |
| 55 void MemoryAllocatorDump::SetAttribute(const std::string& name, int value) { | 61 void MemoryAllocatorDump::Add(const std::string& name, |
| 56 DCHECK(GetAttributesTypeInfo().Exists(allocator_name_, name)) | 62 const char* type, |
| 57 << "attribute '" << name << "' not declared." | 63 const char* units, |
| 58 << "See MemoryDumpProvider.DeclareAllocatorAttribute()"; | 64 scoped_ptr<Value> value) { |
| 59 attributes_values_.SetInteger(name, value); | 65 scoped_ptr<DictionaryValue> attribute(new DictionaryValue()); |
| 66 DCHECK(!attributes_.HasKey(name)); |
| 67 attribute->SetStringWithoutPathExpansion("type", type); |
| 68 attribute->SetStringWithoutPathExpansion("units", units); |
| 69 attribute->SetWithoutPathExpansion("value", value.Pass()); |
| 70 attributes_.SetWithoutPathExpansion(name, attribute.Pass()); |
| 60 } | 71 } |
| 61 | 72 |
| 62 std::string MemoryAllocatorDump::GetAbsoluteName() const { | 73 bool MemoryAllocatorDump::Get(const std::string& name, |
| 63 return GetAbsoluteName(allocator_name_, heap_name_); | 74 const char** out_type, |
| 75 const char** out_units, |
| 76 const Value** out_value) const { |
| 77 const DictionaryValue* attribute = nullptr; |
| 78 if (!attributes_.GetDictionaryWithoutPathExpansion(name, &attribute)) |
| 79 return false; |
| 80 |
| 81 if (!GetDictionaryValueAsCStr(attribute, "type", out_type)) |
| 82 return false; |
| 83 |
| 84 if (!GetDictionaryValueAsCStr(attribute, "units", out_units)) |
| 85 return false; |
| 86 |
| 87 if (!attribute->GetWithoutPathExpansion("value", out_value)) |
| 88 return false; |
| 89 |
| 90 return true; |
| 64 } | 91 } |
| 65 | 92 |
| 66 int MemoryAllocatorDump::GetIntegerAttribute(const std::string& name) const { | 93 void MemoryAllocatorDump::AddScalar(const std::string& name, |
| 67 int value = -1; | 94 const char* units, |
| 68 bool res = attributes_values_.GetInteger(name, &value); | 95 uint64 value) { |
| 69 DCHECK(res) << "Attribute '" << name << "' not found"; | 96 scoped_ptr<Value> hex_value(new StringValue(StringPrintf("%" PRIx64, value))); |
| 70 return value; | 97 Add(name, kTypeScalar, units, hex_value.Pass()); |
| 98 } |
| 99 |
| 100 void MemoryAllocatorDump::AddString(const std::string& name, |
| 101 const char* units, |
| 102 const std::string& value) { |
| 103 scoped_ptr<Value> str_value(new StringValue(value)); |
| 104 Add(name, kTypeString, units, str_value.Pass()); |
| 71 } | 105 } |
| 72 | 106 |
| 73 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const { | 107 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const { |
| 74 static const char kHexFmt[] = "%" PRIx64; | 108 value->BeginDictionary(absolute_name_.c_str()); |
| 75 | |
| 76 value->BeginDictionary(GetAbsoluteName().c_str()); | |
| 77 value->BeginDictionary("attrs"); | 109 value->BeginDictionary("attrs"); |
| 78 | 110 |
| 79 // TODO(primiano): these hard-coded types are temporary to transition to the | 111 for (DictionaryValue::Iterator it(attributes_); !it.IsAtEnd(); it.Advance()) |
| 80 // new generalized attribute format. This code will be refactored by the end | 112 value->SetValue(it.key().c_str(), it.value().DeepCopy()); |
| 81 // of May 2015. | |
| 82 value->BeginDictionary("outer_size"); | |
| 83 value->SetString("type", "scalar"); | |
| 84 value->SetString("units", "bytes"); | |
| 85 value->SetString("value", StringPrintf(kHexFmt, physical_size_in_bytes_)); | |
| 86 value->EndDictionary(); | |
| 87 | |
| 88 value->BeginDictionary("inner_size"); | |
| 89 value->SetString("type", "scalar"); | |
| 90 value->SetString("units", "bytes"); | |
| 91 value->SetString("value", | |
| 92 StringPrintf(kHexFmt, allocated_objects_size_in_bytes_)); | |
| 93 value->EndDictionary(); | |
| 94 | |
| 95 value->BeginDictionary("objects_count"); | |
| 96 value->SetString("type", "scalar"); | |
| 97 value->SetString("units", "objects"); | |
| 98 value->SetString("value", StringPrintf(kHexFmt, allocated_objects_count_)); | |
| 99 value->EndDictionary(); | |
| 100 | |
| 101 // Copy all the extra attributes. | |
| 102 for (DictionaryValue::Iterator it(attributes_values_); !it.IsAtEnd(); | |
| 103 it.Advance()) { | |
| 104 const std::string& attr_name = it.key(); | |
| 105 const Value& attr_value = it.value(); | |
| 106 value->BeginDictionary(attr_name.c_str()); | |
| 107 value->SetValue("value", attr_value.DeepCopy()); | |
| 108 | |
| 109 const std::string& attr_type = | |
| 110 GetAttributesTypeInfo().Get(allocator_name_, attr_name); | |
| 111 DCHECK(!attr_type.empty()); | |
| 112 value->SetString("type", "scalar"); | |
| 113 value->SetString("units", attr_type); | |
| 114 | |
| 115 value->EndDictionary(); // "arg_name": { "type": "...", "value": "..." } | |
| 116 } | |
| 117 | 113 |
| 118 value->EndDictionary(); // "attrs": { ... } | 114 value->EndDictionary(); // "attrs": { ... } |
| 119 value->EndDictionary(); // "allocator_name/heap_subheap": { ... } | 115 value->EndDictionary(); // "allocator_name/heap_subheap": { ... } |
| 120 } | 116 } |
| 121 | 117 |
| 122 const MemoryAllocatorAttributesTypeInfo& | |
| 123 MemoryAllocatorDump::GetAttributesTypeInfo() const { | |
| 124 return process_memory_dump_->session_state()->allocators_attributes_type_info; | |
| 125 } | |
| 126 | |
| 127 } // namespace trace_event | 118 } // namespace trace_event |
| 128 } // namespace base | 119 } // namespace base |
| OLD | NEW |