1 /*
2 * Copyright (C) 2024 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/perf/sample_id.h"
18 #include <cstdint>
19
20 #include "perfetto/base/logging.h"
21 #include "perfetto/base/status.h"
22 #include "perfetto/ext/base/status_or.h"
23 #include "src/trace_processor/importers/perf/perf_event.h"
24 #include "src/trace_processor/importers/perf/perf_event_attr.h"
25 #include "src/trace_processor/importers/perf/reader.h"
26
27 namespace perfetto::trace_processor::perf_importer {
28
ParseFromRecord(const Record & record)29 base::Status SampleId::ParseFromRecord(const Record& record) {
30 PERFETTO_CHECK(record.header.type != PERF_RECORD_SAMPLE);
31 if (!record.attr || !record.attr->sample_id_all()) {
32 sample_type_ = 0;
33 return base::OkStatus();
34 }
35
36 Reader reader(record.payload.copy());
37
38 size_t size = record.attr->sample_id_size();
39 if (size > record.payload.size()) {
40 return base::ErrStatus(
41 "Record is too small to hold a SampleId. Expected at least %zu bytes, "
42 "but found %zu",
43 size, record.payload.size());
44 }
45
46 PERFETTO_CHECK(reader.Skip(record.payload.size() - size));
47 if (!ReadFrom(*record.attr, reader)) {
48 return base::ErrStatus("Failed to parse SampleId");
49 }
50 return base::OkStatus();
51 }
52
ReadFrom(const PerfEventAttr & attr,Reader & reader)53 bool SampleId::ReadFrom(const PerfEventAttr& attr, Reader& reader) {
54 sample_type_ = attr.sample_type();
55
56 if (sample_type_ & PERF_SAMPLE_TID) {
57 if (!reader.Read(pid_) || !reader.Read(tid_)) {
58 return false;
59 }
60 }
61 if (sample_type_ & PERF_SAMPLE_TIME) {
62 if (!reader.Read(time_)) {
63 return false;
64 }
65 }
66 if (sample_type_ & PERF_SAMPLE_ID) {
67 if (!reader.Read(id_)) {
68 return false;
69 }
70 }
71 if (sample_type_ & PERF_SAMPLE_STREAM_ID) {
72 if (!reader.Read(stream_id_)) {
73 return false;
74 }
75 }
76 if (sample_type_ & PERF_SAMPLE_CPU) {
77 if (!reader.Read(cpu_) || !reader.Skip(sizeof(uint32_t))) {
78 return false;
79 }
80 }
81 if (sample_type_ & PERF_SAMPLE_IDENTIFIER) {
82 if (!reader.Read(id_)) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 } // namespace perfetto::trace_processor::perf_importer
90