Enterprise policy: Ignore the deprecated ForceSafeSearch if ForceGoogleSafeSearch...
[chromium-blink-merge.git] / base / trace_event / trace_event_android.cc
blobbcba43637690f85bb372b7971bd4bd1ad3decac5
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 {
17 int g_atrace_fd = -1;
18 const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker";
20 void WriteEvent(
21 char phase,
22 const char* category_group,
23 const char* name,
24 unsigned long long id,
25 const char** arg_names,
26 const unsigned char* arg_types,
27 const base::trace_event::TraceEvent::TraceValue* arg_values,
28 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>*
29 convertable_values,
30 unsigned char flags) {
31 std::string out = base::StringPrintf("%c|%d|%s", phase, getpid(), name);
32 if (flags & TRACE_EVENT_FLAG_HAS_ID)
33 base::StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id));
34 out += '|';
36 for (int i = 0; i < base::trace_event::kTraceMaxNumArgs && arg_names[i];
37 ++i) {
38 if (i)
39 out += ';';
40 out += arg_names[i];
41 out += '=';
42 std::string::size_type value_start = out.length();
43 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE) {
44 convertable_values[i]->AppendAsTraceFormat(&out);
45 } else {
46 base::trace_event::TraceEvent::AppendValueAsJSON(arg_types[i],
47 arg_values[i], &out);
49 // Remove the quotes which may confuse the atrace script.
50 ReplaceSubstringsAfterOffset(&out, value_start, "\\\"", "'");
51 ReplaceSubstringsAfterOffset(&out, value_start, "\"", "");
52 // 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(), '|', '!');
57 out += '|';
58 out += category_group;
59 write(g_atrace_fd, out.c_str(), out.size());
62 void NoOpOutputCallback(base::WaitableEvent* complete_event,
63 const scoped_refptr<base::RefCountedString>&,
64 bool has_more_events) {
65 if (!has_more_events)
66 complete_event->Signal();
69 void EndChromeTracing(base::trace_event::TraceLog* trace_log,
70 base::WaitableEvent* complete_event) {
71 trace_log->SetDisabled();
72 // Delete the buffered trace events as they have been sent to atrace.
73 trace_log->Flush(base::Bind(&NoOpOutputCallback, complete_event));
76 } // namespace
78 namespace base {
79 namespace trace_event {
81 // These functions support Android systrace.py when 'webview' category is
82 // 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
84 // to trace android categories other than 'webview' and chromium categories.
85 // In this way we can avoid the conflict between StartATrace/StopATrace and
86 // the intents.
87 // - TODO(wangxianzhu): after WebView is ready for combined tracing, remove
88 // StartATrace, StopATrace and SendToATrace, and perhaps send Java traces
89 // directly to atrace in trace_event_binding.cc.
91 void TraceLog::StartATrace() {
92 if (g_atrace_fd != -1)
93 return;
95 g_atrace_fd = open(kATraceMarkerFile, O_WRONLY);
96 if (g_atrace_fd == -1) {
97 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
98 return;
100 SetEnabled(CategoryFilter(CategoryFilter::kDefaultCategoryFilterString),
101 TraceLog::RECORDING_MODE,
102 TraceOptions(RECORD_CONTINUOUSLY));
105 void TraceLog::StopATrace() {
106 if (g_atrace_fd == -1)
107 return;
109 close(g_atrace_fd);
110 g_atrace_fd = -1;
112 // TraceLog::Flush() requires the current thread to have a message loop, but
113 // this thread called from Java may not have one, so flush in another thread.
114 Thread end_chrome_tracing_thread("end_chrome_tracing");
115 WaitableEvent complete_event(false, false);
116 end_chrome_tracing_thread.Start();
117 end_chrome_tracing_thread.message_loop()->PostTask(
118 FROM_HERE, base::Bind(&EndChromeTracing, Unretained(this),
119 Unretained(&complete_event)));
120 complete_event.Wait();
123 void TraceEvent::SendToATrace() {
124 if (g_atrace_fd == -1)
125 return;
127 const char* category_group =
128 TraceLog::GetCategoryGroupName(category_group_enabled_);
130 switch (phase_) {
131 case TRACE_EVENT_PHASE_BEGIN:
132 WriteEvent('B', category_group, name_, id_,
133 arg_names_, arg_types_, arg_values_, convertable_values_,
134 flags_);
135 break;
137 case TRACE_EVENT_PHASE_COMPLETE:
138 WriteEvent(duration_.ToInternalValue() == -1 ? 'B' : 'E',
139 category_group, name_, id_,
140 arg_names_, arg_types_, arg_values_, convertable_values_,
141 flags_);
142 break;
144 case TRACE_EVENT_PHASE_END:
145 // Though a single 'E' is enough, here append pid, name and
146 // category_group etc. So that unpaired events can be found easily.
147 WriteEvent('E', category_group, name_, id_,
148 arg_names_, arg_types_, arg_values_, convertable_values_,
149 flags_);
150 break;
152 case TRACE_EVENT_PHASE_INSTANT:
153 // Simulate an instance event with a pair of begin/end events.
154 WriteEvent('B', category_group, name_, id_,
155 arg_names_, arg_types_, arg_values_, convertable_values_,
156 flags_);
157 write(g_atrace_fd, "E", 1);
158 break;
160 case TRACE_EVENT_PHASE_COUNTER:
161 for (int i = 0; i < kTraceMaxNumArgs && arg_names_[i]; ++i) {
162 DCHECK(arg_types_[i] == TRACE_VALUE_TYPE_INT);
163 std::string out = base::StringPrintf(
164 "C|%d|%s-%s", getpid(), name_, arg_names_[i]);
165 if (flags_ & TRACE_EVENT_FLAG_HAS_ID)
166 StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id_));
167 StringAppendF(&out, "|%d|%s",
168 static_cast<int>(arg_values_[i].as_int), category_group);
169 write(g_atrace_fd, out.c_str(), out.size());
171 break;
173 default:
174 // Do nothing.
175 break;
179 void TraceLog::AddClockSyncMetadataEvent() {
180 int atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND);
181 if (atrace_fd == -1) {
182 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
183 return;
186 // Android's kernel trace system has a trace_marker feature: this is a file on
187 // debugfs that takes the written data and pushes it onto the trace
188 // buffer. So, to establish clock sync, we write our monotonic clock into that
189 // trace buffer.
190 TimeTicks now = TimeTicks::NowFromSystemTraceTime();
191 double now_in_seconds = now.ToInternalValue() / 1000000.0;
192 std::string marker = StringPrintf(
193 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
194 if (write(atrace_fd, marker.c_str(), marker.size()) == -1)
195 PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile;
196 close(atrace_fd);
199 } // namespace trace_event
200 } // namespace base