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
36 std::vector<int> GetOnlineCpus();
37 std::vector<int> GetCpusFromString(const std::string& s);
38
39 struct KernelMmap {
40 std::string name;
41 uint64_t start_addr;
42 uint64_t len;
43 std::string filepath;
44 };
45
46 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
47
48 struct ThreadMmap {
49 uint64_t start_addr;
50 uint64_t len;
51 uint64_t pgoff;
52 std::string name;
53 bool executable;
54 };
55
56 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
57
58 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
59
60 bool GetKernelBuildId(BuildId* build_id);
61 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
62
63 bool IsThreadAlive(pid_t tid);
64 std::vector<pid_t> GetAllProcesses();
65 std::vector<pid_t> GetThreadsInProcess(pid_t pid);
66 bool GetProcessForThread(pid_t tid, pid_t* pid);
67 bool GetThreadName(pid_t tid, std::string* name);
68
69 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
70
71 bool CheckPerfEventLimit();
72 bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
73 bool CheckSampleFrequency(uint64_t sample_freq);
74 bool CheckKernelSymbolAddresses();
75
76 #if defined(__linux__)
GetSystemClock()77 static 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__)
gettid()85 static inline int gettid() {
86 return syscall(__NR_gettid);
87 }
88 #endif
89 #endif
90
91 ArchType GetMachineArch();
92
93 #endif // SIMPLE_PERF_ENVIRONMENT_H_
94