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