| 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/trace_event/trace_event_impl.h" | 5 #include "base/trace_event/trace_event_impl.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 14 | 14 |
| 15 namespace base { |
| 16 namespace trace_event { |
| 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 int g_atrace_fd = -1; | 20 int g_atrace_fd = -1; |
| 18 const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker"; | 21 const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker"; |
| 19 | 22 |
| 20 void WriteEvent( | 23 void WriteEvent( |
| 21 char phase, | 24 char phase, |
| 22 const char* category_group, | 25 const char* category_group, |
| 23 const char* name, | 26 const char* name, |
| 24 unsigned long long id, | 27 unsigned long long id, |
| 25 const char** arg_names, | 28 const char** arg_names, |
| 26 const unsigned char* arg_types, | 29 const unsigned char* arg_types, |
| 27 const base::trace_event::TraceEvent::TraceValue* arg_values, | 30 const TraceEvent::TraceValue* arg_values, |
| 28 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>* | 31 const scoped_refptr<ConvertableToTraceFormat>* convertable_values, |
| 29 convertable_values, | |
| 30 unsigned char flags) { | 32 unsigned char flags) { |
| 31 std::string out = base::StringPrintf("%c|%d|%s", phase, getpid(), name); | 33 std::string out = StringPrintf("%c|%d|%s", phase, getpid(), name); |
| 32 if (flags & TRACE_EVENT_FLAG_HAS_ID) | 34 if (flags & TRACE_EVENT_FLAG_HAS_ID) |
| 33 base::StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id)); | 35 StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id)); |
| 34 out += '|'; | 36 out += '|'; |
| 35 | 37 |
| 36 for (int i = 0; i < base::trace_event::kTraceMaxNumArgs && arg_names[i]; | 38 for (int i = 0; i < kTraceMaxNumArgs && arg_names[i]; |
| 37 ++i) { | 39 ++i) { |
| 38 if (i) | 40 if (i) |
| 39 out += ';'; | 41 out += ';'; |
| 40 out += arg_names[i]; | 42 out += arg_names[i]; |
| 41 out += '='; | 43 out += '='; |
| 42 std::string::size_type value_start = out.length(); | 44 std::string::size_type value_start = out.length(); |
| 43 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE) { | 45 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE) |
| 44 convertable_values[i]->AppendAsTraceFormat(&out); | 46 convertable_values[i]->AppendAsTraceFormat(&out); |
| 45 } else { | 47 else |
| 46 base::trace_event::TraceEvent::AppendValueAsJSON(arg_types[i], | 48 TraceEvent::AppendValueAsJSON(arg_types[i], arg_values[i], &out); |
| 47 arg_values[i], &out); | 49 |
| 48 } | |
| 49 // Remove the quotes which may confuse the atrace script. | 50 // Remove the quotes which may confuse the atrace script. |
| 50 ReplaceSubstringsAfterOffset(&out, value_start, "\\\"", "'"); | 51 ReplaceSubstringsAfterOffset(&out, value_start, "\\\"", "'"); |
| 51 ReplaceSubstringsAfterOffset(&out, value_start, "\"", ""); | 52 ReplaceSubstringsAfterOffset(&out, value_start, "\"", ""); |
| 52 // Replace chars used for separators with similar chars in the value. | 53 // Replace chars used for separators with similar chars in the value. |
| 53 std::replace(out.begin() + value_start, out.end(), ';', ','); | 54 std::replace(out.begin() + value_start, out.end(), ';', ','); |
| 54 std::replace(out.begin() + value_start, out.end(), '|', '!'); | 55 std::replace(out.begin() + value_start, out.end(), '|', '!'); |
| 55 } | 56 } |
| 56 | 57 |
| 57 out += '|'; | 58 out += '|'; |
| 58 out += category_group; | 59 out += category_group; |
| 59 write(g_atrace_fd, out.c_str(), out.size()); | 60 write(g_atrace_fd, out.c_str(), out.size()); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void NoOpOutputCallback(base::WaitableEvent* complete_event, | 63 void NoOpOutputCallback(WaitableEvent* complete_event, |
| 63 const scoped_refptr<base::RefCountedString>&, | 64 const scoped_refptr<RefCountedString>&, |
| 64 bool has_more_events) { | 65 bool has_more_events) { |
| 65 if (!has_more_events) | 66 if (!has_more_events) |
| 66 complete_event->Signal(); | 67 complete_event->Signal(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 void EndChromeTracing(base::trace_event::TraceLog* trace_log, | 70 void EndChromeTracing(TraceLog* trace_log, |
| 70 base::WaitableEvent* complete_event) { | 71 WaitableEvent* complete_event) { |
| 71 trace_log->SetDisabled(); | 72 trace_log->SetDisabled(); |
| 72 // Delete the buffered trace events as they have been sent to atrace. | 73 // Delete the buffered trace events as they have been sent to atrace. |
| 73 trace_log->Flush(base::Bind(&NoOpOutputCallback, complete_event)); | 74 trace_log->Flush(Bind(&NoOpOutputCallback, complete_event)); |
| 74 } | 75 } |
| 75 | 76 |
| 76 } // namespace | 77 } // namespace |
| 77 | 78 |
| 78 namespace base { | |
| 79 namespace trace_event { | |
| 80 | |
| 81 // These functions support Android systrace.py when 'webview' category is | 79 // These functions support Android systrace.py when 'webview' category is |
| 82 // traced. With the new adb_profile_chrome, we may have two phases: | 80 // traced. With the new adb_profile_chrome, we may have two phases: |
| 83 // - before WebView is ready for combined tracing, we can use adb_profile_chrome | 81 // - before WebView is ready for combined tracing, we can use adb_profile_chrome |
| 84 // to trace android categories other than 'webview' and chromium categories. | 82 // to trace android categories other than 'webview' and chromium categories. |
| 85 // In this way we can avoid the conflict between StartATrace/StopATrace and | 83 // In this way we can avoid the conflict between StartATrace/StopATrace and |
| 86 // the intents. | 84 // the intents. |
| 87 // - TODO(wangxianzhu): after WebView is ready for combined tracing, remove | 85 // - TODO(wangxianzhu): after WebView is ready for combined tracing, remove |
| 88 // StartATrace, StopATrace and SendToATrace, and perhaps send Java traces | 86 // StartATrace, StopATrace and SendToATrace, and perhaps send Java traces |
| 89 // directly to atrace in trace_event_binding.cc. | 87 // directly to atrace in trace_event_binding.cc. |
| 90 | 88 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 double now_in_seconds = (TraceTicks::Now() - TraceTicks()).InSecondsF(); | 188 double now_in_seconds = (TraceTicks::Now() - TraceTicks()).InSecondsF(); |
| 191 std::string marker = StringPrintf( | 189 std::string marker = StringPrintf( |
| 192 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); | 190 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); |
| 193 if (write(atrace_fd, marker.c_str(), marker.size()) == -1) | 191 if (write(atrace_fd, marker.c_str(), marker.size()) == -1) |
| 194 PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile; | 192 PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile; |
| 195 close(atrace_fd); | 193 close(atrace_fd); |
| 196 } | 194 } |
| 197 | 195 |
| 198 } // namespace trace_event | 196 } // namespace trace_event |
| 199 } // namespace base | 197 } // namespace base |
| OLD | NEW |