1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/importers/systrace/systrace_line_parser.h"
18
19 #include "perfetto/ext/base/flat_hash_map.h"
20 #include "perfetto/ext/base/string_splitter.h"
21 #include "perfetto/ext/base/string_utils.h"
22 #include "perfetto/ext/base/string_view.h"
23 #include "perfetto/trace_processor/status.h"
24 #include "src/trace_processor/importers/common/event_tracker.h"
25 #include "src/trace_processor/importers/common/process_tracker.h"
26 #include "src/trace_processor/importers/common/slice_tracker.h"
27 #include "src/trace_processor/importers/common/thread_state_tracker.h"
28 #include "src/trace_processor/importers/common/track_tracker.h"
29 #include "src/trace_processor/importers/common/tracks.h"
30 #include "src/trace_processor/importers/common/tracks_common.h"
31 #include "src/trace_processor/importers/ftrace/binder_tracker.h"
32 #include "src/trace_processor/importers/ftrace/ftrace_sched_event_tracker.h"
33 #include "src/trace_processor/importers/systrace/systrace_line.h"
34 #include "src/trace_processor/importers/systrace/systrace_parser.h"
35 #include "src/trace_processor/storage/trace_storage.h"
36 #include "src/trace_processor/types/task_state.h"
37
38 #include <cctype>
39 #include <cstdint>
40 #include <cstdlib>
41 #include <optional>
42 #include <string>
43 #include <utility>
44
45 namespace perfetto::trace_processor {
46
SystraceLineParser(TraceProcessorContext * ctx)47 SystraceLineParser::SystraceLineParser(TraceProcessorContext* ctx)
48 : context_(ctx),
49 rss_stat_tracker_(context_),
50 sched_wakeup_name_id_(ctx->storage->InternString("sched_wakeup")),
51 sched_waking_name_id_(ctx->storage->InternString("sched_waking")),
52 workqueue_name_id_(ctx->storage->InternString("workqueue")),
53 sched_blocked_reason_id_(
54 ctx->storage->InternString("sched_blocked_reason")),
55 io_wait_id_(ctx->storage->InternString("io_wait")),
56 waker_utid_id_(ctx->storage->InternString("waker_utid")),
57 unknown_thread_name_id_(ctx->storage->InternString("<...>")) {}
58
ParseLine(const SystraceLine & line)59 util::Status SystraceLineParser::ParseLine(const SystraceLine& line) {
60 const StringId line_task_id{
61 context_->storage->InternString(base::StringView(line.task))};
62 auto utid = context_->process_tracker->UpdateThreadName(
63 line.pid,
64 // Ftrace doesn't always know the thread name (see ftrace documentation
65 // for saved_cmdlines) so some lines name a process "<...>". Don't use
66 // this bogus name for thread naming otherwise a real name from a previous
67 // line could be overwritten.
68 line_task_id == unknown_thread_name_id_ ? StringId::Null() : line_task_id,
69 ThreadNamePriority::kFtrace);
70
71 if (!line.tgid_str.empty() && line.tgid_str != "-----") {
72 std::optional<uint32_t> tgid = base::StringToUInt32(line.tgid_str);
73 if (tgid) {
74 context_->process_tracker->UpdateThread(line.pid, tgid.value());
75 }
76 }
77
78 base::FlatHashMap<std::string, std::string> args;
79 for (base::StringSplitter ss(line.args_str, ' '); ss.Next();) {
80 std::string key;
81 std::string value;
82 if (!base::Contains(ss.cur_token(), "=")) {
83 key = "name";
84 value = ss.cur_token();
85 args.Insert(std::move(key), std::move(value));
86 continue;
87 }
88 for (base::StringSplitter inner(ss.cur_token(), '='); inner.Next();) {
89 if (key.empty()) {
90 key = inner.cur_token();
91 } else {
92 value = inner.cur_token();
93 }
94 }
95 args.Insert(std::move(key), std::move(value));
96 }
97 if (line.event_name == "sched_switch") {
98 auto prev_state_str = args["prev_state"];
99 int64_t prev_state =
100 ftrace_utils::TaskState::FromSystrace(prev_state_str.c_str())
101 .ToRawStateOnlyForSystraceConversions();
102
103 auto prev_pid = base::StringToUInt32(args["prev_pid"]);
104 auto prev_comm = base::StringView(args["prev_comm"]);
105 auto prev_prio = base::StringToInt32(args["prev_prio"]);
106 auto next_pid = base::StringToUInt32(args["next_pid"]);
107 auto next_comm = base::StringView(args["next_comm"]);
108 auto next_prio = base::StringToInt32(args["next_prio"]);
109
110 if (!(prev_pid.has_value() && prev_prio.has_value() &&
111 next_pid.has_value() && next_prio.has_value())) {
112 return util::Status("Could not parse sched_switch");
113 }
114
115 FtraceSchedEventTracker::GetOrCreate(context_)->PushSchedSwitch(
116 line.cpu, line.ts, prev_pid.value(), prev_comm, prev_prio.value(),
117 prev_state, next_pid.value(), next_comm, next_prio.value());
118 } else if (line.event_name == "tracing_mark_write" ||
119 line.event_name == "0" || line.event_name == "print") {
120 SystraceParser::GetOrCreate(context_)->ParsePrintEvent(
121 line.ts, line.pid, line.args_str.c_str());
122 } else if (line.event_name == "sched_waking") {
123 auto comm = args["comm"];
124 std::optional<uint32_t> wakee_pid = base::StringToUInt32(args["pid"]);
125 if (!wakee_pid.has_value()) {
126 return util::Status("Could not convert wakee_pid");
127 }
128
129 StringId name_id = context_->storage->InternString(base::StringView(comm));
130 auto wakee_utid = context_->process_tracker->UpdateThreadName(
131 wakee_pid.value(), name_id, ThreadNamePriority::kFtrace);
132
133 ThreadStateTracker::GetOrCreate(context_)->PushWakingEvent(
134 line.ts, wakee_utid, utid);
135
136 } else if (line.event_name == "cpu_frequency") {
137 std::optional<uint32_t> event_cpu = base::StringToUInt32(args["cpu_id"]);
138 std::optional<double> new_state = base::StringToDouble(args["state"]);
139 if (!event_cpu.has_value()) {
140 return util::Status("Could not convert event cpu");
141 }
142 if (!event_cpu.has_value()) {
143 return util::Status("Could not convert state");
144 }
145
146 TrackId track = context_->track_tracker->InternTrack(
147 tracks::kCpuFrequencyBlueprint, tracks::Dimensions(event_cpu.value()));
148 context_->event_tracker->PushCounter(line.ts, new_state.value(), track);
149 } else if (line.event_name == "cpu_idle") {
150 std::optional<uint32_t> event_cpu = base::StringToUInt32(args["cpu_id"]);
151 std::optional<double> new_state = base::StringToDouble(args["state"]);
152 if (!event_cpu.has_value()) {
153 return util::Status("Could not convert event cpu");
154 }
155 if (!event_cpu.has_value()) {
156 return util::Status("Could not convert state");
157 }
158
159 TrackId track = context_->track_tracker->InternTrack(
160 tracks::kCpuIdleBlueprint, tracks::Dimensions(event_cpu.value()));
161 context_->event_tracker->PushCounter(line.ts, new_state.value(), track);
162 } else if (line.event_name == "binder_transaction") {
163 auto id = base::StringToInt32(args["transaction"]);
164 auto dest_node = base::StringToInt32(args["dest_node"]);
165 auto dest_tgid = base::StringToUInt32(args["dest_proc"]);
166 auto dest_tid = base::StringToUInt32(args["dest_thread"]);
167 auto is_reply = base::StringToInt32(args["reply"]).value() == 1;
168 auto flags_str = args["flags"];
169 char* end;
170 uint32_t flags = static_cast<uint32_t>(strtol(flags_str.c_str(), &end, 16));
171 std::string code_str = args["code"] + " Java Layer Dependent";
172 StringId code = context_->storage->InternString(base::StringView(code_str));
173 if (!dest_tgid.has_value()) {
174 return util::Status("Could not convert dest_tgid");
175 }
176 if (!dest_tid.has_value()) {
177 return util::Status("Could not convert dest_tid");
178 }
179 if (!id.has_value()) {
180 return util::Status("Could not convert transaction id");
181 }
182 if (!dest_node.has_value()) {
183 return util::Status("Could not covert dest node");
184 }
185 BinderTracker::GetOrCreate(context_)->Transaction(
186 line.ts, line.pid, id.value(), dest_node.value(), dest_tgid.value(),
187 dest_tid.value(), is_reply, flags, code);
188 } else if (line.event_name == "binder_transaction_received") {
189 auto id = base::StringToInt32(args["transaction"]);
190 if (!id.has_value()) {
191 return util::Status("Could not convert transaction id");
192 }
193 BinderTracker::GetOrCreate(context_)->TransactionReceived(line.ts, line.pid,
194 id.value());
195 } else if (line.event_name == "binder_command") {
196 auto id = base::StringToUInt32(args["cmd"], 0);
197 if (!id.has_value()) {
198 return util::Status("Could not convert cmd ");
199 }
200 BinderTracker::GetOrCreate(context_)->CommandToKernel(line.ts, line.pid,
201 id.value());
202 } else if (line.event_name == "binder_return") {
203 auto id = base::StringToUInt32(args["cmd"], 0);
204 if (!id.has_value()) {
205 return util::Status("Could not convert cmd");
206 }
207 BinderTracker::GetOrCreate(context_)->ReturnFromKernel(line.ts, line.pid,
208 id.value());
209 } else if (line.event_name == "binder_lock") {
210 BinderTracker::GetOrCreate(context_)->Lock(line.ts, line.pid);
211 } else if (line.event_name == "binder_locked") {
212 BinderTracker::GetOrCreate(context_)->Locked(line.ts, line.pid);
213 } else if (line.event_name == "binder_unlock") {
214 BinderTracker::GetOrCreate(context_)->Unlock(line.ts, line.pid);
215 } else if (line.event_name == "binder_transaction_alloc_buf") {
216 auto data_size = base::StringToUInt64(args["data_size"]);
217 auto offsets_size = base::StringToUInt64(args["offsets_size"]);
218 if (!data_size.has_value()) {
219 return util::Status("Could not convert data size");
220 }
221 if (!offsets_size.has_value()) {
222 return util::Status("Could not convert offsets size");
223 }
224 BinderTracker::GetOrCreate(context_)->TransactionAllocBuf(
225 line.ts, line.pid, data_size.value(), offsets_size.value());
226 } else if (line.event_name == "clock_set_rate") {
227 auto rate = base::StringToUInt32(args["state"]);
228 if (!rate.has_value()) {
229 return util::Status("Could not convert state");
230 }
231 TrackId track = context_->track_tracker->InternTrack(
232 tracks::kClockFrequencyBlueprint,
233 tracks::Dimensions(base::StringView(args["name"])));
234 context_->event_tracker->PushCounter(line.ts, rate.value(), track);
235 } else if (line.event_name == "clock_enable" ||
236 line.event_name == "clock_disable") {
237 auto rate = base::StringToUInt32(args["state"]);
238 if (!rate.has_value()) {
239 return util::Status("Could not convert state");
240 }
241 TrackId track = context_->track_tracker->InternTrack(
242 tracks::kClockStateBlueprint,
243 tracks::Dimensions(base::StringView(args["name"])));
244 context_->event_tracker->PushCounter(line.ts, rate.value(), track);
245 } else if (line.event_name == "workqueue_execute_start") {
246 auto split = base::SplitString(line.args_str, "function ");
247 StringId name_id =
248 context_->storage->InternString(base::StringView(split[1]));
249 TrackId track = context_->track_tracker->InternThreadTrack(utid);
250 context_->slice_tracker->Begin(line.ts, track, workqueue_name_id_, name_id);
251 } else if (line.event_name == "workqueue_execute_end") {
252 TrackId track = context_->track_tracker->InternThreadTrack(utid);
253 context_->slice_tracker->End(line.ts, track, workqueue_name_id_);
254 } else if (line.event_name == "thermal_temperature") {
255 TrackId track = context_->track_tracker->InternTrack(
256 tracks::kThermalTemperatureBlueprint,
257 tracks::Dimensions(base::StringView(args["thermal_zone"])));
258 auto temp = base::StringToInt32(args["temp"]);
259 if (!temp.has_value()) {
260 return util::Status("Could not convert temp");
261 }
262 context_->event_tracker->PushCounter(line.ts, temp.value(), track);
263 } else if (line.event_name == "cdev_update") {
264 TrackId track = context_->track_tracker->InternTrack(
265 tracks::kCoolingDeviceCounterBlueprint,
266 tracks::Dimensions(base::StringView(args["type"])));
267 auto target = base::StringToDouble(args["target"]);
268 if (!target.has_value()) {
269 return util::Status("Could not convert target");
270 }
271 context_->event_tracker->PushCounter(line.ts, target.value(), track);
272 } else if (line.event_name == "sched_blocked_reason") {
273 auto wakee_pid = base::StringToUInt32(args["pid"]);
274 if (!wakee_pid.has_value()) {
275 return util::Status("sched_blocked_reason: could not parse wakee_pid");
276 }
277 auto wakee_utid = context_->process_tracker->GetOrCreateThread(*wakee_pid);
278 auto io_wait = base::StringToInt32(args["iowait"]);
279 if (!io_wait.has_value()) {
280 return util::Status("sched_blocked_reason: could not parse io_wait");
281 }
282 StringId blocked_function =
283 context_->storage->InternString(base::StringView(args["caller"]));
284 ThreadStateTracker::GetOrCreate(context_)->PushBlockedReason(
285 wakee_utid, static_cast<bool>(*io_wait), blocked_function);
286 } else if (line.event_name == "rss_stat") {
287 // Format: rss_stat: size=8437760 member=1 curr=1 mm_id=2824390453
288 auto size = base::StringToInt64(args["size"]);
289 auto member = base::StringToUInt32(args["member"]);
290 auto mm_id = base::StringToInt64(args["mm_id"]);
291 auto opt_curr = base::StringToUInt32(args["curr"]);
292 if (!size.has_value()) {
293 return util::Status("rss_stat: could not parse size");
294 }
295 if (!member.has_value()) {
296 return util::Status("rss_stat: could not parse member");
297 }
298 std::optional<bool> curr;
299 if (!opt_curr.has_value()) {
300 curr = std::make_optional(static_cast<bool>(*opt_curr));
301 }
302 rss_stat_tracker_.ParseRssStat(line.ts, line.pid, *size, *member, curr,
303 mm_id);
304 }
305
306 return util::OkStatus();
307 }
308
309 } // namespace perfetto::trace_processor
310