xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/sample_id.h (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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PERF_SAMPLE_ID_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_SAMPLE_ID_H_
19 
20 #include <cstdint>
21 #include <optional>
22 
23 #include "perfetto/base/status.h"
24 #include "perfetto/trace_processor/trace_blob_view.h"
25 #include "src/trace_processor/importers/perf/perf_event.h"
26 #include "src/trace_processor/importers/perf/record.h"
27 
28 namespace perfetto::trace_processor::perf_importer {
29 
30 class PerfEventAttr;
31 class Reader;
32 
33 class SampleId {
34  public:
35   base::Status ParseFromRecord(const Record& record);
36   bool ReadFrom(const PerfEventAttr& attr, Reader& reader);
37 
SampleId()38   SampleId() : sample_type_(0) {}
39 
tid()40   std::optional<uint32_t> tid() const {
41     return sample_type_ & PERF_SAMPLE_TID ? std::make_optional(tid_)
42                                           : std::nullopt;
43   }
pid()44   std::optional<uint32_t> pid() const {
45     return sample_type_ & PERF_SAMPLE_TID ? std::make_optional(pid_)
46                                           : std::nullopt;
47   }
time()48   std::optional<uint64_t> time() const {
49     return sample_type_ & PERF_SAMPLE_TIME ? std::make_optional(time_)
50                                            : std::nullopt;
51   }
id()52   std::optional<uint64_t> id() const {
53     return sample_type_ & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)
54                ? std::make_optional(id_)
55                : std::nullopt;
56   }
stream_id()57   std::optional<uint64_t> stream_id() const {
58     return sample_type_ & PERF_SAMPLE_STREAM_ID ? std::make_optional(stream_id_)
59                                                 : std::nullopt;
60   }
cpu()61   std::optional<uint32_t> cpu() const {
62     return sample_type_ & PERF_SAMPLE_CPU ? std::make_optional(cpu_)
63                                           : std::nullopt;
64   }
65 
set_cpu(std::optional<uint32_t> cpu)66   void set_cpu(std::optional<uint32_t> cpu) {
67     if (cpu.has_value()) {
68       sample_type_ |= PERF_SAMPLE_CPU;
69       cpu_ = *cpu;
70     } else {
71       sample_type_ &= ~static_cast<uint64_t>(PERF_SAMPLE_CPU);
72     }
73   }
74 
75  private:
76   uint32_t tid_;
77   uint32_t pid_;
78   uint64_t time_;
79   uint64_t id_;
80   uint64_t stream_id_;
81   uint32_t cpu_;
82   uint64_t sample_type_;
83 };
84 
85 }  // namespace perfetto::trace_processor::perf_importer
86 
87 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_SAMPLE_ID_H_
88