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_allocator_attributes.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/memory_dump_provider.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "base/values.h"
16 namespace trace_event
{
18 MemoryAllocatorDump::MemoryAllocatorDump(const std::string
& name
,
19 MemoryAllocatorDump
* parent
)
22 physical_size_in_bytes_(0),
23 allocated_objects_count_(0),
24 allocated_objects_size_in_bytes_(0) {
25 // Dots are not allowed in the name as the underlying base::DictionaryValue
26 // would treat them magically and split in sub-nodes, which is not intended.
27 DCHECK_EQ(std::string::npos
, name
.find_first_of('.'));
30 MemoryAllocatorDump::~MemoryAllocatorDump() {
33 void MemoryAllocatorDump::SetExtraAttribute(const std::string
& name
,
35 extra_attributes_
.SetInteger(name
, value
);
38 int MemoryAllocatorDump::GetExtraIntegerAttribute(
39 const std::string
& name
) const {
42 res
= extra_attributes_
.GetInteger(name
, &value
);
43 DCHECK(res
) << "Allocator attribute '" << name
<< "' not found";
47 void MemoryAllocatorDump::AsValueInto(TracedValue
* value
) const {
48 static const char kHexFmt
[] = "%" PRIx64
;
50 value
->BeginDictionary(name_
.c_str());
52 value
->SetString("parent", parent_
? parent_
->name_
: "");
53 value
->SetString("physical_size_in_bytes",
54 StringPrintf(kHexFmt
, physical_size_in_bytes_
));
55 value
->SetString("allocated_objects_count",
56 StringPrintf(kHexFmt
, allocated_objects_count_
));
57 value
->SetString("allocated_objects_size_in_bytes",
58 StringPrintf(kHexFmt
, allocated_objects_size_in_bytes_
));
60 // Copy all the extra attributes.
61 const MemoryDumpProvider
* mdp
=
62 MemoryDumpManager::GetInstance()->dump_provider_currently_active();
63 const MemoryAllocatorDeclaredAttributes
& extra_attributes_types
=
64 mdp
->allocator_attributes();
66 value
->BeginDictionary("args");
67 for (DictionaryValue::Iterator
it(extra_attributes_
); !it
.IsAtEnd();
69 const std::string
& attr_name
= it
.key();
70 const Value
& attr_value
= it
.value();
71 value
->BeginDictionary(attr_name
.c_str());
72 value
->SetValue("value", attr_value
.DeepCopy());
74 auto attr_it
= extra_attributes_types
.find(attr_name
);
75 DCHECK(attr_it
!= extra_attributes_types
.end())
76 << "Allocator attribute " << attr_name
77 << " not declared for the dumper " << mdp
->GetFriendlyName();
79 // TODO(primiano): the "type" should be dumped just once, not repeated on
80 // on every event. The ability of doing so depends on crbug.com/466121.
81 value
->SetString("type", attr_it
->second
.type
);
83 value
->EndDictionary(); // "arg_name": { "type": "...", "value": "..." }
85 value
->EndDictionary(); // "args": {}
87 value
->EndDictionary(); // "allocator name": {}
90 } // namespace trace_event