Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 #include "webrtc/base/event_tracer.h" | |
| 10 | 11 |
| 11 #include "webrtc/base/event_tracer.h" | 12 #include <inttypes.h> |
| 13 | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "webrtc/base/checks.h" | |
| 17 #include "webrtc/base/criticalsection.h" | |
| 18 #include "webrtc/base/event.h" | |
| 19 #include "webrtc/base/logging.h" | |
| 20 #include "webrtc/base/platform_thread.h" | |
| 21 #include "webrtc/base/timeutils.h" | |
| 22 #include "webrtc/base/trace_event.h" | |
| 12 | 23 |
| 13 namespace webrtc { | 24 namespace webrtc { |
| 14 | 25 |
| 15 namespace { | 26 namespace { |
| 16 | 27 |
| 17 GetCategoryEnabledPtr g_get_category_enabled_ptr = 0; | 28 GetCategoryEnabledPtr g_get_category_enabled_ptr = nullptr; |
| 18 AddTraceEventPtr g_add_trace_event_ptr = 0; | 29 AddTraceEventPtr g_add_trace_event_ptr = nullptr; |
| 19 | 30 |
| 20 } // namespace | 31 } // namespace |
| 21 | 32 |
| 22 void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, | 33 void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, |
| 23 AddTraceEventPtr add_trace_event_ptr) { | 34 AddTraceEventPtr add_trace_event_ptr) { |
| 24 g_get_category_enabled_ptr = get_category_enabled_ptr; | 35 g_get_category_enabled_ptr = get_category_enabled_ptr; |
| 25 g_add_trace_event_ptr = add_trace_event_ptr; | 36 g_add_trace_event_ptr = add_trace_event_ptr; |
| 26 } | 37 } |
| 27 | 38 |
| 28 // static | |
| 29 const unsigned char* EventTracer::GetCategoryEnabled(const char* name) { | 39 const unsigned char* EventTracer::GetCategoryEnabled(const char* name) { |
| 30 if (g_get_category_enabled_ptr) | 40 if (g_get_category_enabled_ptr) |
| 31 return g_get_category_enabled_ptr(name); | 41 return g_get_category_enabled_ptr(name); |
| 32 | 42 |
| 33 // A string with null terminator means category is disabled. | 43 // A string with null terminator means category is disabled. |
| 34 return reinterpret_cast<const unsigned char*>("\0"); | 44 return reinterpret_cast<const unsigned char*>("\0"); |
| 35 } | 45 } |
| 36 | 46 |
| 37 // static | 47 // Arguments to this function (phase, etc.) are as defined in |
| 48 // webrtc/base/trace_event.h. | |
| 38 void EventTracer::AddTraceEvent(char phase, | 49 void EventTracer::AddTraceEvent(char phase, |
| 39 const unsigned char* category_enabled, | 50 const unsigned char* category_enabled, |
| 40 const char* name, | 51 const char* name, |
| 41 unsigned long long id, | 52 unsigned long long id, |
| 42 int num_args, | 53 int num_args, |
| 43 const char** arg_names, | 54 const char** arg_names, |
| 44 const unsigned char* arg_types, | 55 const unsigned char* arg_types, |
| 45 const unsigned long long* arg_values, | 56 const unsigned long long* arg_values, |
| 46 unsigned char flags) { | 57 unsigned char flags) { |
| 47 if (g_add_trace_event_ptr) { | 58 if (g_add_trace_event_ptr) { |
| 48 g_add_trace_event_ptr(phase, | 59 g_add_trace_event_ptr(phase, |
| 49 category_enabled, | 60 category_enabled, |
| 50 name, | 61 name, |
| 51 id, | 62 id, |
| 52 num_args, | 63 num_args, |
| 53 arg_names, | 64 arg_names, |
| 54 arg_types, | 65 arg_types, |
| 55 arg_values, | 66 arg_values, |
| 56 flags); | 67 flags); |
| 57 } | 68 } |
| 58 } | 69 } |
| 59 | 70 |
| 71 namespace { | |
| 72 | |
| 73 static bool EventTracingThreadFunc(void* params); | |
| 74 | |
| 75 // Atomic-int fast path for avoiding logging when disabled. | |
| 76 static volatile int g_event_logging_active = 0; | |
| 77 // TODO(pbos): Log metadata for all threads, etc. | |
| 78 class EventLogger final { | |
| 79 public: | |
| 80 EventLogger() | |
| 81 : logging_thread_(EventTracingThreadFunc, this, "EventTracingThread"), | |
| 82 wakeup_event_(false, false) {} | |
| 83 | |
| 84 void AddTraceEvent(const char* name, | |
| 85 const unsigned char* category_enabled, | |
| 86 char phase, | |
| 87 uint64_t timestamp, | |
| 88 int pid, | |
| 89 rtc::PlatformThreadId thread_id) { | |
| 90 rtc::CritScope lock(&crit_); | |
| 91 if (!enabled_) | |
| 92 return; | |
| 93 trace_events_.push_back( | |
| 94 TraceEvent{name, category_enabled, phase, timestamp, 1, thread_id}); | |
|
tommi
2015/11/24 16:27:09
Are we using this syntax anywhere else?
pbos-webrtc
2015/11/26 12:21:07
I found like one instance of it in a test, removed
| |
| 95 } | |
| 96 | |
| 97 void Log() { | |
|
tommi
2015/11/24 16:27:09
can you add a link to some documentation about thi
pbos-webrtc
2015/11/26 12:21:07
Added documentation link. The upstream repo even l
| |
| 98 RTC_DCHECK(output_file_); | |
| 99 static const int kLoggingIntervalMs = 100; | |
| 100 fprintf(output_file_, "{ \"traceEvents\": [\n"); | |
| 101 bool has_logged_event = false; | |
| 102 while (true) { | |
| 103 wakeup_event_.Wait(kLoggingIntervalMs); | |
| 104 std::vector<TraceEvent> events; | |
| 105 bool shutting_down; | |
| 106 { | |
| 107 rtc::CritScope lock(&crit_); | |
| 108 trace_events_.swap(events); | |
| 109 shutting_down = !enabled_; | |
| 110 } | |
| 111 for (const TraceEvent& e : events) { | |
| 112 fprintf(output_file_, | |
| 113 "%s{ \"name\": \"%s\"" | |
| 114 ", \"cat\": \"%s\"" | |
| 115 ", \"ph\": \"%c\"" | |
| 116 ", \"ts\": %" PRIu64 | |
| 117 ", \"pid\": %d" | |
| 118 ", \"tid\": %d}\n", | |
| 119 has_logged_event ? "," : " ", e.name, e.category_enabled, | |
| 120 e.phase, e.timestamp, e.pid, e.tid); | |
| 121 has_logged_event = true; | |
| 122 } | |
| 123 if (shutting_down) | |
| 124 break; | |
| 125 } | |
| 126 fprintf(output_file_, "]}\n"); | |
| 127 if (output_file_owned_) | |
| 128 fclose(output_file_); | |
| 129 output_file_ = nullptr; | |
| 130 } | |
| 131 | |
| 132 void Start(FILE* file, bool owned) { | |
| 133 RTC_DCHECK_EQ(0, rtc::AtomicOps::AcquireLoad(&g_event_logging_active)) | |
| 134 << "Internal tracing should not start twice."; | |
| 135 RTC_DCHECK(!output_file_); | |
| 136 output_file_ = file; | |
| 137 output_file_owned_ = owned; | |
| 138 { | |
| 139 rtc::CritScope lock(&crit_); | |
| 140 enabled_ = true; | |
|
tommi
2015/11/24 16:27:09
do we need both enabled_ and g_event_logging_activ
pbos-webrtc
2015/11/26 12:21:07
Yes, because g_event_logging_active is a fast path
| |
| 141 } | |
| 142 rtc::AtomicOps::ReleaseStore(&g_event_logging_active, 1); | |
|
tommi
2015/11/24 16:27:09
can we rather do this in a CompareExhange step and
pbos-webrtc
2015/11/26 12:21:07
Done.
| |
| 143 | |
| 144 // Finally start, everything should be set up now. | |
| 145 logging_thread_.Start(); | |
| 146 } | |
| 147 | |
| 148 void Stop() { | |
| 149 // Abort if we're not currently logging. | |
| 150 { | |
| 151 rtc::CritScope lock(&crit_); | |
| 152 if (!enabled_) | |
| 153 return; | |
| 154 enabled_ = false; | |
| 155 } | |
| 156 // Disable event logging (fast-path). | |
| 157 rtc::AtomicOps::ReleaseStore(&g_event_logging_active, 0); | |
|
tommi
2015/11/24 16:27:09
CompareExchange instead.
pbos-webrtc
2015/11/26 12:21:07
Done.
| |
| 158 // Wake up logging thread to finish writing. | |
| 159 wakeup_event_.Set(); | |
| 160 // Join the logging thread. | |
| 161 logging_thread_.Stop(); | |
| 162 } | |
| 163 | |
| 164 private: | |
| 165 struct TraceEvent { | |
| 166 const char* name; | |
| 167 const unsigned char* category_enabled; | |
| 168 char phase; | |
| 169 uint64_t timestamp; | |
| 170 int pid; | |
| 171 rtc::PlatformThreadId tid; | |
| 172 }; | |
| 173 | |
| 174 rtc::CriticalSection crit_; | |
| 175 bool enabled_ GUARDED_BY(crit_) = false; | |
| 176 std::vector<TraceEvent> trace_events_ GUARDED_BY(crit_); | |
| 177 webrtc::PlatformThread logging_thread_; | |
| 178 rtc::Event wakeup_event_; | |
| 179 FILE* output_file_ = nullptr; | |
| 180 bool output_file_owned_ = false; | |
| 181 }; | |
| 182 | |
| 183 static bool EventTracingThreadFunc(void* params) { | |
| 184 static_cast<EventLogger*>(params)->Log(); | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 static EventLogger* g_event_logger = nullptr; | |
| 189 static const char* const kDisabledTracePrefix = TRACE_DISABLED_BY_DEFAULT(""); | |
| 190 const unsigned char* InternalGetCategoryEnabled(const char* name) { | |
| 191 const char* prefix_ptr = kDisabledTracePrefix; | |
| 192 const char* name_ptr = name; | |
| 193 // Check whether name contains the default-disabled prefix. | |
| 194 while (*prefix_ptr == *name_ptr) { | |
| 195 ++prefix_ptr; | |
| 196 ++name_ptr; | |
| 197 } | |
| 198 return reinterpret_cast<const unsigned char*>(*prefix_ptr == '\0' ? "" | |
| 199 : name); | |
| 200 } | |
| 201 | |
| 202 void InternalAddTraceEvent(char phase, | |
| 203 const unsigned char* category_enabled, | |
| 204 const char* name, | |
| 205 unsigned long long id, | |
| 206 int num_args, | |
| 207 const char** arg_names, | |
| 208 const unsigned char* arg_types, | |
| 209 const unsigned long long* arg_values, | |
| 210 unsigned char flags) { | |
| 211 // Fast path for when event tracing is inactive. | |
| 212 if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) | |
| 213 return; | |
| 214 | |
| 215 uint64_t timestamp = rtc::TimeMicros(); | |
| 216 rtc::PlatformThreadId thread_id = rtc::CurrentThreadId(); | |
| 217 g_event_logger->AddTraceEvent(name, category_enabled, phase, timestamp, 1, | |
| 218 thread_id); | |
| 219 } | |
| 220 | |
| 221 } // namespace | |
| 222 | |
| 223 // static | |
| 224 void EventTracer::SetupInternalTracer() { | |
| 225 RTC_DCHECK(!g_get_category_enabled_ptr); | |
| 226 RTC_DCHECK(!g_add_trace_event_ptr); | |
| 227 RTC_DCHECK(!g_event_logger); | |
| 228 g_event_logger = new EventLogger(); | |
| 229 SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); | |
| 230 } | |
| 231 | |
| 232 void EventTracer::StartInternalCaptureToFile(FILE* file) { | |
| 233 RTC_DCHECK(g_event_logger); | |
| 234 g_event_logger->Start(file, false); | |
| 235 } | |
| 236 | |
| 237 bool EventTracer::StartInternalCapture(const char* filename) { | |
| 238 RTC_DCHECK(g_event_logger); | |
| 239 FILE* file = fopen(filename, "w"); | |
| 240 if (!file) { | |
| 241 LOG(LS_ERROR) << "Failed to open trace file '" << filename | |
| 242 << "' for writing."; | |
| 243 return false; | |
| 244 } | |
| 245 g_event_logger->Start(file, true); | |
| 246 return true; | |
| 247 } | |
| 248 | |
| 249 void EventTracer::StopInternalCapture() { | |
| 250 RTC_DCHECK(g_event_logger); | |
| 251 g_event_logger->Stop(); | |
| 252 } | |
| 253 | |
| 254 void EventTracer::ShutdownInternalTracer() { | |
| 255 StopInternalCapture(); | |
| 256 RTC_DCHECK(g_get_category_enabled_ptr == InternalGetCategoryEnabled); | |
| 257 RTC_DCHECK(g_add_trace_event_ptr == InternalAddTraceEvent); | |
| 258 RTC_DCHECK(g_event_logger); | |
| 259 SetupEventTracer(nullptr, nullptr); | |
| 260 delete g_event_logger; | |
| 261 g_event_logger = nullptr; | |
| 262 } | |
| 263 | |
| 60 } // namespace webrtc | 264 } // namespace webrtc |
| OLD | NEW |