Home | History | Annotate | Download | only in compiler-dispatcher
      1 // Copyright 2016 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_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_
      6 #define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_
      7 
      8 #include <utility>
      9 
     10 #include "src/base/macros.h"
     11 #include "src/base/platform/mutex.h"
     12 #include "src/base/ring-buffer.h"
     13 #include "src/counters.h"
     14 #include "src/globals.h"
     15 
     16 namespace v8 {
     17 namespace internal {
     18 
     19 class Isolate;
     20 class RuntimeCallStats;
     21 
     22 #define COMPILER_DISPATCHER_TRACE_SCOPE_WITH_NUM(tracer, scope_id, num)      \
     23   CompilerDispatcherTracer::ScopeID tracer_scope_id(                         \
     24       CompilerDispatcherTracer::ScopeID::scope_id);                          \
     25   CompilerDispatcherTracer::Scope trace_scope(tracer, tracer_scope_id, num); \
     26   TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),                      \
     27                CompilerDispatcherTracer::Scope::Name(tracer_scope_id))
     28 
     29 #define COMPILER_DISPATCHER_TRACE_SCOPE(tracer, scope_id) \
     30   COMPILER_DISPATCHER_TRACE_SCOPE_WITH_NUM(tracer, scope_id, 0)
     31 
     32 class V8_EXPORT_PRIVATE CompilerDispatcherTracer {
     33  public:
     34   enum class ScopeID {
     35     kPrepareToParse,
     36     kParse,
     37     kFinalizeParsing,
     38     kAnalyze,
     39     kPrepareToCompile,
     40     kCompile,
     41     kFinalizeCompiling
     42   };
     43 
     44   class Scope {
     45    public:
     46     Scope(CompilerDispatcherTracer* tracer, ScopeID scope_id, size_t num = 0);
     47     ~Scope();
     48 
     49     static const char* Name(ScopeID scoped_id);
     50 
     51    private:
     52     CompilerDispatcherTracer* tracer_;
     53     ScopeID scope_id_;
     54     size_t num_;
     55     double start_time_;
     56 
     57     DISALLOW_COPY_AND_ASSIGN(Scope);
     58   };
     59 
     60   explicit CompilerDispatcherTracer(Isolate* isolate);
     61   ~CompilerDispatcherTracer();
     62 
     63   void RecordPrepareToParse(double duration_ms);
     64   void RecordParse(double duration_ms, size_t source_length);
     65   void RecordFinalizeParsing(double duration_ms);
     66   void RecordAnalyze(double duration_ms);
     67   void RecordPrepareToCompile(double duration_ms);
     68   void RecordCompile(double duration_ms, size_t ast_size_in_bytes);
     69   void RecordFinalizeCompiling(double duration_ms);
     70 
     71   double EstimatePrepareToParseInMs() const;
     72   double EstimateParseInMs(size_t source_length) const;
     73   double EstimateFinalizeParsingInMs() const;
     74   double EstimateAnalyzeInMs() const;
     75   double EstimatePrepareToCompileInMs() const;
     76   double EstimateCompileInMs(size_t ast_size_in_bytes) const;
     77   double EstimateFinalizeCompilingInMs() const;
     78 
     79   void DumpStatistics() const;
     80 
     81  private:
     82   static double Average(const base::RingBuffer<double>& buffer);
     83   static double Estimate(
     84       const base::RingBuffer<std::pair<size_t, double>>& buffer, size_t num);
     85 
     86   mutable base::Mutex mutex_;
     87   base::RingBuffer<double> prepare_parse_events_;
     88   base::RingBuffer<std::pair<size_t, double>> parse_events_;
     89   base::RingBuffer<double> finalize_parsing_events_;
     90   base::RingBuffer<double> analyze_events_;
     91   base::RingBuffer<double> prepare_compile_events_;
     92   base::RingBuffer<std::pair<size_t, double>> compile_events_;
     93   base::RingBuffer<double> finalize_compiling_events_;
     94 
     95   RuntimeCallStats* runtime_call_stats_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherTracer);
     98 };
     99 
    100 }  // namespace internal
    101 }  // namespace v8
    102 
    103 #endif  // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_TRACER_H_
    104