blob: c7e15b230b2fa7527c04d417b568483e068fcbe8 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 the V8 project 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 <ostream> // NOLINT(readability/streams)
6#include <vector>
7
8#include "src/base/platform/platform.h"
9#include "src/compilation-statistics.h"
10
11namespace v8 {
12namespace internal {
13
14void CompilationStatistics::RecordPhaseStats(const char* phase_kind_name,
15 const char* phase_name,
16 const BasicStats& stats) {
17 std::string phase_name_str(phase_name);
18 auto it = phase_map_.find(phase_name_str);
19 if (it == phase_map_.end()) {
20 PhaseStats phase_stats(phase_map_.size(), phase_kind_name);
21 it = phase_map_.insert(std::make_pair(phase_name_str, phase_stats)).first;
22 }
23 it->second.Accumulate(stats);
24}
25
26
27void CompilationStatistics::RecordPhaseKindStats(const char* phase_kind_name,
28 const BasicStats& stats) {
29 std::string phase_kind_name_str(phase_kind_name);
30 auto it = phase_kind_map_.find(phase_kind_name_str);
31 if (it == phase_kind_map_.end()) {
32 PhaseKindStats phase_kind_stats(phase_kind_map_.size());
33 it = phase_kind_map_.insert(std::make_pair(phase_kind_name_str,
34 phase_kind_stats)).first;
35 }
36 it->second.Accumulate(stats);
37}
38
39
40void CompilationStatistics::RecordTotalStats(size_t source_size,
41 const BasicStats& stats) {
42 source_size += source_size;
43 total_stats_.Accumulate(stats);
44}
45
46
47void CompilationStatistics::BasicStats::Accumulate(const BasicStats& stats) {
48 delta_ += stats.delta_;
49 total_allocated_bytes_ += stats.total_allocated_bytes_;
50 if (stats.absolute_max_allocated_bytes_ > absolute_max_allocated_bytes_) {
51 absolute_max_allocated_bytes_ = stats.absolute_max_allocated_bytes_;
52 max_allocated_bytes_ = stats.max_allocated_bytes_;
53 function_name_ = stats.function_name_;
54 }
55}
56
57
58static void WriteLine(std::ostream& os, const char* name,
59 const CompilationStatistics::BasicStats& stats,
60 const CompilationStatistics::BasicStats& total_stats) {
61 const size_t kBufferSize = 128;
62 char buffer[kBufferSize];
63
64 double ms = stats.delta_.InMillisecondsF();
65 double percent = stats.delta_.PercentOf(total_stats.delta_);
66 double size_percent =
67 static_cast<double>(stats.total_allocated_bytes_ * 100) /
68 static_cast<double>(total_stats.total_allocated_bytes_);
Ben Murdochc5610432016-08-08 18:44:38 +010069 base::OS::SNPrintF(buffer, kBufferSize, "%28s %10.3f (%5.1f%%) %10" PRIuS
70 " (%5.1f%%) %10" PRIuS " %10" PRIuS,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071 name, ms, percent, stats.total_allocated_bytes_,
72 size_percent, stats.max_allocated_bytes_,
73 stats.absolute_max_allocated_bytes_);
74
75 os << buffer;
76 if (stats.function_name_.size() > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 os << " " << stats.function_name_.c_str();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040078 }
79 os << std::endl;
80}
81
82
83static void WriteFullLine(std::ostream& os) {
84 os << "--------------------------------------------------------"
85 "--------------------------------------------------------\n";
86}
87
88
89static void WriteHeader(std::ostream& os) {
90 WriteFullLine(os);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 os << " Turbonfan phase Time (ms) "
92 << " Space (bytes) Function\n"
93 << " "
94 << " Total Max. Abs. max.\n";
Emily Bernierd0a1eb72015-03-24 16:35:39 -040095 WriteFullLine(os);
96}
97
98
99static void WritePhaseKindBreak(std::ostream& os) {
100 os << " ---------------------------"
101 "--------------------------------------------------------\n";
102}
103
104
105std::ostream& operator<<(std::ostream& os, const CompilationStatistics& s) {
106 // phase_kind_map_ and phase_map_ don't get mutated, so store a bunch of
107 // pointers into them.
108
109 typedef std::vector<CompilationStatistics::PhaseKindMap::const_iterator>
110 SortedPhaseKinds;
111 SortedPhaseKinds sorted_phase_kinds(s.phase_kind_map_.size());
112 for (auto it = s.phase_kind_map_.begin(); it != s.phase_kind_map_.end();
113 ++it) {
114 sorted_phase_kinds[it->second.insert_order_] = it;
115 }
116
117 typedef std::vector<CompilationStatistics::PhaseMap::const_iterator>
118 SortedPhases;
119 SortedPhases sorted_phases(s.phase_map_.size());
120 for (auto it = s.phase_map_.begin(); it != s.phase_map_.end(); ++it) {
121 sorted_phases[it->second.insert_order_] = it;
122 }
123
124 WriteHeader(os);
125 for (auto phase_kind_it : sorted_phase_kinds) {
126 const auto& phase_kind_name = phase_kind_it->first;
127 for (auto phase_it : sorted_phases) {
128 const auto& phase_stats = phase_it->second;
129 if (phase_stats.phase_kind_name_ != phase_kind_name) continue;
130 const auto& phase_name = phase_it->first;
131 WriteLine(os, phase_name.c_str(), phase_stats, s.total_stats_);
132 }
133 WritePhaseKindBreak(os);
134 const auto& phase_kind_stats = phase_kind_it->second;
135 WriteLine(os, phase_kind_name.c_str(), phase_kind_stats, s.total_stats_);
136 os << std::endl;
137 }
138 WriteFullLine(os);
139 WriteLine(os, "totals", s.total_stats_, s.total_stats_);
140
141 return os;
142}
143
144} // namespace internal
145} // namespace v8