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> |
| 21 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 22 | #include <functional> |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 23 | #include <set> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 26 | |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 27 | #include "build_id.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 28 | |
| 29 | std::vector<int> GetOnlineCpus(); |
Yabin Cui | cb4c17e | 2015-10-26 16:15:29 -0700 | [diff] [blame] | 30 | std::vector<int> GetCpusFromString(const std::string& s); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 31 | |
Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 32 | constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]_text"; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 33 | |
| 34 | struct KernelMmap { |
| 35 | std::string name; |
| 36 | uint64_t start_addr; |
| 37 | uint64_t len; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 38 | std::string filepath; |
| 39 | }; |
| 40 | |
Yabin Cui | 9fbb28c | 2016-03-25 17:43:43 -0700 | [diff] [blame] | 41 | void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 42 | |
| 43 | struct ThreadComm { |
Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 44 | pid_t pid, tid; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 45 | std::string comm; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | bool GetThreadComms(std::vector<ThreadComm>* thread_comms); |
| 49 | |
Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 50 | constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon"; |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 51 | |
| 52 | struct ThreadMmap { |
| 53 | uint64_t start_addr; |
| 54 | uint64_t len; |
| 55 | uint64_t pgoff; |
| 56 | std::string name; |
| 57 | bool executable; |
| 58 | }; |
| 59 | |
| 60 | bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); |
| 61 | |
Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 62 | constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 63 | |
| 64 | bool GetKernelBuildId(BuildId* build_id); |
| 65 | bool GetModuleBuildId(const std::string& module_name, BuildId* build_id); |
| 66 | |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 67 | bool GetValidThreadsFromProcessString(const std::string& pid_str, std::set<pid_t>* tid_set); |
| 68 | bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set); |
| 69 | |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 70 | bool GetExecPath(std::string* exec_path); |
| 71 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 72 | // Expose the following functions for unit tests. |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 73 | struct KernelSymbol { |
| 74 | uint64_t addr; |
| 75 | char type; |
| 76 | const char* name; |
| 77 | const char* module; // If nullptr, the symbol is not in a kernel module. |
| 78 | }; |
| 79 | |
| 80 | bool ProcessKernelSymbols(const std::string& symbol_file, |
| 81 | std::function<bool(const KernelSymbol&)> callback); |
Yabin Cui | 92c688b | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 82 | bool CheckPerfEventLimit(); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 83 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 84 | #endif // SIMPLE_PERF_ENVIRONMENT_H_ |