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());
53 value
->SetString("parent", parent_
->name_
);
55 value
->SetString("physical_size_in_bytes",
56 StringPrintf(kHexFmt
, physical_size_in_bytes_
));
57 value
->SetString("allocated_objects_count",
58 StringPrintf(kHexFmt
, allocated_objects_count_
));
59 value
->SetString("allocated_objects_size_in_bytes",
60 StringPrintf(kHexFmt
, allocated_objects_size_in_bytes_
));
62 // Copy all the extra attributes.
63 const MemoryDumpProvider
* mdp
=
64 MemoryDumpManager::GetInstance()->dump_provider_currently_active();
65 const MemoryAllocatorDeclaredAttributes
& extra_attributes_types
=
66 mdp
->allocator_attributes();
68 value
->BeginDictionary("args");
69 for (DictionaryValue::Iterator
it(extra_attributes_
); !it
.IsAtEnd();
71 const std::string
& attr_name
= it
.key();
72 const Value
& attr_value
= it
.value();
73 value
->BeginDictionary(attr_name
.c_str());
74 value
->SetValue("value", attr_value
.DeepCopy());
76 auto attr_it
= extra_attributes_types
.find(attr_name
);
77 DCHECK(attr_it
!= extra_attributes_types
.end())
78 << "Allocator attribute " << attr_name
79 << " not declared for the dumper " << mdp
->GetFriendlyName();
81 // TODO(primiano): the "type" should be dumped just once, not repeated on
82 // on every event. The ability of doing so depends on crbug.com/466121.
83 value
->SetString("type", attr_it
->second
.type
);
85 value
->EndDictionary(); // "arg_name": { "type": "...", "value": "..." }
87 value
->EndDictionary(); // "args": {}
89 value
->EndDictionary(); // "allocator name": {}
92 } // namespace trace_event