Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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 | |
Dan Albert | 918e4b7 | 2015-08-11 15:59:43 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 21 | #include <time.h> |
Dan Albert | 918e4b7 | 2015-08-11 15:59:43 -0700 | [diff] [blame] | 22 | |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 23 | #if defined(__linux__) |
| 24 | #include <sys/syscall.h> |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 28 | #include <functional> |
Yabin Cui | 554f3bb | 2021-05-05 13:34:59 -0700 | [diff] [blame] | 29 | #include <memory> |
Yabin Cui | d703bb3 | 2021-01-05 16:45:52 -0800 | [diff] [blame] | 30 | #include <optional> |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 31 | #include <set> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 32 | #include <string> |
Yabin Cui | d703bb3 | 2021-01-05 16:45:52 -0800 | [diff] [blame] | 33 | #include <utility> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 34 | #include <vector> |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 35 | |
Mark Salyzyn | 499a017 | 2018-11-12 12:58:06 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 37 | |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 38 | #include "build_id.h" |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 39 | #include "perf_regs.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 40 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 41 | namespace simpleperf { |
| 42 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 43 | std::vector<int> GetOnlineCpus(); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 44 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 45 | struct KernelMmap { |
| 46 | std::string name; |
| 47 | uint64_t start_addr; |
| 48 | uint64_t len; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 49 | std::string filepath; |
| 50 | }; |
| 51 | |
Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 52 | void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 53 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 54 | struct ThreadMmap { |
| 55 | uint64_t start_addr; |
| 56 | uint64_t len; |
| 57 | uint64_t pgoff; |
| 58 | std::string name; |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 59 | uint32_t prot; |
Yabin Cui | 4f79dc8 | 2018-05-09 18:25:25 -0700 | [diff] [blame] | 60 | ThreadMmap() {} |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 61 | ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot) |
| 62 | : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {} |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); |
| 66 | |
Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 67 | constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 68 | |
| 69 | bool GetKernelBuildId(BuildId* build_id); |
Yabin Cui | ea105c8 | 2020-07-22 13:01:31 -0700 | [diff] [blame] | 70 | bool GetModuleBuildId(const std::string& module_name, BuildId* build_id, |
| 71 | const std::string& sysfs_dir = "/sys"); |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 72 | |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 73 | bool IsThreadAlive(pid_t tid); |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 74 | std::vector<pid_t> GetAllProcesses(); |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 75 | std::vector<pid_t> GetThreadsInProcess(pid_t pid); |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 76 | bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid); |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 77 | bool GetProcessForThread(pid_t tid, pid_t* pid); |
| 78 | bool GetThreadName(pid_t tid, std::string* name); |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 79 | bool CheckPerfEventLimit(); |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 80 | bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb); |
Yabin Cui | 0720d48 | 2017-03-06 17:05:50 -0800 | [diff] [blame] | 81 | bool GetMaxSampleFrequency(uint64_t* max_sample_freq); |
Yabin Cui | 6e173a4 | 2018-08-13 15:58:25 -0700 | [diff] [blame] | 82 | bool SetMaxSampleFrequency(uint64_t max_sample_freq); |
| 83 | bool GetCpuTimeMaxPercent(size_t* percent); |
| 84 | bool SetCpuTimeMaxPercent(size_t percent); |
| 85 | bool GetPerfEventMlockKb(uint64_t* mlock_kb); |
| 86 | bool SetPerfEventMlockKb(uint64_t mlock_kb); |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 87 | bool CanRecordRawData(); |
Yi Kong | d5f247b | 2024-07-08 16:59:51 +0900 | [diff] [blame] | 88 | uint64_t GetMemorySize(); |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 89 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 90 | ArchType GetMachineArch(); |
Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 91 | void PrepareVdsoFile(); |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 92 | |
Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 93 | std::set<pid_t> WaitForAppProcesses(const std::string& package_name); |
Yabin Cui | 94c148d | 2020-02-18 14:32:33 -0800 | [diff] [blame] | 94 | void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner); // for testing only |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 95 | bool RunInAppContext(const std::string& app_package_name, const std::string& cmd, |
| 96 | const std::vector<std::string>& args, size_t workload_args_size, |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 97 | const std::string& output_filepath, bool need_tracepoint_events); |
Yabin Cui | db19d6d | 2021-05-12 17:36:02 -0700 | [diff] [blame] | 98 | std::string GetAppType(const std::string& app_package_name); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 99 | |
Yabin Cui | 7c5fe4e | 2017-12-05 17:44:05 -0800 | [diff] [blame] | 100 | void AllowMoreOpenedFiles(); |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 101 | |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 102 | class ScopedTempFiles { |
| 103 | public: |
Yabin Cui | 554f3bb | 2021-05-05 13:34:59 -0700 | [diff] [blame] | 104 | static std::unique_ptr<ScopedTempFiles> Create(const std::string& tmp_dir); |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 105 | ~ScopedTempFiles(); |
| 106 | // If delete_in_destructor = true, the temp file will be deleted in the destructor of |
| 107 | // ScopedTempFile. Otherwise, it should be deleted by the caller. |
| 108 | static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true); |
Yabin Cui | e32ed2b | 2020-07-23 15:30:14 -0700 | [diff] [blame] | 109 | static void RegisterTempFile(const std::string& path); |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 110 | |
| 111 | private: |
Yabin Cui | 554f3bb | 2021-05-05 13:34:59 -0700 | [diff] [blame] | 112 | ScopedTempFiles(const std::string& tmp_dir); |
| 113 | |
Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 114 | static std::string tmp_dir_; |
| 115 | static std::vector<std::string> files_to_delete_; |
| 116 | }; |
Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 117 | |
Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 118 | bool SignalIsIgnored(int signo); |
Yabin Cui | 8729bc6 | 2021-05-11 11:29:38 -0700 | [diff] [blame] | 119 | |
| 120 | enum { |
| 121 | kAndroidVersionP = 9, |
| 122 | kAndroidVersionQ = 10, |
| 123 | kAndroidVersionR = 11, |
| 124 | kAndroidVersionS = 12, |
| 125 | }; |
| 126 | |
Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 127 | // Return 0 if no android version. |
| 128 | int GetAndroidVersion(); |
Yabin Cui | d703bb3 | 2021-01-05 16:45:52 -0800 | [diff] [blame] | 129 | std::optional<std::pair<int, int>> GetKernelVersion(); |
Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 130 | |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 131 | std::string GetHardwareFromCpuInfo(const std::string& cpu_info); |
| 132 | |
Yabin Cui | 2db05b4 | 2018-07-16 14:04:49 -0700 | [diff] [blame] | 133 | bool MappedFileOnlyExistInMemory(const char* filename); |
| 134 | |
Yabin Cui | 38335f4 | 2018-07-17 16:06:21 -0700 | [diff] [blame] | 135 | std::string GetCompleteProcessName(pid_t pid); |
Yabin Cui | a22efeb | 2020-01-29 13:27:33 -0800 | [diff] [blame] | 136 | const char* GetTraceFsDir(); |
| 137 | |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 138 | #if defined(__linux__) |
| 139 | static inline uint64_t GetSystemClock() { |
| 140 | timespec ts; |
| 141 | // Assume clock_gettime() doesn't fail. |
| 142 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 143 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 144 | } |
| 145 | |
Yabin Cui | 3485af9 | 2021-10-27 10:50:53 -0700 | [diff] [blame] | 146 | #if defined(__ANDROID__) |
| 147 | bool IsInAppUid(); |
Colin Cross | c13a62b | 2021-12-15 14:56:47 -0800 | [diff] [blame] | 148 | #endif |
| 149 | #if !defined(__ANDROID__) && !defined(ANDROID_HOST_MUSL) |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 150 | static inline int gettid() { |
| 151 | return syscall(__NR_gettid); |
| 152 | } |
| 153 | #endif |
| 154 | |
Yabin Cui | 433d6f5 | 2024-01-09 15:29:09 -0800 | [diff] [blame] | 155 | struct ARMCpuModel { |
| 156 | uint32_t implementer = 0; |
| 157 | uint32_t partnum = 0; |
| 158 | std::vector<int> cpus; |
| 159 | }; |
| 160 | |
| 161 | std::vector<ARMCpuModel> GetARMCpuModels(); |
| 162 | |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 163 | #endif // defined(__linux__) |
| 164 | |
Yabin Cui | 5ea3bc1 | 2022-01-05 11:50:00 -0800 | [diff] [blame] | 165 | std::optional<uint32_t> GetProcessUid(pid_t pid); |
| 166 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 167 | } // namespace simpleperf |
| 168 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 169 | #endif // SIMPLE_PERF_ENVIRONMENT_H_ |