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