1 /* 2 * Copyright (C) 2015 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 SIMPLE_PERF_ENVIRONMENT_H_ 18 #define SIMPLE_PERF_ENVIRONMENT_H_ 19 20 #include <sys/types.h> 21 #include <time.h> 22 23 #if defined(__linux__) 24 #include <sys/syscall.h> 25 #include <unistd.h> 26 #endif 27 28 #include <functional> 29 #include <set> 30 #include <string> 31 #include <vector> 32 33 #include <android-base/file.h> 34 35 #include "build_id.h" 36 #include "perf_regs.h" 37 38 std::vector<int> GetOnlineCpus(); 39 40 struct KernelMmap { 41 std::string name; 42 uint64_t start_addr; 43 uint64_t len; 44 std::string filepath; 45 }; 46 47 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps); 48 49 struct ThreadMmap { 50 uint64_t start_addr; 51 uint64_t len; 52 uint64_t pgoff; 53 std::string name; 54 uint32_t prot; 55 ThreadMmap() {} 56 ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot) 57 : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {} 58 }; 59 60 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); 61 62 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; 63 64 bool GetKernelBuildId(BuildId* build_id); 65 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id); 66 67 bool IsThreadAlive(pid_t tid); 68 std::vector<pid_t> GetAllProcesses(); 69 std::vector<pid_t> GetThreadsInProcess(pid_t pid); 70 bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid); 71 bool GetProcessForThread(pid_t tid, pid_t* pid); 72 bool GetThreadName(pid_t tid, std::string* name); 73 74 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set); 75 76 bool CheckPerfEventLimit(); 77 bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb); 78 bool GetMaxSampleFrequency(uint64_t* max_sample_freq); 79 bool SetMaxSampleFrequency(uint64_t max_sample_freq); 80 bool GetCpuTimeMaxPercent(size_t* percent); 81 bool SetCpuTimeMaxPercent(size_t percent); 82 bool GetPerfEventMlockKb(uint64_t* mlock_kb); 83 bool SetPerfEventMlockKb(uint64_t mlock_kb); 84 bool CheckKernelSymbolAddresses(); 85 bool CanRecordRawData(); 86 87 #if defined(__linux__) 88 static inline uint64_t GetSystemClock() { 89 timespec ts; 90 // Assume clock_gettime() doesn't fail. 91 clock_gettime(CLOCK_MONOTONIC, &ts); 92 return ts.tv_sec * 1000000000ULL + ts.tv_nsec; 93 } 94 95 #if !defined(__ANDROID__) 96 static inline int gettid() { 97 return syscall(__NR_gettid); 98 } 99 #endif 100 #endif 101 102 ArchType GetMachineArch(); 103 void PrepareVdsoFile(); 104 105 std::set<pid_t> WaitForAppProcesses(const std::string& package_name); 106 bool IsAppDebuggable(const std::string& package_name); 107 void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner); // for testing only 108 bool RunInAppContext(const std::string& app_package_name, const std::string& cmd, 109 const std::vector<std::string>& args, size_t workload_args_size, 110 const std::string& output_filepath, bool need_tracepoint_events); 111 112 void AllowMoreOpenedFiles(); 113 114 class ScopedTempFiles { 115 public: 116 ScopedTempFiles(const std::string& tmp_dir); 117 ~ScopedTempFiles(); 118 // If delete_in_destructor = true, the temp file will be deleted in the destructor of 119 // ScopedTempFile. Otherwise, it should be deleted by the caller. 120 static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true); 121 122 private: 123 static std::string tmp_dir_; 124 static std::vector<std::string> files_to_delete_; 125 }; 126 127 bool SignalIsIgnored(int signo); 128 // Return 0 if no android version. 129 int GetAndroidVersion(); 130 131 constexpr int kAndroidVersionP = 9; 132 133 std::string GetHardwareFromCpuInfo(const std::string& cpu_info); 134 135 bool MappedFileOnlyExistInMemory(const char* filename); 136 137 std::string GetCompleteProcessName(pid_t pid); 138 139 const char* GetTraceFsDir(); 140 141 #endif // SIMPLE_PERF_ENVIRONMENT_H_ 142