| 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 | 5 |
| 6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
| 7 #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 7 #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
| 8 | 8 |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <stack> | 11 #include <stack> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/atomicops.h" | 15 #include "base/atomicops.h" |
| 16 #include "base/base_export.h" | 16 #include "base/base_export.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/containers/hash_tables.h" | 18 #include "base/containers/hash_tables.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted_memory.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 24 #include "base/synchronization/condition_variable.h" | 24 #include "base/synchronization/condition_variable.h" |
| 25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 27 #include "base/threading/thread_local.h" | 27 #include "base/threading/thread_local.h" |
| 28 #include "base/trace_event/trace_event_memory_overhead.h" | 28 #include "base/trace_event/trace_event_memory_overhead.h" |
| 29 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| 30 | 30 |
| 31 namespace base { | 31 namespace base { |
| 32 | 32 |
| 33 class WaitableEvent; | 33 class WaitableEvent; |
| 34 class MessageLoop; | 34 class MessageLoop; |
| 35 | 35 |
| 36 namespace trace_event { | 36 namespace trace_event { |
| 37 | 37 |
| 38 typedef base::Callback<bool(const char* arg_name)> ArgumentNameFilterPredicate; | 38 typedef base::Callback<bool(const char* arg_name)> ArgumentNameFilterPredicate; |
| 39 | 39 |
| 40 typedef base::Callback<bool(const char* category_group_name, | 40 typedef base::Callback<bool(const char* category_group_name, |
| 41 const char* event_name, | 41 const char* event_name, |
| 42 ArgumentNameFilterPredicate*)> | 42 ArgumentNameFilterPredicate*)> |
| 43 ArgumentFilterPredicate; | 43 ArgumentFilterPredicate; |
| 44 | 44 |
| 45 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided | 45 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided |
| 46 // class must implement this interface. | 46 // class must implement this interface. |
| 47 class BASE_EXPORT ConvertableToTraceFormat | 47 class BASE_EXPORT ConvertableToTraceFormat { |
| 48 : public RefCounted<ConvertableToTraceFormat> { | |
| 49 public: | 48 public: |
| 49 ConvertableToTraceFormat() {} |
| 50 virtual ~ConvertableToTraceFormat() {} |
| 51 |
| 50 // Append the class info to the provided |out| string. The appended | 52 // Append the class info to the provided |out| string. The appended |
| 51 // data must be a valid JSON object. Strings must be properly quoted, and | 53 // data must be a valid JSON object. Strings must be properly quoted, and |
| 52 // escaped. There is no processing applied to the content after it is | 54 // escaped. There is no processing applied to the content after it is |
| 53 // appended. | 55 // appended. |
| 54 virtual void AppendAsTraceFormat(std::string* out) const = 0; | 56 virtual void AppendAsTraceFormat(std::string* out) const = 0; |
| 55 | 57 |
| 56 virtual void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); | 58 virtual void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 57 | 59 |
| 58 std::string ToString() const { | 60 std::string ToString() const { |
| 59 std::string result; | 61 std::string result; |
| 60 AppendAsTraceFormat(&result); | 62 AppendAsTraceFormat(&result); |
| 61 return result; | 63 return result; |
| 62 } | 64 } |
| 63 | 65 |
| 64 protected: | |
| 65 virtual ~ConvertableToTraceFormat() {} | |
| 66 | |
| 67 private: | 66 private: |
| 68 friend class RefCounted<ConvertableToTraceFormat>; | 67 DISALLOW_COPY_AND_ASSIGN(ConvertableToTraceFormat); |
| 69 }; | 68 }; |
| 70 | 69 |
| 71 const int kTraceMaxNumArgs = 2; | 70 const int kTraceMaxNumArgs = 2; |
| 72 | 71 |
| 73 struct TraceEventHandle { | 72 struct TraceEventHandle { |
| 74 uint32_t chunk_seq; | 73 uint32_t chunk_seq; |
| 75 // These numbers of bits must be kept consistent with | 74 // These numbers of bits must be kept consistent with |
| 76 // TraceBufferChunk::kMaxTrunkIndex and | 75 // TraceBufferChunk::kMaxTrunkIndex and |
| 77 // TraceBufferChunk::kTraceBufferChunkSize (in trace_buffer.h). | 76 // TraceBufferChunk::kTraceBufferChunkSize (in trace_buffer.h). |
| 78 unsigned chunk_index : 26; | 77 unsigned chunk_index : 26; |
| 79 unsigned event_index : 6; | 78 unsigned event_index : 6; |
| 80 }; | 79 }; |
| 81 | 80 |
| 82 class BASE_EXPORT TraceEvent { | 81 class BASE_EXPORT TraceEvent { |
| 83 public: | 82 public: |
| 84 union TraceValue { | 83 union TraceValue { |
| 85 bool as_bool; | 84 bool as_bool; |
| 86 unsigned long long as_uint; | 85 unsigned long long as_uint; |
| 87 long long as_int; | 86 long long as_int; |
| 88 double as_double; | 87 double as_double; |
| 89 const void* as_pointer; | 88 const void* as_pointer; |
| 90 const char* as_string; | 89 const char* as_string; |
| 91 }; | 90 }; |
| 92 | 91 |
| 93 TraceEvent(); | 92 TraceEvent(); |
| 94 ~TraceEvent(); | 93 ~TraceEvent(); |
| 95 | 94 |
| 96 // We don't need to copy TraceEvent except when TraceEventBuffer is cloned. | 95 void MoveFrom(scoped_ptr<TraceEvent> other); |
| 97 // Use explicit copy method to avoid accidentally misuse of copy. | |
| 98 void CopyFrom(const TraceEvent& other); | |
| 99 | 96 |
| 100 void Initialize( | 97 void Initialize(int thread_id, |
| 101 int thread_id, | 98 TimeTicks timestamp, |
| 102 TimeTicks timestamp, | 99 ThreadTicks thread_timestamp, |
| 103 ThreadTicks thread_timestamp, | 100 char phase, |
| 104 char phase, | 101 const unsigned char* category_group_enabled, |
| 105 const unsigned char* category_group_enabled, | 102 const char* name, |
| 106 const char* name, | 103 const char* scope, |
| 107 const char* scope, | 104 unsigned long long id, |
| 108 unsigned long long id, | 105 unsigned long long bind_id, |
| 109 unsigned long long bind_id, | 106 int num_args, |
| 110 int num_args, | 107 const char** arg_names, |
| 111 const char** arg_names, | 108 const unsigned char* arg_types, |
| 112 const unsigned char* arg_types, | 109 const unsigned long long* arg_values, |
| 113 const unsigned long long* arg_values, | 110 scoped_ptr<ConvertableToTraceFormat>* convertable_values, |
| 114 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, | 111 unsigned int flags); |
| 115 unsigned int flags); | |
| 116 | 112 |
| 117 void Reset(); | 113 void Reset(); |
| 118 | 114 |
| 119 void UpdateDuration(const TimeTicks& now, const ThreadTicks& thread_now); | 115 void UpdateDuration(const TimeTicks& now, const ThreadTicks& thread_now); |
| 120 | 116 |
| 121 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); | 117 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead); |
| 122 | 118 |
| 123 // Serialize event data to JSON | 119 // Serialize event data to JSON |
| 124 void AppendAsJSON( | 120 void AppendAsJSON( |
| 125 std::string* out, | 121 std::string* out, |
| 126 const ArgumentFilterPredicate& argument_filter_predicate) const; | 122 const ArgumentFilterPredicate& argument_filter_predicate) const; |
| 127 void AppendPrettyPrinted(std::ostringstream* out) const; | 123 void AppendPrettyPrinted(std::ostringstream* out) const; |
| 128 | 124 |
| 129 static void AppendValueAsJSON(unsigned char type, | 125 static void AppendValueAsJSON(unsigned char type, |
| 130 TraceValue value, | 126 TraceValue value, |
| 131 std::string* out); | 127 std::string* out); |
| 132 | 128 |
| 133 TimeTicks timestamp() const { return timestamp_; } | 129 TimeTicks timestamp() const { return timestamp_; } |
| 134 ThreadTicks thread_timestamp() const { return thread_timestamp_; } | 130 ThreadTicks thread_timestamp() const { return thread_timestamp_; } |
| 135 char phase() const { return phase_; } | 131 char phase() const { return phase_; } |
| 136 int thread_id() const { return thread_id_; } | 132 int thread_id() const { return thread_id_; } |
| 137 TimeDelta duration() const { return duration_; } | 133 TimeDelta duration() const { return duration_; } |
| 138 TimeDelta thread_duration() const { return thread_duration_; } | 134 TimeDelta thread_duration() const { return thread_duration_; } |
| 139 const char* scope() const { return scope_; } | 135 const char* scope() const { return scope_; } |
| 140 unsigned long long id() const { return id_; } | 136 unsigned long long id() const { return id_; } |
| 141 unsigned int flags() const { return flags_; } | 137 unsigned int flags() const { return flags_; } |
| 142 | 138 |
| 143 // Exposed for unittesting: | 139 // Exposed for unittesting: |
| 144 | 140 |
| 145 const base::RefCountedString* parameter_copy_storage() const { | 141 const std::string* parameter_copy_storage() const { |
| 146 return parameter_copy_storage_.get(); | 142 return parameter_copy_storage_.get(); |
| 147 } | 143 } |
| 148 | 144 |
| 149 const unsigned char* category_group_enabled() const { | 145 const unsigned char* category_group_enabled() const { |
| 150 return category_group_enabled_; | 146 return category_group_enabled_; |
| 151 } | 147 } |
| 152 | 148 |
| 153 const char* name() const { return name_; } | 149 const char* name() const { return name_; } |
| 154 | 150 |
| 155 #if defined(OS_ANDROID) | 151 #if defined(OS_ANDROID) |
| 156 void SendToATrace(); | 152 void SendToATrace(); |
| 157 #endif | 153 #endif |
| 158 | 154 |
| 159 private: | 155 private: |
| 160 // Note: these are ordered by size (largest first) for optimal packing. | 156 // Note: these are ordered by size (largest first) for optimal packing. |
| 161 TimeTicks timestamp_; | 157 TimeTicks timestamp_; |
| 162 ThreadTicks thread_timestamp_; | 158 ThreadTicks thread_timestamp_; |
| 163 TimeDelta duration_; | 159 TimeDelta duration_; |
| 164 TimeDelta thread_duration_; | 160 TimeDelta thread_duration_; |
| 165 // scope_ and id_ can be used to store phase-specific data. | 161 // scope_ and id_ can be used to store phase-specific data. |
| 166 const char* scope_; | 162 const char* scope_; |
| 167 unsigned long long id_; | 163 unsigned long long id_; |
| 168 TraceValue arg_values_[kTraceMaxNumArgs]; | 164 TraceValue arg_values_[kTraceMaxNumArgs]; |
| 169 const char* arg_names_[kTraceMaxNumArgs]; | 165 const char* arg_names_[kTraceMaxNumArgs]; |
| 170 scoped_refptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; | 166 scoped_ptr<ConvertableToTraceFormat> convertable_values_[kTraceMaxNumArgs]; |
| 171 const unsigned char* category_group_enabled_; | 167 const unsigned char* category_group_enabled_; |
| 172 const char* name_; | 168 const char* name_; |
| 173 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 169 scoped_ptr<std::string> parameter_copy_storage_; |
| 174 // Depending on TRACE_EVENT_FLAG_HAS_PROCESS_ID the event will have either: | 170 // Depending on TRACE_EVENT_FLAG_HAS_PROCESS_ID the event will have either: |
| 175 // tid: thread_id_, pid: current_process_id (default case). | 171 // tid: thread_id_, pid: current_process_id (default case). |
| 176 // tid: -1, pid: process_id_ (when flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID). | 172 // tid: -1, pid: process_id_ (when flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID). |
| 177 union { | 173 union { |
| 178 int thread_id_; | 174 int thread_id_; |
| 179 int process_id_; | 175 int process_id_; |
| 180 }; | 176 }; |
| 181 unsigned int flags_; | 177 unsigned int flags_; |
| 182 unsigned long long bind_id_; | 178 unsigned long long bind_id_; |
| 183 unsigned char arg_types_[kTraceMaxNumArgs]; | 179 unsigned char arg_types_[kTraceMaxNumArgs]; |
| 184 char phase_; | 180 char phase_; |
| 185 | 181 |
| 186 DISALLOW_COPY_AND_ASSIGN(TraceEvent); | 182 DISALLOW_COPY_AND_ASSIGN(TraceEvent); |
| 187 }; | 183 }; |
| 188 | 184 |
| 189 } // namespace trace_event | 185 } // namespace trace_event |
| 190 } // namespace base | 186 } // namespace base |
| 191 | 187 |
| 192 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ | 188 #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_ |
| OLD | NEW |