OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include "base/debug/trace_event_win.h" | 10 #include "base/debug/trace_event_win.h" |
11 #endif | 11 #endif |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/ref_counted_memory.h" |
14 #include "base/process_util.h" | 15 #include "base/process_util.h" |
15 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
16 #include "base/threading/thread_local.h" | 17 #include "base/threading/thread_local.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
19 #include "base/time.h" | 20 #include "base/time.h" |
20 | 21 |
21 #define USE_UNRELIABLE_NOW | 22 #define USE_UNRELIABLE_NOW |
22 | 23 |
23 class DeleteTraceLogForTesting { | 24 class DeleteTraceLogForTesting { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 112 } |
112 | 113 |
113 //////////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////////// |
114 // | 115 // |
115 // TraceEvent | 116 // TraceEvent |
116 // | 117 // |
117 //////////////////////////////////////////////////////////////////////////////// | 118 //////////////////////////////////////////////////////////////////////////////// |
118 | 119 |
119 namespace { | 120 namespace { |
120 | 121 |
| 122 const char* GetPhaseStr(TraceEventPhase phase) { |
| 123 switch(phase) { |
| 124 case TRACE_EVENT_PHASE_BEGIN: |
| 125 return "B"; |
| 126 case TRACE_EVENT_PHASE_INSTANT: |
| 127 return "I"; |
| 128 case TRACE_EVENT_PHASE_END: |
| 129 return "E"; |
| 130 case TRACE_EVENT_PHASE_METADATA: |
| 131 return "M"; |
| 132 default: |
| 133 NOTREACHED() << "Invalid phase argument"; |
| 134 return "?"; |
| 135 } |
| 136 } |
| 137 |
121 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } | 138 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } |
122 | 139 |
123 // Copies |*member| into |*buffer|, sets |*member| to point to this new | 140 // Copies |*member| into |*buffer|, sets |*member| to point to this new |
124 // location, and then advances |*buffer| by the amount written. | 141 // location, and then advances |*buffer| by the amount written. |
125 void CopyTraceEventParameter(char** buffer, | 142 void CopyTraceEventParameter(char** buffer, |
126 const char** member, | 143 const char** member, |
127 const char* end) { | 144 const char* end) { |
128 if (*member) { | 145 if (*member) { |
129 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; | 146 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; |
130 DCHECK_LE(static_cast<int>(written), end - *buffer); | 147 DCHECK_LE(static_cast<int>(written), end - *buffer); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); | 214 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); |
198 if (arg2_is_copy) | 215 if (arg2_is_copy) |
199 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); | 216 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); |
200 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; | 217 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; |
201 } | 218 } |
202 } | 219 } |
203 | 220 |
204 TraceEvent::~TraceEvent() { | 221 TraceEvent::~TraceEvent() { |
205 } | 222 } |
206 | 223 |
207 const char* TraceEvent::GetPhaseString(TraceEventPhase phase) { | |
208 switch(phase) { | |
209 case TRACE_EVENT_PHASE_BEGIN: | |
210 return "B"; | |
211 case TRACE_EVENT_PHASE_INSTANT: | |
212 return "I"; | |
213 case TRACE_EVENT_PHASE_END: | |
214 return "E"; | |
215 case TRACE_EVENT_PHASE_METADATA: | |
216 return "M"; | |
217 default: | |
218 NOTREACHED() << "Invalid phase argument"; | |
219 return "?"; | |
220 } | |
221 } | |
222 | |
223 TraceEventPhase TraceEvent::GetPhase(const char* phase) { | |
224 switch(*phase) { | |
225 case 'B': | |
226 return TRACE_EVENT_PHASE_BEGIN; | |
227 case 'I': | |
228 return TRACE_EVENT_PHASE_INSTANT; | |
229 case 'E': | |
230 return TRACE_EVENT_PHASE_END; | |
231 case 'M': | |
232 return TRACE_EVENT_PHASE_METADATA; | |
233 default: | |
234 NOTREACHED() << "Invalid phase name"; | |
235 return TRACE_EVENT_PHASE_METADATA; | |
236 } | |
237 } | |
238 | |
239 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 224 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
240 size_t start, | 225 size_t start, |
241 size_t count, | 226 size_t count, |
242 std::string* out) { | 227 std::string* out) { |
243 for (size_t i = 0; i < count && start + i < events.size(); ++i) { | 228 for (size_t i = 0; i < count && start + i < events.size(); ++i) { |
244 if (i > 0) | 229 if (i > 0) |
245 *out += ","; | 230 *out += ","; |
246 events[i + start].AppendAsJSON(out); | 231 events[i + start].AppendAsJSON(out); |
247 } | 232 } |
248 } | 233 } |
249 | 234 |
250 void TraceEvent::AppendAsJSON(std::string* out) const { | 235 void TraceEvent::AppendAsJSON(std::string* out) const { |
251 const char* phase_str = GetPhaseString(phase_); | 236 const char* phase_str = GetPhaseStr(phase_); |
252 int64 time_int64 = timestamp_.ToInternalValue(); | 237 int64 time_int64 = timestamp_.ToInternalValue(); |
253 // Category name checked at category creation time. | 238 // Category name checked at category creation time. |
254 DCHECK(!strchr(name_, '"')); | 239 DCHECK(!strchr(name_, '"')); |
255 StringAppendF(out, | 240 StringAppendF(out, |
256 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," | 241 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," |
257 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", | 242 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", |
258 category_->name, | 243 category_->name, |
259 static_cast<int>(process_id_), | 244 static_cast<int>(process_id_), |
260 static_cast<int>(thread_id_), | 245 static_cast<int>(thread_id_), |
261 static_cast<long long>(time_int64), | 246 static_cast<long long>(time_int64), |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 NULL, 0, NULL, 0, | 651 NULL, 0, NULL, 0, |
667 p_data_->threshold_begin_id, p_data_->threshold, | 652 p_data_->threshold_begin_id, p_data_->threshold, |
668 TraceLog::EVENT_FLAG_NONE); | 653 TraceLog::EVENT_FLAG_NONE); |
669 } | 654 } |
670 } | 655 } |
671 | 656 |
672 } // namespace internal | 657 } // namespace internal |
673 | 658 |
674 } // namespace debug | 659 } // namespace debug |
675 } // namespace base | 660 } // namespace base |
OLD | NEW |