Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: base/debug/trace_event_impl.cc

Issue 11366109: Adding raw tracing to trace framework. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changing AddTraceEvent API to always accept a tid and timestamp. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/string_tokenizer.h" 18 #include "base/string_tokenizer.h"
19 #include "base/string_util.h" 19 #include "base/string_util.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 25 matching lines...) Expand all
58 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES", 59 "tracing categories exhausted; must increase TRACE_EVENT_MAX_CATEGORIES",
59 "__metadata", 60 "__metadata",
60 }; 61 };
61 // The enabled flag is char instead of bool so that the API can be used from C. 62 // The enabled flag is char instead of bool so that the API can be used from C.
62 unsigned char g_category_enabled[TRACE_EVENT_MAX_CATEGORIES] = { 0 }; 63 unsigned char g_category_enabled[TRACE_EVENT_MAX_CATEGORIES] = { 0 };
63 const int g_category_already_shutdown = 0; 64 const int g_category_already_shutdown = 0;
64 const int g_category_categories_exhausted = 1; 65 const int g_category_categories_exhausted = 1;
65 const int g_category_metadata = 2; 66 const int g_category_metadata = 2;
66 int g_category_index = 3; // skip initial 3 categories 67 int g_category_index = 3; // skip initial 3 categories
67 68
68 // The most-recently captured name of the current thread 69 // The most-recently captured version of the current thread name
69 LazyInstance<ThreadLocalPointer<const char> >::Leaky 70 uint32 g_current_thread_name_version = 0;
70 g_current_thread_name = LAZY_INSTANCE_INITIALIZER;
71 71
72 } // namespace 72 } // namespace
73 73
74 //////////////////////////////////////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////////
75 // 75 //
76 // TraceEvent 76 // TraceEvent
77 // 77 //
78 //////////////////////////////////////////////////////////////////////////////// 78 ////////////////////////////////////////////////////////////////////////////////
79 79
80 namespace { 80 namespace {
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 new RefCountedString(); 603 new RefCountedString();
604 TraceEvent::AppendEventsAsJSON(previous_logged_events, 604 TraceEvent::AppendEventsAsJSON(previous_logged_events,
605 i, 605 i,
606 kTraceEventBatchSize, 606 kTraceEventBatchSize,
607 &(json_events_str_ptr->data())); 607 &(json_events_str_ptr->data()));
608 cb.Run(json_events_str_ptr); 608 cb.Run(json_events_str_ptr);
609 } 609 }
610 } 610 }
611 611
612 void TraceLog::AddTraceEvent(char phase, 612 void TraceLog::AddTraceEvent(char phase,
613 const unsigned char* category_enabled, 613 const unsigned char* category_enabled,
614 const char* name, 614 const char* name,
615 unsigned long long id, 615 unsigned long long id,
616 int num_args, 616 int thread_id,
617 const char** arg_names, 617 int64 timestamp,
618 const unsigned char* arg_types, 618 int num_args,
619 const unsigned long long* arg_values, 619 const char** arg_names,
620 unsigned char flags) { 620 const unsigned char* arg_types,
621 const unsigned long long* arg_values,
622 unsigned char flags) {
621 DCHECK(name); 623 DCHECK(name);
622 624
623 #if defined(OS_ANDROID) 625 #if defined(OS_ANDROID)
624 SendToATrace(phase, GetCategoryName(category_enabled), name, 626 SendToATrace(phase, GetCategoryName(category_enabled), name,
625 num_args, arg_names, arg_types, arg_values); 627 num_args, arg_names, arg_types, arg_values);
626 #endif 628 #endif
627 629
628 TimeTicks now = TimeTicks::NowFromSystemTraceTime() - time_offset_; 630 TimeTicks now = TimeTicks::FromInternalValue(timestamp);
629 NotificationHelper notifier(this); 631 NotificationHelper notifier(this);
632
630 { 633 {
631 AutoLock lock(lock_); 634 AutoLock lock(lock_);
632 if (*category_enabled != CATEGORY_ENABLED) 635 if (*category_enabled != CATEGORY_ENABLED)
633 return; 636 return;
634 if (logged_events_.size() >= kTraceEventBufferSize) 637 if (logged_events_.size() >= kTraceEventBufferSize)
635 return; 638 return;
636 639
637 int thread_id = static_cast<int>(PlatformThread::CurrentId()); 640 ThreadIdNameManager* manager = ThreadIdNameManager::GetInstance();
641 const char* new_name = manager->GetNameForId(thread_id);
642 uint32 new_version = manager->GetVersionForId(thread_id);
638 643
639 const char* new_name = PlatformThread::GetName();
640 // Check if the thread name has been set or changed since the previous 644 // Check if the thread name has been set or changed since the previous
641 // call (if any), but don't bother if the new name is empty. Note this will 645 // call (if any), but don't bother if the new name is empty. Note this will
642 // not detect a thread name change within the same char* buffer address: we 646 // not detect a thread name change within the same char* buffer address: we
643 // favor common case performance over corner case correctness. 647 // favor common case performance over corner case correctness.
644 if (new_name != g_current_thread_name.Get().Get() && 648 if (new_version != g_current_thread_name_version && new_name && *new_name) {
645 new_name && *new_name) { 649 g_current_thread_name_version = new_version;
646 g_current_thread_name.Get().Set(new_name); 650
647 base::hash_map<int, std::string>::iterator existing_name = 651 base::hash_map<int, std::string>::iterator existing_name =
648 thread_names_.find(thread_id); 652 thread_names_.find(thread_id);
649 if (existing_name == thread_names_.end()) { 653 if (existing_name == thread_names_.end()) {
650 // This is a new thread id, and a new name. 654 // This is a new thread id, and a new name.
651 thread_names_[thread_id] = new_name; 655 thread_names_[thread_id] = new_name;
652 } else { 656 } else {
653 // This is a thread id that we've seen before, but potentially with a 657 // This is a thread id that we've seen before, but potentially with a
654 // new name. 658 // new name.
655 std::vector<base::StringPiece> existing_names; 659 std::vector<base::StringPiece> existing_names;
656 Tokenize(existing_name->second, ",", &existing_names); 660 Tokenize(existing_name->second, ",", &existing_names);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 unsigned long long offset_basis = 14695981039346656037ull; 782 unsigned long long offset_basis = 14695981039346656037ull;
779 unsigned long long fnv_prime = 1099511628211ull; 783 unsigned long long fnv_prime = 1099511628211ull;
780 unsigned long long pid = static_cast<unsigned long long>(process_id_); 784 unsigned long long pid = static_cast<unsigned long long>(process_id_);
781 process_id_hash_ = (offset_basis ^ pid) * fnv_prime; 785 process_id_hash_ = (offset_basis ^ pid) * fnv_prime;
782 } 786 }
783 787
784 void TraceLog::SetTimeOffset(TimeDelta offset) { 788 void TraceLog::SetTimeOffset(TimeDelta offset) {
785 time_offset_ = offset; 789 time_offset_ = offset;
786 } 790 }
787 791
792 TimeDelta TraceLog::GetTimeOffset() {
793 return time_offset_;
794 }
795
788 } // namespace debug 796 } // namespace debug
789 } // namespace base 797 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698