1*288bf522SAndroid Build Coastguard Worker /* 2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker * 4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker * 8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker * 10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker * limitations under the License. 15*288bf522SAndroid Build Coastguard Worker */ 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Worker #pragma once 18*288bf522SAndroid Build Coastguard Worker 19*288bf522SAndroid Build Coastguard Worker #include <inttypes.h> 20*288bf522SAndroid Build Coastguard Worker 21*288bf522SAndroid Build Coastguard Worker #include <map> 22*288bf522SAndroid Build Coastguard Worker #include <memory> 23*288bf522SAndroid Build Coastguard Worker 24*288bf522SAndroid Build Coastguard Worker #include <android-base/expected.h> 25*288bf522SAndroid Build Coastguard Worker 26*288bf522SAndroid Build Coastguard Worker #include "event_type.h" 27*288bf522SAndroid Build Coastguard Worker #include "perf_event.h" 28*288bf522SAndroid Build Coastguard Worker #include "record.h" 29*288bf522SAndroid Build Coastguard Worker 30*288bf522SAndroid Build Coastguard Worker namespace simpleperf { 31*288bf522SAndroid Build Coastguard Worker 32*288bf522SAndroid Build Coastguard Worker struct ETMPerCpu { 33*288bf522SAndroid Build Coastguard Worker uint32_t trcidr0; 34*288bf522SAndroid Build Coastguard Worker uint32_t trcidr1; 35*288bf522SAndroid Build Coastguard Worker uint32_t trcidr2; 36*288bf522SAndroid Build Coastguard Worker uint32_t trcidr4; 37*288bf522SAndroid Build Coastguard Worker uint32_t trcidr8; 38*288bf522SAndroid Build Coastguard Worker uint32_t trcauthstatus; 39*288bf522SAndroid Build Coastguard Worker uint32_t trcdevarch; 40*288bf522SAndroid Build Coastguard Worker 41*288bf522SAndroid Build Coastguard Worker int GetMajorVersion() const; 42*288bf522SAndroid Build Coastguard Worker bool IsContextIDSupported() const; 43*288bf522SAndroid Build Coastguard Worker bool IsTimestampSupported() const; 44*288bf522SAndroid Build Coastguard Worker bool IsCycAccSupported() const; 45*288bf522SAndroid Build Coastguard Worker bool IsEnabled() const; 46*288bf522SAndroid Build Coastguard Worker }; 47*288bf522SAndroid Build Coastguard Worker 48*288bf522SAndroid Build Coastguard Worker // Help recording Coresight ETM data on ARM devices. 49*288bf522SAndroid Build Coastguard Worker // 1. Get etm event type on device. 50*288bf522SAndroid Build Coastguard Worker // 2. Get sink config, which selects the ETR device moving etm data to memory. 51*288bf522SAndroid Build Coastguard Worker // 3. Get etm info on each cpu. 52*288bf522SAndroid Build Coastguard Worker // The etm event type and sink config are used to build perf_event_attr for etm data tracing. 53*288bf522SAndroid Build Coastguard Worker // The etm info is kept in perf.data to help etm decoding. 54*288bf522SAndroid Build Coastguard Worker class ETMRecorder { 55*288bf522SAndroid Build Coastguard Worker public: 56*288bf522SAndroid Build Coastguard Worker static ETMRecorder& GetInstance(); 57*288bf522SAndroid Build Coastguard Worker 58*288bf522SAndroid Build Coastguard Worker // If not found, return -1. 59*288bf522SAndroid Build Coastguard Worker int GetEtmEventType(); 60*288bf522SAndroid Build Coastguard Worker std::unique_ptr<EventType> BuildEventType(); 61*288bf522SAndroid Build Coastguard Worker bool IsETMDriverAvailable(); 62*288bf522SAndroid Build Coastguard Worker android::base::expected<bool, std::string> CheckEtmSupport(); 63*288bf522SAndroid Build Coastguard Worker void SetEtmPerfEventAttr(perf_event_attr* attr); 64*288bf522SAndroid Build Coastguard Worker AuxTraceInfoRecord CreateAuxTraceInfoRecord(); 65*288bf522SAndroid Build Coastguard Worker size_t GetAddrFilterPairs(); 66*288bf522SAndroid Build Coastguard Worker void SetRecordTimestamp(bool record); 67*288bf522SAndroid Build Coastguard Worker void SetRecordCycles(bool record); 68*288bf522SAndroid Build Coastguard Worker void SetCycleThreshold(size_t threshold); 69*288bf522SAndroid Build Coastguard Worker 70*288bf522SAndroid Build Coastguard Worker private: 71*288bf522SAndroid Build Coastguard Worker bool ReadEtmInfo(); 72*288bf522SAndroid Build Coastguard Worker bool FindSinkConfig(); 73*288bf522SAndroid Build Coastguard Worker void BuildEtmConfig(); 74*288bf522SAndroid Build Coastguard Worker 75*288bf522SAndroid Build Coastguard Worker int event_type_ = 0; 76*288bf522SAndroid Build Coastguard Worker bool etm_supported_ = false; 77*288bf522SAndroid Build Coastguard Worker // select ETR device, setting in perf_event_attr->config2 78*288bf522SAndroid Build Coastguard Worker uint32_t sink_config_ = 0; 79*288bf522SAndroid Build Coastguard Worker // use EL2 PID tracing or not 80*288bf522SAndroid Build Coastguard Worker bool use_contextid2_ = false; 81*288bf522SAndroid Build Coastguard Worker // select etm options (timestamp, context_id, ...), setting in perf_event_attr->config 82*288bf522SAndroid Build Coastguard Worker uint64_t etm_event_config_ = 0; 83*288bf522SAndroid Build Coastguard Worker // set cycle count threshold using perf_event_attr->config3 84*288bf522SAndroid Build Coastguard Worker uint64_t cc_threshold_config_ = 0; 85*288bf522SAndroid Build Coastguard Worker // record etm options in AuxTraceInfoRecord 86*288bf522SAndroid Build Coastguard Worker uint32_t etm_config_reg_ = 0; 87*288bf522SAndroid Build Coastguard Worker std::map<int, ETMPerCpu> etm_info_; 88*288bf522SAndroid Build Coastguard Worker 89*288bf522SAndroid Build Coastguard Worker bool record_timestamp_ = false; 90*288bf522SAndroid Build Coastguard Worker bool record_cycles_ = false; 91*288bf522SAndroid Build Coastguard Worker uint64_t cycle_threshold_ = 0; 92*288bf522SAndroid Build Coastguard Worker }; 93*288bf522SAndroid Build Coastguard Worker 94*288bf522SAndroid Build Coastguard Worker } // namespace simpleperf 95