Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: base/trace_event/memory_profiler_heap_dumper.cc

Issue 1419633004: [Tracing] Introduce HeapDumpWriter helper class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove WriteStackFrames, DISALLOW_COPY_AND_ASSIGN Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/trace_event/memory_profiler_heap_dumper.h"
6
7 #include <numeric>
8
9 #include "base/format_macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/memory_profiler_allocation_register.h"
12
13 namespace base {
14 namespace trace_event {
15
16 namespace {
17
18 template <typename T>
19 bool PairSizeGt(const std::pair<T, size_t>& lhs,
20 const std::pair<T, size_t>& rhs) {
21 return lhs.second > rhs.second;
22 }
23
24 template <typename T>
25 size_t PairSizeAdd(size_t acc, const std::pair<T, size_t>& rhs) {
26 return acc + rhs.second;
27 }
28
29 } // namespace
30
31 HeapDumper::HeapDumper(scoped_refptr<TracedValue> traced_value,
32 StackFrameDeduplicator* sf_deduplicator)
33 : traced_value_(traced_value), sf_deduplicator_(sf_deduplicator) {}
34 HeapDumper::~HeapDumper() {}
35
36 void HeapDumper::Fill(const AllocationRegister& allocation_register) {
37 for (auto alloc : allocation_register)
38 acc_by_backtrace_[alloc.context.backtrace] += alloc.size;
39 }
40
41 void HeapDumper::WriteHeapDump() {
42 // Sort the backtraces by size in descending order.
43 std::vector<std::pair<Backtrace, size_t>> sorted_by_backtrace;
44
45 std::copy(acc_by_backtrace_.begin(), acc_by_backtrace_.end(),
46 std::back_inserter(sorted_by_backtrace));
47 std::sort(sorted_by_backtrace.begin(), sorted_by_backtrace.end(),
48 PairSizeGt<Backtrace>);
49
50 traced_value_->BeginArray("heap");
51
52 // The global size, no column specified.
53 {
54 size_t total_size =
55 std::accumulate(sorted_by_backtrace.begin(), sorted_by_backtrace.end(),
56 size_t(0), PairSizeAdd<Backtrace>);
57 traced_value_->BeginDictionary();
58 WriteSize(total_size);
59 traced_value_->EndDictionary();
60 }
61
62 // Size per backtrace.
63 for (auto bt_sz = sorted_by_backtrace.begin();
64 bt_sz != sorted_by_backtrace.end(); bt_sz++) {
65 traced_value_->BeginDictionary();
66 // Insert a forward reference to the backtrace that will be written to the
67 // |stackFrames| dictionary later on.
68 traced_value_->SetInteger("bt", sf_deduplicator_->Insert(bt_sz->first));
69 WriteSize(bt_sz->second);
70 traced_value_->EndDictionary();
71 }
72
73 traced_value_->EndArray(); // heap
74 }
75
76 void HeapDumper::WriteSize(size_t size) {
77 // Format size as hexadecimal string into |buffer_|.
78 SStringPrintf(&buffer_, "%" PRIx64, static_cast<uint64_t>(size));
79 traced_value_->SetString("size", buffer_);
80 }
81
82 } // namespace trace_event
83 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698