aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/importers/systrace/systrace_line_parser.cc
blob: 4cfd788e8cef267b148f63fbd501a997db871611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/trace_processor/importers/systrace/systrace_line_parser.h"

#include "perfetto/ext/base/flat_hash_map.h"
#include "perfetto/ext/base/string_splitter.h"
#include "perfetto/ext/base/string_utils.h"
#include "src/trace_processor/importers/common/args_tracker.h"
#include "src/trace_processor/importers/common/event_tracker.h"
#include "src/trace_processor/importers/common/process_tracker.h"
#include "src/trace_processor/importers/common/slice_tracker.h"
#include "src/trace_processor/importers/common/track_tracker.h"
#include "src/trace_processor/importers/ftrace/binder_tracker.h"
#include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
#include "src/trace_processor/importers/systrace/systrace_parser.h"
#include "src/trace_processor/types/task_state.h"

#include <cctype>
#include <cinttypes>
#include <string>

namespace perfetto {
namespace trace_processor {

SystraceLineParser::SystraceLineParser(TraceProcessorContext* ctx)
    : context_(ctx),
      rss_stat_tracker_(context_),
      sched_wakeup_name_id_(ctx->storage->InternString("sched_wakeup")),
      sched_waking_name_id_(ctx->storage->InternString("sched_waking")),
      cpufreq_name_id_(ctx->storage->InternString("cpufreq")),
      cpuidle_name_id_(ctx->storage->InternString("cpuidle")),
      workqueue_name_id_(ctx->storage->InternString("workqueue")),
      sched_blocked_reason_id_(
          ctx->storage->InternString("sched_blocked_reason")),
      io_wait_id_(ctx->storage->InternString("io_wait")),
      waker_utid_id_(ctx->storage->InternString("waker_utid")) {}

util::Status SystraceLineParser::ParseLine(const SystraceLine& line) {
  auto utid = context_->process_tracker->UpdateThreadName(
      line.pid, context_->storage->InternString(base::StringView(line.task)),
      ThreadNamePriority::kFtrace);

  if (!line.tgid_str.empty() && line.tgid_str != "-----") {
    base::Optional<uint32_t> tgid = base::StringToUInt32(line.tgid_str);
    if (tgid) {
      context_->process_tracker->UpdateThread(line.pid, tgid.value());
    }
  }

  base::FlatHashMap<std::string, std::string> args;
  for (base::StringSplitter ss(line.args_str, ' '); ss.Next();) {
    std::string key;
    std::string value;
    if (!base::Contains(ss.cur_token(), "=")) {
      key = "name";
      value = ss.cur_token();
      args.Insert(std::move(key), std::move(value));
      continue;
    }
    for (base::StringSplitter inner(ss.cur_token(), '='); inner.Next();) {
      if (key.empty()) {
        key = inner.cur_token();
      } else {
        value = inner.cur_token();
      }
    }
    args.Insert(std::move(key), std::move(value));
  }
  if (line.event_name == "sched_switch") {
    auto prev_state_str = args["prev_state"];
    int64_t prev_state =
        ftrace_utils::TaskState(prev_state_str.c_str()).raw_state();

    auto prev_pid = base::StringToUInt32(args["prev_pid"]);
    auto prev_comm = base::StringView(args["prev_comm"]);
    auto prev_prio = base::StringToInt32(args["prev_prio"]);
    auto next_pid = base::StringToUInt32(args["next_pid"]);
    auto next_comm = base::StringView(args["next_comm"]);
    auto next_prio = base::StringToInt32(args["next_prio"]);

    if (!(prev_pid.has_value() && prev_prio.has_value() &&
          next_pid.has_value() && next_prio.has_value())) {
      return util::Status("Could not parse sched_switch");
    }

    SchedEventTracker::GetOrCreate(context_)->PushSchedSwitch(
        line.cpu, line.ts, prev_pid.value(), prev_comm, prev_prio.value(),
        prev_state, next_pid.value(), next_comm, next_prio.value());
  } else if (line.event_name == "tracing_mark_write" ||
             line.event_name == "0" || line.event_name == "print") {
    SystraceParser::GetOrCreate(context_)->ParsePrintEvent(
        line.ts, line.pid, line.args_str.c_str());
  } else if (line.event_name == "sched_wakeup" ||
             line.event_name == "sched_waking") {
    auto comm = args["comm"];
    base::Optional<uint32_t> wakee_pid = base::StringToUInt32(args["pid"]);
    if (!wakee_pid.has_value()) {
      return util::Status("Could not convert wakee_pid");
    }

    StringId name_id = context_->storage->InternString(base::StringView(comm));
    auto wakee_utid = context_->process_tracker->UpdateThreadName(
        wakee_pid.value(), name_id, ThreadNamePriority::kFtrace);

    StringId event_name_id = line.event_name == "sched_wakeup"
                                 ? sched_wakeup_name_id_
                                 : sched_waking_name_id_;
    InstantId instant_id = context_->event_tracker->PushInstant(
        line.ts, event_name_id, wakee_utid, RefType::kRefUtid);
    context_->args_tracker->AddArgsTo(instant_id)
        .AddArg(waker_utid_id_, Variadic::UnsignedInteger(utid));

  } else if (line.event_name == "cpu_frequency") {
    base::Optional<uint32_t> event_cpu = base::StringToUInt32(args["cpu_id"]);
    base::Optional<double> new_state = base::StringToDouble(args["state"]);
    if (!event_cpu.has_value()) {
      return util::Status("Could not convert event cpu");
    }
    if (!event_cpu.has_value()) {
      return util::Status("Could not convert state");
    }

    TrackId track = context_->track_tracker->InternCpuCounterTrack(
        cpufreq_name_id_, event_cpu.value());
    context_->event_tracker->PushCounter(line.ts, new_state.value(), track);
  } else if (line.event_name == "cpu_idle") {
    base::Optional<uint32_t> event_cpu = base::StringToUInt32(args["cpu_id"]);
    base::Optional<double> new_state = base::StringToDouble(args["state"]);
    if (!event_cpu.has_value()) {
      return util::Status("Could not convert event cpu");
    }
    if (!event_cpu.has_value()) {
      return util::Status("Could not convert state");
    }

    TrackId track = context_->track_tracker->InternCpuCounterTrack(
        cpuidle_name_id_, event_cpu.value());
    context_->event_tracker->PushCounter(line.ts, new_state.value(), track);
  } else if (line.event_name == "binder_transaction") {
    auto id = base::StringToInt32(args["transaction"]);
    auto dest_node = base::StringToInt32(args["dest_node"]);
    auto dest_tgid = base::StringToUInt32(args["dest_proc"]);
    auto dest_tid = base::StringToUInt32(args["dest_thread"]);
    auto is_reply = base::StringToInt32(args["reply"]).value() == 1;
    auto flags_str = args["flags"];
    char* end;
    uint32_t flags = static_cast<uint32_t>(strtol(flags_str.c_str(), &end, 16));
    std::string code_str = args["code"] + " Java Layer Dependent";
    StringId code = context_->storage->InternString(base::StringView(code_str));
    if (!dest_tgid.has_value()) {
      return util::Status("Could not convert dest_tgid");
    }
    if (!dest_tid.has_value()) {
      return util::Status("Could not convert dest_tid");
    }
    if (!id.has_value()) {
      return util::Status("Could not convert transaction id");
    }
    if (!dest_node.has_value()) {
      return util::Status("Could not covert dest node");
    }
    BinderTracker::GetOrCreate(context_)->Transaction(
        line.ts, line.pid, id.value(), dest_node.value(), dest_tgid.value(),
        dest_tid.value(), is_reply, flags, code);
  } else if (line.event_name == "binder_transaction_received") {
    auto id = base::StringToInt32(args["transaction"]);
    if (!id.has_value()) {
      return util::Status("Could not convert transaction id");
    }
    BinderTracker::GetOrCreate(context_)->TransactionReceived(line.ts, line.pid,
                                                              id.value());
  } else if (line.event_name == "binder_lock") {
    BinderTracker::GetOrCreate(context_)->Lock(line.ts, line.pid);
  } else if (line.event_name == "binder_locked") {
    BinderTracker::GetOrCreate(context_)->Locked(line.ts, line.pid);
  } else if (line.event_name == "binder_unlock") {
    BinderTracker::GetOrCreate(context_)->Unlock(line.ts, line.pid);
  } else if (line.event_name == "binder_transaction_alloc_buf") {
    auto data_size = base::StringToUInt64(args["data_size"]);
    auto offsets_size = base::StringToUInt64(args["offsets_size"]);
    if (!data_size.has_value()) {
      return util::Status("Could not convert data size");
    }
    if (!offsets_size.has_value()) {
      return util::Status("Could not convert offsets size");
    }
    BinderTracker::GetOrCreate(context_)->TransactionAllocBuf(
        line.ts, line.pid, data_size.value(), offsets_size.value());
  } else if (line.event_name == "clock_set_rate" ||
             line.event_name == "clock_enable" ||
             line.event_name == "clock_disable") {
    std::string subtitle =
        line.event_name == "clock_set_rate" ? " Frequency" : " State";
    auto rate = base::StringToUInt32(args["state"]);
    if (!rate.has_value()) {
      return util::Status("Could not convert state");
    }
    std::string clock_name_str = args["name"] + subtitle;
    StringId clock_name =
        context_->storage->InternString(base::StringView(clock_name_str));
    TrackId track =
        context_->track_tracker->InternGlobalCounterTrack(clock_name);
    context_->event_tracker->PushCounter(line.ts, rate.value(), track);
  } else if (line.event_name == "workqueue_execute_start") {
    auto split = base::SplitString(line.args_str, "function ");
    StringId name_id =
        context_->storage->InternString(base::StringView(split[1]));
    TrackId track = context_->track_tracker->InternThreadTrack(utid);
    context_->slice_tracker->Begin(line.ts, track, workqueue_name_id_, name_id);
  } else if (line.event_name == "workqueue_execute_end") {
    TrackId track = context_->track_tracker->InternThreadTrack(utid);
    context_->slice_tracker->End(line.ts, track, workqueue_name_id_);
  } else if (line.event_name == "thermal_temperature") {
    std::string thermal_zone = args["thermal_zone"] + " Temperature";
    StringId track_name =
        context_->storage->InternString(base::StringView(thermal_zone));
    TrackId track =
        context_->track_tracker->InternGlobalCounterTrack(track_name);
    auto temp = base::StringToInt32(args["temp"]);
    if (!temp.has_value()) {
      return util::Status("Could not convert temp");
    }
    context_->event_tracker->PushCounter(line.ts, temp.value(), track);
  } else if (line.event_name == "cdev_update") {
    std::string type = args["type"] + " Cooling Device";
    StringId track_name =
        context_->storage->InternString(base::StringView(type));
    TrackId track =
        context_->track_tracker->InternGlobalCounterTrack(track_name);
    auto target = base::StringToDouble(args["target"]);
    if (!target.has_value()) {
      return util::Status("Could not convert target");
    }
    context_->event_tracker->PushCounter(line.ts, target.value(), track);
  } else if (line.event_name == "sched_blocked_reason") {
    auto wakee_pid = base::StringToUInt32(args["pid"]);
    if (!wakee_pid.has_value()) {
      return util::Status("sched_blocked_reason: could not parse wakee_pid");
    }
    auto wakee_utid = context_->process_tracker->GetOrCreateThread(*wakee_pid);

    InstantId id = context_->event_tracker->PushInstant(
        line.ts, sched_blocked_reason_id_, wakee_utid, RefType::kRefUtid,
        false);

    auto inserter = context_->args_tracker->AddArgsTo(id);
    auto io_wait = base::StringToInt32(args["iowait"]);
    if (!io_wait.has_value()) {
      return util::Status("sched_blocked_reason: could not parse io_wait");
    }
    inserter.AddArg(io_wait_id_, Variadic::Boolean(*io_wait));
    context_->args_tracker->Flush();
  } else if (line.event_name == "rss_stat") {
    // Format: rss_stat: size=8437760 member=1 curr=1 mm_id=2824390453
    auto size = base::StringToInt64(args["size"]);
    auto member = base::StringToUInt32(args["member"]);
    auto mm_id = base::StringToInt64(args["mm_id"]);
    auto opt_curr = base::StringToUInt32(args["curr"]);
    if (!size.has_value()) {
      return util::Status("rss_stat: could not parse size");
    }
    if (!member.has_value()) {
      return util::Status("rss_stat: could not parse member");
    }
    base::Optional<bool> curr;
    if (!opt_curr.has_value()) {
      curr = base::make_optional(static_cast<bool>(*opt_curr));
    }
    rss_stat_tracker_.ParseRssStat(line.ts, line.pid, *size, *member, curr,
                                   mm_id);
  }

  return util::OkStatus();
}

}  // namespace trace_processor
}  // namespace perfetto