blob: 445dda15d7fb73211f72eea2ec0c07b9b7b43f16 [file] [log] [blame]
Lalit Magantid54d7522019-05-30 14:36:08 +01001/*
2 * Copyright (C) 2018 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
Eric Secklerd8b52082019-10-17 15:58:38 +010017#include "src/trace_processor/importers/systrace/systrace_trace_parser.h"
Lalit Magantid54d7522019-05-30 14:36:08 +010018
Hector Dearmanb6390cc2020-04-22 12:54:19 +010019#include "perfetto/base/logging.h"
20#include "perfetto/ext/base/string_splitter.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010021#include "perfetto/ext/base/string_utils.h"
Hector Dearmanb6390cc2020-04-22 12:54:19 +010022#include "src/trace_processor/importers/common/process_tracker.h"
Lalit Maganti2f0b41d2020-02-27 13:35:39 +000023#include "src/trace_processor/trace_sorter.h"
Lalit Magantid54d7522019-05-30 14:36:08 +010024
Bruno Bussdb8546e2020-01-17 12:35:05 +000025#include <cctype>
Primiano Tucci58d2dc62021-06-24 16:03:24 +010026#include <cinttypes>
Lalit Magantid54d7522019-05-30 14:36:08 +010027#include <string>
28#include <unordered_map>
29
30namespace perfetto {
31namespace trace_processor {
Hector Dearmanb6390cc2020-04-22 12:54:19 +010032namespace {
33
34std::vector<base::StringView> SplitOnSpaces(base::StringView str) {
35 std::vector<base::StringView> result;
36 for (size_t i = 0; i < str.size(); ++i) {
37 // Consume all spaces.
38 for (; i < str.size() && str.data()[i] == ' '; ++i)
39 ;
40 // If we haven't reached the end consume all non-spaces and add result.
41 if (i != str.size()) {
42 size_t start = i;
43 for (; i < str.size() && str.data()[i] != ' '; ++i)
44 ;
45 result.push_back(base::StringView(str.data() + start, i - start));
46 }
47 }
48 return result;
49}
50
51bool IsProcessDumpShortHeader(const std::vector<base::StringView>& tokens) {
52 return tokens.size() == 4 && tokens[0] == "USER" && tokens[1] == "PID" &&
53 tokens[2] == "TID" && tokens[3] == "CMD";
54}
55
56bool IsProcessDumpLongHeader(const std::vector<base::StringView>& tokens) {
57 return tokens.size() > 4 && tokens[0] == "USER" && tokens[1] == "PID" &&
58 tokens[2] == "PPID" && tokens[3] == "VSZ";
59}
60
61} // namespace
Lalit Magantid54d7522019-05-30 14:36:08 +010062
Lalit Magantid54d7522019-05-30 14:36:08 +010063SystraceTraceParser::SystraceTraceParser(TraceProcessorContext* ctx)
Hector Dearmanb6390cc2020-04-22 12:54:19 +010064 : line_parser_(ctx), ctx_(ctx) {}
Lalit Magantid54d7522019-05-30 14:36:08 +010065SystraceTraceParser::~SystraceTraceParser() = default;
66
Primiano Tucci3264b592021-11-08 18:20:51 +000067util::Status SystraceTraceParser::Parse(TraceBlobView blob) {
Lalit Magantid54d7522019-05-30 14:36:08 +010068 if (state_ == ParseState::kEndOfSystrace)
69 return util::OkStatus();
Primiano Tucci3264b592021-11-08 18:20:51 +000070 partial_buf_.insert(partial_buf_.end(), blob.data(),
71 blob.data() + blob.size());
Lalit Magantid54d7522019-05-30 14:36:08 +010072
73 if (state_ == ParseState::kBeforeParse) {
74 state_ = partial_buf_[0] == '<' ? ParseState::kHtmlBeforeSystrace
75 : ParseState::kSystrace;
76 }
77
Isabelle Taylor8b4740b2019-10-25 10:25:24 +010078 // There can be multiple trace data sections in an HTML trace, we want to
79 // ignore any that don't contain systrace data. In the future it would be
80 // good to also parse the process dump section.
81 const char kTraceDataSection[] =
Lalit Magantid54d7522019-05-30 14:36:08 +010082 R"(<script class="trace-data" type="application/text">)";
83 auto start_it = partial_buf_.begin();
84 for (;;) {
85 auto line_it = std::find(start_it, partial_buf_.end(), '\n');
86 if (line_it == partial_buf_.end())
87 break;
88
89 std::string buffer(start_it, line_it);
Isabelle Taylor8b4740b2019-10-25 10:25:24 +010090
Lalit Magantid54d7522019-05-30 14:36:08 +010091 if (state_ == ParseState::kHtmlBeforeSystrace) {
Isabelle Taylor8b4740b2019-10-25 10:25:24 +010092 if (base::Contains(buffer, kTraceDataSection)) {
93 state_ = ParseState::kTraceDataSection;
94 }
95 } else if (state_ == ParseState::kTraceDataSection) {
Lalit Maganti9cee2d62021-02-01 16:19:50 +000096 if (base::StartsWith(buffer, "#") && base::Contains(buffer, "TASK-PID")) {
Lalit Magantid54d7522019-05-30 14:36:08 +010097 state_ = ParseState::kSystrace;
Hector Dearmanb6390cc2020-04-22 12:54:19 +010098 } else if (base::StartsWith(buffer, "PROCESS DUMP")) {
99 state_ = ParseState::kProcessDumpLong;
Lalit Maganti86dfcdb2020-08-19 17:25:51 +0100100 } else if (base::StartsWith(buffer, "CGROUP DUMP")) {
101 state_ = ParseState::kCgroupDump;
Isabelle Taylor8b4740b2019-10-25 10:25:24 +0100102 } else if (base::Contains(buffer, R"(</script>)")) {
103 state_ = ParseState::kHtmlBeforeSystrace;
Lalit Magantid54d7522019-05-30 14:36:08 +0100104 }
105 } else if (state_ == ParseState::kSystrace) {
106 if (base::Contains(buffer, R"(</script>)")) {
Isabelle Taylor8b4740b2019-10-25 10:25:24 +0100107 state_ = ParseState::kEndOfSystrace;
Lalit Magantid54d7522019-05-30 14:36:08 +0100108 break;
Lalit Maganti5e7e1482020-03-31 13:23:25 +0100109 } else if (!base::StartsWith(buffer, "#") && !buffer.empty()) {
Lalit Maganti2f0b41d2020-02-27 13:35:39 +0000110 SystraceLine line;
111 util::Status status = line_tokenizer_.Tokenize(buffer, &line);
Lalit Magantic00223d2020-07-15 13:17:07 +0100112 if (status.ok()) {
113 line_parser_.ParseLine(std::move(line));
114 } else {
115 ctx_->storage->IncrementStats(stats::systrace_parse_failure);
116 }
Lalit Magantid54d7522019-05-30 14:36:08 +0100117 }
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100118 } else if (state_ == ParseState::kProcessDumpLong ||
119 state_ == ParseState::kProcessDumpShort) {
120 if (base::Contains(buffer, R"(</script>)")) {
121 state_ = ParseState::kHtmlBeforeSystrace;
122 } else {
123 std::vector<base::StringView> tokens =
124 SplitOnSpaces(base::StringView(buffer));
125 if (IsProcessDumpShortHeader(tokens)) {
126 state_ = ParseState::kProcessDumpShort;
127 } else if (IsProcessDumpLongHeader(tokens)) {
128 state_ = ParseState::kProcessDumpLong;
129 } else if (state_ == ParseState::kProcessDumpLong &&
130 tokens.size() >= 10) {
131 // Format is:
132 // user pid ppid vsz rss wchan pc s name my cmd line
133 const base::Optional<uint32_t> pid =
134 base::StringToUInt32(tokens[1].ToStdString());
135 const base::Optional<uint32_t> ppid =
136 base::StringToUInt32(tokens[2].ToStdString());
Hector Dearman1389d0b2020-04-23 13:15:46 +0100137 base::StringView name = tokens[8];
138 // Command line may contain spaces, merge all remaining tokens:
139 const char* cmd_start = tokens[9].data();
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100140 base::StringView cmd(
Hector Dearman1389d0b2020-04-23 13:15:46 +0100141 cmd_start,
142 static_cast<size_t>((buffer.data() + buffer.size()) - cmd_start));
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100143 if (!pid || !ppid) {
144 PERFETTO_ELOG("Could not parse line '%s'", buffer.c_str());
145 return util::ErrStatus("Could not parse PROCESS DUMP line");
146 }
Lalit Maganti4662a202020-07-08 13:09:52 +0100147 ctx_->process_tracker->SetProcessMetadata(pid.value(), ppid, name,
148 base::StringView());
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100149 } else if (state_ == ParseState::kProcessDumpShort &&
150 tokens.size() >= 4) {
151 // Format is:
152 // username pid tid my cmd line
153 const base::Optional<uint32_t> tgid =
154 base::StringToUInt32(tokens[1].ToStdString());
155 const base::Optional<uint32_t> tid =
156 base::StringToUInt32(tokens[2].ToStdString());
Hector Dearman1389d0b2020-04-23 13:15:46 +0100157 // Command line may contain spaces, merge all remaining tokens:
158 const char* cmd_start = tokens[3].data();
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100159 base::StringView cmd(
Hector Dearman1389d0b2020-04-23 13:15:46 +0100160 cmd_start,
161 static_cast<size_t>((buffer.data() + buffer.size()) - cmd_start));
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100162 StringId cmd_id =
163 ctx_->storage->mutable_string_pool()->InternString(cmd);
164 if (!tid || !tgid) {
165 PERFETTO_ELOG("Could not parse line '%s'", buffer.c_str());
166 return util::ErrStatus("Could not parse PROCESS DUMP line");
167 }
Mikhail Khokhlov642b8352020-07-24 10:04:39 +0100168 UniqueTid utid =
169 ctx_->process_tracker->UpdateThread(tid.value(), tgid.value());
170 ctx_->process_tracker->UpdateThreadNameByUtid(
171 utid, cmd_id, ThreadNamePriority::kOther);
Hector Dearmanb6390cc2020-04-22 12:54:19 +0100172 }
173 }
Lalit Maganti86dfcdb2020-08-19 17:25:51 +0100174 } else if (state_ == ParseState::kCgroupDump) {
175 if (base::Contains(buffer, R"(</script>)")) {
176 state_ = ParseState::kHtmlBeforeSystrace;
177 }
178 // TODO(lalitm): see if it is important to parse this.
Lalit Magantid54d7522019-05-30 14:36:08 +0100179 }
180 start_it = line_it + 1;
181 }
182 if (state_ == ParseState::kEndOfSystrace) {
183 partial_buf_.clear();
184 } else {
185 partial_buf_.erase(partial_buf_.begin(), start_it);
186 }
187 return util::OkStatus();
188}
189
Primiano Tucci40da82f2020-02-13 18:04:35 +0000190void SystraceTraceParser::NotifyEndOfFile() {}
191
Lalit Magantid54d7522019-05-30 14:36:08 +0100192} // namespace trace_processor
193} // namespace perfetto