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 <android-base/file.h>
34
35 #include "build_id.h"
36 #include "perf_regs.h"
37
38 std::vector<int> GetOnlineCpus();
39
40 struct KernelMmap {
41 std::string name;
42 uint64_t start_addr;
43 uint64_t len;
44 std::string filepath;
45 };
46
47 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
48
49 struct ThreadMmap {
50 uint64_t start_addr;
51 uint64_t len;
52 uint64_t pgoff;
53 std::string name;
54 uint32_t prot;
ThreadMmapThreadMmap55 ThreadMmap() {}
ThreadMmapThreadMmap56 ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot)
57 : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {}
58 };
59
60 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
61
62 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
63
64 bool GetKernelBuildId(BuildId* build_id);
65 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id,
66 const std::string& sysfs_dir = "/sys");
67
68 bool IsThreadAlive(pid_t tid);
69 std::vector<pid_t> GetAllProcesses();
70 std::vector<pid_t> GetThreadsInProcess(pid_t pid);
71 bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid);
72 bool GetProcessForThread(pid_t tid, pid_t* pid);
73 bool GetThreadName(pid_t tid, std::string* name);
74
75 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
76
77 bool CheckPerfEventLimit();
78 bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb);
79 bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
80 bool SetMaxSampleFrequency(uint64_t max_sample_freq);
81 bool GetCpuTimeMaxPercent(size_t* percent);
82 bool SetCpuTimeMaxPercent(size_t percent);
83 bool GetPerfEventMlockKb(uint64_t* mlock_kb);
84 bool SetPerfEventMlockKb(uint64_t mlock_kb);
85 bool CheckKernelSymbolAddresses();
86 bool CanRecordRawData();
87
88 #if defined(__linux__)
GetSystemClock()89 static inline uint64_t GetSystemClock() {
90 timespec ts;
91 // Assume clock_gettime() doesn't fail.
92 clock_gettime(CLOCK_MONOTONIC, &ts);
93 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
94 }
95
96 #if !defined(__ANDROID__)
gettid()97 static inline int gettid() {
98 return syscall(__NR_gettid);
99 }
100 #endif
101 #endif
102
103 ArchType GetMachineArch();
104 void PrepareVdsoFile();
105
106 std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
107 bool IsAppDebuggable(const std::string& package_name);
108 void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner); // for testing only
109 bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
110 const std::vector<std::string>& args, size_t workload_args_size,
111 const std::string& output_filepath, bool need_tracepoint_events);
112
113 void AllowMoreOpenedFiles();
114
115 class ScopedTempFiles {
116 public:
117 ScopedTempFiles(const std::string& tmp_dir);
118 ~ScopedTempFiles();
119 // If delete_in_destructor = true, the temp file will be deleted in the destructor of
120 // ScopedTempFile. Otherwise, it should be deleted by the caller.
121 static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
122 static void RegisterTempFile(const std::string& path);
123
124 private:
125 static std::string tmp_dir_;
126 static std::vector<std::string> files_to_delete_;
127 };
128
129 bool SignalIsIgnored(int signo);
130 // Return 0 if no android version.
131 int GetAndroidVersion();
132 bool GetKernelVersion(int* major, int* minor);
133
134 constexpr int kAndroidVersionP = 9;
135
136 std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
137
138 bool MappedFileOnlyExistInMemory(const char* filename);
139
140 std::string GetCompleteProcessName(pid_t pid);
141
142 const char* GetTraceFsDir();
143
144 #endif // SIMPLE_PERF_ENVIRONMENT_H_
145