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_RECORD_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_H_ 19 20 #include <optional> 21 22 #include "perfetto/trace_processor/ref_counted.h" 23 #include "perfetto/trace_processor/trace_blob_view.h" 24 #include "protos/perfetto/trace/profiling/profile_packet.pbzero.h" 25 #include "src/trace_processor/importers/perf/perf_event.h" 26 #include "src/trace_processor/importers/perf/perf_event_attr.h" 27 #include "src/trace_processor/importers/perf/perf_session.h" 28 29 namespace perfetto::trace_processor::perf_importer { 30 31 // Minimally parsed perf event record. Contains enough information to be able to 32 // send the record to the sorting stage. 33 struct Record { 34 public: GetCpuModeRecord35 protos::pbzero::Profiling::CpuMode GetCpuMode() const { 36 switch (header.misc & kPerfRecordMiscCpumodeMask) { 37 case PERF_RECORD_MISC_KERNEL: 38 return protos::pbzero::Profiling::MODE_KERNEL; 39 case PERF_RECORD_MISC_USER: 40 return protos::pbzero::Profiling::MODE_USER; 41 case PERF_RECORD_MISC_HYPERVISOR: 42 return protos::pbzero::Profiling::MODE_HYPERVISOR; 43 case PERF_RECORD_MISC_GUEST_KERNEL: 44 return protos::pbzero::Profiling::MODE_GUEST_KERNEL; 45 case PERF_RECORD_MISC_GUEST_USER: 46 return protos::pbzero::Profiling::MODE_GUEST_USER; 47 default: 48 return protos::pbzero::Profiling::MODE_UNKNOWN; 49 } 50 } 51 mmap_has_build_idRecord52 bool mmap_has_build_id() const { 53 return header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID; 54 } 55 56 // Returns the payload offset to the time field if present. time_offsetRecord57 std::optional<size_t> time_offset() const { 58 if (!attr) { 59 return std::nullopt; 60 } 61 return header.type == PERF_RECORD_SAMPLE ? attr->time_offset_from_start() 62 : attr->time_offset_from_end(); 63 } 64 65 RefPtr<PerfSession> session; 66 RefPtr<const PerfEventAttr> attr; 67 perf_event_header header; 68 TraceBlobView payload; 69 }; 70 71 } // namespace perfetto::trace_processor::perf_importer 72 73 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_H_ 74