• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PERF_EVENT_ATTR_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_EVENT_ATTR_H_
19 
20 #include <sys/types.h>
21 #include <cstddef>
22 #include <cstdint>
23 #include <optional>
24 #include <unordered_map>
25 
26 #include "perfetto/trace_processor/ref_counted.h"
27 #include "src/trace_processor/importers/perf/perf_counter.h"
28 #include "src/trace_processor/importers/perf/perf_event.h"
29 #include "src/trace_processor/tables/profiler_tables_py.h"
30 
31 namespace perfetto::trace_processor {
32 
33 class TraceProcessorContext;
34 
35 namespace perf_importer {
36 
37 // Wrapper around a `perf_event_attr` object that add some helper methods.
38 class PerfEventAttr : public RefCounted {
39  public:
40   PerfEventAttr(TraceProcessorContext* context,
41                 tables::PerfSessionTable::Id perf_session_id_,
42                 perf_event_attr attr);
43   ~PerfEventAttr();
type()44   uint32_t type() const { return attr_.type; }
config()45   uint64_t config() const { return attr_.config; }
sample_type()46   uint64_t sample_type() const { return attr_.sample_type; }
read_format()47   uint64_t read_format() const { return attr_.read_format; }
sample_id_all()48   bool sample_id_all() const { return !!attr_.sample_id_all; }
49 
50   // Returns period if set.
sample_period()51   std::optional<uint64_t> sample_period() const {
52     // attr_.freq tells whether attr_.sample_period or attr_.sample_freq is set.
53     return attr_.freq ? std::nullopt : std::make_optional(attr_.sample_period);
54   }
55 
56   // Returns frequency if set.
sample_freq()57   std::optional<uint64_t> sample_freq() const {
58     // attr_.freq tells whether attr_.sample_period or attr_.sample_freq is set.
59     return attr_.freq ? std::make_optional(attr_.sample_freq) : std::nullopt;
60   }
61 
62   // Offset from the end of a record's payload to the time filed (if present).
63   // To be used with non `PERF_RECORD_SAMPLE` records
time_offset_from_end()64   std::optional<size_t> time_offset_from_end() const {
65     return time_offset_from_end_;
66   }
67 
68   // Offset from the start of a record's payload to the time filed (if present).
69   // To be used with `PERF_RECORD SAMPLE` records
time_offset_from_start()70   std::optional<size_t> time_offset_from_start() const {
71     return time_offset_from_start_;
72   }
73 
74   // Offsets from start and end of record payload to the id field. These offsets
75   // are used to determine the event_id and thus the perf_event_attr value of a
76   // record. During tokenization we need to determine the `sample_type` to be
77   // able to later parse the record. The `sample_type` is stored in the
78   // `perf_event_attr` structure.
79 
80   // To be used with PERF_SAMPLE_RECORD records
id_offset_from_start()81   std::optional<size_t> id_offset_from_start() const {
82     return id_offset_from_start_;
83   }
84   // To be used with non PERF_SAMPLE_RECORD records if `sample_id_all` is set.
id_offset_from_end()85   std::optional<size_t> id_offset_from_end() const {
86     return id_offset_from_end_;
87   }
88 
set_event_name(std::string event_name)89   void set_event_name(std::string event_name) {
90     event_name_ = std::move(event_name);
91   }
92 
93   PerfCounter& GetOrCreateCounter(uint32_t cpu) const;
94 
95  private:
is_timebase()96   bool is_timebase() const {
97     // This is what simpleperf uses for events that are not supposed to sample
98     // TODO(b/334978369): Determine if there is a better way to figure this out.
99     return attr_.sample_period < (1ull << 62);
100   }
101 
102   PerfCounter CreateCounter(uint32_t cpu) const;
103 
104   TraceProcessorContext* const context_;
105   tables::PerfSessionTable::Id perf_session_id_;
106   perf_event_attr attr_;
107   std::optional<size_t> time_offset_from_start_;
108   std::optional<size_t> time_offset_from_end_;
109   std::optional<size_t> id_offset_from_start_;
110   std::optional<size_t> id_offset_from_end_;
111   mutable std::unordered_map<uint32_t, PerfCounter> counters_;
112   std::string event_name_;
113 };
114 
115 }  // namespace perf_importer
116 }  // namespace perfetto::trace_processor
117 
118 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_EVENT_ATTR_H_
119