1// 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#ifndef V8_COMPILER_PIPELINE_STATISTICS_H_
6#define V8_COMPILER_PIPELINE_STATISTICS_H_
7
8#include <memory>
9#include <string>
10
11#include "src/base/platform/elapsed-timer.h"
12#include "src/compiler/zone-stats.h"
13#include "src/diagnostics/compilation-statistics.h"
14#include "src/objects/code-kind.h"
15#include "src/tracing/trace-event.h"
16
17namespace v8 {
18namespace internal {
19namespace compiler {
20
21class PhaseScope;
22
23class PipelineStatistics : public Malloced {
24 public:
25  PipelineStatistics(OptimizedCompilationInfo* info,
26                     CompilationStatistics* turbo_stats, ZoneStats* zone_stats);
27  ~PipelineStatistics();
28  PipelineStatistics(const PipelineStatistics&) = delete;
29  PipelineStatistics& operator=(const PipelineStatistics&) = delete;
30
31  void BeginPhaseKind(const char* phase_kind_name);
32  void EndPhaseKind();
33
34  // We log detailed phase information about the pipeline
35  // in both the v8.turbofan and the v8.wasm.turbofan categories.
36  static constexpr char kTraceCategory[] =
37      TRACE_DISABLED_BY_DEFAULT("v8.turbofan") ","  // --
38      TRACE_DISABLED_BY_DEFAULT("v8.wasm.turbofan");
39
40 private:
41  size_t OuterZoneSize() {
42    return static_cast<size_t>(outer_zone_->allocation_size());
43  }
44
45  class CommonStats {
46   public:
47    CommonStats() : outer_zone_initial_size_(0) {}
48    CommonStats(const CommonStats&) = delete;
49    CommonStats& operator=(const CommonStats&) = delete;
50
51    void Begin(PipelineStatistics* pipeline_stats);
52    void End(PipelineStatistics* pipeline_stats,
53             CompilationStatistics::BasicStats* diff);
54
55    std::unique_ptr<ZoneStats::StatsScope> scope_;
56    base::ElapsedTimer timer_;
57    size_t outer_zone_initial_size_;
58    size_t allocated_bytes_at_start_;
59  };
60
61  bool InPhaseKind() { return !!phase_kind_stats_.scope_; }
62
63  friend class PhaseScope;
64  bool InPhase() { return !!phase_stats_.scope_; }
65  void BeginPhase(const char* name);
66  void EndPhase();
67
68  Zone* outer_zone_;
69  ZoneStats* zone_stats_;
70  CompilationStatistics* compilation_stats_;
71  CodeKind code_kind_;
72  std::string function_name_;
73
74  // Stats for the entire compilation.
75  CommonStats total_stats_;
76
77  // Stats for phase kind.
78  const char* phase_kind_name_;
79  CommonStats phase_kind_stats_;
80
81  // Stats for phase.
82  const char* phase_name_;
83  CommonStats phase_stats_;
84};
85
86class V8_NODISCARD PhaseScope {
87 public:
88  PhaseScope(PipelineStatistics* pipeline_stats, const char* name)
89      : pipeline_stats_(pipeline_stats) {
90    if (pipeline_stats_ != nullptr) pipeline_stats_->BeginPhase(name);
91  }
92  ~PhaseScope() {
93    if (pipeline_stats_ != nullptr) pipeline_stats_->EndPhase();
94  }
95  PhaseScope(const PhaseScope&) = delete;
96  PhaseScope& operator=(const PhaseScope&) = delete;
97
98 private:
99  PipelineStatistics* const pipeline_stats_;
100};
101
102}  // namespace compiler
103}  // namespace internal
104}  // namespace v8
105
106#endif  // V8_COMPILER_PIPELINE_STATISTICS_H_
107