| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/trace_event/trace_log.h" | 5 #include "base/trace_event/trace_log.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <memory> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/base_switches.h" | 12 #include "base/base_switches.h" |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 14 #include "base/debug/leak_annotations.h" | 15 #include "base/debug/leak_annotations.h" |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/location.h" | 17 #include "base/location.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted_memory.h" | 19 #include "base/memory/ref_counted_memory.h" |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
| 21 #include "base/process/process_metrics.h" | 21 #include "base/process/process_metrics.h" |
| 22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
| 23 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
| 24 #include "base/strings/string_tokenizer.h" | 24 #include "base/strings/string_tokenizer.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/sys_info.h" | 26 #include "base/sys_info.h" |
| 27 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 27 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 28 #include "base/thread_task_runner_handle.h" | 28 #include "base/thread_task_runner_handle.h" |
| 29 #include "base/threading/platform_thread.h" | 29 #include "base/threading/platform_thread.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 234 |
| 235 void FlushWhileLocked(); | 235 void FlushWhileLocked(); |
| 236 | 236 |
| 237 void CheckThisIsCurrentBuffer() const { | 237 void CheckThisIsCurrentBuffer() const { |
| 238 DCHECK(trace_log_->thread_local_event_buffer_.Get() == this); | 238 DCHECK(trace_log_->thread_local_event_buffer_.Get() == this); |
| 239 } | 239 } |
| 240 | 240 |
| 241 // Since TraceLog is a leaky singleton, trace_log_ will always be valid | 241 // Since TraceLog is a leaky singleton, trace_log_ will always be valid |
| 242 // as long as the thread exists. | 242 // as long as the thread exists. |
| 243 TraceLog* trace_log_; | 243 TraceLog* trace_log_; |
| 244 scoped_ptr<TraceBufferChunk> chunk_; | 244 std::unique_ptr<TraceBufferChunk> chunk_; |
| 245 size_t chunk_index_; | 245 size_t chunk_index_; |
| 246 int generation_; | 246 int generation_; |
| 247 | 247 |
| 248 DISALLOW_COPY_AND_ASSIGN(ThreadLocalEventBuffer); | 248 DISALLOW_COPY_AND_ASSIGN(ThreadLocalEventBuffer); |
| 249 }; | 249 }; |
| 250 | 250 |
| 251 TraceLog::ThreadLocalEventBuffer::ThreadLocalEventBuffer(TraceLog* trace_log) | 251 TraceLog::ThreadLocalEventBuffer::ThreadLocalEventBuffer(TraceLog* trace_log) |
| 252 : trace_log_(trace_log), | 252 : trace_log_(trace_log), |
| 253 chunk_index_(0), | 253 chunk_index_(0), |
| 254 generation_(trace_log->generation()) { | 254 generation_(trace_log->generation()) { |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 894 discard_events), | 894 discard_events), |
| 895 TimeDelta::FromMilliseconds(kThreadFlushTimeoutMs)); | 895 TimeDelta::FromMilliseconds(kThreadFlushTimeoutMs)); |
| 896 return; | 896 return; |
| 897 } | 897 } |
| 898 | 898 |
| 899 FinishFlush(generation, discard_events); | 899 FinishFlush(generation, discard_events); |
| 900 } | 900 } |
| 901 | 901 |
| 902 // Usually it runs on a different thread. | 902 // Usually it runs on a different thread. |
| 903 void TraceLog::ConvertTraceEventsToTraceFormat( | 903 void TraceLog::ConvertTraceEventsToTraceFormat( |
| 904 scoped_ptr<TraceBuffer> logged_events, | 904 std::unique_ptr<TraceBuffer> logged_events, |
| 905 const OutputCallback& flush_output_callback, | 905 const OutputCallback& flush_output_callback, |
| 906 const ArgumentFilterPredicate& argument_filter_predicate) { | 906 const ArgumentFilterPredicate& argument_filter_predicate) { |
| 907 if (flush_output_callback.is_null()) | 907 if (flush_output_callback.is_null()) |
| 908 return; | 908 return; |
| 909 | 909 |
| 910 // The callback need to be called at least once even if there is no events | 910 // The callback need to be called at least once even if there is no events |
| 911 // to let the caller know the completion of flush. | 911 // to let the caller know the completion of flush. |
| 912 scoped_refptr<RefCountedString> json_events_str_ptr = new RefCountedString(); | 912 scoped_refptr<RefCountedString> json_events_str_ptr = new RefCountedString(); |
| 913 while (const TraceBufferChunk* chunk = logged_events->NextChunk()) { | 913 while (const TraceBufferChunk* chunk = logged_events->NextChunk()) { |
| 914 for (size_t j = 0; j < chunk->size(); ++j) { | 914 for (size_t j = 0; j < chunk->size(); ++j) { |
| 915 size_t size = json_events_str_ptr->size(); | 915 size_t size = json_events_str_ptr->size(); |
| 916 if (size > kTraceEventBufferSizeInBytes) { | 916 if (size > kTraceEventBufferSizeInBytes) { |
| 917 flush_output_callback.Run(json_events_str_ptr, true); | 917 flush_output_callback.Run(json_events_str_ptr, true); |
| 918 json_events_str_ptr = new RefCountedString(); | 918 json_events_str_ptr = new RefCountedString(); |
| 919 } else if (size) { | 919 } else if (size) { |
| 920 json_events_str_ptr->data().append(",\n"); | 920 json_events_str_ptr->data().append(",\n"); |
| 921 } | 921 } |
| 922 chunk->GetEventAt(j)->AppendAsJSON(&(json_events_str_ptr->data()), | 922 chunk->GetEventAt(j)->AppendAsJSON(&(json_events_str_ptr->data()), |
| 923 argument_filter_predicate); | 923 argument_filter_predicate); |
| 924 } | 924 } |
| 925 } | 925 } |
| 926 flush_output_callback.Run(json_events_str_ptr, false); | 926 flush_output_callback.Run(json_events_str_ptr, false); |
| 927 } | 927 } |
| 928 | 928 |
| 929 void TraceLog::FinishFlush(int generation, bool discard_events) { | 929 void TraceLog::FinishFlush(int generation, bool discard_events) { |
| 930 scoped_ptr<TraceBuffer> previous_logged_events; | 930 std::unique_ptr<TraceBuffer> previous_logged_events; |
| 931 OutputCallback flush_output_callback; | 931 OutputCallback flush_output_callback; |
| 932 ArgumentFilterPredicate argument_filter_predicate; | 932 ArgumentFilterPredicate argument_filter_predicate; |
| 933 | 933 |
| 934 if (!CheckGeneration(generation)) | 934 if (!CheckGeneration(generation)) |
| 935 return; | 935 return; |
| 936 | 936 |
| 937 { | 937 { |
| 938 AutoLock lock(lock_); | 938 AutoLock lock(lock_); |
| 939 | 939 |
| 940 previous_logged_events.swap(logged_events_); | 940 previous_logged_events.swap(logged_events_); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 TraceEventHandle TraceLog::AddTraceEvent( | 1028 TraceEventHandle TraceLog::AddTraceEvent( |
| 1029 char phase, | 1029 char phase, |
| 1030 const unsigned char* category_group_enabled, | 1030 const unsigned char* category_group_enabled, |
| 1031 const char* name, | 1031 const char* name, |
| 1032 const char* scope, | 1032 const char* scope, |
| 1033 unsigned long long id, | 1033 unsigned long long id, |
| 1034 int num_args, | 1034 int num_args, |
| 1035 const char** arg_names, | 1035 const char** arg_names, |
| 1036 const unsigned char* arg_types, | 1036 const unsigned char* arg_types, |
| 1037 const unsigned long long* arg_values, | 1037 const unsigned long long* arg_values, |
| 1038 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1038 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1039 unsigned int flags) { | 1039 unsigned int flags) { |
| 1040 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); | 1040 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); |
| 1041 base::TimeTicks now = base::TimeTicks::Now(); | 1041 base::TimeTicks now = base::TimeTicks::Now(); |
| 1042 return AddTraceEventWithThreadIdAndTimestamp( | 1042 return AddTraceEventWithThreadIdAndTimestamp( |
| 1043 phase, | 1043 phase, |
| 1044 category_group_enabled, | 1044 category_group_enabled, |
| 1045 name, | 1045 name, |
| 1046 scope, | 1046 scope, |
| 1047 id, | 1047 id, |
| 1048 trace_event_internal::kNoId, // bind_id | 1048 trace_event_internal::kNoId, // bind_id |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1060 char phase, | 1060 char phase, |
| 1061 const unsigned char* category_group_enabled, | 1061 const unsigned char* category_group_enabled, |
| 1062 const char* name, | 1062 const char* name, |
| 1063 const char* scope, | 1063 const char* scope, |
| 1064 unsigned long long id, | 1064 unsigned long long id, |
| 1065 unsigned long long bind_id, | 1065 unsigned long long bind_id, |
| 1066 int num_args, | 1066 int num_args, |
| 1067 const char** arg_names, | 1067 const char** arg_names, |
| 1068 const unsigned char* arg_types, | 1068 const unsigned char* arg_types, |
| 1069 const unsigned long long* arg_values, | 1069 const unsigned long long* arg_values, |
| 1070 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1070 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1071 unsigned int flags) { | 1071 unsigned int flags) { |
| 1072 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); | 1072 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); |
| 1073 base::TimeTicks now = base::TimeTicks::Now(); | 1073 base::TimeTicks now = base::TimeTicks::Now(); |
| 1074 return AddTraceEventWithThreadIdAndTimestamp( | 1074 return AddTraceEventWithThreadIdAndTimestamp( |
| 1075 phase, | 1075 phase, |
| 1076 category_group_enabled, | 1076 category_group_enabled, |
| 1077 name, | 1077 name, |
| 1078 scope, | 1078 scope, |
| 1079 id, | 1079 id, |
| 1080 bind_id, | 1080 bind_id, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1092 char phase, | 1092 char phase, |
| 1093 const unsigned char* category_group_enabled, | 1093 const unsigned char* category_group_enabled, |
| 1094 const char* name, | 1094 const char* name, |
| 1095 const char* scope, | 1095 const char* scope, |
| 1096 unsigned long long id, | 1096 unsigned long long id, |
| 1097 int process_id, | 1097 int process_id, |
| 1098 int num_args, | 1098 int num_args, |
| 1099 const char** arg_names, | 1099 const char** arg_names, |
| 1100 const unsigned char* arg_types, | 1100 const unsigned char* arg_types, |
| 1101 const unsigned long long* arg_values, | 1101 const unsigned long long* arg_values, |
| 1102 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1102 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1103 unsigned int flags) { | 1103 unsigned int flags) { |
| 1104 base::TimeTicks now = base::TimeTicks::Now(); | 1104 base::TimeTicks now = base::TimeTicks::Now(); |
| 1105 return AddTraceEventWithThreadIdAndTimestamp( | 1105 return AddTraceEventWithThreadIdAndTimestamp( |
| 1106 phase, | 1106 phase, |
| 1107 category_group_enabled, | 1107 category_group_enabled, |
| 1108 name, | 1108 name, |
| 1109 scope, | 1109 scope, |
| 1110 id, | 1110 id, |
| 1111 trace_event_internal::kNoId, // bind_id | 1111 trace_event_internal::kNoId, // bind_id |
| 1112 process_id, | 1112 process_id, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1126 const unsigned char* category_group_enabled, | 1126 const unsigned char* category_group_enabled, |
| 1127 const char* name, | 1127 const char* name, |
| 1128 const char* scope, | 1128 const char* scope, |
| 1129 unsigned long long id, | 1129 unsigned long long id, |
| 1130 int thread_id, | 1130 int thread_id, |
| 1131 const TimeTicks& timestamp, | 1131 const TimeTicks& timestamp, |
| 1132 int num_args, | 1132 int num_args, |
| 1133 const char** arg_names, | 1133 const char** arg_names, |
| 1134 const unsigned char* arg_types, | 1134 const unsigned char* arg_types, |
| 1135 const unsigned long long* arg_values, | 1135 const unsigned long long* arg_values, |
| 1136 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1136 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1137 unsigned int flags) { | 1137 unsigned int flags) { |
| 1138 return AddTraceEventWithThreadIdAndTimestamp( | 1138 return AddTraceEventWithThreadIdAndTimestamp( |
| 1139 phase, | 1139 phase, |
| 1140 category_group_enabled, | 1140 category_group_enabled, |
| 1141 name, | 1141 name, |
| 1142 scope, | 1142 scope, |
| 1143 id, | 1143 id, |
| 1144 trace_event_internal::kNoId, // bind_id | 1144 trace_event_internal::kNoId, // bind_id |
| 1145 thread_id, | 1145 thread_id, |
| 1146 timestamp, | 1146 timestamp, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1158 const char* name, | 1158 const char* name, |
| 1159 const char* scope, | 1159 const char* scope, |
| 1160 unsigned long long id, | 1160 unsigned long long id, |
| 1161 unsigned long long bind_id, | 1161 unsigned long long bind_id, |
| 1162 int thread_id, | 1162 int thread_id, |
| 1163 const TimeTicks& timestamp, | 1163 const TimeTicks& timestamp, |
| 1164 int num_args, | 1164 int num_args, |
| 1165 const char** arg_names, | 1165 const char** arg_names, |
| 1166 const unsigned char* arg_types, | 1166 const unsigned char* arg_types, |
| 1167 const unsigned long long* arg_values, | 1167 const unsigned long long* arg_values, |
| 1168 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1168 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1169 unsigned int flags) { | 1169 unsigned int flags) { |
| 1170 TraceEventHandle handle = {0, 0, 0}; | 1170 TraceEventHandle handle = {0, 0, 0}; |
| 1171 if (!*category_group_enabled) | 1171 if (!*category_group_enabled) |
| 1172 return handle; | 1172 return handle; |
| 1173 | 1173 |
| 1174 // Avoid re-entrance of AddTraceEvent. This may happen in GPU process when | 1174 // Avoid re-entrance of AddTraceEvent. This may happen in GPU process when |
| 1175 // ECHO_TO_CONSOLE is enabled: AddTraceEvent -> LOG(ERROR) -> | 1175 // ECHO_TO_CONSOLE is enabled: AddTraceEvent -> LOG(ERROR) -> |
| 1176 // GpuProcessLogMessageHandler -> PostPendingTask -> TRACE_EVENT ... | 1176 // GpuProcessLogMessageHandler -> PostPendingTask -> TRACE_EVENT ... |
| 1177 if (thread_is_in_trace_event_.Get()) | 1177 if (thread_is_in_trace_event_.Get()) |
| 1178 return handle; | 1178 return handle; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1332 return handle; | 1332 return handle; |
| 1333 } | 1333 } |
| 1334 | 1334 |
| 1335 void TraceLog::AddMetadataEvent( | 1335 void TraceLog::AddMetadataEvent( |
| 1336 const unsigned char* category_group_enabled, | 1336 const unsigned char* category_group_enabled, |
| 1337 const char* name, | 1337 const char* name, |
| 1338 int num_args, | 1338 int num_args, |
| 1339 const char** arg_names, | 1339 const char** arg_names, |
| 1340 const unsigned char* arg_types, | 1340 const unsigned char* arg_types, |
| 1341 const unsigned long long* arg_values, | 1341 const unsigned long long* arg_values, |
| 1342 scoped_ptr<ConvertableToTraceFormat>* convertable_values, | 1342 std::unique_ptr<ConvertableToTraceFormat>* convertable_values, |
| 1343 unsigned int flags) { | 1343 unsigned int flags) { |
| 1344 scoped_ptr<TraceEvent> trace_event(new TraceEvent); | 1344 std::unique_ptr<TraceEvent> trace_event(new TraceEvent); |
| 1345 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); | 1345 int thread_id = static_cast<int>(base::PlatformThread::CurrentId()); |
| 1346 ThreadTicks thread_now = ThreadNow(); | 1346 ThreadTicks thread_now = ThreadNow(); |
| 1347 TimeTicks now = OffsetNow(); | 1347 TimeTicks now = OffsetNow(); |
| 1348 AutoLock lock(lock_); | 1348 AutoLock lock(lock_); |
| 1349 trace_event->Initialize( | 1349 trace_event->Initialize( |
| 1350 thread_id, now, thread_now, TRACE_EVENT_PHASE_METADATA, | 1350 thread_id, now, thread_now, TRACE_EVENT_PHASE_METADATA, |
| 1351 category_group_enabled, name, | 1351 category_group_enabled, name, |
| 1352 trace_event_internal::kGlobalScope, // scope | 1352 trace_event_internal::kGlobalScope, // scope |
| 1353 trace_event_internal::kNoId, // id | 1353 trace_event_internal::kNoId, // id |
| 1354 trace_event_internal::kNoId, // bind_id | 1354 trace_event_internal::kNoId, // bind_id |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1736 } | 1736 } |
| 1737 | 1737 |
| 1738 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { | 1738 ScopedTraceBinaryEfficient::~ScopedTraceBinaryEfficient() { |
| 1739 if (*category_group_enabled_) { | 1739 if (*category_group_enabled_) { |
| 1740 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_, | 1740 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_, name_, |
| 1741 event_handle_); | 1741 event_handle_); |
| 1742 } | 1742 } |
| 1743 } | 1743 } |
| 1744 | 1744 |
| 1745 } // namespace trace_event_internal | 1745 } // namespace trace_event_internal |
| OLD | NEW |