1*288bf522SAndroid Build Coastguard Worker /* 2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2020 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 #include <stdio.h> 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard Worker #include <atomic> 23*288bf522SAndroid Build Coastguard Worker #include <functional> 24*288bf522SAndroid Build Coastguard Worker #include <memory> 25*288bf522SAndroid Build Coastguard Worker #include <thread> 26*288bf522SAndroid Build Coastguard Worker #include <unordered_set> 27*288bf522SAndroid Build Coastguard Worker #include <vector> 28*288bf522SAndroid Build Coastguard Worker 29*288bf522SAndroid Build Coastguard Worker #include "event_attr.h" 30*288bf522SAndroid Build Coastguard Worker #include "record.h" 31*288bf522SAndroid Build Coastguard Worker 32*288bf522SAndroid Build Coastguard Worker namespace simpleperf { 33*288bf522SAndroid Build Coastguard Worker 34*288bf522SAndroid Build Coastguard Worker class MapRecordReader { 35*288bf522SAndroid Build Coastguard Worker public: MapRecordReader(const perf_event_attr & attr,uint64_t event_id,bool keep_non_executable_maps)36*288bf522SAndroid Build Coastguard Worker MapRecordReader(const perf_event_attr& attr, uint64_t event_id, bool keep_non_executable_maps) 37*288bf522SAndroid Build Coastguard Worker : attr_(attr), event_id_(event_id), keep_non_executable_maps_(keep_non_executable_maps) {} 38*288bf522SAndroid Build Coastguard Worker Attr()39*288bf522SAndroid Build Coastguard Worker const perf_event_attr& Attr() { return attr_; } SetCallback(const std::function<bool (Record *)> & callback)40*288bf522SAndroid Build Coastguard Worker void SetCallback(const std::function<bool(Record*)>& callback) { callback_ = callback; } 41*288bf522SAndroid Build Coastguard Worker bool ReadKernelMaps(); 42*288bf522SAndroid Build Coastguard Worker // Read process maps and all thread names in a process. 43*288bf522SAndroid Build Coastguard Worker bool ReadProcessMaps(pid_t pid, uint64_t timestamp); 44*288bf522SAndroid Build Coastguard Worker // Read process maps and selected thread names in a process. 45*288bf522SAndroid Build Coastguard Worker bool ReadProcessMaps(pid_t pid, const std::unordered_set<pid_t>& tids, uint64_t timestamp); 46*288bf522SAndroid Build Coastguard Worker 47*288bf522SAndroid Build Coastguard Worker private: 48*288bf522SAndroid Build Coastguard Worker const perf_event_attr& attr_; 49*288bf522SAndroid Build Coastguard Worker const uint64_t event_id_; 50*288bf522SAndroid Build Coastguard Worker const bool keep_non_executable_maps_; 51*288bf522SAndroid Build Coastguard Worker std::function<bool(Record*)> callback_; 52*288bf522SAndroid Build Coastguard Worker }; 53*288bf522SAndroid Build Coastguard Worker 54*288bf522SAndroid Build Coastguard Worker // Create a thread for reading maps while recording. The maps are stored in a temporary file, and 55*288bf522SAndroid Build Coastguard Worker // read back after recording. 56*288bf522SAndroid Build Coastguard Worker class MapRecordThread { 57*288bf522SAndroid Build Coastguard Worker public: 58*288bf522SAndroid Build Coastguard Worker MapRecordThread(const MapRecordReader& map_record_reader); 59*288bf522SAndroid Build Coastguard Worker ~MapRecordThread(); 60*288bf522SAndroid Build Coastguard Worker 61*288bf522SAndroid Build Coastguard Worker bool Join(); 62*288bf522SAndroid Build Coastguard Worker bool ReadMapRecordData(const std::function<bool(const char*, size_t)>& callback); 63*288bf522SAndroid Build Coastguard Worker bool ReadMapRecords(const std::function<void(const Record*)>& callback, bool only_kernel_maps); 64*288bf522SAndroid Build Coastguard Worker 65*288bf522SAndroid Build Coastguard Worker private: 66*288bf522SAndroid Build Coastguard Worker // functions running in the map record thread 67*288bf522SAndroid Build Coastguard Worker bool RunThread(); 68*288bf522SAndroid Build Coastguard Worker bool WriteRecordToFile(Record* record); 69*288bf522SAndroid Build Coastguard Worker 70*288bf522SAndroid Build Coastguard Worker MapRecordReader map_record_reader_; 71*288bf522SAndroid Build Coastguard Worker std::unique_ptr<TemporaryFile> tmpfile_; 72*288bf522SAndroid Build Coastguard Worker std::unique_ptr<FILE, decltype(&fclose)> fp_; 73*288bf522SAndroid Build Coastguard Worker std::thread thread_; 74*288bf522SAndroid Build Coastguard Worker bool thread_joined_ = false; 75*288bf522SAndroid Build Coastguard Worker std::atomic<bool> early_stop_ = false; 76*288bf522SAndroid Build Coastguard Worker std::atomic<bool> thread_result_ = false; 77*288bf522SAndroid Build Coastguard Worker }; 78*288bf522SAndroid Build Coastguard Worker 79*288bf522SAndroid Build Coastguard Worker } // namespace simpleperf 80