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 | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 23 | #include <functional> |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 24 | #include <set> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 27 | |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 28 | #include "build_id.h" |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 29 | #include "perf_regs.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 30 | |
| 31 | std::vector<int> GetOnlineCpus(); |
Yabin Cui | cb4c17e | 2015-10-26 16:15:29 -0700 | [diff] [blame] | 32 | std::vector<int> GetCpusFromString(const std::string& s); |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 33 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 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 | 7134f38 | 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 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 43 | struct ThreadMmap { |
| 44 | uint64_t start_addr; |
| 45 | uint64_t len; |
| 46 | uint64_t pgoff; |
| 47 | std::string name; |
| 48 | bool executable; |
| 49 | }; |
| 50 | |
| 51 | bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); |
| 52 | |
Yabin Cui | be6afa3 | 2015-06-25 13:23:55 -0700 | [diff] [blame] | 53 | constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; |
Yabin Cui | 8f62251 | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 54 | |
| 55 | bool GetKernelBuildId(BuildId* build_id); |
| 56 | bool GetModuleBuildId(const std::string& module_name, BuildId* build_id); |
| 57 | |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 58 | bool IsThreadAlive(pid_t tid); |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 59 | std::vector<pid_t> GetAllProcesses(); |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 60 | std::vector<pid_t> GetThreadsInProcess(pid_t pid); |
Yabin Cui | 0a7a06d | 2016-10-28 13:27:41 -0700 | [diff] [blame] | 61 | bool GetProcessForThread(pid_t tid, pid_t* pid); |
| 62 | bool GetThreadName(pid_t tid, std::string* name); |
| 63 | |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 64 | bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set); |
| 65 | |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 66 | bool CheckPerfEventLimit(); |
Yabin Cui | b891549 | 2016-06-22 12:43:09 -0700 | [diff] [blame] | 67 | bool CheckSampleFrequency(uint64_t sample_freq); |
Yabin Cui | af69895 | 2016-07-08 11:27:48 -0700 | [diff] [blame] | 68 | bool CheckKernelSymbolAddresses(); |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 69 | |
Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 70 | #if defined(__linux__) |
| 71 | static inline uint64_t GetSystemClock() { |
| 72 | timespec ts; |
| 73 | // Assume clock_gettime() doesn't fail. |
| 74 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 75 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 76 | } |
| 77 | #endif |
| 78 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 79 | ArchType GetMachineArch(); |
| 80 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 81 | #endif // SIMPLE_PERF_ENVIRONMENT_H_ |