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 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
69 // | 69 // |
70 // TraceValue | 70 // TraceValue |
71 // | 71 // |
72 //////////////////////////////////////////////////////////////////////////////// | 72 //////////////////////////////////////////////////////////////////////////////// |
73 | 73 |
74 void TraceValue::AppendAsJSON(std::string* out) const { | 74 void TraceValue::AppendAsJSON(std::string* out) const { |
75 char temp_string[128]; | |
76 std::string::size_type start_pos; | 75 std::string::size_type start_pos; |
77 switch (type_) { | 76 switch (type_) { |
78 case TRACE_TYPE_BOOL: | 77 case TRACE_TYPE_BOOL: |
79 *out += as_bool() ? "true" : "false"; | 78 *out += as_bool() ? "true" : "false"; |
80 break; | 79 break; |
81 case TRACE_TYPE_UINT: | 80 case TRACE_TYPE_UINT: |
82 base::snprintf(temp_string, arraysize(temp_string), "%llu", | 81 StringAppendF(out, "%llu", static_cast<unsigned long long>(as_uint())); |
jar (doing other things)
2011/12/01 01:47:49
Have you looked at using the "portable" definition
jbates
2011/12/01 23:07:50
Done.
| |
83 static_cast<unsigned long long>(as_uint())); | |
84 *out += temp_string; | |
85 break; | 82 break; |
86 case TRACE_TYPE_INT: | 83 case TRACE_TYPE_INT: |
87 base::snprintf(temp_string, arraysize(temp_string), "%lld", | 84 StringAppendF(out, "%lld", static_cast<long long>(as_int())); |
88 static_cast<long long>(as_int())); | |
89 *out += temp_string; | |
90 break; | 85 break; |
91 case TRACE_TYPE_DOUBLE: | 86 case TRACE_TYPE_DOUBLE: |
92 base::snprintf(temp_string, arraysize(temp_string), "%f", as_double()); | 87 StringAppendF(out, "%f", as_double()); |
93 *out += temp_string; | |
94 break; | 88 break; |
95 case TRACE_TYPE_POINTER: | 89 case TRACE_TYPE_POINTER: |
96 base::snprintf(temp_string, arraysize(temp_string), "%llu", | 90 // JSON only supports double and int numbers. |
97 static_cast<unsigned long long>( | 91 // So as not to lose bits from a 64-bit pointer, output as a hex string. |
98 reinterpret_cast<intptr_t>( | 92 StringAppendF(out, "\"%llx\"", static_cast<unsigned long long>( |
99 as_pointer()))); | 93 reinterpret_cast<intptr_t>( |
100 *out += temp_string; | 94 as_pointer()))); |
101 break; | 95 break; |
102 case TRACE_TYPE_STRING: | 96 case TRACE_TYPE_STRING: |
103 case TRACE_TYPE_STATIC_STRING: | 97 case TRACE_TYPE_STATIC_STRING: |
104 *out += "\""; | 98 *out += "\""; |
105 start_pos = out->size(); | 99 start_pos = out->size(); |
106 *out += as_string() ? as_string() : "NULL"; | 100 *out += as_string() ? as_string() : "NULL"; |
107 // insert backslash before special characters for proper json format. | 101 // insert backslash before special characters for proper json format. |
108 while ((start_pos = out->find_first_of("\\\"", start_pos)) != | 102 while ((start_pos = out->find_first_of("\\\"", start_pos)) != |
109 std::string::npos) { | 103 std::string::npos) { |
110 out->insert(start_pos, 1, '\\'); | 104 out->insert(start_pos, 1, '\\'); |
111 // skip inserted escape character and following character. | 105 // skip inserted escape character and following character. |
112 start_pos += 2; | 106 start_pos += 2; |
113 } | 107 } |
114 *out += "\""; | 108 *out += "\""; |
115 break; | 109 break; |
116 default: | 110 default: |
117 NOTREACHED() << "Don't know how to print this value"; | 111 NOTREACHED() << "Don't know how to print this value"; |
118 break; | 112 break; |
119 } | 113 } |
120 } | 114 } |
121 | 115 |
122 //////////////////////////////////////////////////////////////////////////////// | 116 //////////////////////////////////////////////////////////////////////////////// |
123 // | 117 // |
118 // TraceID | |
119 // | |
120 //////////////////////////////////////////////////////////////////////////////// | |
121 | |
122 TraceID::TraceID(void* rhs) { | |
123 data_ = base::debug::TraceLog::GetInstance()->GetIntraProcessID( | |
124 static_cast<uint64>(reinterpret_cast<uintptr_t>(rhs))); | |
125 } | |
126 | |
127 //////////////////////////////////////////////////////////////////////////////// | |
128 // | |
124 // TraceEvent | 129 // TraceEvent |
125 // | 130 // |
126 //////////////////////////////////////////////////////////////////////////////// | 131 //////////////////////////////////////////////////////////////////////////////// |
127 | 132 |
128 namespace { | 133 namespace { |
129 | 134 |
130 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } | 135 size_t GetAllocLength(const char* str) { return str ? strlen(str) + 1 : 0; } |
131 | 136 |
132 // Copies |*member| into |*buffer|, sets |*member| to point to this new | 137 // Copies |*member| into |*buffer|, sets |*member| to point to this new |
133 // location, and then advances |*buffer| by the amount written. | 138 // location, and then advances |*buffer| by the amount written. |
134 void CopyTraceEventParameter(char** buffer, | 139 void CopyTraceEventParameter(char** buffer, |
135 const char** member, | 140 const char** member, |
136 const char* end) { | 141 const char* end) { |
137 if (*member) { | 142 if (*member) { |
138 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; | 143 size_t written = strlcpy(*buffer, *member, end - *buffer) + 1; |
139 DCHECK_LE(static_cast<int>(written), end - *buffer); | 144 DCHECK_LE(static_cast<int>(written), end - *buffer); |
140 *member = *buffer; | 145 *member = *buffer; |
141 *buffer += written; | 146 *buffer += written; |
142 } | 147 } |
143 } | 148 } |
144 | 149 |
145 } // namespace | 150 } // namespace |
146 | 151 |
147 TraceEvent::TraceEvent() | 152 TraceEvent::TraceEvent() |
148 : process_id_(0), | 153 : id_(0u), |
154 category_(NULL), | |
155 name_(NULL), | |
149 thread_id_(0), | 156 thread_id_(0), |
150 phase_(TRACE_EVENT_PHASE_BEGIN), | 157 phase_(TRACE_EVENT_PHASE_BEGIN), |
151 category_(NULL), | 158 flags_(0) { |
152 name_(NULL) { | |
153 arg_names_[0] = NULL; | 159 arg_names_[0] = NULL; |
154 arg_names_[1] = NULL; | 160 arg_names_[1] = NULL; |
155 } | 161 } |
156 | 162 |
157 TraceEvent::TraceEvent(unsigned long process_id, | 163 TraceEvent::TraceEvent(int thread_id, |
158 unsigned long thread_id, | |
159 TimeTicks timestamp, | 164 TimeTicks timestamp, |
160 TraceEventPhase phase, | 165 TraceEventPhase phase, |
161 const TraceCategory* category, | 166 const TraceCategory* category, |
162 const char* name, | 167 const char* name, |
168 TraceID id, | |
163 const char* arg1_name, const TraceValue& arg1_val, | 169 const char* arg1_name, const TraceValue& arg1_val, |
164 const char* arg2_name, const TraceValue& arg2_val, | 170 const char* arg2_name, const TraceValue& arg2_val, |
165 bool copy) | 171 TraceEventFlags flags) |
166 : process_id_(process_id), | 172 : timestamp_(timestamp), |
173 id_(id), | |
174 category_(category), | |
175 name_(name), | |
167 thread_id_(thread_id), | 176 thread_id_(thread_id), |
168 timestamp_(timestamp), | |
169 phase_(phase), | 177 phase_(phase), |
170 category_(category), | 178 flags_(flags) { |
171 name_(name) { | |
172 COMPILE_ASSERT(kTraceMaxNumArgs == 2, TraceEvent_arg_count_out_of_sync); | 179 COMPILE_ASSERT(kTraceMaxNumArgs == 2, TraceEvent_arg_count_out_of_sync); |
173 arg_names_[0] = arg1_name; | 180 arg_names_[0] = arg1_name; |
174 arg_names_[1] = arg2_name; | 181 arg_names_[1] = arg2_name; |
175 arg_values_[0] = arg1_val; | 182 arg_values_[0] = arg1_val; |
176 arg_values_[1] = arg2_val; | 183 arg_values_[1] = arg2_val; |
177 | 184 |
185 bool copy = !!(flags & TRACE_EVENT_FLAG_COPY); | |
178 size_t alloc_size = 0; | 186 size_t alloc_size = 0; |
179 if (copy) { | 187 if (copy) { |
180 alloc_size += GetAllocLength(name); | 188 alloc_size += GetAllocLength(name); |
181 alloc_size += GetAllocLength(arg1_name); | 189 alloc_size += GetAllocLength(arg1_name); |
182 alloc_size += GetAllocLength(arg2_name); | 190 alloc_size += GetAllocLength(arg2_name); |
183 } | 191 } |
184 | 192 |
185 bool arg1_is_copy = (arg1_val.type() == TraceValue::TRACE_TYPE_STRING); | 193 bool arg1_is_copy = (arg1_val.type() == TraceValue::TRACE_TYPE_STRING); |
186 bool arg2_is_copy = (arg2_val.type() == TraceValue::TRACE_TYPE_STRING); | 194 bool arg2_is_copy = (arg2_val.type() == TraceValue::TRACE_TYPE_STRING); |
187 | 195 |
(...skipping 18 matching lines...) Expand all Loading... | |
206 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); | 214 CopyTraceEventParameter(&ptr, arg_values_[0].as_assignable_string(), end); |
207 if (arg2_is_copy) | 215 if (arg2_is_copy) |
208 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); | 216 CopyTraceEventParameter(&ptr, arg_values_[1].as_assignable_string(), end); |
209 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; | 217 DCHECK_EQ(end, ptr) << "Overrun by " << ptr - end; |
210 } | 218 } |
211 } | 219 } |
212 | 220 |
213 TraceEvent::~TraceEvent() { | 221 TraceEvent::~TraceEvent() { |
214 } | 222 } |
215 | 223 |
216 const char* TraceEvent::GetPhaseString(TraceEventPhase phase) { | |
217 switch(phase) { | |
218 case TRACE_EVENT_PHASE_BEGIN: | |
219 return "B"; | |
220 case TRACE_EVENT_PHASE_INSTANT: | |
221 return "I"; | |
222 case TRACE_EVENT_PHASE_END: | |
223 return "E"; | |
224 case TRACE_EVENT_PHASE_METADATA: | |
225 return "M"; | |
226 case TRACE_EVENT_PHASE_COUNTER: | |
227 return "C"; | |
228 default: | |
229 NOTREACHED() << "Invalid phase argument"; | |
230 return "?"; | |
231 } | |
232 } | |
233 | |
234 TraceEventPhase TraceEvent::GetPhase(const char* phase) { | |
235 switch(*phase) { | |
236 case 'B': | |
237 return TRACE_EVENT_PHASE_BEGIN; | |
238 case 'I': | |
239 return TRACE_EVENT_PHASE_INSTANT; | |
240 case 'E': | |
241 return TRACE_EVENT_PHASE_END; | |
242 case 'M': | |
243 return TRACE_EVENT_PHASE_METADATA; | |
244 case 'C': | |
245 return TRACE_EVENT_PHASE_COUNTER; | |
246 default: | |
247 NOTREACHED() << "Invalid phase name"; | |
248 return TRACE_EVENT_PHASE_METADATA; | |
249 } | |
250 } | |
251 | |
252 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 224 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
253 size_t start, | 225 size_t start, |
254 size_t count, | 226 size_t count, |
255 std::string* out) { | 227 std::string* out) { |
256 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) { |
257 if (i > 0) | 229 if (i > 0) |
258 *out += ","; | 230 *out += ","; |
259 events[i + start].AppendAsJSON(out); | 231 events[i + start].AppendAsJSON(out); |
260 } | 232 } |
261 } | 233 } |
262 | 234 |
263 void TraceEvent::AppendAsJSON(std::string* out) const { | 235 void TraceEvent::AppendAsJSON(std::string* out) const { |
264 const char* phase_str = GetPhaseString(phase_); | 236 const char phase_char = GetPhaseChar(phase_); |
265 int64 time_int64 = timestamp_.ToInternalValue(); | 237 int64 time_int64 = timestamp_.ToInternalValue(); |
238 int process_id = TraceLog::GetInstance()->process_id(); | |
266 // Category name checked at category creation time. | 239 // Category name checked at category creation time. |
267 DCHECK(!strchr(name_, '"')); | 240 DCHECK(!strchr(name_, '"')); |
268 StringAppendF(out, | 241 StringAppendF(out, |
269 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," | 242 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," |
270 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", | 243 "\"ph\":\"%c\",\"name\":\"%s\",\"args\":{", |
271 category_->name, | 244 category_->name, |
272 static_cast<int>(process_id_), | 245 process_id, |
273 static_cast<int>(thread_id_), | 246 thread_id_, |
274 static_cast<long long>(time_int64), | 247 static_cast<long long>(time_int64), |
275 phase_str, | 248 phase_char, |
276 name_); | 249 name_); |
277 | 250 |
278 // Output argument names and values, stop at first NULL argument name. | 251 // Output argument names and values, stop at first NULL argument name. |
279 for (size_t i = 0; i < kTraceMaxNumArgs && arg_names_[i]; ++i) { | 252 for (size_t i = 0; i < kTraceMaxNumArgs && arg_names_[i]; ++i) { |
280 if (i > 0) | 253 if (i > 0) |
281 *out += ","; | 254 *out += ","; |
282 *out += "\""; | 255 *out += "\""; |
283 *out += arg_names_[i]; | 256 *out += arg_names_[i]; |
284 *out += "\":"; | 257 *out += "\":"; |
285 arg_values_[i].AppendAsJSON(out); | 258 arg_values_[i].AppendAsJSON(out); |
286 } | 259 } |
287 *out += "}}"; | 260 *out += "}"; |
261 | |
262 // If id_ is set, print it out as a hex string so we don't loose any | |
263 // bits (it might be a 64-bit pointer). | |
264 if (flags_ & TRACE_EVENT_FLAG_HAS_ID) | |
265 StringAppendF(out, ",\"id\":\"%llx\"", | |
266 static_cast<unsigned long long>(id_.data())); | |
267 *out += "}"; | |
288 } | 268 } |
289 | 269 |
290 //////////////////////////////////////////////////////////////////////////////// | 270 //////////////////////////////////////////////////////////////////////////////// |
291 // | 271 // |
292 // TraceResultBuffer | 272 // TraceResultBuffer |
293 // | 273 // |
294 //////////////////////////////////////////////////////////////////////////////// | 274 //////////////////////////////////////////////////////////////////////////////// |
295 | 275 |
296 TraceResultBuffer::OutputCallback | 276 TraceResultBuffer::OutputCallback |
297 TraceResultBuffer::SimpleOutput::GetCallback() { | 277 TraceResultBuffer::SimpleOutput::GetCallback() { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 // | 315 // |
336 //////////////////////////////////////////////////////////////////////////////// | 316 //////////////////////////////////////////////////////////////////////////////// |
337 | 317 |
338 // static | 318 // static |
339 TraceLog* TraceLog::GetInstance() { | 319 TraceLog* TraceLog::GetInstance() { |
340 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); | 320 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); |
341 } | 321 } |
342 | 322 |
343 TraceLog::TraceLog() | 323 TraceLog::TraceLog() |
344 : enabled_(false) { | 324 : enabled_(false) { |
325 SetProcessID(static_cast<int>(base::GetCurrentProcId())); | |
345 } | 326 } |
346 | 327 |
347 TraceLog::~TraceLog() { | 328 TraceLog::~TraceLog() { |
348 } | 329 } |
349 | 330 |
350 const TraceCategory* TraceLog::GetCategory(const char* name) { | 331 const TraceCategory* TraceLog::GetCategory(const char* name) { |
351 TraceLog* tracelog = GetInstance(); | 332 TraceLog* tracelog = GetInstance(); |
352 if (!tracelog){ | 333 if (!tracelog){ |
353 DCHECK(!g_category_already_shutdown->enabled); | 334 DCHECK(!g_category_already_shutdown->enabled); |
354 return g_category_already_shutdown; | 335 return g_category_already_shutdown; |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 i, | 507 i, |
527 kTraceEventBatchSize, | 508 kTraceEventBatchSize, |
528 &(json_events_str_ptr->data)); | 509 &(json_events_str_ptr->data)); |
529 output_callback_copy.Run(json_events_str_ptr); | 510 output_callback_copy.Run(json_events_str_ptr); |
530 } | 511 } |
531 } | 512 } |
532 | 513 |
533 int TraceLog::AddTraceEvent(TraceEventPhase phase, | 514 int TraceLog::AddTraceEvent(TraceEventPhase phase, |
534 const TraceCategory* category, | 515 const TraceCategory* category, |
535 const char* name, | 516 const char* name, |
517 TraceID id, | |
536 const char* arg1_name, TraceValue arg1_val, | 518 const char* arg1_name, TraceValue arg1_val, |
537 const char* arg2_name, TraceValue arg2_val, | 519 const char* arg2_name, TraceValue arg2_val, |
538 int threshold_begin_id, | 520 int threshold_begin_id, |
539 int64 threshold, | 521 int64 threshold, |
540 EventFlags flags) { | 522 TraceEventFlags flags) { |
541 DCHECK(name); | 523 DCHECK(name); |
542 TimeTicks now = TimeTicks::HighResNow(); | 524 TimeTicks now = TimeTicks::HighResNow(); |
543 BufferFullCallback buffer_full_callback_copy; | 525 BufferFullCallback buffer_full_callback_copy; |
544 int ret_begin_id = -1; | 526 int ret_begin_id = -1; |
545 { | 527 { |
546 AutoLock lock(lock_); | 528 AutoLock lock(lock_); |
547 if (!category->enabled) | 529 if (!category->enabled) |
548 return -1; | 530 return -1; |
549 if (logged_events_.size() >= kTraceEventBufferSize) | 531 if (logged_events_.size() >= kTraceEventBufferSize) |
550 return -1; | 532 return -1; |
551 | 533 |
552 PlatformThreadId thread_id = PlatformThread::CurrentId(); | 534 int thread_id = static_cast<int>(PlatformThread::CurrentId()); |
553 | 535 |
554 const char* new_name = PlatformThread::GetName(); | 536 const char* new_name = PlatformThread::GetName(); |
555 // Check if the thread name has been set or changed since the previous | 537 // Check if the thread name has been set or changed since the previous |
556 // call (if any), but don't bother if the new name is empty. Note this will | 538 // call (if any), but don't bother if the new name is empty. Note this will |
557 // not detect a thread name change within the same char* buffer address: we | 539 // not detect a thread name change within the same char* buffer address: we |
558 // favor common case performance over corner case correctness. | 540 // favor common case performance over corner case correctness. |
559 if (new_name != g_current_thread_name.Get().Get() && | 541 if (new_name != g_current_thread_name.Get().Get() && |
560 new_name && *new_name) { | 542 new_name && *new_name) { |
561 // Benign const cast. | 543 // Benign const cast. |
562 g_current_thread_name.Get().Set(const_cast<char*>(new_name)); | 544 g_current_thread_name.Get().Set(const_cast<char*>(new_name)); |
563 base::hash_map<PlatformThreadId, std::string>::iterator existing_name = | 545 base::hash_map<int, std::string>::iterator existing_name = |
564 thread_names_.find(thread_id); | 546 thread_names_.find(thread_id); |
565 if (existing_name == thread_names_.end()) { | 547 if (existing_name == thread_names_.end()) { |
566 // This is a new thread id, and a new name. | 548 // This is a new thread id, and a new name. |
567 thread_names_[thread_id] = new_name; | 549 thread_names_[thread_id] = new_name; |
568 } else { | 550 } else { |
569 // This is a thread id that we've seen before, but potentially with a | 551 // This is a thread id that we've seen before, but potentially with a |
570 // new name. | 552 // new name. |
571 std::vector<base::StringPiece> existing_names; | 553 std::vector<base::StringPiece> existing_names; |
572 Tokenize(existing_name->second, ",", &existing_names); | 554 Tokenize(existing_name->second, ",", &existing_names); |
573 bool found = std::find(existing_names.begin(), | 555 bool found = std::find(existing_names.begin(), |
574 existing_names.end(), | 556 existing_names.end(), |
575 new_name) != existing_names.end(); | 557 new_name) != existing_names.end(); |
576 if (!found) { | 558 if (!found) { |
577 existing_name->second.push_back(','); | 559 existing_name->second.push_back(','); |
578 existing_name->second.append(new_name); | 560 existing_name->second.append(new_name); |
579 } | 561 } |
580 } | 562 } |
581 } | 563 } |
582 | 564 |
583 if (threshold_begin_id > -1) { | 565 if (threshold_begin_id > -1) { |
584 DCHECK(phase == base::debug::TRACE_EVENT_PHASE_END); | 566 DCHECK(phase == TRACE_EVENT_PHASE_END); |
585 size_t begin_i = static_cast<size_t>(threshold_begin_id); | 567 size_t begin_i = static_cast<size_t>(threshold_begin_id); |
586 // Return now if there has been a flush since the begin event was posted. | 568 // Return now if there has been a flush since the begin event was posted. |
587 if (begin_i >= logged_events_.size()) | 569 if (begin_i >= logged_events_.size()) |
588 return -1; | 570 return -1; |
589 // Determine whether to drop the begin/end pair. | 571 // Determine whether to drop the begin/end pair. |
590 TimeDelta elapsed = now - logged_events_[begin_i].timestamp(); | 572 TimeDelta elapsed = now - logged_events_[begin_i].timestamp(); |
591 if (elapsed < TimeDelta::FromMicroseconds(threshold)) { | 573 if (elapsed < TimeDelta::FromMicroseconds(threshold)) { |
592 // Remove begin event and do not add end event. | 574 // Remove begin event and do not add end event. |
593 // This will be expensive if there have been other events in the | 575 // This will be expensive if there have been other events in the |
594 // mean time (should be rare). | 576 // mean time (should be rare). |
595 logged_events_.erase(logged_events_.begin() + begin_i); | 577 logged_events_.erase(logged_events_.begin() + begin_i); |
596 return -1; | 578 return -1; |
597 } | 579 } |
598 } | 580 } |
599 ret_begin_id = static_cast<int>(logged_events_.size()); | 581 ret_begin_id = static_cast<int>(logged_events_.size()); |
600 logged_events_.push_back( | 582 logged_events_.push_back( |
601 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), | 583 TraceEvent(thread_id, |
602 thread_id, | 584 now, phase, category, name, id, |
603 now, phase, category, name, | |
604 arg1_name, arg1_val, | 585 arg1_name, arg1_val, |
605 arg2_name, arg2_val, | 586 arg2_name, arg2_val, |
606 flags & EVENT_FLAG_COPY)); | 587 flags)); |
607 | 588 |
608 if (logged_events_.size() == kTraceEventBufferSize) { | 589 if (logged_events_.size() == kTraceEventBufferSize) { |
609 buffer_full_callback_copy = buffer_full_callback_; | 590 buffer_full_callback_copy = buffer_full_callback_; |
610 } | 591 } |
611 } // release lock | 592 } // release lock |
612 | 593 |
613 if (!buffer_full_callback_copy.is_null()) | 594 if (!buffer_full_callback_copy.is_null()) |
614 buffer_full_callback_copy.Run(); | 595 buffer_full_callback_copy.Run(); |
615 | 596 |
616 return ret_begin_id; | 597 return ret_begin_id; |
617 } | 598 } |
618 | 599 |
619 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, | 600 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, |
620 const char* name, | 601 const char* name, |
621 const void* id, | 602 const void* id, |
622 const char* extra) { | 603 const char* extra) { |
623 #if defined(OS_WIN) | 604 #if defined(OS_WIN) |
624 TraceEventETWProvider::Trace(name, phase, id, extra); | 605 TraceEventETWProvider::Trace(name, phase, id, extra); |
625 #endif | 606 #endif |
626 INTERNAL_TRACE_EVENT_ADD(phase, | 607 INTERNAL_TRACE_EVENT_ADD(phase, |
627 "ETW Trace Event", name, "id", id, "extra", TRACE_STR_COPY(extra), | 608 "ETW Trace Event", name, "id", id, "extra", TRACE_STR_COPY(extra), |
628 base::debug::TraceLog::EVENT_FLAG_COPY); | 609 TRACE_EVENT_FLAG_COPY); |
629 } | 610 } |
630 | 611 |
631 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, | 612 void TraceLog::AddTraceEventEtw(TraceEventPhase phase, |
632 const char* name, | 613 const char* name, |
633 const void* id, | 614 const void* id, |
634 const std::string& extra) | 615 const std::string& extra) |
635 { | 616 { |
636 #if defined(OS_WIN) | 617 #if defined(OS_WIN) |
637 TraceEventETWProvider::Trace(name, phase, id, extra); | 618 TraceEventETWProvider::Trace(name, phase, id, extra); |
638 #endif | 619 #endif |
639 INTERNAL_TRACE_EVENT_ADD(phase, | 620 INTERNAL_TRACE_EVENT_ADD(phase, |
640 "ETW Trace Event", name, "id", id, "extra", extra, | 621 "ETW Trace Event", name, "id", id, "extra", extra, |
641 base::debug::TraceLog::EVENT_FLAG_COPY); | 622 TRACE_EVENT_FLAG_COPY); |
642 } | 623 } |
643 | 624 |
644 int TraceLog::AddCounterEvent(const TraceCategory* category, | 625 int TraceLog::AddCounterEvent(const TraceCategory* category, |
645 const char* name, | 626 const char* name, |
646 const char* value1_name, int32 value1_val, | 627 const char* value1_name, int32 value1_val, |
647 const char* value2_name, int32 value2_val, | 628 const char* value2_name, int32 value2_val, |
648 EventFlags flags) { | 629 TraceEventFlags flags) { |
649 return AddTraceEvent(TRACE_EVENT_PHASE_COUNTER, | 630 return AddTraceEvent(TRACE_EVENT_PHASE_COUNTER, |
650 category, | 631 category, |
651 name, | 632 name, |
633 0, | |
652 value1_name, value1_val, | 634 value1_name, value1_val, |
653 value2_name, value2_val, | 635 value2_name, value2_val, |
654 -1, 0, | 636 -1, 0, |
655 flags); | 637 flags); |
656 } | 638 } |
657 | 639 |
658 void TraceLog::AddCurrentMetadataEvents() { | 640 void TraceLog::AddCurrentMetadataEvents() { |
659 lock_.AssertAcquired(); | 641 lock_.AssertAcquired(); |
660 for(base::hash_map<PlatformThreadId, std::string>::iterator it = | 642 for(base::hash_map<int, std::string>::iterator it = thread_names_.begin(); |
661 thread_names_.begin(); | |
662 it != thread_names_.end(); | 643 it != thread_names_.end(); |
663 it++) { | 644 it++) { |
664 if (!it->second.empty()) | 645 if (!it->second.empty()) |
665 logged_events_.push_back( | 646 logged_events_.push_back( |
666 TraceEvent(static_cast<unsigned long>(base::GetCurrentProcId()), | 647 TraceEvent(it->first, |
667 it->first, | 648 TimeTicks(), TRACE_EVENT_PHASE_METADATA, |
668 TimeTicks(), base::debug::TRACE_EVENT_PHASE_METADATA, | 649 g_category_metadata, "thread_name", 0, |
669 g_category_metadata, "thread_name", | |
670 "name", it->second, | 650 "name", it->second, |
671 NULL, 0, | 651 NULL, 0, |
672 false)); | 652 TRACE_EVENT_FLAG_NONE)); |
673 } | 653 } |
674 } | 654 } |
675 | 655 |
676 void TraceLog::DeleteForTesting() { | 656 void TraceLog::DeleteForTesting() { |
677 DeleteTraceLogForTesting::Delete(); | 657 DeleteTraceLogForTesting::Delete(); |
678 } | 658 } |
679 | 659 |
680 void TraceLog::Resurrect() { | 660 void TraceLog::Resurrect() { |
681 StaticMemorySingletonTraits<TraceLog>::Resurrect(); | 661 StaticMemorySingletonTraits<TraceLog>::Resurrect(); |
682 } | 662 } |
683 | 663 |
664 void TraceLog::SetProcessID(int process_id) { | |
665 process_id_ = process_id; | |
666 // Create a hash from the process ID for XORing. | |
667 unsigned long long pid = static_cast<unsigned long long>(process_id_); | |
668 process_id_hash_ = (14695981039346656037ull ^ pid) * 1099511628211ull; | |
jar (doing other things)
2011/12/01 01:47:49
nit: Add a comment (url?) as a reference, or menti
jbates
2011/12/01 23:07:50
Done.
| |
669 } | |
670 | |
684 namespace internal { | 671 namespace internal { |
685 | 672 |
686 void TraceEndOnScopeClose::Initialize(const TraceCategory* category, | 673 void TraceEndOnScopeClose::Initialize(const TraceCategory* category, |
687 const char* name) { | 674 const char* name) { |
688 data_.category = category; | 675 data_.category = category; |
689 data_.name = name; | 676 data_.name = name; |
690 p_data_ = &data_; | 677 p_data_ = &data_; |
691 } | 678 } |
692 | 679 |
693 void TraceEndOnScopeClose::AddEventIfEnabled() { | 680 void TraceEndOnScopeClose::AddEventIfEnabled() { |
694 // Only called when p_data_ is non-null. | 681 // Only called when p_data_ is non-null. |
695 if (p_data_->category->enabled) { | 682 if (p_data_->category->enabled) { |
696 base::debug::TraceLog::GetInstance()->AddTraceEvent( | 683 base::debug::TraceLog::GetInstance()->AddTraceEvent( |
697 base::debug::TRACE_EVENT_PHASE_END, | 684 TRACE_EVENT_PHASE_END, |
698 p_data_->category, | 685 p_data_->category, |
699 p_data_->name, | 686 p_data_->name, 0, |
jar (doing other things)
2011/12/01 01:47:49
It would really help readability if you assigned t
jbates
2011/12/01 23:07:50
Done.
| |
700 NULL, 0, NULL, 0, | 687 NULL, 0, NULL, 0, |
701 -1, 0, TraceLog::EVENT_FLAG_NONE); | 688 -1, 0, TRACE_EVENT_FLAG_NONE); |
702 } | 689 } |
703 } | 690 } |
704 | 691 |
705 void TraceEndOnScopeCloseThreshold::Initialize(const TraceCategory* category, | 692 void TraceEndOnScopeCloseThreshold::Initialize(const TraceCategory* category, |
706 const char* name, | 693 const char* name, |
707 int threshold_begin_id, | 694 int threshold_begin_id, |
708 int64 threshold) { | 695 int64 threshold) { |
709 data_.category = category; | 696 data_.category = category; |
710 data_.name = name; | 697 data_.name = name; |
711 data_.threshold_begin_id = threshold_begin_id; | 698 data_.threshold_begin_id = threshold_begin_id; |
712 data_.threshold = threshold; | 699 data_.threshold = threshold; |
713 p_data_ = &data_; | 700 p_data_ = &data_; |
714 } | 701 } |
715 | 702 |
716 void TraceEndOnScopeCloseThreshold::AddEventIfEnabled() { | 703 void TraceEndOnScopeCloseThreshold::AddEventIfEnabled() { |
717 // Only called when p_data_ is non-null. | 704 // Only called when p_data_ is non-null. |
718 if (p_data_->category->enabled) { | 705 if (p_data_->category->enabled) { |
719 base::debug::TraceLog::GetInstance()->AddTraceEvent( | 706 base::debug::TraceLog::GetInstance()->AddTraceEvent( |
720 base::debug::TRACE_EVENT_PHASE_END, | 707 TRACE_EVENT_PHASE_END, |
721 p_data_->category, | 708 p_data_->category, |
722 p_data_->name, | 709 p_data_->name, 0, |
723 NULL, 0, NULL, 0, | 710 NULL, 0, NULL, 0, |
724 p_data_->threshold_begin_id, p_data_->threshold, | 711 p_data_->threshold_begin_id, p_data_->threshold, |
725 TraceLog::EVENT_FLAG_NONE); | 712 TRACE_EVENT_FLAG_NONE); |
726 } | 713 } |
727 } | 714 } |
728 | 715 |
729 } // namespace internal | 716 } // namespace internal |
730 | 717 |
731 } // namespace debug | 718 } // namespace debug |
732 } // namespace base | 719 } // namespace base |
OLD | NEW |