compiler-dispatcher-tracer.h 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2016 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.

#ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_
#define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_

#include <utility>

#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/base/ring-buffer.h"
#include "src/counters.h"
14
#include "src/globals.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

namespace v8 {
namespace internal {

class Isolate;
class RuntimeCallStats;

#define COMPILER_DISPATCHER_TRACE_SCOPE_WITH_NUM(tracer, scope_id, num)      \
  CompilerDispatcherTracer::ScopeID tracer_scope_id(                         \
      CompilerDispatcherTracer::ScopeID::scope_id);                          \
  CompilerDispatcherTracer::Scope trace_scope(tracer, tracer_scope_id, num); \
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),                      \
               CompilerDispatcherTracer::Scope::Name(tracer_scope_id))

#define COMPILER_DISPATCHER_TRACE_SCOPE(tracer, scope_id) \
  COMPILER_DISPATCHER_TRACE_SCOPE_WITH_NUM(tracer, scope_id, 0)

32
class V8_EXPORT_PRIVATE CompilerDispatcherTracer {
33 34 35 36 37
 public:
  enum class ScopeID {
    kPrepareToParse,
    kParse,
    kFinalizeParsing,
38
    kAnalyze,
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
    kPrepareToCompile,
    kCompile,
    kFinalizeCompiling
  };

  class Scope {
   public:
    Scope(CompilerDispatcherTracer* tracer, ScopeID scope_id, size_t num = 0);
    ~Scope();

    static const char* Name(ScopeID scoped_id);

   private:
    CompilerDispatcherTracer* tracer_;
    ScopeID scope_id_;
    size_t num_;
    double start_time_;

    DISALLOW_COPY_AND_ASSIGN(Scope);
  };

  explicit CompilerDispatcherTracer(Isolate* isolate);
  ~CompilerDispatcherTracer();

  void RecordPrepareToParse(double duration_ms);
  void RecordParse(double duration_ms, size_t source_length);
  void RecordFinalizeParsing(double duration_ms);
66
  void RecordAnalyze(double duration_ms);
67
  void RecordPrepareToCompile(double duration_ms);
68
  void RecordCompile(double duration_ms);
69 70 71 72
  void RecordFinalizeCompiling(double duration_ms);

  double EstimatePrepareToParseInMs() const;
  double EstimateParseInMs(size_t source_length) const;
73
  double EstimateFinalizeParsingInMs() const;
74
  double EstimateAnalyzeInMs() const;
75
  double EstimatePrepareToCompileInMs() const;
76
  double EstimateCompileInMs() const;
77
  double EstimateFinalizeCompilingInMs() const;
78

79 80
  void DumpStatistics() const;

81 82 83 84 85 86 87 88 89
 private:
  static double Average(const base::RingBuffer<double>& buffer);
  static double Estimate(
      const base::RingBuffer<std::pair<size_t, double>>& buffer, size_t num);

  mutable base::Mutex mutex_;
  base::RingBuffer<double> prepare_parse_events_;
  base::RingBuffer<std::pair<size_t, double>> parse_events_;
  base::RingBuffer<double> finalize_parsing_events_;
90
  base::RingBuffer<double> analyze_events_;
91
  base::RingBuffer<double> prepare_compile_events_;
92
  base::RingBuffer<double> compile_events_;
93 94 95 96 97 98 99 100 101 102 103
  base::RingBuffer<double> finalize_compiling_events_;

  RuntimeCallStats* runtime_call_stats_;

  DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTracer);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_