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

Side by Side Diff: webrtc/base/event_tracer.cc

Issue 1457383002: Implement standalone event tracing in AppRTCDemo. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: readd jitter_buffer trace, rebase Created 5 years 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 | « webrtc/base/event_tracer.h ('k') | webrtc/call/call.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
60 } // namespace webrtc 71 } // namespace webrtc
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-PhYUmn5OOQtYMH4h6I0nSsKchNAy SU/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
OLDNEW
« no previous file with comments | « webrtc/base/event_tracer.h ('k') | webrtc/call/call.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698