summaryrefslogtreecommitdiffstats
path: root/base/trace_event/heap_profiler_heap_dump_writer.cc
blob: b0ed86f32326d0ec66be40b9eca8c1f1c1f2766c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/trace_event/heap_profiler_heap_dump_writer.h"

#include <algorithm>
#include <iterator>
#include <utility>
#include <vector>

#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/heap_profiler_stack_frame_deduplicator.h"
#include "base/trace_event/heap_profiler_type_name_deduplicator.h"
#include "base/trace_event/trace_event_argument.h"

namespace base {
namespace trace_event {

namespace {

template <typename T>
bool PairSizeGt(const std::pair<T, size_t>& lhs,
                const std::pair<T, size_t>& rhs) {
  return lhs.second > rhs.second;
}

// Converts a |hash_map<T, size_t>| into a vector of (T, size_t) pairs that is
// ordered from high |size_t| to low |size_t|.
template <typename T>
std::vector<std::pair<T, size_t>> SortBySizeDescending(
    const hash_map<T, size_t>& grouped) {
  std::vector<std::pair<T, size_t>> sorted;
  sorted.reserve(grouped.size());
  std::copy(grouped.begin(), grouped.end(), std::back_inserter(sorted));
  std::sort(sorted.begin(), sorted.end(), PairSizeGt<T>);
  return sorted;
}

}  // namespace

HeapDumpWriter::HeapDumpWriter(StackFrameDeduplicator* stack_frame_deduplicator,
                               TypeNameDeduplicator* type_name_deduplicator)
    : traced_value_(new TracedValue()),
      stack_frame_deduplicator_(stack_frame_deduplicator),
      type_name_deduplicator_(type_name_deduplicator) {}

HeapDumpWriter::~HeapDumpWriter() {}

void HeapDumpWriter::InsertAllocation(const AllocationContext& context,
                                      size_t size) {
  bytes_by_context_[context] += size;
}

scoped_refptr<TracedValue> HeapDumpWriter::WriteHeapDump() {
  // Group by backtrace and by type ID, and compute the total heap size while
  // iterating anyway.
  size_t total_size = 0;
  hash_map<Backtrace, size_t> bytes_by_backtrace;
  hash_map<const char*, size_t> bytes_by_type;

  for (auto context_size : bytes_by_context_) {
    total_size += context_size.second;
    bytes_by_backtrace[context_size.first.backtrace] += context_size.second;
    bytes_by_type[context_size.first.type_name] += context_size.second;
  }

  auto sorted_bytes_by_backtrace = SortBySizeDescending(bytes_by_backtrace);
  auto sorted_bytes_by_type = SortBySizeDescending(bytes_by_type);

  traced_value_->BeginArray("entries");

  // The global size, no column specified.
  {
    traced_value_->BeginDictionary();
    WriteSize(total_size);
    traced_value_->EndDictionary();
  }

  // Entries with the size per backtrace.
  for (const auto& entry : sorted_bytes_by_backtrace) {
    traced_value_->BeginDictionary();
    // Insert a forward reference to the backtrace that will be written to the
    // |stackFrames| dictionary later on.
    int idx = stack_frame_deduplicator_->Insert(std::begin(entry.first.frames),
                                                std::end(entry.first.frames));
    WriteStackFrameIndex(idx);
    WriteSize(entry.second);
    traced_value_->EndDictionary();
  }

  // Entries with the size per type.
  for (const auto& entry : sorted_bytes_by_type) {
    traced_value_->BeginDictionary();
    // Insert a forward reference to the type name that will be written to the
    // trace when it is flushed.
    WriteTypeId(type_name_deduplicator_->Insert(entry.first));
    WriteSize(entry.second);
    traced_value_->EndDictionary();
  }

  traced_value_->EndArray();  // "entries"

  return traced_value_;
}

void HeapDumpWriter::WriteStackFrameIndex(int index) {
  if (index == -1) {
    // An empty backtrace (which will have index -1) is represented by the empty
    // string, because there is no leaf frame to reference in |stackFrames|.
    traced_value_->SetString("bt", "");
  } else {
    // Format index of the leaf frame as a string, because |stackFrames| is a
    // dictionary, not an array.
    SStringPrintf(&buffer_, "%i", index);
    traced_value_->SetString("bt", buffer_);
  }
}

void HeapDumpWriter::WriteTypeId(int type_id) {
  // Format the type ID as a string.
  SStringPrintf(&buffer_, "%i", type_id);
  traced_value_->SetString("type", buffer_);
}

void HeapDumpWriter::WriteSize(size_t size) {
  // Format size as hexadecimal string into |buffer_|.
  SStringPrintf(&buffer_, "%" PRIx64, static_cast<uint64_t>(size));
  traced_value_->SetString("size", buffer_);
}

}  // namespace trace_event
}  // namespace base