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> |
tommi
2015/11/27 22:07:26
Hm.. have you tried this on the win bots?
pbos-webrtc
2015/12/03 15:23:04
Yep!
| |
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 // TODO(pbos): Log metadata for all threads, etc. | |
tommi
2015/11/27 22:07:26
nit: add empty line above this one.
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
82 class EventLogger final { | |
83 public: | |
84 EventLogger() | |
85 : logging_thread_(EventTracingThreadFunc, this, "EventTracingThread"), | |
86 wakeup_event_(false, false) {} | |
87 | |
88 void AddTraceEvent(const char* name, | |
89 const unsigned char* category_enabled, | |
90 char phase, | |
91 uint64_t timestamp, | |
92 int pid, | |
93 rtc::PlatformThreadId thread_id) { | |
94 rtc::CritScope lock(&crit_); | |
95 if (!enabled_) | |
tommi
2015/11/27 22:07:26
Is this check necessary? We've just checked that
pbos-webrtc
2015/12/03 15:23:04
Done, adding stale events is OK, we clear them on
| |
96 return; | |
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 wakeup_event_.Wait(kLoggingIntervalMs); | |
tommi
2015/11/27 22:07:26
let's check for the return value here. If the eve
pbos-webrtc
2015/12/03 15:23:04
Done + renamed.
| |
110 std::vector<TraceEvent> events; | |
111 bool shutting_down; | |
112 { | |
113 rtc::CritScope lock(&crit_); | |
114 trace_events_.swap(events); | |
115 shutting_down = !enabled_; | |
tommi
2015/11/27 22:07:26
and then this check isn't necessary
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
116 } | |
117 for (const TraceEvent& e : events) { | |
118 fprintf(output_file_, | |
119 "%s{ \"name\": \"%s\"" | |
120 ", \"cat\": \"%s\"" | |
121 ", \"ph\": \"%c\"" | |
122 ", \"ts\": %" PRIu64 | |
123 ", \"pid\": %d" | |
124 ", \"tid\": %d}\n", | |
125 has_logged_event ? "," : " ", e.name, e.category_enabled, | |
126 e.phase, e.timestamp, e.pid, e.tid); | |
127 has_logged_event = true; | |
128 } | |
129 if (shutting_down) | |
130 break; | |
131 } | |
132 fprintf(output_file_, "]}\n"); | |
133 if (output_file_owned_) | |
134 fclose(output_file_); | |
135 output_file_ = nullptr; | |
136 } | |
137 | |
138 void Start(FILE* file, bool owned) { | |
139 RTC_DCHECK(file); | |
140 RTC_DCHECK(!output_file_); | |
141 output_file_ = file; | |
142 output_file_owned_ = owned; | |
143 { | |
144 rtc::CritScope lock(&crit_); | |
145 RTC_DCHECK(!enabled_); | |
146 enabled_ = true; | |
tommi
2015/11/27 22:07:26
I don't think we need this variable or this scope
pbos-webrtc
2015/12/03 15:23:04
Done for the variable, scope needed because we nee
| |
147 } | |
148 // Enable event logging (fast-path). This should be disabled since starting | |
149 // shouldn't be done twice. | |
150 RTC_CHECK_EQ(0, | |
151 rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 0, 1)); | |
152 | |
153 // Finally start, everything should be set up now. | |
154 logging_thread_.Start(); | |
155 } | |
156 | |
157 void Stop() { | |
158 // Abort if we're not currently logging. | |
159 { | |
160 rtc::CritScope lock(&crit_); | |
tommi
2015/11/27 22:07:26
can lose this scope
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
161 if (!enabled_) | |
162 return; | |
163 enabled_ = false; | |
164 } | |
165 // Disable event logging (fast-path). This should be enabled or we should've | |
166 // aborted. Start/Stop should not be called concurrently. | |
167 RTC_CHECK_EQ(1, | |
168 rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 1, 0)); | |
tommi
2015/11/27 22:07:26
if we want to allow calling Stop() when not runnin
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
169 // Wake up logging thread to finish writing. | |
170 wakeup_event_.Set(); | |
171 // Join the logging thread. | |
172 logging_thread_.Stop(); | |
173 } | |
174 | |
175 private: | |
176 struct TraceEvent { | |
177 const char* name; | |
178 const unsigned char* category_enabled; | |
179 char phase; | |
180 uint64_t timestamp; | |
181 int pid; | |
182 rtc::PlatformThreadId tid; | |
183 }; | |
184 | |
185 rtc::CriticalSection crit_; | |
186 bool enabled_ GUARDED_BY(crit_) = false; | |
187 std::vector<TraceEvent> trace_events_ GUARDED_BY(crit_); | |
188 webrtc::PlatformThread logging_thread_; | |
189 rtc::Event wakeup_event_; | |
190 FILE* output_file_ = nullptr; | |
191 bool output_file_owned_ = false; | |
192 }; | |
193 | |
194 static bool EventTracingThreadFunc(void* params) { | |
195 static_cast<EventLogger*>(params)->Log(); | |
196 return true; | |
197 } | |
198 | |
199 static EventLogger* g_event_logger = nullptr; | |
200 static const char* const kDisabledTracePrefix = TRACE_DISABLED_BY_DEFAULT(""); | |
201 const unsigned char* InternalGetCategoryEnabled(const char* name) { | |
202 const char* prefix_ptr = kDisabledTracePrefix; | |
tommi
2015/11/27 22:07:26
nit: &kDisabledTracePrefix[0]
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
203 const char* name_ptr = name; | |
204 // Check whether name contains the default-disabled prefix. | |
205 while (*prefix_ptr == *name_ptr) { | |
tommi
2015/11/27 22:07:26
what if both strings are "foo"? Won't we read pas
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
206 ++prefix_ptr; | |
207 ++name_ptr; | |
208 } | |
209 return reinterpret_cast<const unsigned char*>(*prefix_ptr == '\0' ? "" | |
210 : name); | |
211 } | |
212 | |
213 void InternalAddTraceEvent(char phase, | |
214 const unsigned char* category_enabled, | |
215 const char* name, | |
216 unsigned long long id, | |
217 int num_args, | |
218 const char** arg_names, | |
219 const unsigned char* arg_types, | |
220 const unsigned long long* arg_values, | |
221 unsigned char flags) { | |
222 // Fast path for when event tracing is inactive. | |
223 if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) | |
224 return; | |
225 | |
226 uint64_t timestamp = rtc::TimeMicros(); | |
227 rtc::PlatformThreadId thread_id = rtc::CurrentThreadId(); | |
228 g_event_logger->AddTraceEvent(name, category_enabled, phase, timestamp, 1, | |
229 thread_id); | |
tommi
2015/11/27 22:07:26
nit: can just pass rtc::CurrentThreadId(). (and r
pbos-webrtc
2015/12/03 15:23:04
Genious! Done. Derp.
| |
230 } | |
231 | |
232 } // namespace | |
233 | |
234 void SetupInternalTracer() { | |
235 RTC_DCHECK(!webrtc::g_get_category_enabled_ptr); | |
236 RTC_DCHECK(!webrtc::g_add_trace_event_ptr); | |
237 RTC_DCHECK(!g_event_logger); | |
238 g_event_logger = new EventLogger(); | |
tommi
2015/11/27 22:07:26
since there's no synchronization here, we should a
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
239 webrtc::SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); | |
240 } | |
241 | |
242 void StartInternalCaptureToFile(FILE* file) { | |
243 RTC_DCHECK(g_event_logger); | |
tommi
2015/11/27 22:07:26
not necessary since you dereference anyway
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
244 g_event_logger->Start(file, false); | |
245 } | |
246 | |
247 bool StartInternalCapture(const char* filename) { | |
248 RTC_DCHECK(g_event_logger); | |
249 FILE* file = fopen(filename, "w"); | |
250 if (!file) { | |
251 LOG(LS_ERROR) << "Failed to open trace file '" << filename | |
252 << "' for writing."; | |
253 return false; | |
254 } | |
255 g_event_logger->Start(file, true); | |
256 return true; | |
257 } | |
258 | |
259 void StopInternalCapture() { | |
260 RTC_DCHECK(g_event_logger); | |
tommi
2015/11/27 22:07:26
same here
pbos-webrtc
2015/12/03 15:23:04
Done.
| |
261 g_event_logger->Stop(); | |
262 } | |
263 | |
264 void ShutdownInternalTracer() { | |
265 StopInternalCapture(); | |
266 RTC_DCHECK(webrtc::g_get_category_enabled_ptr == InternalGetCategoryEnabled); | |
267 RTC_DCHECK(webrtc::g_add_trace_event_ptr == InternalAddTraceEvent); | |
268 RTC_DCHECK(g_event_logger); | |
269 webrtc::SetupEventTracer(nullptr, nullptr); | |
270 delete g_event_logger; | |
271 g_event_logger = nullptr; | |
tommi
2015/11/27 22:07:26
we need to free this object atomically
pbos-webrtc
2015/12/03 15:23:04
Assuming you mean the CAS we talked about, done.
| |
272 } | |
273 | |
274 } // namespace tracing | |
275 } // namespace rtc | |
OLD | NEW |