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/heap_profiler_stack_frame_deduplicator.h" | 5 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 10 matching lines...) Expand all Loading... | |
21 StackFrameDeduplicator::FrameNode::FrameNode(StackFrame frame, | 21 StackFrameDeduplicator::FrameNode::FrameNode(StackFrame frame, |
22 int parent_frame_index) | 22 int parent_frame_index) |
23 : frame(frame), parent_frame_index(parent_frame_index) {} | 23 : frame(frame), parent_frame_index(parent_frame_index) {} |
24 StackFrameDeduplicator::FrameNode::FrameNode(const FrameNode& other) = default; | 24 StackFrameDeduplicator::FrameNode::FrameNode(const FrameNode& other) = default; |
25 StackFrameDeduplicator::FrameNode::~FrameNode() {} | 25 StackFrameDeduplicator::FrameNode::~FrameNode() {} |
26 | 26 |
27 size_t StackFrameDeduplicator::FrameNode::EstimateMemoryUsage() const { | 27 size_t StackFrameDeduplicator::FrameNode::EstimateMemoryUsage() const { |
28 return base::trace_event::EstimateMemoryUsage(children); | 28 return base::trace_event::EstimateMemoryUsage(children); |
29 } | 29 } |
30 | 30 |
31 StackFrameDeduplicator::StackFrameDeduplicator() {} | 31 StackFrameDeduplicator::StackFrameDeduplicator() : last_appended_index_(0) {} |
32 StackFrameDeduplicator::~StackFrameDeduplicator() {} | 32 StackFrameDeduplicator::~StackFrameDeduplicator() {} |
33 | 33 |
34 int StackFrameDeduplicator::Insert(const StackFrame* beginFrame, | 34 int StackFrameDeduplicator::Insert(const StackFrame* beginFrame, |
35 const StackFrame* endFrame) { | 35 const StackFrame* endFrame) { |
36 int frame_index = -1; | 36 int frame_index = -1; |
37 std::map<StackFrame, int>* nodes = &roots_; | 37 std::map<StackFrame, int>* nodes = &roots_; |
38 | 38 |
39 // Loop through the frames, early out when a frame is null. | 39 // Loop through the frames, early out when a frame is null. |
40 for (const StackFrame* it = beginFrame; it != endFrame; it++) { | 40 for (const StackFrame* it = beginFrame; it != endFrame; it++) { |
41 StackFrame frame = *it; | 41 StackFrame frame = *it; |
(...skipping 18 matching lines...) Expand all Loading... | |
60 // A tree node for this frame exists. Look for the next one. | 60 // A tree node for this frame exists. Look for the next one. |
61 frame_index = node->second; | 61 frame_index = node->second; |
62 } | 62 } |
63 | 63 |
64 nodes = &frames_[frame_index].children; | 64 nodes = &frames_[frame_index].children; |
65 } | 65 } |
66 | 66 |
67 return frame_index; | 67 return frame_index; |
68 } | 68 } |
69 | 69 |
70 void StackFrameDeduplicator::AppendAsTraceFormat(std::string* out) const { | 70 void StackFrameDeduplicator::AppendIncrementally(TracedValue* traced_value) { |
71 out->append("{"); // Begin the |stackFrames| dictionary. | |
72 | |
73 int i = 0; | |
74 auto frame_node = begin(); | |
75 auto it_end = end(); | |
76 std::string stringify_buffer; | 71 std::string stringify_buffer; |
77 | 72 |
78 while (frame_node != it_end) { | 73 for (; last_appended_index_ < frames_.size(); ++last_appended_index_) { |
79 // The |stackFrames| format is a dictionary, not an array, so the | 74 const auto& frame_node = frames_[last_appended_index_]; |
80 // keys are stringified indices. Write the index manually, then use | 75 traced_value->BeginDictionary(); |
81 // |TracedValue| to format the object. This is to avoid building the | |
82 // entire dictionary as a |TracedValue| in memory. | |
83 SStringPrintf(&stringify_buffer, "\"%d\":", i); | |
84 out->append(stringify_buffer); | |
85 | 76 |
86 std::unique_ptr<TracedValue> frame_node_value(new TracedValue); | 77 traced_value->SetInteger("id", last_appended_index_); |
87 const StackFrame& frame = frame_node->frame; | 78 |
79 const StackFrame& frame = frame_node.frame; | |
88 switch (frame.type) { | 80 switch (frame.type) { |
89 case StackFrame::Type::TRACE_EVENT_NAME: | 81 case StackFrame::Type::TRACE_EVENT_NAME: |
90 frame_node_value->SetString( | 82 traced_value->SetString("name", static_cast<const char*>(frame.value)); |
91 "name", static_cast<const char*>(frame.value)); | |
92 break; | 83 break; |
93 case StackFrame::Type::THREAD_NAME: | 84 case StackFrame::Type::THREAD_NAME: |
94 SStringPrintf(&stringify_buffer, | 85 SStringPrintf(&stringify_buffer, |
95 "[Thread: %s]", | 86 "[Thread: %s]", |
96 static_cast<const char*>(frame.value)); | 87 static_cast<const char*>(frame.value)); |
97 frame_node_value->SetString("name", stringify_buffer); | 88 traced_value->SetString("name", stringify_buffer); |
98 break; | 89 break; |
99 case StackFrame::Type::PROGRAM_COUNTER: | 90 case StackFrame::Type::PROGRAM_COUNTER: |
100 SStringPrintf(&stringify_buffer, | 91 SStringPrintf(&stringify_buffer, |
101 "pc:%" PRIxPTR, | 92 "pc:%" PRIxPTR, |
102 reinterpret_cast<uintptr_t>(frame.value)); | 93 reinterpret_cast<uintptr_t>(frame.value)); |
103 frame_node_value->SetString("name", stringify_buffer); | 94 traced_value->SetString("name", stringify_buffer); |
104 break; | 95 break; |
105 } | 96 } |
106 if (frame_node->parent_frame_index >= 0) { | 97 |
107 SStringPrintf(&stringify_buffer, "%d", frame_node->parent_frame_index); | 98 if (frame_node.parent_frame_index >= 0) { |
108 frame_node_value->SetString("parent", stringify_buffer); | 99 traced_value->SetInteger("parent", frame_node.parent_frame_index); |
Primiano Tucci (use gerrit)
2017/02/17 17:07:05
do we have the guarantee that the parent is always
DmitrySkiba
2017/02/23 07:17:19
Yes, because we dump nodes_ sequentially, and in t
| |
109 } | 100 } |
110 frame_node_value->AppendAsTraceFormat(out); | |
111 | 101 |
112 i++; | 102 traced_value->EndDictionary(); |
113 frame_node++; | |
114 | |
115 if (frame_node != it_end) | |
116 out->append(","); | |
117 } | 103 } |
118 | |
119 out->append("}"); // End the |stackFrames| dictionary. | |
120 } | 104 } |
121 | 105 |
122 void StackFrameDeduplicator::EstimateTraceMemoryOverhead( | 106 void StackFrameDeduplicator::EstimateTraceMemoryOverhead( |
123 TraceEventMemoryOverhead* overhead) { | 107 TraceEventMemoryOverhead* overhead) { |
124 size_t memory_usage = | 108 size_t memory_usage = |
125 EstimateMemoryUsage(frames_) + EstimateMemoryUsage(roots_); | 109 EstimateMemoryUsage(frames_) + EstimateMemoryUsage(roots_); |
126 overhead->Add("StackFrameDeduplicator", | 110 overhead->Add("StackFrameDeduplicator", |
127 sizeof(StackFrameDeduplicator) + memory_usage); | 111 sizeof(StackFrameDeduplicator) + memory_usage); |
128 } | 112 } |
129 | 113 |
130 } // namespace trace_event | 114 } // namespace trace_event |
131 } // namespace base | 115 } // namespace base |
OLD | NEW |