OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_impl.h" | 5 #include "base/debug/trace_event_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 if (!(ret & RECORD_UNTIL_FULL) && !(ret & RECORD_CONTINUOUSLY)) | 768 if (!(ret & RECORD_UNTIL_FULL) && !(ret & RECORD_CONTINUOUSLY)) |
769 ret |= RECORD_UNTIL_FULL; // Default when no options are specified. | 769 ret |= RECORD_UNTIL_FULL; // Default when no options are specified. |
770 | 770 |
771 return static_cast<Options>(ret); | 771 return static_cast<Options>(ret); |
772 } | 772 } |
773 | 773 |
774 TraceLog::TraceLog() | 774 TraceLog::TraceLog() |
775 : enable_count_(0), | 775 : enable_count_(0), |
776 num_traces_recorded_(0), | 776 num_traces_recorded_(0), |
777 dispatching_to_observer_list_(false), | 777 dispatching_to_observer_list_(false), |
| 778 process_sort_index_(0), |
778 watch_category_(NULL), | 779 watch_category_(NULL), |
779 trace_options_(RECORD_UNTIL_FULL), | 780 trace_options_(RECORD_UNTIL_FULL), |
780 sampling_thread_handle_(0), | 781 sampling_thread_handle_(0), |
781 category_filter_(CategoryFilter::kDefaultCategoryFilterString) { | 782 category_filter_(CategoryFilter::kDefaultCategoryFilterString) { |
782 // Trace is enabled or disabled on one thread while other threads are | 783 // Trace is enabled or disabled on one thread while other threads are |
783 // accessing the enabled flag. We don't care whether edge-case events are | 784 // accessing the enabled flag. We don't care whether edge-case events are |
784 // traced or not, so we allow races on the enabled flag to keep the trace | 785 // traced or not, so we allow races on the enabled flag to keep the trace |
785 // macros fast. | 786 // macros fast. |
786 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: | 787 // TODO(jbates): ANNOTATE_BENIGN_RACE_SIZED crashes windows TSAN bots: |
787 // ANNOTATE_BENIGN_RACE_SIZED(g_category_group_enabled, | 788 // ANNOTATE_BENIGN_RACE_SIZED(g_category_group_enabled, |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1004 lock_.Acquire(); | 1005 lock_.Acquire(); |
1005 sampling_thread_handle_ = PlatformThreadHandle(); | 1006 sampling_thread_handle_ = PlatformThreadHandle(); |
1006 sampling_thread_.reset(); | 1007 sampling_thread_.reset(); |
1007 } | 1008 } |
1008 | 1009 |
1009 category_filter_.Clear(); | 1010 category_filter_.Clear(); |
1010 watch_category_ = NULL; | 1011 watch_category_ = NULL; |
1011 watch_event_name_ = ""; | 1012 watch_event_name_ = ""; |
1012 for (int i = 0; i < g_category_index; i++) | 1013 for (int i = 0; i < g_category_index; i++) |
1013 SetCategoryGroupEnabled(i, false); | 1014 SetCategoryGroupEnabled(i, false); |
1014 AddThreadNameMetadataEvents(); | 1015 AddMetadataEvents(); |
1015 | 1016 |
1016 dispatching_to_observer_list_ = true; | 1017 dispatching_to_observer_list_ = true; |
1017 observer_list = enabled_state_observer_list_; | 1018 observer_list = enabled_state_observer_list_; |
1018 } | 1019 } |
1019 | 1020 |
1020 // Dispatch to observers outside the lock in case the observer triggers a | 1021 // Dispatch to observers outside the lock in case the observer triggers a |
1021 // trace event. | 1022 // trace event. |
1022 for (size_t i = 0; i < observer_list.size(); ++i) | 1023 for (size_t i = 0; i < observer_list.size(); ++i) |
1023 observer_list[i]->OnTraceLogDisabled(); | 1024 observer_list[i]->OnTraceLogDisabled(); |
1024 | 1025 |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 notifier.SendNotificationIfAny(); | 1288 notifier.SendNotificationIfAny(); |
1288 } | 1289 } |
1289 } | 1290 } |
1290 | 1291 |
1291 void TraceLog::CancelWatchEvent() { | 1292 void TraceLog::CancelWatchEvent() { |
1292 AutoLock lock(lock_); | 1293 AutoLock lock(lock_); |
1293 watch_category_ = NULL; | 1294 watch_category_ = NULL; |
1294 watch_event_name_ = ""; | 1295 watch_event_name_ = ""; |
1295 } | 1296 } |
1296 | 1297 |
1297 void TraceLog::AddThreadNameMetadataEvents() { | 1298 namespace { |
| 1299 |
| 1300 template <typename T> |
| 1301 void AddMetadataEventToBuffer( |
| 1302 TraceBuffer* logged_events, |
| 1303 int thread_id, |
| 1304 const char* metadata_name, const char* arg_name, |
| 1305 const T& value) { |
| 1306 int num_args = 1; |
| 1307 unsigned char arg_type; |
| 1308 unsigned long long arg_value; |
| 1309 trace_event_internal::SetTraceValue(value, &arg_type, &arg_value); |
| 1310 logged_events->AddEvent(TraceEvent(thread_id, |
| 1311 TimeTicks(), TRACE_EVENT_PHASE_METADATA, |
| 1312 &g_category_group_enabled[g_category_metadata], |
| 1313 metadata_name, trace_event_internal::kNoEventId, |
| 1314 num_args, &arg_name, &arg_type, &arg_value, NULL, |
| 1315 TRACE_EVENT_FLAG_NONE)); |
| 1316 } |
| 1317 |
| 1318 } |
| 1319 |
| 1320 void TraceLog::AddMetadataEvents() { |
1298 lock_.AssertAcquired(); | 1321 lock_.AssertAcquired(); |
| 1322 |
| 1323 int current_thread_id = static_cast<int>(base::PlatformThread::CurrentId()); |
| 1324 if (process_sort_index_ != 0) { |
| 1325 AddMetadataEventToBuffer(logged_events_.get(), |
| 1326 current_thread_id, |
| 1327 "process_sort_index", "sort_index", |
| 1328 process_sort_index_); |
| 1329 } |
| 1330 |
| 1331 // Process labels are joined together with commas to form a process_name. |
| 1332 if (process_labels_.size() > 0) { |
| 1333 std::vector<std::string> all_labels; |
| 1334 for(std::map<int, std::string>::iterator it = process_labels_.begin(); |
| 1335 it != process_labels_.end(); |
| 1336 it++) { |
| 1337 if (it->second.empty()) |
| 1338 continue; |
| 1339 all_labels.push_back(it->second); |
| 1340 } |
| 1341 AddMetadataEventToBuffer(logged_events_.get(), |
| 1342 current_thread_id, |
| 1343 "process_name", "name", |
| 1344 JoinString(all_labels, ',')); |
| 1345 } |
| 1346 |
| 1347 // Thread sort indices. |
| 1348 for(hash_map<int, int>::iterator it = thread_sort_indices_.begin(); |
| 1349 it != thread_sort_indices_.end(); |
| 1350 it++) { |
| 1351 if (it->second == 0) |
| 1352 continue; |
| 1353 AddMetadataEventToBuffer(logged_events_.get(), |
| 1354 it->first, |
| 1355 "thread_sort_index", "sort_index", |
| 1356 it->second); |
| 1357 } |
| 1358 |
| 1359 // Thread names. |
1299 for(hash_map<int, std::string>::iterator it = thread_names_.begin(); | 1360 for(hash_map<int, std::string>::iterator it = thread_names_.begin(); |
1300 it != thread_names_.end(); | 1361 it != thread_names_.end(); |
1301 it++) { | 1362 it++) { |
1302 if (!it->second.empty()) { | 1363 if (it->second.empty()) |
1303 int num_args = 1; | 1364 continue; |
1304 const char* arg_name = "name"; | 1365 AddMetadataEventToBuffer(logged_events_.get(), |
1305 unsigned char arg_type; | 1366 it->first, |
1306 unsigned long long arg_value; | 1367 "thread_name", "name", |
1307 trace_event_internal::SetTraceValue(it->second, &arg_type, &arg_value); | 1368 it->second); |
1308 logged_events_->AddEvent(TraceEvent(it->first, | |
1309 TimeTicks(), TRACE_EVENT_PHASE_METADATA, | |
1310 &g_category_group_enabled[g_category_metadata], | |
1311 "thread_name", trace_event_internal::kNoEventId, | |
1312 num_args, &arg_name, &arg_type, &arg_value, NULL, | |
1313 TRACE_EVENT_FLAG_NONE)); | |
1314 } | |
1315 } | 1369 } |
1316 } | 1370 } |
1317 | 1371 |
1318 void TraceLog::InstallWaitableEventForSamplingTesting( | 1372 void TraceLog::InstallWaitableEventForSamplingTesting( |
1319 WaitableEvent* waitable_event) { | 1373 WaitableEvent* waitable_event) { |
1320 sampling_thread_->InstallWaitableEventForSamplingTesting(waitable_event); | 1374 sampling_thread_->InstallWaitableEventForSamplingTesting(waitable_event); |
1321 } | 1375 } |
1322 | 1376 |
1323 void TraceLog::DeleteForTesting() { | 1377 void TraceLog::DeleteForTesting() { |
1324 DeleteTraceLogForTesting::Delete(); | 1378 DeleteTraceLogForTesting::Delete(); |
1325 } | 1379 } |
1326 | 1380 |
1327 void TraceLog::Resurrect() { | 1381 void TraceLog::Resurrect() { |
1328 StaticMemorySingletonTraits<TraceLog>::Resurrect(); | 1382 StaticMemorySingletonTraits<TraceLog>::Resurrect(); |
1329 } | 1383 } |
1330 | 1384 |
1331 void TraceLog::SetProcessID(int process_id) { | 1385 void TraceLog::SetProcessID(int process_id) { |
1332 process_id_ = process_id; | 1386 process_id_ = process_id; |
1333 // Create a FNV hash from the process ID for XORing. | 1387 // Create a FNV hash from the process ID for XORing. |
1334 // See http://isthe.com/chongo/tech/comp/fnv/ for algorithm details. | 1388 // See http://isthe.com/chongo/tech/comp/fnv/ for algorithm details. |
1335 unsigned long long offset_basis = 14695981039346656037ull; | 1389 unsigned long long offset_basis = 14695981039346656037ull; |
1336 unsigned long long fnv_prime = 1099511628211ull; | 1390 unsigned long long fnv_prime = 1099511628211ull; |
1337 unsigned long long pid = static_cast<unsigned long long>(process_id_); | 1391 unsigned long long pid = static_cast<unsigned long long>(process_id_); |
1338 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; | 1392 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; |
1339 } | 1393 } |
1340 | 1394 |
| 1395 void TraceLog::SetProcessSortIndex(int sort_index) { |
| 1396 AutoLock lock(lock_); |
| 1397 process_sort_index_ = sort_index; |
| 1398 } |
| 1399 |
| 1400 void TraceLog::SetProcessLabel( |
| 1401 int label_id, const std::string& current_label) { |
| 1402 AutoLock lock(lock_); |
| 1403 if (current_label.length() == 0) { |
| 1404 std::map<int, std::string>::iterator it = process_labels_.find( |
| 1405 label_id); |
| 1406 if (it != process_labels_.end()) |
| 1407 process_labels_.erase(it); |
| 1408 return; |
| 1409 } |
| 1410 process_labels_[label_id] = current_label; |
| 1411 } |
| 1412 |
| 1413 void TraceLog::SetThreadSortIndex(int thread_id, int sort_index) { |
| 1414 AutoLock lock(lock_); |
| 1415 thread_sort_indices_[thread_id] = sort_index; |
| 1416 } |
| 1417 |
1341 void TraceLog::SetTimeOffset(TimeDelta offset) { | 1418 void TraceLog::SetTimeOffset(TimeDelta offset) { |
1342 time_offset_ = offset; | 1419 time_offset_ = offset; |
1343 } | 1420 } |
1344 | 1421 |
1345 size_t TraceLog::GetObserverCountForTest() const { | 1422 size_t TraceLog::GetObserverCountForTest() const { |
1346 return enabled_state_observer_list_.size(); | 1423 return enabled_state_observer_list_.size(); |
1347 } | 1424 } |
1348 | 1425 |
1349 bool CategoryFilter::IsEmptyOrContainsLeadingOrTrailingWhitespace( | 1426 bool CategoryFilter::IsEmptyOrContainsLeadingOrTrailingWhitespace( |
1350 const std::string& str) { | 1427 const std::string& str) { |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1546 0, // num_args | 1623 0, // num_args |
1547 NULL, // arg_names | 1624 NULL, // arg_names |
1548 NULL, // arg_types | 1625 NULL, // arg_types |
1549 NULL, // arg_values | 1626 NULL, // arg_values |
1550 NULL, // convertable values | 1627 NULL, // convertable values |
1551 TRACE_EVENT_FLAG_NONE); // flags | 1628 TRACE_EVENT_FLAG_NONE); // flags |
1552 } | 1629 } |
1553 } | 1630 } |
1554 | 1631 |
1555 } // namespace trace_event_internal | 1632 } // namespace trace_event_internal |
OLD | NEW |