| 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 | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 29 | #include <set> |
| Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
| Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 32 | |
| Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 33 | #include <android-base/test_utils.h> |
| 34 | |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 35 | #include "build_id.h" |
| Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 36 | #include "perf_regs.h" |
| Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 37 | |
| 38 | std::vector<int> GetOnlineCpus(); |
| Yabin Cui | cb4c17e | 2015-10-26 16:15:29 -0700 | [diff] [blame] | 39 | std::vector<int> GetCpusFromString(const std::string& s); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 40 | |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 41 | struct KernelMmap { |
| 42 | std::string name; |
| 43 | uint64_t start_addr; |
| 44 | uint64_t len; |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 45 | std::string filepath; |
| 46 | }; |
| 47 | |
| Yabin Cui | 7134f38 | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 48 | void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps); |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 49 | |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 50 | struct ThreadMmap { |
| 51 | uint64_t start_addr; |
| 52 | uint64_t len; |
| 53 | uint64_t pgoff; |
| 54 | std::string name; |
| 55 | bool executable; |
| Yabin Cui | 4f79dc8 | 2018-05-09 18:25:25 -0700 | [diff] [blame] | 56 | ThreadMmap() {} |
| 57 | ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, bool executable) |
| 58 | : start_addr(start), len(len), pgoff(pgoff), name(name), executable(executable) {} |
| Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); |
| 62 | |
| Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 63 | constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; |
| Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 64 | |
| 65 | bool GetKernelBuildId(BuildId* build_id); |
| 66 | bool GetModuleBuildId(const std::string& module_name, BuildId* build_id); |
| 67 | |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 68 | bool IsThreadAlive(pid_t tid); |
| Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 69 | std::vector<pid_t> GetAllProcesses(); |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 70 | std::vector<pid_t> GetThreadsInProcess(pid_t pid); |
| Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 71 | bool GetProcessForThread(pid_t tid, pid_t* pid); |
| 72 | bool GetThreadName(pid_t tid, std::string* name); |
| 73 | |
| Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 74 | bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set); |
| 75 | |
| Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 76 | bool CheckPerfEventLimit(); |
| Yabin Cui | 0720d48 | 2017-03-06 17:05:50 -0800 | [diff] [blame] | 77 | bool GetMaxSampleFrequency(uint64_t* max_sample_freq); |
| Yabin Cui | af69895 | 2016-07-08 11:27:48 -0700 | [diff] [blame] | 78 | bool CheckKernelSymbolAddresses(); |
| Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 79 | bool CanRecordRawData(); |
| Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 80 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 81 | #if defined(__linux__) |
| 82 | static inline uint64_t GetSystemClock() { |
| 83 | timespec ts; |
| 84 | // Assume clock_gettime() doesn't fail. |
| 85 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 86 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 87 | } |
| Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 88 | |
| 89 | #if !defined(__ANDROID__) |
| 90 | static inline int gettid() { |
| 91 | return syscall(__NR_gettid); |
| 92 | } |
| 93 | #endif |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 94 | #endif |
| 95 | |
| Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 96 | ArchType GetMachineArch(); |
| Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 97 | void PrepareVdsoFile(); |
| Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 98 | |
| Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 99 | std::set<pid_t> WaitForAppProcesses(const std::string& package_name); |
| Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 100 | bool RunInAppContext(const std::string& app_package_name, const std::string& cmd, |
| 101 | const std::vector<std::string>& args, size_t workload_args_size, |
| Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 102 | const std::string& output_filepath, bool need_tracepoint_events); |
| Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 103 | |
| Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 104 | // Below two functions are only used in cts tests, to force stat/record cmd to run in app's context. |
| 105 | void SetDefaultAppPackageName(const std::string& package_name); |
| 106 | const std::string& GetDefaultAppPackageName(); |
| Yabin Cui | 7c5fe4e | 2017-12-05 17:44:05 -0800 | [diff] [blame] | 107 | void AllowMoreOpenedFiles(); |
| Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 108 | |
| Yabin Cui | c68e66d | 2018-03-07 15:47:15 -0800 | [diff] [blame] | 109 | class ScopedTempFiles { |
| 110 | public: |
| 111 | ScopedTempFiles(const std::string& tmp_dir); |
| 112 | ~ScopedTempFiles(); |
| 113 | // If delete_in_destructor = true, the temp file will be deleted in the destructor of |
| 114 | // ScopedTempFile. Otherwise, it should be deleted by the caller. |
| 115 | static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true); |
| 116 | |
| 117 | private: |
| 118 | static std::string tmp_dir_; |
| 119 | static std::vector<std::string> files_to_delete_; |
| 120 | }; |
| Yabin Cui | 38b6a90 | 2017-12-06 14:15:48 -0800 | [diff] [blame] | 121 | |
| Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 122 | bool SignalIsIgnored(int signo); |
| Yabin Cui | 9e43e9f | 2018-03-09 15:57:13 -0800 | [diff] [blame] | 123 | // Return 0 if no android version. |
| 124 | int GetAndroidVersion(); |
| Josh Gao | 30535f2 | 2017-12-14 16:10:28 -0800 | [diff] [blame] | 125 | |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 126 | constexpr int kAndroidVersionP = 9; |
| 127 | |
| Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 128 | #endif // SIMPLE_PERF_ENVIRONMENT_H_ |