| 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/base_switches.h" | 9 #include "base/base_switches.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 namespace base { | 52 namespace base { |
| 53 namespace debug { | 53 namespace debug { |
| 54 | 54 |
| 55 namespace { | 55 namespace { |
| 56 | 56 |
| 57 // The overhead of TraceEvent above this threshold will be reported in the | 57 // The overhead of TraceEvent above this threshold will be reported in the |
| 58 // trace. | 58 // trace. |
| 59 const int kOverheadReportThresholdInMicroseconds = 50; | 59 const int kOverheadReportThresholdInMicroseconds = 50; |
| 60 | 60 |
| 61 // String options that can be used to initialize TraceOptions. |
| 62 const char kRecordUntilFull[] = "record-until-full"; |
| 63 const char kRecordContinuously[] = "record-continuously"; |
| 64 const char kTraceToConsole[] = "trace-to-console"; |
| 65 const char kEnableSampling[] = "enable-sampling"; |
| 66 const char kEnableSystrace[] = "enable-systrace"; |
| 67 |
| 61 // Controls the number of trace events we will buffer in-memory | 68 // Controls the number of trace events we will buffer in-memory |
| 62 // before throwing them away. | 69 // before throwing them away. |
| 63 const size_t kTraceBufferChunkSize = TraceBufferChunk::kTraceBufferChunkSize; | 70 const size_t kTraceBufferChunkSize = TraceBufferChunk::kTraceBufferChunkSize; |
| 64 const size_t kTraceEventVectorBufferChunks = 256000 / kTraceBufferChunkSize; | 71 const size_t kTraceEventVectorBufferChunks = 256000 / kTraceBufferChunkSize; |
| 65 const size_t kTraceEventRingBufferChunks = kTraceEventVectorBufferChunks / 4; | 72 const size_t kTraceEventRingBufferChunks = kTraceEventVectorBufferChunks / 4; |
| 66 const size_t kTraceEventBatchChunks = 1000 / kTraceBufferChunkSize; | 73 const size_t kTraceEventBatchChunks = 1000 / kTraceBufferChunkSize; |
| 67 // Can store results for 30 seconds with 1 ms sampling interval. | 74 // Can store results for 30 seconds with 1 ms sampling interval. |
| 68 const size_t kMonitorTraceEventBufferChunks = 30000 / kTraceBufferChunkSize; | 75 const size_t kMonitorTraceEventBufferChunks = 30000 / kTraceBufferChunkSize; |
| 69 // ECHO_TO_CONSOLE needs a small buffer to hold the unfinished COMPLETE events. | 76 // ECHO_TO_CONSOLE needs a small buffer to hold the unfinished COMPLETE events. |
| 70 const size_t kEchoToConsoleTraceEventBufferChunks = 256; | 77 const size_t kEchoToConsoleTraceEventBufferChunks = 256; |
| (...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 963 : bucket(bucket), | 970 : bucket(bucket), |
| 964 bucket_name(name), | 971 bucket_name(name), |
| 965 callback(callback) { | 972 callback(callback) { |
| 966 } | 973 } |
| 967 | 974 |
| 968 TraceBucketData::~TraceBucketData() { | 975 TraceBucketData::~TraceBucketData() { |
| 969 } | 976 } |
| 970 | 977 |
| 971 //////////////////////////////////////////////////////////////////////////////// | 978 //////////////////////////////////////////////////////////////////////////////// |
| 972 // | 979 // |
| 980 // TraceOptions |
| 981 // |
| 982 //////////////////////////////////////////////////////////////////////////////// |
| 983 |
| 984 TraceOptions::TraceOptions(StringPiece options) |
| 985 : record_mode(RECORD_UNTIL_FULL), |
| 986 enable_sampling(false), |
| 987 enable_systrace(false) { |
| 988 std::vector<std::string> split; |
| 989 std::vector<std::string>::iterator iter; |
| 990 |
| 991 base::SplitString(options.as_string(), ',', &split); |
| 992 for (iter = split.begin(); iter != split.end(); ++iter) { |
| 993 if (*iter == kRecordUntilFull) { |
| 994 record_mode = RECORD_UNTIL_FULL; |
| 995 } else if (*iter == kRecordContinuously) { |
| 996 record_mode = RECORD_CONTINUOUSLY; |
| 997 } else if (*iter == kTraceToConsole) { |
| 998 record_mode = ECHO_TO_CONSOLE; |
| 999 } else if (*iter == kEnableSampling) { |
| 1000 enable_sampling = true; |
| 1001 } else if (*iter == kEnableSystrace) { |
| 1002 enable_systrace = true; |
| 1003 } |
| 1004 } |
| 1005 } |
| 1006 |
| 1007 std::string TraceOptions::ToString() const { |
| 1008 std::string ret; |
| 1009 switch (record_mode) { |
| 1010 case RECORD_UNTIL_FULL: |
| 1011 ret = kRecordUntilFull; |
| 1012 break; |
| 1013 case RECORD_CONTINUOUSLY: |
| 1014 ret = kRecordContinuously; |
| 1015 break; |
| 1016 case ECHO_TO_CONSOLE: |
| 1017 ret = kTraceToConsole; |
| 1018 break; |
| 1019 default: |
| 1020 NOTREACHED(); |
| 1021 } |
| 1022 if (enable_sampling) |
| 1023 ret = ret + "," + kEnableSampling; |
| 1024 if (enable_systrace) |
| 1025 ret = ret + "," + kEnableSystrace; |
| 1026 return ret; |
| 1027 } |
| 1028 |
| 1029 //////////////////////////////////////////////////////////////////////////////// |
| 1030 // |
| 973 // TraceLog | 1031 // TraceLog |
| 974 // | 1032 // |
| 975 //////////////////////////////////////////////////////////////////////////////// | 1033 //////////////////////////////////////////////////////////////////////////////// |
| 976 | 1034 |
| 977 class TraceLog::ThreadLocalEventBuffer | 1035 class TraceLog::ThreadLocalEventBuffer |
| 978 : public MessageLoop::DestructionObserver { | 1036 : public MessageLoop::DestructionObserver { |
| 979 public: | 1037 public: |
| 980 ThreadLocalEventBuffer(TraceLog* trace_log); | 1038 ThreadLocalEventBuffer(TraceLog* trace_log); |
| 981 virtual ~ThreadLocalEventBuffer(); | 1039 virtual ~ThreadLocalEventBuffer(); |
| 982 | 1040 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 switches::kTraceToConsole); | 1226 switches::kTraceToConsole); |
| 1169 if (filter.empty()) { | 1227 if (filter.empty()) { |
| 1170 filter = kEchoToConsoleCategoryFilter; | 1228 filter = kEchoToConsoleCategoryFilter; |
| 1171 } else { | 1229 } else { |
| 1172 filter.append(","); | 1230 filter.append(","); |
| 1173 filter.append(kEchoToConsoleCategoryFilter); | 1231 filter.append(kEchoToConsoleCategoryFilter); |
| 1174 } | 1232 } |
| 1175 | 1233 |
| 1176 LOG(ERROR) << "Start " << switches::kTraceToConsole | 1234 LOG(ERROR) << "Start " << switches::kTraceToConsole |
| 1177 << " with CategoryFilter '" << filter << "'."; | 1235 << " with CategoryFilter '" << filter << "'."; |
| 1178 SetEnabled(CategoryFilter(filter), RECORDING_MODE, ECHO_TO_CONSOLE); | 1236 SetEnabled(CategoryFilter(filter), |
| 1237 RECORDING_MODE, |
| 1238 TraceOptions(TraceOptions::ECHO_TO_CONSOLE)); |
| 1179 } | 1239 } |
| 1180 #endif | 1240 #endif |
| 1181 | 1241 |
| 1182 logged_events_.reset(CreateTraceBuffer()); | 1242 logged_events_.reset(CreateTraceBuffer()); |
| 1183 } | 1243 } |
| 1184 | 1244 |
| 1185 TraceLog::~TraceLog() { | 1245 TraceLog::~TraceLog() { |
| 1186 } | 1246 } |
| 1187 | 1247 |
| 1188 const unsigned char* TraceLog::GetCategoryGroupEnabled( | 1248 const unsigned char* TraceLog::GetCategoryGroupEnabled( |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 AutoLock lock(lock_); | 1378 AutoLock lock(lock_); |
| 1319 category_groups->push_back( | 1379 category_groups->push_back( |
| 1320 g_category_groups[g_category_trace_event_overhead]); | 1380 g_category_groups[g_category_trace_event_overhead]); |
| 1321 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index); | 1381 size_t category_index = base::subtle::NoBarrier_Load(&g_category_index); |
| 1322 for (size_t i = g_num_builtin_categories; i < category_index; i++) | 1382 for (size_t i = g_num_builtin_categories; i < category_index; i++) |
| 1323 category_groups->push_back(g_category_groups[i]); | 1383 category_groups->push_back(g_category_groups[i]); |
| 1324 } | 1384 } |
| 1325 | 1385 |
| 1326 void TraceLog::SetEnabled(const CategoryFilter& category_filter, | 1386 void TraceLog::SetEnabled(const CategoryFilter& category_filter, |
| 1327 Mode mode, | 1387 Mode mode, |
| 1328 Options options) { | 1388 TraceOptions options) { |
| 1329 std::vector<EnabledStateObserver*> observer_list; | 1389 std::vector<EnabledStateObserver*> observer_list; |
| 1330 { | 1390 { |
| 1331 AutoLock lock(lock_); | 1391 AutoLock lock(lock_); |
| 1332 | 1392 |
| 1333 // Can't enable tracing when Flush() is in progress. | 1393 // Can't enable tracing when Flush() is in progress. |
| 1334 DCHECK(!flush_message_loop_proxy_.get()); | 1394 DCHECK(!flush_message_loop_proxy_.get()); |
| 1335 | 1395 |
| 1336 Options old_options = trace_options(); | 1396 InternalTraceOptions new_options = |
| 1397 GetInternalOptionsFromTraceOptions(options); |
| 1398 |
| 1399 InternalTraceOptions old_options = trace_options(); |
| 1337 | 1400 |
| 1338 if (IsEnabled()) { | 1401 if (IsEnabled()) { |
| 1339 if (options != old_options) { | 1402 if (new_options != old_options) { |
| 1340 DLOG(ERROR) << "Attempting to re-enable tracing with a different " | 1403 DLOG(ERROR) << "Attempting to re-enable tracing with a different " |
| 1341 << "set of options."; | 1404 << "set of options."; |
| 1342 } | 1405 } |
| 1343 | 1406 |
| 1344 if (mode != mode_) { | 1407 if (mode != mode_) { |
| 1345 DLOG(ERROR) << "Attempting to re-enable tracing with a different mode."; | 1408 DLOG(ERROR) << "Attempting to re-enable tracing with a different mode."; |
| 1346 } | 1409 } |
| 1347 | 1410 |
| 1348 category_filter_.Merge(category_filter); | 1411 category_filter_.Merge(category_filter); |
| 1349 UpdateCategoryGroupEnabledFlags(); | 1412 UpdateCategoryGroupEnabledFlags(); |
| 1350 return; | 1413 return; |
| 1351 } | 1414 } |
| 1352 | 1415 |
| 1353 if (dispatching_to_observer_list_) { | 1416 if (dispatching_to_observer_list_) { |
| 1354 DLOG(ERROR) << | 1417 DLOG(ERROR) << |
| 1355 "Cannot manipulate TraceLog::Enabled state from an observer."; | 1418 "Cannot manipulate TraceLog::Enabled state from an observer."; |
| 1356 return; | 1419 return; |
| 1357 } | 1420 } |
| 1358 | 1421 |
| 1359 mode_ = mode; | 1422 mode_ = mode; |
| 1360 | 1423 |
| 1361 if (options != old_options) { | 1424 if (new_options != old_options) { |
| 1362 subtle::NoBarrier_Store(&trace_options_, options); | 1425 subtle::NoBarrier_Store(&trace_options_, new_options); |
| 1363 UseNextTraceBuffer(); | 1426 UseNextTraceBuffer(); |
| 1364 } | 1427 } |
| 1365 | 1428 |
| 1366 num_traces_recorded_++; | 1429 num_traces_recorded_++; |
| 1367 | 1430 |
| 1368 category_filter_ = CategoryFilter(category_filter); | 1431 category_filter_ = CategoryFilter(category_filter); |
| 1369 UpdateCategoryGroupEnabledFlags(); | 1432 UpdateCategoryGroupEnabledFlags(); |
| 1370 UpdateSyntheticDelaysFromCategoryFilter(); | 1433 UpdateSyntheticDelaysFromCategoryFilter(); |
| 1371 | 1434 |
| 1372 if (options & ENABLE_SAMPLING) { | 1435 if (new_options & ENABLE_SAMPLING) { |
| 1373 sampling_thread_.reset(new TraceSamplingThread); | 1436 sampling_thread_.reset(new TraceSamplingThread); |
| 1374 sampling_thread_->RegisterSampleBucket( | 1437 sampling_thread_->RegisterSampleBucket( |
| 1375 &g_trace_state[0], | 1438 &g_trace_state[0], |
| 1376 "bucket0", | 1439 "bucket0", |
| 1377 Bind(&TraceSamplingThread::DefaultSamplingCallback)); | 1440 Bind(&TraceSamplingThread::DefaultSamplingCallback)); |
| 1378 sampling_thread_->RegisterSampleBucket( | 1441 sampling_thread_->RegisterSampleBucket( |
| 1379 &g_trace_state[1], | 1442 &g_trace_state[1], |
| 1380 "bucket1", | 1443 "bucket1", |
| 1381 Bind(&TraceSamplingThread::DefaultSamplingCallback)); | 1444 Bind(&TraceSamplingThread::DefaultSamplingCallback)); |
| 1382 sampling_thread_->RegisterSampleBucket( | 1445 sampling_thread_->RegisterSampleBucket( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1395 // Notify observers outside the lock in case they trigger trace events. | 1458 // Notify observers outside the lock in case they trigger trace events. |
| 1396 for (size_t i = 0; i < observer_list.size(); ++i) | 1459 for (size_t i = 0; i < observer_list.size(); ++i) |
| 1397 observer_list[i]->OnTraceLogEnabled(); | 1460 observer_list[i]->OnTraceLogEnabled(); |
| 1398 | 1461 |
| 1399 { | 1462 { |
| 1400 AutoLock lock(lock_); | 1463 AutoLock lock(lock_); |
| 1401 dispatching_to_observer_list_ = false; | 1464 dispatching_to_observer_list_ = false; |
| 1402 } | 1465 } |
| 1403 } | 1466 } |
| 1404 | 1467 |
| 1468 TraceLog::InternalTraceOptions TraceLog::GetInternalOptionsFromTraceOptions( |
| 1469 const TraceOptions& options) { |
| 1470 InternalTraceOptions ret = options.enable_sampling ? ENABLE_SAMPLING : NONE; |
| 1471 switch (options.record_mode) { |
| 1472 case TraceOptions::RECORD_UNTIL_FULL: |
| 1473 return ret | RECORD_UNTIL_FULL; |
| 1474 case TraceOptions::RECORD_CONTINUOUSLY: |
| 1475 return ret | RECORD_CONTINUOUSLY; |
| 1476 case TraceOptions::ECHO_TO_CONSOLE: |
| 1477 return ret | ECHO_TO_CONSOLE; |
| 1478 } |
| 1479 NOTREACHED(); |
| 1480 return NONE; |
| 1481 } |
| 1482 |
| 1405 CategoryFilter TraceLog::GetCurrentCategoryFilter() { | 1483 CategoryFilter TraceLog::GetCurrentCategoryFilter() { |
| 1406 AutoLock lock(lock_); | 1484 AutoLock lock(lock_); |
| 1407 return category_filter_; | 1485 return category_filter_; |
| 1408 } | 1486 } |
| 1409 | 1487 |
| 1488 TraceOptions TraceLog::GetCurrentTraceOptions() const { |
| 1489 TraceOptions ret; |
| 1490 InternalTraceOptions option = trace_options(); |
| 1491 ret.enable_sampling = (option & ENABLE_SAMPLING) != 0; |
| 1492 if (option & RECORD_UNTIL_FULL) |
| 1493 ret.record_mode = TraceOptions::RECORD_UNTIL_FULL; |
| 1494 else if (option & RECORD_CONTINUOUSLY) |
| 1495 ret.record_mode = TraceOptions::RECORD_CONTINUOUSLY; |
| 1496 else if (option & ECHO_TO_CONSOLE) |
| 1497 ret.record_mode = TraceOptions::ECHO_TO_CONSOLE; |
| 1498 else |
| 1499 NOTREACHED(); |
| 1500 return ret; |
| 1501 } |
| 1502 |
| 1410 void TraceLog::SetDisabled() { | 1503 void TraceLog::SetDisabled() { |
| 1411 AutoLock lock(lock_); | 1504 AutoLock lock(lock_); |
| 1412 SetDisabledWhileLocked(); | 1505 SetDisabledWhileLocked(); |
| 1413 } | 1506 } |
| 1414 | 1507 |
| 1415 void TraceLog::SetDisabledWhileLocked() { | 1508 void TraceLog::SetDisabledWhileLocked() { |
| 1416 lock_.AssertAcquired(); | 1509 lock_.AssertAcquired(); |
| 1417 | 1510 |
| 1418 if (!IsEnabled()) | 1511 if (!IsEnabled()) |
| 1419 return; | 1512 return; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1489 return static_cast<float>(static_cast<double>(logged_events_->Size()) / | 1582 return static_cast<float>(static_cast<double>(logged_events_->Size()) / |
| 1490 logged_events_->Capacity()); | 1583 logged_events_->Capacity()); |
| 1491 } | 1584 } |
| 1492 | 1585 |
| 1493 bool TraceLog::BufferIsFull() const { | 1586 bool TraceLog::BufferIsFull() const { |
| 1494 AutoLock lock(lock_); | 1587 AutoLock lock(lock_); |
| 1495 return logged_events_->IsFull(); | 1588 return logged_events_->IsFull(); |
| 1496 } | 1589 } |
| 1497 | 1590 |
| 1498 TraceBuffer* TraceLog::CreateTraceBuffer() { | 1591 TraceBuffer* TraceLog::CreateTraceBuffer() { |
| 1499 Options options = trace_options(); | 1592 InternalTraceOptions options = trace_options(); |
| 1500 if (options & RECORD_CONTINUOUSLY) | 1593 if (options & RECORD_CONTINUOUSLY) |
| 1501 return new TraceBufferRingBuffer(kTraceEventRingBufferChunks); | 1594 return new TraceBufferRingBuffer(kTraceEventRingBufferChunks); |
| 1502 else if ((options & ENABLE_SAMPLING) && mode_ == MONITORING_MODE) | 1595 else if ((options & ENABLE_SAMPLING) && mode_ == MONITORING_MODE) |
| 1503 return new TraceBufferRingBuffer(kMonitorTraceEventBufferChunks); | 1596 return new TraceBufferRingBuffer(kMonitorTraceEventBufferChunks); |
| 1504 else if (options & ECHO_TO_CONSOLE) | 1597 else if (options & ECHO_TO_CONSOLE) |
| 1505 return new TraceBufferRingBuffer(kEchoToConsoleTraceEventBufferChunks); | 1598 return new TraceBufferRingBuffer(kEchoToConsoleTraceEventBufferChunks); |
| 1506 return CreateTraceBufferVectorOfSize(kTraceEventVectorBufferChunks); | 1599 return CreateTraceBufferVectorOfSize(kTraceEventVectorBufferChunks); |
| 1507 } | 1600 } |
| 1508 | 1601 |
| 1509 TraceBuffer* TraceLog::CreateTraceBufferVectorOfSize(size_t max_chunks) { | 1602 TraceBuffer* TraceLog::CreateTraceBufferVectorOfSize(size_t max_chunks) { |
| (...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2447 } | 2540 } |
| 2448 | 2541 |
| 2449 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { | 2542 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { |
| 2450 if (*category_group_enabled_) { | 2543 if (*category_group_enabled_) { |
| 2451 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, | 2544 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, |
| 2452 name_, event_handle_); | 2545 name_, event_handle_); |
| 2453 } | 2546 } |
| 2454 } | 2547 } |
| 2455 | 2548 |
| 2456 } // namespace trace_event_internal | 2549 } // namespace trace_event_internal |
| OLD | NEW |