Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / base / trace_event / memory_allocator_dump.cc
blob77c32ec3d8ad7ccdeabe6655c9ae0e85cbe1871b
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 : absolute_name_(absolute_name), process_memory_dump_(process_memory_dump) {
46 // The |absolute_name| cannot be empty.
47 DCHECK(!absolute_name.empty());
49 // The |absolute_name| can contain slash separator, but not leading or
50 // trailing ones.
51 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/');
53 // Dots are not allowed anywhere as the underlying base::DictionaryValue
54 // would treat them magically and split in sub-nodes, which is not intended.
55 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.'));
58 MemoryAllocatorDump::~MemoryAllocatorDump() {
61 void MemoryAllocatorDump::Add(const std::string& name,
62 const char* type,
63 const char* units,
64 scoped_ptr<Value> 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());
73 bool MemoryAllocatorDump::Get(const std::string& 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;
81 if (!GetDictionaryValueAsCStr(attribute, "type", out_type))
82 return false;
84 if (!GetDictionaryValueAsCStr(attribute, "units", out_units))
85 return false;
87 if (!attribute->GetWithoutPathExpansion("value", out_value))
88 return false;
90 return true;
93 void MemoryAllocatorDump::AddScalar(const std::string& name,
94 const char* units,
95 uint64 value) {
96 scoped_ptr<Value> hex_value(new StringValue(StringPrintf("%" PRIx64, value)));
97 Add(name, kTypeScalar, units, hex_value.Pass());
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());
107 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const {
108 value->BeginDictionary(absolute_name_.c_str());
109 value->BeginDictionary("attrs");
111 for (DictionaryValue::Iterator it(attributes_); !it.IsAtEnd(); it.Advance())
112 value->SetValue(it.key().c_str(), it.value().DeepCopy());
114 value->EndDictionary(); // "attrs": { ... }
115 value->EndDictionary(); // "allocator_name/heap_subheap": { ... }
118 } // namespace trace_event
119 } // namespace base