1 /* 2 * Copyright (C) 2019 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 #pragma once 18 19 #include <inttypes.h> 20 21 #include <map> 22 #include <memory> 23 24 #include <android-base/expected.h> 25 26 #include "event_type.h" 27 #include "perf_event.h" 28 #include "record.h" 29 30 namespace simpleperf { 31 32 struct ETMPerCpu { 33 uint32_t trcidr0; 34 uint32_t trcidr1; 35 uint32_t trcidr2; 36 uint32_t trcidr4; 37 uint32_t trcidr8; 38 uint32_t trcauthstatus; 39 uint32_t trcdevarch; 40 41 int GetMajorVersion() const; 42 bool IsContextIDSupported() const; 43 bool IsTimestampSupported() const; 44 bool IsCycAccSupported() const; 45 bool IsEnabled() const; 46 }; 47 48 // Help recording Coresight ETM data on ARM devices. 49 // 1. Get etm event type on device. 50 // 2. Get sink config, which selects the ETR device moving etm data to memory. 51 // 3. Get etm info on each cpu. 52 // The etm event type and sink config are used to build perf_event_attr for etm data tracing. 53 // The etm info is kept in perf.data to help etm decoding. 54 class ETMRecorder { 55 public: 56 static ETMRecorder& GetInstance(); 57 58 // If not found, return -1. 59 int GetEtmEventType(); 60 std::unique_ptr<EventType> BuildEventType(); 61 bool IsETMDriverAvailable(); 62 android::base::expected<bool, std::string> CheckEtmSupport(); 63 void SetEtmPerfEventAttr(perf_event_attr* attr); 64 AuxTraceInfoRecord CreateAuxTraceInfoRecord(); 65 size_t GetAddrFilterPairs(); 66 void SetRecordTimestamp(bool record); 67 void SetRecordCycles(bool record); 68 void SetCycleThreshold(size_t threshold); 69 70 private: 71 bool ReadEtmInfo(); 72 bool FindSinkConfig(); 73 void BuildEtmConfig(); 74 75 int event_type_ = 0; 76 bool etm_supported_ = false; 77 // select ETR device, setting in perf_event_attr->config2 78 uint32_t sink_config_ = 0; 79 // use EL2 PID tracing or not 80 bool use_contextid2_ = false; 81 // select etm options (timestamp, context_id, ...), setting in perf_event_attr->config 82 uint64_t etm_event_config_ = 0; 83 // set cycle count threshold using perf_event_attr->config3 84 uint64_t cc_threshold_config_ = 0; 85 // record etm options in AuxTraceInfoRecord 86 uint32_t etm_config_reg_ = 0; 87 std::map<int, ETMPerCpu> etm_info_; 88 89 bool record_timestamp_ = false; 90 bool record_cycles_ = false; 91 uint64_t cycle_threshold_ = 0; 92 }; 93 94 } // namespace simpleperf 95