tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 10 | #include "webrtc/base/event_tracer.h" |
| 11 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 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" |
| 23 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 28 | GetCategoryEnabledPtr g_get_category_enabled_ptr = nullptr; |
| 29 | AddTraceEventPtr g_add_trace_event_ptr = nullptr; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, |
| 34 | AddTraceEventPtr add_trace_event_ptr) { |
| 35 | g_get_category_enabled_ptr = get_category_enabled_ptr; |
| 36 | g_add_trace_event_ptr = add_trace_event_ptr; |
| 37 | } |
| 38 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 39 | const unsigned char* EventTracer::GetCategoryEnabled(const char* name) { |
| 40 | if (g_get_category_enabled_ptr) |
| 41 | return g_get_category_enabled_ptr(name); |
| 42 | |
| 43 | // A string with null terminator means category is disabled. |
| 44 | return reinterpret_cast<const unsigned char*>("\0"); |
| 45 | } |
| 46 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 47 | // Arguments to this function (phase, etc.) are as defined in |
| 48 | // webrtc/base/trace_event.h. |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 49 | void EventTracer::AddTraceEvent(char phase, |
| 50 | const unsigned char* category_enabled, |
| 51 | const char* name, |
| 52 | unsigned long long id, |
| 53 | int num_args, |
| 54 | const char** arg_names, |
| 55 | const unsigned char* arg_types, |
| 56 | const unsigned long long* arg_values, |
| 57 | unsigned char flags) { |
| 58 | if (g_add_trace_event_ptr) { |
| 59 | g_add_trace_event_ptr(phase, |
| 60 | category_enabled, |
| 61 | name, |
| 62 | id, |
| 63 | num_args, |
| 64 | arg_names, |
| 65 | arg_types, |
| 66 | arg_values, |
| 67 | flags); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } // namespace webrtc |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 72 | |
| 73 | namespace rtc { |
| 74 | namespace tracing { |
| 75 | namespace { |
| 76 | |
| 77 | static bool EventTracingThreadFunc(void* params); |
| 78 | |
| 79 | // Atomic-int fast path for avoiding logging when disabled. |
| 80 | static volatile int g_event_logging_active = 0; |
| 81 | |
| 82 | // TODO(pbos): Log metadata for all threads, etc. |
| 83 | class EventLogger final { |
| 84 | public: |
| 85 | EventLogger() |
| 86 | : logging_thread_(EventTracingThreadFunc, this, "EventTracingThread"), |
| 87 | shutdown_event_(false, false) {} |
| 88 | ~EventLogger() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); } |
| 89 | |
| 90 | void AddTraceEvent(const char* name, |
| 91 | const unsigned char* category_enabled, |
| 92 | char phase, |
| 93 | uint64_t timestamp, |
| 94 | int pid, |
| 95 | rtc::PlatformThreadId thread_id) { |
| 96 | rtc::CritScope lock(&crit_); |
| 97 | trace_events_.push_back( |
| 98 | {name, category_enabled, phase, timestamp, 1, thread_id}); |
| 99 | } |
| 100 | |
| 101 | // The TraceEvent format is documented here: |
| 102 | // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview |
| 103 | void Log() { |
| 104 | RTC_DCHECK(output_file_); |
| 105 | static const int kLoggingIntervalMs = 100; |
| 106 | fprintf(output_file_, "{ \"traceEvents\": [\n"); |
| 107 | bool has_logged_event = false; |
| 108 | while (true) { |
| 109 | bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs); |
| 110 | std::vector<TraceEvent> events; |
| 111 | { |
| 112 | rtc::CritScope lock(&crit_); |
| 113 | trace_events_.swap(events); |
| 114 | } |
| 115 | for (const TraceEvent& e : events) { |
| 116 | fprintf(output_file_, |
| 117 | "%s{ \"name\": \"%s\"" |
| 118 | ", \"cat\": \"%s\"" |
| 119 | ", \"ph\": \"%c\"" |
| 120 | ", \"ts\": %" PRIu64 |
| 121 | ", \"pid\": %d" |
| 122 | ", \"tid\": %d}\n", |
| 123 | has_logged_event ? "," : " ", e.name, e.category_enabled, |
| 124 | e.phase, e.timestamp, e.pid, e.tid); |
| 125 | has_logged_event = true; |
| 126 | } |
| 127 | if (shutting_down) |
| 128 | break; |
| 129 | } |
| 130 | fprintf(output_file_, "]}\n"); |
| 131 | if (output_file_owned_) |
| 132 | fclose(output_file_); |
| 133 | output_file_ = nullptr; |
| 134 | } |
| 135 | |
| 136 | void Start(FILE* file, bool owned) { |
| 137 | RTC_DCHECK(file); |
| 138 | RTC_DCHECK(!output_file_); |
| 139 | output_file_ = file; |
| 140 | output_file_owned_ = owned; |
| 141 | { |
| 142 | rtc::CritScope lock(&crit_); |
| 143 | // Since the atomic fast-path for adding events to the queue can be |
| 144 | // bypassed while the logging thread is shutting down there may be some |
| 145 | // stale events in the queue, hence the vector needs to be cleared to not |
| 146 | // log events from a previous logging session (which may be days old). |
| 147 | trace_events_.clear(); |
| 148 | } |
| 149 | // Enable event logging (fast-path). This should be disabled since starting |
| 150 | // shouldn't be done twice. |
| 151 | RTC_CHECK_EQ(0, |
| 152 | rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 0, 1)); |
| 153 | |
| 154 | // Finally start, everything should be set up now. |
| 155 | logging_thread_.Start(); |
| 156 | } |
| 157 | |
| 158 | void Stop() { |
| 159 | // Try to stop. Abort if we're not currently logging. |
| 160 | if (rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 1, 0) == 0) |
| 161 | return; |
| 162 | |
| 163 | // Wake up logging thread to finish writing. |
| 164 | shutdown_event_.Set(); |
| 165 | // Join the logging thread. |
| 166 | logging_thread_.Stop(); |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | struct TraceEvent { |
| 171 | const char* name; |
| 172 | const unsigned char* category_enabled; |
| 173 | char phase; |
| 174 | uint64_t timestamp; |
| 175 | int pid; |
| 176 | rtc::PlatformThreadId tid; |
| 177 | }; |
| 178 | |
| 179 | rtc::CriticalSection crit_; |
| 180 | std::vector<TraceEvent> trace_events_ GUARDED_BY(crit_); |
| 181 | rtc::PlatformThread logging_thread_; |
| 182 | rtc::Event shutdown_event_; |
| 183 | rtc::ThreadChecker thread_checker_; |
| 184 | FILE* output_file_ = nullptr; |
| 185 | bool output_file_owned_ = false; |
| 186 | }; |
| 187 | |
| 188 | static bool EventTracingThreadFunc(void* params) { |
| 189 | static_cast<EventLogger*>(params)->Log(); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | static EventLogger* volatile g_event_logger = nullptr; |
| 194 | static const char* const kDisabledTracePrefix = TRACE_DISABLED_BY_DEFAULT(""); |
| 195 | const unsigned char* InternalGetCategoryEnabled(const char* name) { |
| 196 | const char* prefix_ptr = &kDisabledTracePrefix[0]; |
| 197 | const char* name_ptr = name; |
| 198 | // Check whether name contains the default-disabled prefix. |
| 199 | while (*prefix_ptr == *name_ptr && *prefix_ptr != '\0') { |
| 200 | ++prefix_ptr; |
| 201 | ++name_ptr; |
| 202 | } |
| 203 | return reinterpret_cast<const unsigned char*>(*prefix_ptr == '\0' ? "" |
| 204 | : name); |
| 205 | } |
| 206 | |
| 207 | void InternalAddTraceEvent(char phase, |
| 208 | const unsigned char* category_enabled, |
| 209 | const char* name, |
| 210 | unsigned long long id, |
| 211 | int num_args, |
| 212 | const char** arg_names, |
| 213 | const unsigned char* arg_types, |
| 214 | const unsigned long long* arg_values, |
| 215 | unsigned char flags) { |
| 216 | // Fast path for when event tracing is inactive. |
| 217 | if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) |
| 218 | return; |
| 219 | |
| 220 | g_event_logger->AddTraceEvent(name, category_enabled, phase, |
| 221 | rtc::TimeMicros(), 1, rtc::CurrentThreadId()); |
| 222 | } |
| 223 | |
| 224 | } // namespace |
| 225 | |
| 226 | void SetupInternalTracer() { |
| 227 | RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
| 228 | &g_event_logger, static_cast<EventLogger*>(nullptr), |
| 229 | new EventLogger()) == nullptr); |
| 230 | g_event_logger = new EventLogger(); |
| 231 | webrtc::SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); |
| 232 | } |
| 233 | |
| 234 | void StartInternalCaptureToFile(FILE* file) { |
| 235 | g_event_logger->Start(file, false); |
| 236 | } |
| 237 | |
| 238 | bool StartInternalCapture(const char* filename) { |
| 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 StopInternalCapture() { |
| 250 | g_event_logger->Stop(); |
| 251 | } |
| 252 | |
| 253 | void ShutdownInternalTracer() { |
| 254 | StopInternalCapture(); |
| 255 | EventLogger* old_logger = rtc::AtomicOps::AtomicLoadPtr(&g_event_logger); |
| 256 | RTC_DCHECK(old_logger); |
| 257 | RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
| 258 | &g_event_logger, old_logger, |
| 259 | static_cast<EventLogger*>(nullptr)) == old_logger); |
| 260 | delete old_logger; |
| 261 | webrtc::SetupEventTracer(nullptr, nullptr); |
| 262 | } |
| 263 | |
| 264 | } // namespace tracing |
| 265 | } // namespace rtc |