v8  8.6.395 (node 15.0.1)
V8 is Google's open source JavaScript engine
v8-metrics.h
Go to the documentation of this file.
1 // Copyright 2020 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_METRICS_H_
6 #define V8_METRICS_H_
7 
8 #include "v8.h" // NOLINT(build/include_directory)
9 
10 namespace v8 {
11 namespace metrics {
12 
14  bool async = false;
15  bool streamed = false;
16  bool success = false;
18  size_t function_count = 0;
19  int64_t wall_clock_time_in_us = 0;
20 };
21 
23  bool async = false;
24  bool streamed = false;
25  bool cached = false;
26  bool deserialized = false;
27  bool lazy = false;
28  bool success = false;
29  size_t code_size_in_bytes = 0;
31  int64_t wall_clock_time_in_us = 0;
32 };
33 
35  bool async = false;
36  bool success = false;
38  int64_t wall_clock_time_in_us = 0;
39 };
40 
42  bool lazy = false;
43  size_t code_size_in_bytes = 0;
44  int64_t wall_clock_time_in_us = 0;
45 };
46 
48  size_t count = 0;
49 };
50 
51 #define V8_MAIN_THREAD_METRICS_EVENTS(V)
52  V(WasmModuleDecoded)
53  V(WasmModuleCompiled)
54  V(WasmModuleInstantiated)
55  V(WasmModuleTieredUp)
56 
57 #define V8_THREAD_SAFE_METRICS_EVENTS(V) V(WasmModulesPerIsolate)
58 
59 /**
60  * This class serves as a base class for recording event-based metrics in V8.
61  * There a two kinds of metrics, those which are expected to be thread-safe and
62  * whose implementation is required to fulfill this requirement and those whose
63  * implementation does not have that requirement and only needs to be
64  * executable on the main thread. If such an event is triggered from a
65  * background thread, it will be delayed and executed by the foreground task
66  * runner.
67  *
68  * The thread-safe events are listed in the V8_THREAD_SAFE_METRICS_EVENTS
69  * macro above while the main thread event are listed in
70  * V8_MAIN_THREAD_METRICS_EVENTS above. For the former, a virtual method
71  * AddMainThreadEvent(const E& event, v8::Context::Token token) will be
72  * generated and for the latter AddThreadSafeEvent(const E& event).
73  *
74  * Thread-safe events are not allowed to access the context and therefore do
75  * not carry a context ID with them. These IDs can be generated using
76  * Recorder::GetContextId() and the ID will be valid throughout the lifetime
77  * of the isolate. It is not guaranteed that the ID will still resolve to
78  * a valid context using Recorder::GetContext() at the time the metric is
79  * recorded. In this case, an empty handle will be returned.
80  *
81  * The embedder is expected to call v8::Isolate::SetMetricsRecorder()
82  * providing its implementation and have the virtual methods overwritten
83  * for the events it cares about.
84  */
86  public:
87  // A unique identifier for a context in this Isolate.
88  // It is guaranteed to not be reused throughout the lifetime of the Isolate.
89  class ContextId {
90  public:
91  ContextId() : id_(kEmptyId) {}
92 
93  bool IsEmpty() const { return id_ == kEmptyId; }
94  static const ContextId Empty() { return ContextId{kEmptyId}; }
95 
96  bool operator==(const ContextId& other) const { return id_ == other.id_; }
97  bool operator!=(const ContextId& other) const { return id_ != other.id_; }
98 
99  private:
100  friend class ::v8::Context;
101  friend class ::v8::internal::Isolate;
102 
103  explicit ContextId(uintptr_t id) : id_(id) {}
104 
105  static constexpr uintptr_t kEmptyId = 0;
106  uintptr_t id_;
107  };
108 
109  virtual ~Recorder() = default;
110 
111 #define ADD_MAIN_THREAD_EVENT(E)
112  virtual void AddMainThreadEvent(const E& event, ContextId context_id) {}
114 #undef ADD_MAIN_THREAD_EVENT
115 
116 #define ADD_THREAD_SAFE_EVENT(E)
117  virtual void AddThreadSafeEvent(const E& event) {}
119 #undef ADD_THREAD_SAFE_EVENT
120 
121  virtual void NotifyIsolateDisposal() {}
122 
123  // Return the context with the given id or an empty handle if the context
124  // was already garbage collected.
126  // Return the unique id corresponding to the given context.
128 };
129 
130 } // namespace metrics
131 } // namespace v8
132 
133 #endif // V8_METRICS_H_