compilation-statistics.cc revision 13e2dadd00298019ed862f2b2fc5068bba730bcf
1958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier// Copyright 2014 the V8 project authors. All rights reserved.
2958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier// Use of this source code is governed by a BSD-style license that can be
3958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier// found in the LICENSE file.
4958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
5958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier#include <ostream>  // NOLINT(readability/streams)
6958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier#include <vector>
7958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
8958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier#include "src/base/platform/platform.h"
9958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier#include "src/compilation-statistics.h"
10958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
11958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniernamespace v8 {
12958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniernamespace internal {
13958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
14958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniervoid CompilationStatistics::RecordPhaseStats(const char* phase_kind_name,
15958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                                             const char* phase_name,
16958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                                             const BasicStats& stats) {
17958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  std::string phase_name_str(phase_name);
18958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  auto it = phase_map_.find(phase_name_str);
19958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  if (it == phase_map_.end()) {
20958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    PhaseStats phase_stats(phase_map_.size(), phase_kind_name);
21958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    it = phase_map_.insert(std::make_pair(phase_name_str, phase_stats)).first;
22958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
23958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  it->second.Accumulate(stats);
24958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
25958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
26958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
27958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniervoid CompilationStatistics::RecordPhaseKindStats(const char* phase_kind_name,
28958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                                                 const BasicStats& stats) {
29958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  std::string phase_kind_name_str(phase_kind_name);
30958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  auto it = phase_kind_map_.find(phase_kind_name_str);
31958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  if (it == phase_kind_map_.end()) {
32958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    PhaseKindStats phase_kind_stats(phase_kind_map_.size());
33958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    it = phase_kind_map_.insert(std::make_pair(phase_kind_name_str,
34958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                                               phase_kind_stats)).first;
35958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
36958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  it->second.Accumulate(stats);
37958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
38958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
39958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
40958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniervoid CompilationStatistics::RecordTotalStats(size_t source_size,
41958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                                             const BasicStats& stats) {
42958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  source_size += source_size;
43958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  total_stats_.Accumulate(stats);
44958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
45958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
46958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
47958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Berniervoid CompilationStatistics::BasicStats::Accumulate(const BasicStats& stats) {
48958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  delta_ += stats.delta_;
49958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  total_allocated_bytes_ += stats.total_allocated_bytes_;
50958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  if (stats.absolute_max_allocated_bytes_ > absolute_max_allocated_bytes_) {
51958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    absolute_max_allocated_bytes_ = stats.absolute_max_allocated_bytes_;
52958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    max_allocated_bytes_ = stats.max_allocated_bytes_;
53958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    function_name_ = stats.function_name_;
54958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
55958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
56958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
5713e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdochstatic void WriteLine(std::ostream& os, bool machine_format, const char* name,
58958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                      const CompilationStatistics::BasicStats& stats,
59958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier                      const CompilationStatistics::BasicStats& total_stats) {
60958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  const size_t kBufferSize = 128;
61958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  char buffer[kBufferSize];
62958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
63958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  double ms = stats.delta_.InMillisecondsF();
64958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  double percent = stats.delta_.PercentOf(total_stats.delta_);
65958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  double size_percent =
66958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier      static_cast<double>(stats.total_allocated_bytes_ * 100) /
67958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier      static_cast<double>(total_stats.total_allocated_bytes_);
6813e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  if (machine_format) {
6913e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    base::OS::SNPrintF(buffer, kBufferSize,
7013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                       "\"%s_time\"=%.3f\n\"%s_space\"=%" PRIuS, name, ms, name,
7113e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                       stats.total_allocated_bytes_);
7213e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    os << buffer;
7313e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  } else {
7413e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    base::OS::SNPrintF(buffer, kBufferSize, "%28s %10.3f (%5.1f%%)  %10" PRIuS
7513e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                                            " (%5.1f%%) %10" PRIuS " %10" PRIuS,
7613e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                       name, ms, percent, stats.total_allocated_bytes_,
7713e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                       size_percent, stats.max_allocated_bytes_,
7813e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                       stats.absolute_max_allocated_bytes_);
7913e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch
8013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    os << buffer;
8113e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    if (stats.function_name_.size() > 0) {
8213e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch      os << "   " << stats.function_name_.c_str();
8313e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    }
8413e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    os << std::endl;
85958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
86958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
87958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
88958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
89958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierstatic void WriteFullLine(std::ostream& os) {
90958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  os << "--------------------------------------------------------"
91958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier        "--------------------------------------------------------\n";
92958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
93958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
94958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
95958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierstatic void WriteHeader(std::ostream& os) {
96958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  WriteFullLine(os);
97014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  os << "             Turbonfan phase        Time (ms)             "
98014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch     << "          Space (bytes)             Function\n"
99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch     << "                                                         "
100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch     << "  Total          Max.     Abs. max.\n";
101958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  WriteFullLine(os);
102958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
103958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
104958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
105958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernierstatic void WritePhaseKindBreak(std::ostream& os) {
106958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  os << "                             ---------------------------"
107958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier        "--------------------------------------------------------\n";
108958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
109958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
11013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdochstd::ostream& operator<<(std::ostream& os, const AsPrintableStatistics& ps) {
111958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  // phase_kind_map_ and phase_map_ don't get mutated, so store a bunch of
112958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  // pointers into them.
11313e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  const CompilationStatistics& s = ps.s;
114958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
115958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  typedef std::vector<CompilationStatistics::PhaseKindMap::const_iterator>
116958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier      SortedPhaseKinds;
117958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  SortedPhaseKinds sorted_phase_kinds(s.phase_kind_map_.size());
118958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  for (auto it = s.phase_kind_map_.begin(); it != s.phase_kind_map_.end();
119958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier       ++it) {
120958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    sorted_phase_kinds[it->second.insert_order_] = it;
121958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
122958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
123958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  typedef std::vector<CompilationStatistics::PhaseMap::const_iterator>
124958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier      SortedPhases;
125958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  SortedPhases sorted_phases(s.phase_map_.size());
126958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  for (auto it = s.phase_map_.begin(); it != s.phase_map_.end(); ++it) {
127958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    sorted_phases[it->second.insert_order_] = it;
128958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
129958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
13013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  if (!ps.machine_output) WriteHeader(os);
131958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  for (auto phase_kind_it : sorted_phase_kinds) {
132958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    const auto& phase_kind_name = phase_kind_it->first;
13313e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    if (!ps.machine_output) {
13413e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch      for (auto phase_it : sorted_phases) {
13513e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch        const auto& phase_stats = phase_it->second;
13613e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch        if (phase_stats.phase_kind_name_ != phase_kind_name) continue;
13713e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch        const auto& phase_name = phase_it->first;
13813e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch        WriteLine(os, ps.machine_output, phase_name.c_str(), phase_stats,
13913e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch                  s.total_stats_);
14013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch      }
14113e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch      WritePhaseKindBreak(os);
142958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    }
143958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    const auto& phase_kind_stats = phase_kind_it->second;
14413e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch    WriteLine(os, ps.machine_output, phase_kind_name.c_str(), phase_kind_stats,
14513e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch              s.total_stats_);
146958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier    os << std::endl;
147958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  }
14813e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch
14913e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  if (!ps.machine_output) WriteFullLine(os);
15013e2dadd00298019ed862f2b2fc5068bba730bcfBen Murdoch  WriteLine(os, ps.machine_output, "totals", s.total_stats_, s.total_stats_);
151958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
152958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier  return os;
153958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}
154958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier
155958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}  // namespace internal
156958fae7ec3f466955f8e5b50fa5b8d38b9e91675Emily Bernier}  // namespace v8
157