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 #include "webrtc/base/event_tracer.h" |
11 | 11 |
12 #include <inttypes.h> | 12 #include <inttypes.h> |
13 | 13 |
14 #include <string> | |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/criticalsection.h" | 18 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/base/event.h" | 19 #include "webrtc/base/event.h" |
19 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/platform_thread.h" | 21 #include "webrtc/base/platform_thread.h" |
21 #include "webrtc/base/timeutils.h" | 22 #include "webrtc/base/timeutils.h" |
22 #include "webrtc/base/trace_event.h" | 23 #include "webrtc/base/trace_event.h" |
23 | 24 |
25 static const int kTraceArgBufferLength = 32; | |
26 | |
24 namespace webrtc { | 27 namespace webrtc { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 GetCategoryEnabledPtr g_get_category_enabled_ptr = nullptr; | 31 GetCategoryEnabledPtr g_get_category_enabled_ptr = nullptr; |
29 AddTraceEventPtr g_add_trace_event_ptr = nullptr; | 32 AddTraceEventPtr g_add_trace_event_ptr = nullptr; |
30 | 33 |
31 } // namespace | 34 } // namespace |
32 | 35 |
33 void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, | 36 void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 class EventLogger final { | 86 class EventLogger final { |
84 public: | 87 public: |
85 EventLogger() | 88 EventLogger() |
86 : logging_thread_(EventTracingThreadFunc, this, "EventTracingThread"), | 89 : logging_thread_(EventTracingThreadFunc, this, "EventTracingThread"), |
87 shutdown_event_(false, false) {} | 90 shutdown_event_(false, false) {} |
88 ~EventLogger() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); } | 91 ~EventLogger() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); } |
89 | 92 |
90 void AddTraceEvent(const char* name, | 93 void AddTraceEvent(const char* name, |
91 const unsigned char* category_enabled, | 94 const unsigned char* category_enabled, |
92 char phase, | 95 char phase, |
96 int num_args, | |
97 const char** arg_names, | |
98 const unsigned char* arg_types, | |
99 const unsigned long long* arg_values, | |
93 uint64_t timestamp, | 100 uint64_t timestamp, |
94 int pid, | 101 int pid, |
95 rtc::PlatformThreadId thread_id) { | 102 rtc::PlatformThreadId thread_id) { |
103 std::vector<TraceArg> args(num_args); | |
104 for (int i = 0; i < num_args; i++) { | |
tommi
2016/08/05 10:58:42
++i
sakal
2016/08/05 12:21:07
Done.
| |
105 TraceArg& arg = args[i]; | |
106 arg.name = arg_names[i]; | |
107 arg.type = arg_types[i]; | |
108 arg.value.as_uint = arg_values[i]; | |
tommi
2016/08/05 10:58:41
At first I wasn't sure if this would always copy a
sakal
2016/08/05 12:21:07
I copied the union from webrtc/base/trace_event.h
tommi
2016/08/05 12:50:15
Acknowledged.
| |
109 | |
110 // Value is a pointer to a temporary string, so we have to make a copy. | |
111 if (arg.type == TRACE_VALUE_TYPE_COPY_STRING) { | |
112 // Space for the string and for the terminating null character. | |
113 char* str_copy = new char[strlen(arg.value.as_string) + 1]; | |
114 strcpy(str_copy, arg.value.as_string); | |
tommi
2016/08/05 10:58:41
nit: since you call strlen above, you could use th
sakal
2016/08/05 12:21:06
Changed this to memcpy. Should be safe and even mo
| |
115 arg.value.as_string = str_copy; | |
116 } | |
117 } | |
96 rtc::CritScope lock(&crit_); | 118 rtc::CritScope lock(&crit_); |
97 trace_events_.push_back( | 119 trace_events_.push_back( |
98 {name, category_enabled, phase, timestamp, 1, thread_id}); | 120 {name, category_enabled, phase, args, timestamp, 1, thread_id}); |
99 } | 121 } |
100 | 122 |
101 // The TraceEvent format is documented here: | 123 // The TraceEvent format is documented here: |
102 // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAy SU/preview | 124 // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAy SU/preview |
103 void Log() { | 125 void Log() { |
104 RTC_DCHECK(output_file_); | 126 RTC_DCHECK(output_file_); |
105 static const int kLoggingIntervalMs = 100; | 127 static const int kLoggingIntervalMs = 100; |
106 fprintf(output_file_, "{ \"traceEvents\": [\n"); | 128 fprintf(output_file_, "{ \"traceEvents\": [\n"); |
107 bool has_logged_event = false; | 129 bool has_logged_event = false; |
108 while (true) { | 130 while (true) { |
109 bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs); | 131 bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs); |
110 std::vector<TraceEvent> events; | 132 std::vector<TraceEvent> events; |
111 { | 133 { |
112 rtc::CritScope lock(&crit_); | 134 rtc::CritScope lock(&crit_); |
113 trace_events_.swap(events); | 135 trace_events_.swap(events); |
114 } | 136 } |
115 for (const TraceEvent& e : events) { | 137 for (TraceEvent& e : events) { |
138 std::string args_str; | |
139 if (!e.args.empty()) { | |
140 args_str += ", \"args\": {"; | |
tommi
2016/08/05 10:58:41
nit: since we know that args_str will always be at
sakal
2016/08/05 12:21:07
I moved args_str to the outside of the loop and ma
| |
141 bool is_first_argument = true; | |
142 for (TraceArg& arg : e.args) { | |
143 if (!is_first_argument) { | |
tommi
2016/08/05 10:58:42
no {} (see rest of code)
sakal
2016/08/05 12:21:06
Done.
| |
144 args_str += ","; | |
145 } | |
146 is_first_argument = false; | |
147 args_str += " \""; | |
148 args_str += arg.name; | |
149 args_str += "\": "; | |
150 args_str += TraceArgValueAsString(arg); | |
151 | |
152 // Delete our copy of the string. | |
153 if (arg.type == TRACE_VALUE_TYPE_COPY_STRING) { | |
154 delete[] arg.value.as_string; | |
155 arg.value.as_string = nullptr; | |
156 } | |
157 } | |
158 args_str += " }"; | |
159 } | |
116 fprintf(output_file_, | 160 fprintf(output_file_, |
117 "%s{ \"name\": \"%s\"" | 161 "%s{ \"name\": \"%s\"" |
118 ", \"cat\": \"%s\"" | 162 ", \"cat\": \"%s\"" |
119 ", \"ph\": \"%c\"" | 163 ", \"ph\": \"%c\"" |
120 ", \"ts\": %" PRIu64 | 164 ", \"ts\": %" PRIu64 |
121 ", \"pid\": %d" | 165 ", \"pid\": %d" |
122 #if defined(WEBRTC_WIN) | 166 #if defined(WEBRTC_WIN) |
123 ", \"tid\": %lu" | 167 ", \"tid\": %lu" |
124 #else | 168 #else |
125 ", \"tid\": %d" | 169 ", \"tid\": %d" |
126 #endif // defined(WEBRTC_WIN) | 170 #endif // defined(WEBRTC_WIN) |
171 "%s" | |
127 "}\n", | 172 "}\n", |
128 has_logged_event ? "," : " ", e.name, e.category_enabled, | 173 has_logged_event ? "," : " ", e.name, e.category_enabled, |
129 e.phase, e.timestamp, e.pid, e.tid); | 174 e.phase, e.timestamp, e.pid, e.tid, args_str.c_str()); |
130 has_logged_event = true; | 175 has_logged_event = true; |
131 } | 176 } |
132 if (shutting_down) | 177 if (shutting_down) |
133 break; | 178 break; |
134 } | 179 } |
135 fprintf(output_file_, "]}\n"); | 180 fprintf(output_file_, "]}\n"); |
136 if (output_file_owned_) | 181 if (output_file_owned_) |
137 fclose(output_file_); | 182 fclose(output_file_); |
138 output_file_ = nullptr; | 183 output_file_ = nullptr; |
139 } | 184 } |
(...skipping 30 matching lines...) Expand all Loading... | |
170 if (rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 1, 0) == 0) | 215 if (rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 1, 0) == 0) |
171 return; | 216 return; |
172 | 217 |
173 // Wake up logging thread to finish writing. | 218 // Wake up logging thread to finish writing. |
174 shutdown_event_.Set(); | 219 shutdown_event_.Set(); |
175 // Join the logging thread. | 220 // Join the logging thread. |
176 logging_thread_.Stop(); | 221 logging_thread_.Stop(); |
177 } | 222 } |
178 | 223 |
179 private: | 224 private: |
225 struct TraceArg { | |
226 const char* name; | |
227 unsigned char type; | |
228 union { | |
229 bool as_bool; | |
230 unsigned long long as_uint; | |
tommi
2016/08/05 10:58:42
'as_uint' is a bit misleading. uint can be 32 bit
sakal
2016/08/05 12:21:07
I copied this union from webrtc/base/trace_event.h
tommi
2016/08/05 12:50:15
Acknowledged. Thanks for the static_assert.
| |
231 long long as_int; | |
232 double as_double; | |
233 const void* as_pointer; | |
234 const char* as_string; | |
235 } value; | |
236 }; | |
237 | |
180 struct TraceEvent { | 238 struct TraceEvent { |
181 const char* name; | 239 const char* name; |
182 const unsigned char* category_enabled; | 240 const unsigned char* category_enabled; |
183 char phase; | 241 char phase; |
242 std::vector<TraceArg> args; | |
tommi
2016/08/05 10:58:42
is there a way for us to avoid allocating a vector
sakal
2016/08/05 12:21:06
Discussed this offline.
| |
184 uint64_t timestamp; | 243 uint64_t timestamp; |
185 int pid; | 244 int pid; |
186 rtc::PlatformThreadId tid; | 245 rtc::PlatformThreadId tid; |
187 }; | 246 }; |
188 | 247 |
248 static std::string TraceArgValueAsString(TraceArg arg) { | |
249 std::string output; | |
250 | |
251 if (arg.type == TRACE_VALUE_TYPE_STRING || | |
252 arg.type == TRACE_VALUE_TYPE_COPY_STRING) { | |
253 // Space for every character to be an espaced character + two for | |
254 // quatation marks. | |
255 output.reserve(strlen(arg.value.as_string) * 2 + 2); | |
256 output += "\""; | |
tommi
2016/08/05 10:58:42
nit: this does a "string append" and not a "char a
sakal
2016/08/05 12:21:07
Done.
| |
257 const char* c = arg.value.as_string; | |
258 do { | |
259 if (*c == '"' || *c == '\\') { | |
260 output += '\\'; | |
261 output += *c; | |
262 } else { | |
263 output += *c; | |
264 } | |
265 } while (*++c); | |
266 output += "\""; | |
tommi
2016/08/05 10:58:42
same here.
sakal
2016/08/05 12:21:07
Done.
| |
267 } else { | |
268 output.resize(kTraceArgBufferLength); | |
269 int print_length; | |
270 switch (arg.type) { | |
271 case TRACE_VALUE_TYPE_BOOL: | |
272 print_length = snprintf(&output[0], kTraceArgBufferLength, "%s", | |
tommi
2016/08/05 10:58:41
you don't need snprintf here, just use strcpy()
sakal
2016/08/05 12:21:06
Done.
| |
273 arg.value.as_bool ? "true" : "false"); | |
274 break; | |
275 case TRACE_VALUE_TYPE_UINT: | |
276 print_length = snprintf(&output[0], kTraceArgBufferLength, "%llu", | |
277 arg.value.as_uint); | |
278 break; | |
279 case TRACE_VALUE_TYPE_INT: | |
280 print_length = snprintf(&output[0], kTraceArgBufferLength, "%lld", | |
281 arg.value.as_int); | |
282 break; | |
283 case TRACE_VALUE_TYPE_DOUBLE: | |
284 print_length = snprintf(&output[0], kTraceArgBufferLength, "%f", | |
285 arg.value.as_double); | |
286 break; | |
287 case TRACE_VALUE_TYPE_POINTER: | |
288 print_length = snprintf(&output[0], kTraceArgBufferLength, "\"%p\"", | |
289 arg.value.as_pointer); | |
290 break; | |
291 } | |
292 int output_length = print_length < kTraceArgBufferLength | |
293 ? print_length | |
294 : kTraceArgBufferLength - 1; | |
295 output.resize(output_length); | |
296 } | |
297 | |
298 return output; | |
299 } | |
300 | |
189 rtc::CriticalSection crit_; | 301 rtc::CriticalSection crit_; |
190 std::vector<TraceEvent> trace_events_ GUARDED_BY(crit_); | 302 std::vector<TraceEvent> trace_events_ GUARDED_BY(crit_); |
191 rtc::PlatformThread logging_thread_; | 303 rtc::PlatformThread logging_thread_; |
192 rtc::Event shutdown_event_; | 304 rtc::Event shutdown_event_; |
193 rtc::ThreadChecker thread_checker_; | 305 rtc::ThreadChecker thread_checker_; |
194 FILE* output_file_ = nullptr; | 306 FILE* output_file_ = nullptr; |
195 bool output_file_owned_ = false; | 307 bool output_file_owned_ = false; |
196 }; | 308 }; |
197 | 309 |
198 static bool EventTracingThreadFunc(void* params) { | 310 static bool EventTracingThreadFunc(void* params) { |
(...skipping 23 matching lines...) Expand all Loading... | |
222 unsigned long long id, | 334 unsigned long long id, |
223 int num_args, | 335 int num_args, |
224 const char** arg_names, | 336 const char** arg_names, |
225 const unsigned char* arg_types, | 337 const unsigned char* arg_types, |
226 const unsigned long long* arg_values, | 338 const unsigned long long* arg_values, |
227 unsigned char flags) { | 339 unsigned char flags) { |
228 // Fast path for when event tracing is inactive. | 340 // Fast path for when event tracing is inactive. |
229 if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) | 341 if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) |
230 return; | 342 return; |
231 | 343 |
232 g_event_logger->AddTraceEvent(name, category_enabled, phase, | 344 g_event_logger->AddTraceEvent(name, category_enabled, phase, num_args, |
345 arg_names, arg_types, arg_values, | |
233 rtc::TimeMicros(), 1, rtc::CurrentThreadId()); | 346 rtc::TimeMicros(), 1, rtc::CurrentThreadId()); |
234 } | 347 } |
235 | 348 |
236 } // namespace | 349 } // namespace |
237 | 350 |
238 void SetupInternalTracer() { | 351 void SetupInternalTracer() { |
239 RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( | 352 RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
240 &g_event_logger, static_cast<EventLogger*>(nullptr), | 353 &g_event_logger, static_cast<EventLogger*>(nullptr), |
241 new EventLogger()) == nullptr); | 354 new EventLogger()) == nullptr); |
242 webrtc::SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); | 355 webrtc::SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); |
(...skipping 24 matching lines...) Expand all Loading... | |
267 RTC_DCHECK(old_logger); | 380 RTC_DCHECK(old_logger); |
268 RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( | 381 RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
269 &g_event_logger, old_logger, | 382 &g_event_logger, old_logger, |
270 static_cast<EventLogger*>(nullptr)) == old_logger); | 383 static_cast<EventLogger*>(nullptr)) == old_logger); |
271 delete old_logger; | 384 delete old_logger; |
272 webrtc::SetupEventTracer(nullptr, nullptr); | 385 webrtc::SetupEventTracer(nullptr, nullptr); |
273 } | 386 } |
274 | 387 |
275 } // namespace tracing | 388 } // namespace tracing |
276 } // namespace rtc | 389 } // namespace rtc |
OLD | NEW |