1 // Copyright 2015 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 #include "src/tracing/trace-event.h"
6 
7 #include <string.h>
8 
9 #include "src/counters.h"
10 #include "src/isolate.h"
11 #include "src/tracing/traced-value.h"
12 #include "src/v8.h"
13 
14 namespace v8 {
15 namespace internal {
16 namespace tracing {
17 
18 v8::Platform* TraceEventHelper::GetCurrentPlatform() {
19   return v8::internal::V8::GetCurrentPlatform();
20 }
21 
22 void CallStatsScopedTracer::AddEndTraceEvent() {
23   if (!has_parent_scope_ && p_data_->isolate) {
24     auto value = v8::tracing::TracedValue::Create();
25     p_data_->isolate->counters()->runtime_call_stats()->Dump(value.get());
26     v8::internal::tracing::AddTraceEvent(
27         TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name,
28         v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId,
29         v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE,
30         "runtime-call-stats", std::move(value));
31   } else {
32     v8::internal::tracing::AddTraceEvent(
33         TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name,
34         v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId,
35         v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE);
36   }
37 }
38 
39 void CallStatsScopedTracer::Initialize(v8::internal::Isolate* isolate,
40                                        const uint8_t* category_group_enabled,
41                                        const char* name) {
42   data_.isolate = isolate;
43   data_.category_group_enabled = category_group_enabled;
44   data_.name = name;
45   p_data_ = &data_;
46   RuntimeCallStats* table = isolate->counters()->runtime_call_stats();
47   has_parent_scope_ = table->InUse();
48   if (!has_parent_scope_) table->Reset();
49   v8::internal::tracing::AddTraceEvent(
50       TRACE_EVENT_PHASE_BEGIN, category_group_enabled, name,
51       v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId,
52       TRACE_EVENT_FLAG_NONE, v8::internal::tracing::kNoId);
53 }
54 
55 }  // namespace tracing
56 }  // namespace internal
57 }  // namespace v8
58