Use PPB_Console interface to send logs from the client plugin
[chromium-blink-merge.git] / base / trace_event / trace_event_android.cc
blob7815107b76e871f8ea9d06fde38945e8ec633a39
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
3 // found in the LICENSE file.
5 #include "base/trace_event/trace_event_impl.h"
7 #include <fcntl.h>
9 #include "base/format_macros.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/trace_event/trace_event.h"
15 namespace base {
16 namespace trace_event {
18 namespace {
20 int g_atrace_fd = -1;
21 const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker";
23 void WriteEvent(
24 char phase,
25 const char* category_group,
26 const char* name,
27 unsigned long long id,
28 const char** arg_names,
29 const unsigned char* arg_types,
30 const TraceEvent::TraceValue* arg_values,
31 const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
32 unsigned int flags) {
33 std::string out = StringPrintf("%c|%d|%s", phase, getpid(), name);
34 if (flags & TRACE_EVENT_FLAG_HAS_ID)
35 StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id));
36 out += '|';
38 for (int i = 0; i < kTraceMaxNumArgs && arg_names[i];
39 ++i) {
40 if (i)
41 out += ';';
42 out += arg_names[i];
43 out += '=';
44 std::string::size_type value_start = out.length();
45 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE)
46 convertable_values[i]->AppendAsTraceFormat(&out);
47 else
48 TraceEvent::AppendValueAsJSON(arg_types[i], arg_values[i], &out);
50 // Remove the quotes which may confuse the atrace script.
51 ReplaceSubstringsAfterOffset(&out, value_start, "\\\"", "'");
52 ReplaceSubstringsAfterOffset(&out, value_start, "\"", "");
53 // Replace chars used for separators with similar chars in the value.
54 std::replace(out.begin() + value_start, out.end(), ';', ',');
55 std::replace(out.begin() + value_start, out.end(), '|', '!');
58 out += '|';
59 out += category_group;
60 write(g_atrace_fd, out.c_str(), out.size());
63 void NoOpOutputCallback(WaitableEvent* complete_event,
64 const scoped_refptr<RefCountedString>&,
65 bool has_more_events) {
66 if (!has_more_events)
67 complete_event->Signal();
70 void EndChromeTracing(TraceLog* trace_log,
71 WaitableEvent* complete_event) {
72 trace_log->SetDisabled();
73 // Delete the buffered trace events as they have been sent to atrace.
74 trace_log->Flush(Bind(&NoOpOutputCallback, complete_event));
77 } // namespace
79 // These functions support Android systrace.py when 'webview' category is
80 // traced. With the new adb_profile_chrome, we may have two phases:
81 // - before WebView is ready for combined tracing, we can use adb_profile_chrome
82 // to trace android categories other than 'webview' and chromium categories.
83 // In this way we can avoid the conflict between StartATrace/StopATrace and
84 // the intents.
85 // - TODO(wangxianzhu): after WebView is ready for combined tracing, remove
86 // StartATrace, StopATrace and SendToATrace, and perhaps send Java traces
87 // directly to atrace in trace_event_binding.cc.
89 void TraceLog::StartATrace() {
90 if (g_atrace_fd != -1)
91 return;
93 g_atrace_fd = open(kATraceMarkerFile, O_WRONLY);
94 if (g_atrace_fd == -1) {
95 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
96 return;
98 TraceConfig trace_config;
99 trace_config.SetTraceRecordMode(RECORD_CONTINUOUSLY);
100 SetEnabled(trace_config, TraceLog::RECORDING_MODE);
103 void TraceLog::StopATrace() {
104 if (g_atrace_fd == -1)
105 return;
107 close(g_atrace_fd);
108 g_atrace_fd = -1;
110 // TraceLog::Flush() requires the current thread to have a message loop, but
111 // this thread called from Java may not have one, so flush in another thread.
112 Thread end_chrome_tracing_thread("end_chrome_tracing");
113 WaitableEvent complete_event(false, false);
114 end_chrome_tracing_thread.Start();
115 end_chrome_tracing_thread.task_runner()->PostTask(
116 FROM_HERE, base::Bind(&EndChromeTracing, Unretained(this),
117 Unretained(&complete_event)));
118 complete_event.Wait();
121 void TraceEvent::SendToATrace() {
122 if (g_atrace_fd == -1)
123 return;
125 const char* category_group =
126 TraceLog::GetCategoryGroupName(category_group_enabled_);
128 switch (phase_) {
129 case TRACE_EVENT_PHASE_BEGIN:
130 WriteEvent('B', category_group, name_, id_,
131 arg_names_, arg_types_, arg_values_, convertable_values_,
132 flags_);
133 break;
135 case TRACE_EVENT_PHASE_COMPLETE:
136 WriteEvent(duration_.ToInternalValue() == -1 ? 'B' : 'E',
137 category_group, name_, id_,
138 arg_names_, arg_types_, arg_values_, convertable_values_,
139 flags_);
140 break;
142 case TRACE_EVENT_PHASE_END:
143 // Though a single 'E' is enough, here append pid, name and
144 // category_group etc. So that unpaired events can be found easily.
145 WriteEvent('E', category_group, name_, id_,
146 arg_names_, arg_types_, arg_values_, convertable_values_,
147 flags_);
148 break;
150 case TRACE_EVENT_PHASE_INSTANT:
151 // Simulate an instance event with a pair of begin/end events.
152 WriteEvent('B', category_group, name_, id_,
153 arg_names_, arg_types_, arg_values_, convertable_values_,
154 flags_);
155 write(g_atrace_fd, "E", 1);
156 break;
158 case TRACE_EVENT_PHASE_COUNTER:
159 for (int i = 0; i < kTraceMaxNumArgs && arg_names_[i]; ++i) {
160 DCHECK(arg_types_[i] == TRACE_VALUE_TYPE_INT);
161 std::string out = base::StringPrintf(
162 "C|%d|%s-%s", getpid(), name_, arg_names_[i]);
163 if (flags_ & TRACE_EVENT_FLAG_HAS_ID)
164 StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id_));
165 StringAppendF(&out, "|%d|%s",
166 static_cast<int>(arg_values_[i].as_int), category_group);
167 write(g_atrace_fd, out.c_str(), out.size());
169 break;
171 default:
172 // Do nothing.
173 break;
177 void TraceLog::AddClockSyncMetadataEvent() {
178 int atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND);
179 if (atrace_fd == -1) {
180 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
181 return;
184 // Android's kernel trace system has a trace_marker feature: this is a file on
185 // debugfs that takes the written data and pushes it onto the trace
186 // buffer. So, to establish clock sync, we write our monotonic clock into that
187 // trace buffer.
188 double now_in_seconds = (TraceTicks::Now() - TraceTicks()).InSecondsF();
189 std::string marker = StringPrintf(
190 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
191 if (write(atrace_fd, marker.c_str(), marker.size()) == -1)
192 PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile;
193 close(atrace_fd);
196 } // namespace trace_event
197 } // namespace base