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 has_trailing_sample_idRecord52 bool has_trailing_sample_id() const { 53 if (!attr) { 54 return false; 55 } 56 return attr->sample_id_all() && header.type != PERF_RECORD_SAMPLE && 57 header.type < PERF_RECORD_USER_TYPE_START; 58 } 59 mmap_has_build_idRecord60 bool mmap_has_build_id() const { 61 return header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID; 62 } 63 64 // Returns the payload offset to the time field if present. time_offsetRecord65 std::optional<size_t> time_offset() const { 66 if (!attr) { 67 return std::nullopt; 68 } 69 return header.type == PERF_RECORD_SAMPLE ? attr->time_offset_from_start() 70 : attr->time_offset_from_end(); 71 } 72 73 RefPtr<PerfSession> session; 74 RefPtr<PerfEventAttr> attr; 75 perf_event_header header; 76 TraceBlobView payload; 77 }; 78 79 } // namespace perfetto::trace_processor::perf_importer 80 81 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_H_ 82