compilation-statistics.cc 5.74 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include <ostream>  // NOLINT(readability/streams)
6 7 8
#include <vector>

#include "src/base/platform/platform.h"
9
#include "src/diagnostics/compilation-statistics.h"
10 11 12 13 14 15 16

namespace v8 {
namespace internal {

void CompilationStatistics::RecordPhaseStats(const char* phase_kind_name,
                                             const char* phase_name,
                                             const BasicStats& stats) {
17
  base::MutexGuard guard(&record_mutex_);
18

19 20 21 22 23 24 25 26 27 28 29
  std::string phase_name_str(phase_name);
  auto it = phase_map_.find(phase_name_str);
  if (it == phase_map_.end()) {
    PhaseStats phase_stats(phase_map_.size(), phase_kind_name);
    it = phase_map_.insert(std::make_pair(phase_name_str, phase_stats)).first;
  }
  it->second.Accumulate(stats);
}

void CompilationStatistics::RecordPhaseKindStats(const char* phase_kind_name,
                                                 const BasicStats& stats) {
30
  base::MutexGuard guard(&record_mutex_);
31

32 33 34 35
  std::string phase_kind_name_str(phase_kind_name);
  auto it = phase_kind_map_.find(phase_kind_name_str);
  if (it == phase_kind_map_.end()) {
    PhaseKindStats phase_kind_stats(phase_kind_map_.size());
36 37 38
    it = phase_kind_map_
             .insert(std::make_pair(phase_kind_name_str, phase_kind_stats))
             .first;
39 40 41 42 43 44
  }
  it->second.Accumulate(stats);
}

void CompilationStatistics::RecordTotalStats(size_t source_size,
                                             const BasicStats& stats) {
45
  base::MutexGuard guard(&record_mutex_);
46

47 48 49 50 51 52 53
  source_size += source_size;
  total_stats_.Accumulate(stats);
}

void CompilationStatistics::BasicStats::Accumulate(const BasicStats& stats) {
  delta_ += stats.delta_;
  total_allocated_bytes_ += stats.total_allocated_bytes_;
54 55
  if (stats.absolute_max_allocated_bytes_ > absolute_max_allocated_bytes_) {
    absolute_max_allocated_bytes_ = stats.absolute_max_allocated_bytes_;
56 57 58 59 60
    max_allocated_bytes_ = stats.max_allocated_bytes_;
    function_name_ = stats.function_name_;
  }
}

61
static void WriteLine(std::ostream& os, bool machine_format, const char* name,
62 63 64 65 66 67 68 69 70 71
                      const CompilationStatistics::BasicStats& stats,
                      const CompilationStatistics::BasicStats& total_stats) {
  const size_t kBufferSize = 128;
  char buffer[kBufferSize];

  double ms = stats.delta_.InMillisecondsF();
  double percent = stats.delta_.PercentOf(total_stats.delta_);
  double size_percent =
      static_cast<double>(stats.total_allocated_bytes_ * 100) /
      static_cast<double>(total_stats.total_allocated_bytes_);
72 73
  if (machine_format) {
    base::OS::SNPrintF(buffer, kBufferSize,
74
                       "\"%s_time\"=%.3f\n\"%s_space\"=%zu", name, ms, name,
75 76 77
                       stats.total_allocated_bytes_);
    os << buffer;
  } else {
78 79 80 81 82
    base::OS::SNPrintF(buffer, kBufferSize,
                       "%34s %10.3f (%5.1f%%)  %10zu (%5.1f%%) %10zu %10zu",
                       name, ms, percent, stats.total_allocated_bytes_,
                       size_percent, stats.max_allocated_bytes_,
                       stats.absolute_max_allocated_bytes_);
83 84

    os << buffer;
85
    if (!stats.function_name_.empty()) {
86 87 88
      os << "   " << stats.function_name_.c_str();
    }
    os << std::endl;
89 90 91 92
  }
}

static void WriteFullLine(std::ostream& os) {
93 94
  os << "-----------------------------------------------------------"
        "-----------------------------------------------------------\n";
95 96 97 98
}

static void WriteHeader(std::ostream& os) {
  WriteFullLine(os);
99 100 101 102
  os << "                Turbofan phase            Time (ms)    "
     << "                   Space (bytes)             Function\n"
     << "                                                       "
     << "          Total          Max.     Abs. max.\n";
103 104 105 106
  WriteFullLine(os);
}

static void WritePhaseKindBreak(std::ostream& os) {
107 108
  os << "                                   ------------------------"
        "-----------------------------------------------------------\n";
109 110
}

111
std::ostream& operator<<(std::ostream& os, const AsPrintableStatistics& ps) {
112 113
  // phase_kind_map_ and phase_map_ don't get mutated, so store a bunch of
  // pointers into them.
114
  const CompilationStatistics& s = ps.s;
115

116 117
  using SortedPhaseKinds =
      std::vector<CompilationStatistics::PhaseKindMap::const_iterator>;
118 119 120 121 122 123
  SortedPhaseKinds sorted_phase_kinds(s.phase_kind_map_.size());
  for (auto it = s.phase_kind_map_.begin(); it != s.phase_kind_map_.end();
       ++it) {
    sorted_phase_kinds[it->second.insert_order_] = it;
  }

124 125
  using SortedPhases =
      std::vector<CompilationStatistics::PhaseMap::const_iterator>;
126 127 128 129 130
  SortedPhases sorted_phases(s.phase_map_.size());
  for (auto it = s.phase_map_.begin(); it != s.phase_map_.end(); ++it) {
    sorted_phases[it->second.insert_order_] = it;
  }

131
  if (!ps.machine_output) WriteHeader(os);
132
  for (const auto& phase_kind_it : sorted_phase_kinds) {
133
    const auto& phase_kind_name = phase_kind_it->first;
134
    if (!ps.machine_output) {
135
      for (const auto& phase_it : sorted_phases) {
136 137 138 139 140 141 142
        const auto& phase_stats = phase_it->second;
        if (phase_stats.phase_kind_name_ != phase_kind_name) continue;
        const auto& phase_name = phase_it->first;
        WriteLine(os, ps.machine_output, phase_name.c_str(), phase_stats,
                  s.total_stats_);
      }
      WritePhaseKindBreak(os);
143 144
    }
    const auto& phase_kind_stats = phase_kind_it->second;
145 146
    WriteLine(os, ps.machine_output, phase_kind_name.c_str(), phase_kind_stats,
              s.total_stats_);
147 148
    os << std::endl;
  }
149 150 151

  if (!ps.machine_output) WriteFullLine(os);
  WriteLine(os, ps.machine_output, "totals", s.total_stats_, s.total_stats_);
152 153 154 155 156 157

  return os;
}

}  // namespace internal
}  // namespace v8