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