Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / trace_event / memory_allocator_dump.cc
blob4037f946c9ef046e678ff261af270792a631d67b
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 const char MemoryAllocatorDump::kNameSize[] = "size";
19 const char MemoryAllocatorDump::kNameObjectsCount[] = "objects_count";
20 const char MemoryAllocatorDump::kTypeScalar[] = "scalar";
21 const char MemoryAllocatorDump::kTypeString[] = "string";
22 const char MemoryAllocatorDump::kUnitsBytes[] = "bytes";
23 const char MemoryAllocatorDump::kUnitsObjects[] = "objects";
25 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name,
26 ProcessMemoryDump* process_memory_dump,
27 const MemoryAllocatorDumpGuid& guid)
28 : absolute_name_(absolute_name),
29 process_memory_dump_(process_memory_dump),
30 attributes_(new TracedValue),
31 guid_(guid) {
32 // The |absolute_name| cannot be empty.
33 DCHECK(!absolute_name.empty());
35 // The |absolute_name| can contain slash separator, but not leading or
36 // trailing ones.
37 DCHECK(absolute_name[0] != '/' && *absolute_name.rbegin() != '/');
39 // Dots are not allowed anywhere as the underlying base::DictionaryValue
40 // would treat them magically and split in sub-nodes, which is not intended.
41 DCHECK_EQ(std::string::npos, absolute_name.find_first_of('.'));
44 // If the caller didn't provide a guid, make one up by hashing the
45 // absolute_name with the current PID.
46 // Rationale: |absolute_name| is already supposed to be unique within a
47 // process, the pid will make it unique among all processes.
48 MemoryAllocatorDump::MemoryAllocatorDump(const std::string& absolute_name,
49 ProcessMemoryDump* process_memory_dump)
50 : MemoryAllocatorDump(absolute_name,
51 process_memory_dump,
52 MemoryAllocatorDumpGuid(StringPrintf(
53 "%d:%s",
54 TraceLog::GetInstance()->process_id(),
55 absolute_name.c_str()))) {
56 string_conversion_buffer_.reserve(16);
59 MemoryAllocatorDump::~MemoryAllocatorDump() {
62 void MemoryAllocatorDump::AddScalar(const char* name,
63 const char* units,
64 uint64 value) {
65 SStringPrintf(&string_conversion_buffer_, "%" PRIx64, value);
66 attributes_->BeginDictionary(name);
67 attributes_->SetString("type", kTypeScalar);
68 attributes_->SetString("units", units);
69 attributes_->SetString("value", string_conversion_buffer_);
70 attributes_->EndDictionary();
73 void MemoryAllocatorDump::AddScalarF(const char* name,
74 const char* units,
75 double value) {
76 attributes_->BeginDictionary(name);
77 attributes_->SetString("type", kTypeScalar);
78 attributes_->SetString("units", units);
79 attributes_->SetDouble("value", value);
80 attributes_->EndDictionary();
83 void MemoryAllocatorDump::AddString(const char* name,
84 const char* units,
85 const std::string& value) {
86 attributes_->BeginDictionary(name);
87 attributes_->SetString("type", kTypeString);
88 attributes_->SetString("units", units);
89 attributes_->SetString("value", value);
90 attributes_->EndDictionary();
93 void MemoryAllocatorDump::AsValueInto(TracedValue* value) const {
94 value->BeginDictionaryWithCopiedName(absolute_name_);
95 value->SetString("guid", guid_.ToString());
96 value->SetValue("attrs", *attributes_);
97 value->EndDictionary(); // "allocator_name/heap_subheap": { ... }
100 } // namespace trace_event
101 } // namespace base