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 #include "src/tracing/tracing-category-observer.h"
6 
7 #include "src/base/atomic-utils.h"
8 #include "src/flags.h"
9 #include "src/tracing/trace-event.h"
10 #include "src/v8.h"
11 
12 namespace v8 {
13 namespace tracing {
14 
15 TracingCategoryObserver* TracingCategoryObserver::instance_ = nullptr;
16 
SetUp()17 void TracingCategoryObserver::SetUp() {
18   TracingCategoryObserver::instance_ = new TracingCategoryObserver();
19   v8::internal::V8::GetCurrentPlatform()
20       ->GetTracingController()
21       ->AddTraceStateObserver(TracingCategoryObserver::instance_);
22   TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats"));
23   TRACE_EVENT_WARMUP_CATEGORY(
24       TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats_sampling"));
25   TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("v8.gc_stats"));
26   TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("v8.ic_stats"));
27 }
28 
TearDown()29 void TracingCategoryObserver::TearDown() {
30   v8::internal::V8::GetCurrentPlatform()
31       ->GetTracingController()
32       ->RemoveTraceStateObserver(TracingCategoryObserver::instance_);
33   delete TracingCategoryObserver::instance_;
34 }
35 
OnTraceEnabled()36 void TracingCategoryObserver::OnTraceEnabled() {
37   bool enabled = false;
38   TRACE_EVENT_CATEGORY_GROUP_ENABLED(
39       TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats"), &enabled);
40   if (enabled) {
41     base::AsAtomic32::Relaxed_Store(
42         &v8::internal::FLAG_runtime_stats,
43         (v8::internal::FLAG_runtime_stats | ENABLED_BY_TRACING));
44   }
45   TRACE_EVENT_CATEGORY_GROUP_ENABLED(
46       TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats_sampling"), &enabled);
47   if (enabled) {
48     base::AsAtomic32::Relaxed_Store(
49         &v8::internal::FLAG_runtime_stats,
50         v8::internal::FLAG_runtime_stats | ENABLED_BY_SAMPLING);
51   }
52   TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("v8.gc_stats"),
53                                      &enabled);
54   if (enabled) {
55     v8::internal::FLAG_gc_stats |= ENABLED_BY_TRACING;
56   }
57   TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("v8.ic_stats"),
58                                      &enabled);
59   if (enabled) {
60     v8::internal::FLAG_ic_stats |= ENABLED_BY_TRACING;
61   }
62 }
63 
OnTraceDisabled()64 void TracingCategoryObserver::OnTraceDisabled() {
65   base::AsAtomic32::Relaxed_Store(
66       &v8::internal::FLAG_runtime_stats,
67       v8::internal::FLAG_runtime_stats &
68           ~(ENABLED_BY_TRACING | ENABLED_BY_SAMPLING));
69   v8::internal::FLAG_gc_stats &= ~ENABLED_BY_TRACING;
70   v8::internal::FLAG_ic_stats &= ~ENABLED_BY_TRACING;
71 }
72 
73 }  // namespace tracing
74 }  // namespace v8
75