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 20#include <sys/types.h> 21#include <time.h> 22 23#if defined(__linux__) 24#include <sys/syscall.h> 25#include <unistd.h> 26#endif 27 28#include <functional> 29#include <set> 30#include <string> 31#include <vector> 32 33#include "build_id.h" 34#include "perf_regs.h" 35 36std::vector<int> GetOnlineCpus(); 37std::vector<int> GetCpusFromString(const std::string& s); 38 39struct KernelMmap { 40 std::string name; 41 uint64_t start_addr; 42 uint64_t len; 43 std::string filepath; 44}; 45 46void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps); 47 48struct ThreadMmap { 49 uint64_t start_addr; 50 uint64_t len; 51 uint64_t pgoff; 52 std::string name; 53 bool executable; 54}; 55 56bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps); 57 58constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]"; 59 60bool GetKernelBuildId(BuildId* build_id); 61bool GetModuleBuildId(const std::string& module_name, BuildId* build_id); 62 63bool IsThreadAlive(pid_t tid); 64std::vector<pid_t> GetAllProcesses(); 65std::vector<pid_t> GetThreadsInProcess(pid_t pid); 66bool GetProcessForThread(pid_t tid, pid_t* pid); 67bool GetThreadName(pid_t tid, std::string* name); 68 69bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set); 70 71bool CheckPerfEventLimit(); 72bool GetMaxSampleFrequency(uint64_t* max_sample_freq); 73bool CheckSampleFrequency(uint64_t sample_freq); 74bool CheckKernelSymbolAddresses(); 75 76#if defined(__linux__) 77static inline uint64_t GetSystemClock() { 78 timespec ts; 79 // Assume clock_gettime() doesn't fail. 80 clock_gettime(CLOCK_MONOTONIC, &ts); 81 return ts.tv_sec * 1000000000ULL + ts.tv_nsec; 82} 83 84#if !defined(__ANDROID__) 85static inline int gettid() { 86 return syscall(__NR_gettid); 87} 88#endif 89#endif 90 91ArchType GetMachineArch(); 92 93#endif // SIMPLE_PERF_ENVIRONMENT_H_ 94