xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/attrs_section_reader.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/attrs_section_reader.h"
18 
19 #include <cinttypes>
20 #include <cstddef>
21 #include <utility>
22 
23 #include "perfetto/base/logging.h"
24 #include "perfetto/base/status.h"
25 #include "perfetto/ext/base/status_or.h"
26 #include "perfetto/trace_processor/trace_blob_view.h"
27 #include "src/trace_processor/importers/perf/perf_file.h"
28 
29 namespace perfetto::trace_processor::perf_importer {
30 
31 // static
Create(const PerfFile::Header & header,TraceBlobView section)32 base::StatusOr<AttrsSectionReader> AttrsSectionReader::Create(
33     const PerfFile::Header& header,
34     TraceBlobView section) {
35   PERFETTO_CHECK(section.size() == header.attrs.size);
36 
37   if (header.attr_size == 0) {
38     return base::ErrStatus("Invalid attr_size (0) in perf file header.");
39   }
40 
41   if (header.attrs.size % header.attr_size != 0) {
42     return base::ErrStatus("Invalid attrs section size %" PRIu64
43                            " for attr_size %" PRIu64 " in perf file header.",
44                            header.attrs.size, header.attr_size);
45   }
46 
47   const size_t num_attr = header.attrs.size / header.attr_size;
48 
49   // Each entry is a perf_event_attr followed by a Section, but the size of
50   // the perf_event_attr struct written in the file might not be the same as
51   // sizeof(perf_event_attr) as this struct might grow over time (can be
52   // bigger or smaller).
53   static constexpr size_t kSectionSize = sizeof(PerfFile::Section);
54   if (header.attr_size < kSectionSize) {
55     return base::ErrStatus(
56         "Invalid attr_size in file header. Expected at least %zu, found "
57         "%" PRIu64,
58         kSectionSize, header.attr_size);
59   }
60   const size_t attr_size = header.attr_size - kSectionSize;
61 
62   return AttrsSectionReader(std::move(section), num_attr, attr_size);
63 }
64 
ReadNext(PerfFile::AttrsEntry & entry)65 base::Status AttrsSectionReader::ReadNext(PerfFile::AttrsEntry& entry) {
66   PERFETTO_CHECK(reader_.ReadPerfEventAttr(entry.attr, attr_size_));
67 
68   if (entry.attr.size != attr_size_) {
69     return base::ErrStatus(
70         "Invalid attr.size. Expected %zu, but found %" PRIu32, attr_size_,
71         entry.attr.size);
72   }
73 
74   PERFETTO_CHECK(reader_.Read(entry.ids));
75   --num_attr_;
76   return base::OkStatus();
77 }
78 
79 }  // namespace perfetto::trace_processor::perf_importer
80