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