xref: /aosp_15_r20/system/extras/simpleperf/environment.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 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   kAndroidVersionT = 13,
126   kAndroidVersionU = 14,
127   kAndroidVersionV = 15,
128 };
129 
130 // Return 0 if no android version.
131 int GetAndroidVersion();
132 std::optional<std::pair<int, int>> GetKernelVersion();
133 
134 std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
135 
136 bool MappedFileOnlyExistInMemory(const char* filename);
137 
138 std::string GetCompleteProcessName(pid_t pid);
139 const char* GetTraceFsDir();
140 
141 #if defined(__linux__)
GetSystemClock()142 static inline uint64_t GetSystemClock() {
143   timespec ts;
144   // Assume clock_gettime() doesn't fail.
145   clock_gettime(CLOCK_MONOTONIC, &ts);
146   return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
147 }
148 
149 #if defined(__ANDROID__)
150 bool IsInAppUid();
151 #endif
152 #if !defined(__ANDROID__) && !defined(ANDROID_HOST_MUSL)
gettid()153 static inline int gettid() {
154   return syscall(__NR_gettid);
155 }
156 #endif
157 
158 struct CpuModel {
159   std::string arch;  // "arm", "riscv" or "x86"
160   struct {
161     uint32_t implementer = 0;
162     uint32_t partnum = 0;
163   } arm_data;
164   struct {
165     uint64_t mvendorid = 0;
166     uint64_t marchid = 0;
167     uint64_t mimpid = 0;
168   } riscv_data;
169   struct {
170     std::string vendor_id;
171   } x86_data;
172   std::vector<int> cpus;
173 };
174 
175 std::vector<CpuModel> GetCpuModels();
176 
177 #endif  // defined(__linux__)
178 
179 std::optional<uint32_t> GetProcessUid(pid_t pid);
180 
181 }  // namespace simpleperf
182 
183 #endif  // SIMPLE_PERF_ENVIRONMENT_H_
184