Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: base/trace_event/trace_event_android.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/trace_event.h ('k') | base/trace_event/trace_event_argument.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/trace_event/trace_event_impl.h"
6
7 #include <fcntl.h>
8
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"
14
15 namespace base {
16 namespace trace_event {
17
18 namespace {
19
20 int g_atrace_fd = -1;
21 const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker";
22
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 += '|';
37
38 for (int i = 0; i < kTraceMaxNumArgs && arg_names[i]; ++i) {
39 if (i)
40 out += ';';
41 out += arg_names[i];
42 out += '=';
43 std::string::size_type value_start = out.length();
44 if (arg_types[i] == TRACE_VALUE_TYPE_CONVERTABLE)
45 convertable_values[i]->AppendAsTraceFormat(&out);
46 else
47 TraceEvent::AppendValueAsJSON(arg_types[i], arg_values[i], &out);
48
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(), '|', '!');
55 }
56
57 out += '|';
58 out += category_group;
59 write(g_atrace_fd, out.c_str(), out.size());
60 }
61
62 void NoOpOutputCallback(WaitableEvent* complete_event,
63 const scoped_refptr<RefCountedString>&,
64 bool has_more_events) {
65 if (!has_more_events)
66 complete_event->Signal();
67 }
68
69 void EndChromeTracing(TraceLog* trace_log, WaitableEvent* complete_event) {
70 trace_log->SetDisabled();
71 // Delete the buffered trace events as they have been sent to atrace.
72 trace_log->Flush(Bind(&NoOpOutputCallback, complete_event));
73 }
74
75 } // namespace
76
77 // These functions support Android systrace.py when 'webview' category is
78 // traced. With the new adb_profile_chrome, we may have two phases:
79 // - before WebView is ready for combined tracing, we can use adb_profile_chrome
80 // to trace android categories other than 'webview' and chromium categories.
81 // In this way we can avoid the conflict between StartATrace/StopATrace and
82 // the intents.
83 // - TODO(wangxianzhu): after WebView is ready for combined tracing, remove
84 // StartATrace, StopATrace and SendToATrace, and perhaps send Java traces
85 // directly to atrace in trace_event_binding.cc.
86
87 void TraceLog::StartATrace() {
88 if (g_atrace_fd != -1)
89 return;
90
91 g_atrace_fd = open(kATraceMarkerFile, O_WRONLY);
92 if (g_atrace_fd == -1) {
93 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
94 return;
95 }
96 TraceConfig trace_config;
97 trace_config.SetTraceRecordMode(RECORD_CONTINUOUSLY);
98 SetEnabled(trace_config, TraceLog::RECORDING_MODE);
99 }
100
101 void TraceLog::StopATrace() {
102 if (g_atrace_fd == -1)
103 return;
104
105 close(g_atrace_fd);
106 g_atrace_fd = -1;
107
108 // TraceLog::Flush() requires the current thread to have a message loop, but
109 // this thread called from Java may not have one, so flush in another thread.
110 Thread end_chrome_tracing_thread("end_chrome_tracing");
111 WaitableEvent complete_event(false, false);
112 end_chrome_tracing_thread.Start();
113 end_chrome_tracing_thread.task_runner()->PostTask(
114 FROM_HERE, base::Bind(&EndChromeTracing, Unretained(this),
115 Unretained(&complete_event)));
116 complete_event.Wait();
117 }
118
119 void TraceEvent::SendToATrace() {
120 if (g_atrace_fd == -1)
121 return;
122
123 const char* category_group =
124 TraceLog::GetCategoryGroupName(category_group_enabled_);
125
126 switch (phase_) {
127 case TRACE_EVENT_PHASE_BEGIN:
128 WriteEvent('B', category_group, name_, id_,
129 arg_names_, arg_types_, arg_values_, convertable_values_,
130 flags_);
131 break;
132
133 case TRACE_EVENT_PHASE_COMPLETE:
134 WriteEvent(duration_.ToInternalValue() == -1 ? 'B' : 'E',
135 category_group, name_, id_,
136 arg_names_, arg_types_, arg_values_, convertable_values_,
137 flags_);
138 break;
139
140 case TRACE_EVENT_PHASE_END:
141 // Though a single 'E' is enough, here append pid, name and
142 // category_group etc. So that unpaired events can be found easily.
143 WriteEvent('E', category_group, name_, id_,
144 arg_names_, arg_types_, arg_values_, convertable_values_,
145 flags_);
146 break;
147
148 case TRACE_EVENT_PHASE_INSTANT:
149 // Simulate an instance event with a pair of begin/end events.
150 WriteEvent('B', category_group, name_, id_,
151 arg_names_, arg_types_, arg_values_, convertable_values_,
152 flags_);
153 write(g_atrace_fd, "E", 1);
154 break;
155
156 case TRACE_EVENT_PHASE_COUNTER:
157 for (int i = 0; i < kTraceMaxNumArgs && arg_names_[i]; ++i) {
158 DCHECK(arg_types_[i] == TRACE_VALUE_TYPE_INT);
159 std::string out = base::StringPrintf(
160 "C|%d|%s-%s", getpid(), name_, arg_names_[i]);
161 if (flags_ & TRACE_EVENT_FLAG_HAS_ID)
162 StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id_));
163 StringAppendF(&out, "|%d|%s",
164 static_cast<int>(arg_values_[i].as_int), category_group);
165 write(g_atrace_fd, out.c_str(), out.size());
166 }
167 break;
168
169 default:
170 // Do nothing.
171 break;
172 }
173 }
174
175 void TraceLog::AddClockSyncMetadataEvent() {
176 int atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND);
177 if (atrace_fd == -1) {
178 PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
179 return;
180 }
181
182 // Android's kernel trace system has a trace_marker feature: this is a file on
183 // debugfs that takes the written data and pushes it onto the trace
184 // buffer. So, to establish clock sync, we write our monotonic clock into that
185 // trace buffer.
186 double now_in_seconds = (TraceTicks::Now() - TraceTicks()).InSecondsF();
187 std::string marker = StringPrintf(
188 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
189 if (write(atrace_fd, marker.c_str(), marker.size()) == -1)
190 PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile;
191 close(atrace_fd);
192 }
193
194 } // namespace trace_event
195 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/trace_event.h ('k') | base/trace_event/trace_event_argument.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698