OLD | NEW |
(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 TraceConfig* trace_config) { |
| 43 trace_buffer_.reset(trace_buffer); |
| 44 trace_config_.reset(trace_config); |
| 45 } |
| 46 |
| 47 uint64_t TracingController::AddTraceEvent( |
| 48 char phase, const uint8_t* category_enabled_flag, const char* name, |
| 49 const char* scope, uint64_t id, uint64_t bind_id, int num_args, |
| 50 const char** arg_names, const uint8_t* arg_types, |
| 51 const uint64_t* arg_values, unsigned int flags) { |
| 52 uint64_t handle; |
| 53 TraceObject* trace_object = trace_buffer_->AddTraceEvent(&handle); |
| 54 if (trace_object) { |
| 55 trace_object->Initialize( |
| 56 phase, std::string(name), |
| 57 std::string(GetCategoryGroupName(category_enabled_flag)), id, bind_id, |
| 58 num_args, flags); |
| 59 } |
| 60 return handle; |
| 61 } |
| 62 |
| 63 void TracingController::UpdateTraceEventDuration( |
| 64 const uint8_t* category_enabled_flag, const char* name, uint64_t handle) { |
| 65 TraceObject* trace_object = trace_buffer_->GetEventByHandle(handle); |
| 66 if (!trace_object) return; |
| 67 trace_object->UpdateDuration(); |
| 68 } |
| 69 |
| 70 const uint8_t* TracingController::GetCategoryGroupEnabled( |
| 71 const char* category_group) { |
| 72 if (!is_recording) { |
| 73 DCHECK(!g_category_group_enabled[g_category_already_shutdown]); |
| 74 return &g_category_group_enabled[g_category_already_shutdown]; |
| 75 } |
| 76 return GetCategoryGroupEnabledInternal(category_group); |
| 77 } |
| 78 |
| 79 const char* TracingController::GetCategoryGroupName( |
| 80 const uint8_t* category_group_enabled) { |
| 81 // Calculate the index of the category group by finding |
| 82 // category_group_enabled in g_category_group_enabled array. |
| 83 uintptr_t category_begin = |
| 84 reinterpret_cast<uintptr_t>(g_category_group_enabled); |
| 85 uintptr_t category_ptr = reinterpret_cast<uintptr_t>(category_group_enabled); |
| 86 // Check for out of bounds category pointers. |
| 87 DCHECK(category_ptr >= category_begin && |
| 88 category_ptr < reinterpret_cast<uintptr_t>(g_category_group_enabled + |
| 89 MAX_CATEGORY_GROUPS)); |
| 90 uintptr_t category_index = |
| 91 (category_ptr - category_begin) / sizeof(g_category_group_enabled[0]); |
| 92 return g_category_groups[category_index]; |
| 93 } |
| 94 |
| 95 void TracingController::Start() { is_recording = true; } |
| 96 void TracingController::Stop() { is_recording = false; } |
| 97 |
| 98 void TracingController::UpdateCategoryGroupEnabledFlag(size_t category_index) { |
| 99 unsigned char enabled_flag = 0; |
| 100 const char* category_group = g_category_groups[category_index]; |
| 101 if (mode_ == RECORDING_MODE && |
| 102 trace_config_->IsCategoryGroupEnabled(category_group)) { |
| 103 enabled_flag |= ENABLED_FOR_RECORDING; |
| 104 } |
| 105 |
| 106 // TODO(fmeawad): EventCallback and ETW modes are not yet supported in V8. |
| 107 // TODO(primiano): this is a temporary workaround for catapult:#2341, |
| 108 // to guarantee that metadata events are always added even if the category |
| 109 // filter is "-*". See crbug.com/618054 for more details and long-term fix. |
| 110 if (mode_ == RECORDING_MODE && !strcmp(category_group, "__metadata")) { |
| 111 enabled_flag |= ENABLED_FOR_RECORDING; |
| 112 } |
| 113 |
| 114 g_category_group_enabled[category_index] = enabled_flag; |
| 115 } |
| 116 |
| 117 void TracingController::UpdateCategoryGroupEnabledFlags() { |
| 118 size_t category_index = base::NoBarrier_Load(&g_category_index); |
| 119 for (size_t i = 0; i < category_index; i++) UpdateCategoryGroupEnabledFlag(i); |
| 120 } |
| 121 |
| 122 const uint8_t* TracingController::GetCategoryGroupEnabledInternal( |
| 123 const char* category_group) { |
| 124 // Check that category groups does not contain double quote |
| 125 DCHECK(!strchr(category_group, '"')); |
| 126 |
| 127 // The g_category_groups is append only, avoid using a lock for the fast path. |
| 128 size_t current_category_index = v8::base::Acquire_Load(&g_category_index); |
| 129 |
| 130 // Search for pre-existing category group. |
| 131 for (size_t i = 0; i < current_category_index; ++i) { |
| 132 if (strcmp(g_category_groups[i], category_group) == 0) { |
| 133 return &g_category_group_enabled[i]; |
| 134 } |
| 135 } |
| 136 |
| 137 unsigned char* category_group_enabled = NULL; |
| 138 size_t category_index = base::Acquire_Load(&g_category_index); |
| 139 for (size_t i = 0; i < category_index; ++i) { |
| 140 if (strcmp(g_category_groups[i], category_group) == 0) { |
| 141 return &g_category_group_enabled[i]; |
| 142 } |
| 143 } |
| 144 |
| 145 // Create a new category group. |
| 146 // Check that there is a slot for the new category_group. |
| 147 DCHECK(category_index < MAX_CATEGORY_GROUPS); |
| 148 if (category_index < MAX_CATEGORY_GROUPS) { |
| 149 // Don't hold on to the category_group pointer, so that we can create |
| 150 // category groups with strings not known at compile time (this is |
| 151 // required by SetWatchEvent). |
| 152 const char* new_group = strdup(category_group); |
| 153 g_category_groups[category_index] = new_group; |
| 154 DCHECK(!g_category_group_enabled[category_index]); |
| 155 // Note that if both included and excluded patterns in the |
| 156 // TraceConfig are empty, we exclude nothing, |
| 157 // thereby enabling this category group. |
| 158 UpdateCategoryGroupEnabledFlag(category_index); |
| 159 category_group_enabled = &g_category_group_enabled[category_index]; |
| 160 // Update the max index now. |
| 161 base::Release_Store(&g_category_index, category_index + 1); |
| 162 } else { |
| 163 category_group_enabled = |
| 164 &g_category_group_enabled[g_category_categories_exhausted]; |
| 165 } |
| 166 return category_group_enabled; |
| 167 } |
| 168 |
| 169 } // namespace tracing |
| 170 } // namespace platform |
| 171 } // namespace v8 |
OLD | NEW |