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" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
19 #include "base/strings/string_tokenizer.h" | 19 #include "base/strings/string_tokenizer.h" |
20 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 21 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
22 #include "base/threading/platform_thread.h" | 22 #include "base/threading/platform_thread.h" |
| 23 #include "base/threading/thread_id_name_manager.h" |
23 #include "base/threading/thread_local.h" | 24 #include "base/threading/thread_local.h" |
24 #include "base/time.h" | 25 #include "base/time.h" |
25 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
26 | 27 |
27 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
28 #include "base/debug/trace_event_win.h" | 29 #include "base/debug/trace_event_win.h" |
29 #endif | 30 #endif |
30 | 31 |
31 class DeleteTraceLogForTesting { | 32 class DeleteTraceLogForTesting { |
32 public: | 33 public: |
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 new RefCountedString(); | 617 new RefCountedString(); |
617 TraceEvent::AppendEventsAsJSON(previous_logged_events, | 618 TraceEvent::AppendEventsAsJSON(previous_logged_events, |
618 i, | 619 i, |
619 kTraceEventBatchSize, | 620 kTraceEventBatchSize, |
620 &(json_events_str_ptr->data())); | 621 &(json_events_str_ptr->data())); |
621 cb.Run(json_events_str_ptr); | 622 cb.Run(json_events_str_ptr); |
622 } | 623 } |
623 } | 624 } |
624 | 625 |
625 void TraceLog::AddTraceEvent(char phase, | 626 void TraceLog::AddTraceEvent(char phase, |
626 const unsigned char* category_enabled, | 627 const unsigned char* category_enabled, |
627 const char* name, | 628 const char* name, |
628 unsigned long long id, | 629 unsigned long long id, |
629 int num_args, | 630 int num_args, |
630 const char** arg_names, | 631 const char** arg_names, |
631 const unsigned char* arg_types, | 632 const unsigned char* arg_types, |
632 const unsigned long long* arg_values, | 633 const unsigned long long* arg_values, |
633 unsigned char flags) { | 634 unsigned char flags) { |
| 635 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); |
| 636 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime(); |
| 637 AddTraceEventWithThreadIdAndTimestamp(phase, category_enabled, name, id, |
| 638 thread_id, now, num_args, arg_names, |
| 639 arg_types, arg_values, flags); |
| 640 } |
| 641 |
| 642 void TraceLog::AddTraceEventWithThreadIdAndTimestamp( |
| 643 char phase, |
| 644 const unsigned char* category_enabled, |
| 645 const char* name, |
| 646 unsigned long long id, |
| 647 int thread_id, |
| 648 const TimeTicks& timestamp, |
| 649 int num_args, |
| 650 const char** arg_names, |
| 651 const unsigned char* arg_types, |
| 652 const unsigned long long* arg_values, |
| 653 unsigned char flags) { |
634 DCHECK(name); | 654 DCHECK(name); |
635 | 655 |
636 #if defined(OS_ANDROID) | 656 #if defined(OS_ANDROID) |
637 SendToATrace(phase, GetCategoryName(category_enabled), name, | 657 SendToATrace(phase, GetCategoryName(category_enabled), name, |
638 num_args, arg_names, arg_types, arg_values); | 658 num_args, arg_names, arg_types, arg_values); |
639 #endif | 659 #endif |
640 | 660 |
641 TimeTicks now = TimeTicks::NowFromSystemTraceTime() - time_offset_; | 661 TimeTicks now = timestamp - time_offset_; |
642 NotificationHelper notifier(this); | 662 NotificationHelper notifier(this); |
| 663 |
643 { | 664 { |
644 AutoLock lock(lock_); | 665 AutoLock lock(lock_); |
645 if (*category_enabled != CATEGORY_ENABLED) | 666 if (*category_enabled != CATEGORY_ENABLED) |
646 return; | 667 return; |
647 if (logged_events_.size() >= kTraceEventBufferSize) | 668 if (logged_events_.size() >= kTraceEventBufferSize) |
648 return; | 669 return; |
649 | 670 |
650 int thread_id = static_cast<int>(PlatformThread::CurrentId()); | 671 const char* new_name = ThreadIdNameManager::GetInstance()-> |
651 | 672 GetName(thread_id); |
652 const char* new_name = PlatformThread::GetName(); | |
653 // Check if the thread name has been set or changed since the previous | 673 // Check if the thread name has been set or changed since the previous |
654 // call (if any), but don't bother if the new name is empty. Note this will | 674 // call (if any), but don't bother if the new name is empty. Note this will |
655 // not detect a thread name change within the same char* buffer address: we | 675 // not detect a thread name change within the same char* buffer address: we |
656 // favor common case performance over corner case correctness. | 676 // favor common case performance over corner case correctness. |
657 if (new_name != g_current_thread_name.Get().Get() && | 677 if (new_name != g_current_thread_name.Get().Get() && |
658 new_name && *new_name) { | 678 new_name && *new_name) { |
659 g_current_thread_name.Get().Set(new_name); | 679 g_current_thread_name.Get().Set(new_name); |
| 680 |
660 hash_map<int, std::string>::iterator existing_name = | 681 hash_map<int, std::string>::iterator existing_name = |
661 thread_names_.find(thread_id); | 682 thread_names_.find(thread_id); |
662 if (existing_name == thread_names_.end()) { | 683 if (existing_name == thread_names_.end()) { |
663 // This is a new thread id, and a new name. | 684 // This is a new thread id, and a new name. |
664 thread_names_[thread_id] = new_name; | 685 thread_names_[thread_id] = new_name; |
665 } else { | 686 } else { |
666 // This is a thread id that we've seen before, but potentially with a | 687 // This is a thread id that we've seen before, but potentially with a |
667 // new name. | 688 // new name. |
668 std::vector<StringPiece> existing_names; | 689 std::vector<StringPiece> existing_names; |
669 Tokenize(existing_name->second, ",", &existing_names); | 690 Tokenize(existing_name->second, ",", &existing_names); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 unsigned long long pid = static_cast<unsigned long long>(process_id_); | 814 unsigned long long pid = static_cast<unsigned long long>(process_id_); |
794 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; | 815 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; |
795 } | 816 } |
796 | 817 |
797 void TraceLog::SetTimeOffset(TimeDelta offset) { | 818 void TraceLog::SetTimeOffset(TimeDelta offset) { |
798 time_offset_ = offset; | 819 time_offset_ = offset; |
799 } | 820 } |
800 | 821 |
801 } // namespace debug | 822 } // namespace debug |
802 } // namespace base | 823 } // namespace base |
OLD | NEW |