Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/libplatform/tracing/tracing-controller.cc

Issue 2137013006: [Tracing] V8 Tracing Controller (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Start/StopTracing + TestTracingController Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/libplatform/tracing/trace-writer.cc ('k') | src/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <stdio.h>
6 #include <string.h>
7
8 #include "include/libplatform/v8-tracing.h"
9
10 #include "src/base/platform/mutex.h"
11
12 namespace v8 {
13 namespace platform {
14 namespace tracing {
15
16 #define MAX_CATEGORY_GROUPS 200
17
18 // Parallel arrays g_category_groups and g_category_group_enabled are separate
19 // so that a pointer to a member of g_category_group_enabled can be easily
20 // converted to an index into g_category_groups. This allows macros to deal
21 // only with char enabled pointers from g_category_group_enabled, and we can
22 // convert internally to determine the category name from the char enabled
23 // pointer.
24 const char* g_category_groups[MAX_CATEGORY_GROUPS] = {
25 "toplevel", "tracing already shutdown",
26 "tracing categories exhausted; must increase MAX_CATEGORY_GROUPS",
27 "__metadata"};
28
29 // The enabled flag is char instead of bool so that the API can be used from C.
30 unsigned char g_category_group_enabled[MAX_CATEGORY_GROUPS] = {0};
31 // Indexes here have to match the g_category_groups array indexes above.
32 const int g_category_already_shutdown = 1;
33 const int g_category_categories_exhausted = 2;
34 // Metadata category not used in V8.
35 // const int g_category_metadata = 3;
36 const int g_num_builtin_categories = 4;
37
38 // Skip default categories.
39 v8::base::AtomicWord g_category_index = g_num_builtin_categories;
40
41 void TracingController::Initialize(TraceBuffer* trace_buffer) {
42 trace_buffer_.reset(trace_buffer);
43 }
44
45 uint64_t TracingController::AddTraceEvent(
46 char phase, const uint8_t* category_enabled_flag, const char* name,
47 const char* scope, uint64_t id, uint64_t bind_id, int num_args,
48 const char** arg_names, const uint8_t* arg_types,
49 const uint64_t* arg_values, unsigned int flags) {
50 uint64_t handle;
51 TraceObject* trace_object = trace_buffer_->AddTraceEvent(&handle);
52 if (trace_object) {
53 trace_object->Initialize(
54 phase, std::string(name),
55 std::string(GetCategoryGroupName(category_enabled_flag)), id, bind_id,
56 num_args, flags);
57 }
58 return handle;
59 }
60
61 void TracingController::UpdateTraceEventDuration(
62 const uint8_t* category_enabled_flag, const char* name, uint64_t handle) {
63 TraceObject* trace_object = trace_buffer_->GetEventByHandle(handle);
64 if (!trace_object) return;
65 trace_object->UpdateDuration();
66 }
67
68 const uint8_t* TracingController::GetCategoryGroupEnabled(
69 const char* category_group) {
70 if (!trace_buffer_) {
71 DCHECK(!g_category_group_enabled[g_category_already_shutdown]);
72 return &g_category_group_enabled[g_category_already_shutdown];
73 }
74 return GetCategoryGroupEnabledInternal(category_group);
75 }
76
77 const char* TracingController::GetCategoryGroupName(
78 const uint8_t* category_group_enabled) {
79 // Calculate the index of the category group by finding
80 // category_group_enabled in g_category_group_enabled array.
81 uintptr_t category_begin =
82 reinterpret_cast<uintptr_t>(g_category_group_enabled);
83 uintptr_t category_ptr = reinterpret_cast<uintptr_t>(category_group_enabled);
84 // Check for out of bounds category pointers.
85 DCHECK(category_ptr >= category_begin &&
86 category_ptr < reinterpret_cast<uintptr_t>(g_category_group_enabled +
87 MAX_CATEGORY_GROUPS));
88 uintptr_t category_index =
89 (category_ptr - category_begin) / sizeof(g_category_group_enabled[0]);
90 return g_category_groups[category_index];
91 }
92
93 void TracingController::StartTracing(TraceConfig* trace_config) {
94 trace_config_.reset(trace_config);
95 mode_ = RECORDING_MODE;
96 UpdateCategoryGroupEnabledFlags();
97 }
98
99 void TracingController::StopTracing() {
100 trace_buffer_->Flush();
101 mode_ = DISABLED;
102 UpdateCategoryGroupEnabledFlags();
mattloring 2016/07/15 00:19:01 Move this before Flush to disable more traces befo
fmeawad 2016/07/15 21:21:58 Done.
103 }
104
105 void TracingController::UpdateCategoryGroupEnabledFlag(size_t category_index) {
106 unsigned char enabled_flag = 0;
107 const char* category_group = g_category_groups[category_index];
108 if (mode_ == RECORDING_MODE &&
109 trace_config_->IsCategoryGroupEnabled(category_group)) {
110 enabled_flag |= ENABLED_FOR_RECORDING;
111 }
112
113 // TODO(fmeawad): EventCallback and ETW modes are not yet supported in V8.
114 // TODO(primiano): this is a temporary workaround for catapult:#2341,
115 // to guarantee that metadata events are always added even if the category
116 // filter is "-*". See crbug.com/618054 for more details and long-term fix.
117 if (mode_ == RECORDING_MODE && !strcmp(category_group, "__metadata")) {
118 enabled_flag |= ENABLED_FOR_RECORDING;
119 }
120
121 g_category_group_enabled[category_index] = enabled_flag;
122 }
123
124 void TracingController::UpdateCategoryGroupEnabledFlags() {
125 size_t category_index = base::NoBarrier_Load(&g_category_index);
126 for (size_t i = 0; i < category_index; i++) UpdateCategoryGroupEnabledFlag(i);
127 }
128
129 const uint8_t* TracingController::GetCategoryGroupEnabledInternal(
130 const char* category_group) {
131 // Check that category groups does not contain double quote
132 DCHECK(!strchr(category_group, '"'));
133
134 // The g_category_groups is append only, avoid using a lock for the fast path.
135 size_t current_category_index = v8::base::Acquire_Load(&g_category_index);
136
137 // Search for pre-existing category group.
138 for (size_t i = 0; i < current_category_index; ++i) {
139 if (strcmp(g_category_groups[i], category_group) == 0) {
140 return &g_category_group_enabled[i];
141 }
142 }
143
144 unsigned char* category_group_enabled = NULL;
145 size_t category_index = base::Acquire_Load(&g_category_index);
146 for (size_t i = 0; i < category_index; ++i) {
147 if (strcmp(g_category_groups[i], category_group) == 0) {
148 return &g_category_group_enabled[i];
149 }
150 }
151
152 // Create a new category group.
153 // Check that there is a slot for the new category_group.
154 DCHECK(category_index < MAX_CATEGORY_GROUPS);
155 if (category_index < MAX_CATEGORY_GROUPS) {
156 // Don't hold on to the category_group pointer, so that we can create
157 // category groups with strings not known at compile time (this is
158 // required by SetWatchEvent).
159 const char* new_group = strdup(category_group);
160 g_category_groups[category_index] = new_group;
161 DCHECK(!g_category_group_enabled[category_index]);
162 // Note that if both included and excluded patterns in the
163 // TraceConfig are empty, we exclude nothing,
164 // thereby enabling this category group.
165 UpdateCategoryGroupEnabledFlag(category_index);
166 category_group_enabled = &g_category_group_enabled[category_index];
167 // Update the max index now.
168 base::Release_Store(&g_category_index, category_index + 1);
169 } else {
170 category_group_enabled =
171 &g_category_group_enabled[g_category_categories_exhausted];
172 }
173 return category_group_enabled;
174 }
175
176 } // namespace tracing
177 } // namespace platform
178 } // namespace v8
OLDNEW
« no previous file with comments | « src/libplatform/tracing/trace-writer.cc ('k') | src/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698