xref: /aosp_15_r20/system/extras/simpleperf/environment.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef SIMPLE_PERF_ENVIRONMENT_H_
18*288bf522SAndroid Build Coastguard Worker #define SIMPLE_PERF_ENVIRONMENT_H_
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
21*288bf522SAndroid Build Coastguard Worker #include <time.h>
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
24*288bf522SAndroid Build Coastguard Worker #include <sys/syscall.h>
25*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
26*288bf522SAndroid Build Coastguard Worker #endif
27*288bf522SAndroid Build Coastguard Worker 
28*288bf522SAndroid Build Coastguard Worker #include <functional>
29*288bf522SAndroid Build Coastguard Worker #include <memory>
30*288bf522SAndroid Build Coastguard Worker #include <optional>
31*288bf522SAndroid Build Coastguard Worker #include <set>
32*288bf522SAndroid Build Coastguard Worker #include <string>
33*288bf522SAndroid Build Coastguard Worker #include <utility>
34*288bf522SAndroid Build Coastguard Worker #include <vector>
35*288bf522SAndroid Build Coastguard Worker 
36*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
37*288bf522SAndroid Build Coastguard Worker 
38*288bf522SAndroid Build Coastguard Worker #include "build_id.h"
39*288bf522SAndroid Build Coastguard Worker #include "perf_regs.h"
40*288bf522SAndroid Build Coastguard Worker 
41*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
42*288bf522SAndroid Build Coastguard Worker 
43*288bf522SAndroid Build Coastguard Worker std::vector<int> GetOnlineCpus();
44*288bf522SAndroid Build Coastguard Worker 
45*288bf522SAndroid Build Coastguard Worker struct KernelMmap {
46*288bf522SAndroid Build Coastguard Worker   std::string name;
47*288bf522SAndroid Build Coastguard Worker   uint64_t start_addr;
48*288bf522SAndroid Build Coastguard Worker   uint64_t len;
49*288bf522SAndroid Build Coastguard Worker   std::string filepath;
50*288bf522SAndroid Build Coastguard Worker };
51*288bf522SAndroid Build Coastguard Worker 
52*288bf522SAndroid Build Coastguard Worker void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
53*288bf522SAndroid Build Coastguard Worker 
54*288bf522SAndroid Build Coastguard Worker struct ThreadMmap {
55*288bf522SAndroid Build Coastguard Worker   uint64_t start_addr;
56*288bf522SAndroid Build Coastguard Worker   uint64_t len;
57*288bf522SAndroid Build Coastguard Worker   uint64_t pgoff;
58*288bf522SAndroid Build Coastguard Worker   std::string name;
59*288bf522SAndroid Build Coastguard Worker   uint32_t prot;
ThreadMmapThreadMmap60*288bf522SAndroid Build Coastguard Worker   ThreadMmap() {}
ThreadMmapThreadMmap61*288bf522SAndroid Build Coastguard Worker   ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot)
62*288bf522SAndroid Build Coastguard Worker       : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {}
63*288bf522SAndroid Build Coastguard Worker };
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
66*288bf522SAndroid Build Coastguard Worker 
67*288bf522SAndroid Build Coastguard Worker constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
68*288bf522SAndroid Build Coastguard Worker 
69*288bf522SAndroid Build Coastguard Worker bool GetKernelBuildId(BuildId* build_id);
70*288bf522SAndroid Build Coastguard Worker bool GetModuleBuildId(const std::string& module_name, BuildId* build_id,
71*288bf522SAndroid Build Coastguard Worker                       const std::string& sysfs_dir = "/sys");
72*288bf522SAndroid Build Coastguard Worker 
73*288bf522SAndroid Build Coastguard Worker bool IsThreadAlive(pid_t tid);
74*288bf522SAndroid Build Coastguard Worker std::vector<pid_t> GetAllProcesses();
75*288bf522SAndroid Build Coastguard Worker std::vector<pid_t> GetThreadsInProcess(pid_t pid);
76*288bf522SAndroid Build Coastguard Worker bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid);
77*288bf522SAndroid Build Coastguard Worker bool GetProcessForThread(pid_t tid, pid_t* pid);
78*288bf522SAndroid Build Coastguard Worker bool GetThreadName(pid_t tid, std::string* name);
79*288bf522SAndroid Build Coastguard Worker bool CheckPerfEventLimit();
80*288bf522SAndroid Build Coastguard Worker bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb);
81*288bf522SAndroid Build Coastguard Worker bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
82*288bf522SAndroid Build Coastguard Worker bool SetMaxSampleFrequency(uint64_t max_sample_freq);
83*288bf522SAndroid Build Coastguard Worker bool GetCpuTimeMaxPercent(size_t* percent);
84*288bf522SAndroid Build Coastguard Worker bool SetCpuTimeMaxPercent(size_t percent);
85*288bf522SAndroid Build Coastguard Worker bool GetPerfEventMlockKb(uint64_t* mlock_kb);
86*288bf522SAndroid Build Coastguard Worker bool SetPerfEventMlockKb(uint64_t mlock_kb);
87*288bf522SAndroid Build Coastguard Worker bool CanRecordRawData();
88*288bf522SAndroid Build Coastguard Worker uint64_t GetMemorySize();
89*288bf522SAndroid Build Coastguard Worker 
90*288bf522SAndroid Build Coastguard Worker ArchType GetMachineArch();
91*288bf522SAndroid Build Coastguard Worker void PrepareVdsoFile();
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
94*288bf522SAndroid Build Coastguard Worker void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner);  // for testing only
95*288bf522SAndroid Build Coastguard Worker bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
96*288bf522SAndroid Build Coastguard Worker                      const std::vector<std::string>& args, size_t workload_args_size,
97*288bf522SAndroid Build Coastguard Worker                      const std::string& output_filepath, bool need_tracepoint_events);
98*288bf522SAndroid Build Coastguard Worker std::string GetAppType(const std::string& app_package_name);
99*288bf522SAndroid Build Coastguard Worker 
100*288bf522SAndroid Build Coastguard Worker void AllowMoreOpenedFiles();
101*288bf522SAndroid Build Coastguard Worker 
102*288bf522SAndroid Build Coastguard Worker class ScopedTempFiles {
103*288bf522SAndroid Build Coastguard Worker  public:
104*288bf522SAndroid Build Coastguard Worker   static std::unique_ptr<ScopedTempFiles> Create(const std::string& tmp_dir);
105*288bf522SAndroid Build Coastguard Worker   ~ScopedTempFiles();
106*288bf522SAndroid Build Coastguard Worker   // If delete_in_destructor = true, the temp file will be deleted in the destructor of
107*288bf522SAndroid Build Coastguard Worker   // ScopedTempFile. Otherwise, it should be deleted by the caller.
108*288bf522SAndroid Build Coastguard Worker   static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
109*288bf522SAndroid Build Coastguard Worker   static void RegisterTempFile(const std::string& path);
110*288bf522SAndroid Build Coastguard Worker 
111*288bf522SAndroid Build Coastguard Worker  private:
112*288bf522SAndroid Build Coastguard Worker   ScopedTempFiles(const std::string& tmp_dir);
113*288bf522SAndroid Build Coastguard Worker 
114*288bf522SAndroid Build Coastguard Worker   static std::string tmp_dir_;
115*288bf522SAndroid Build Coastguard Worker   static std::vector<std::string> files_to_delete_;
116*288bf522SAndroid Build Coastguard Worker };
117*288bf522SAndroid Build Coastguard Worker 
118*288bf522SAndroid Build Coastguard Worker bool SignalIsIgnored(int signo);
119*288bf522SAndroid Build Coastguard Worker 
120*288bf522SAndroid Build Coastguard Worker enum {
121*288bf522SAndroid Build Coastguard Worker   kAndroidVersionP = 9,
122*288bf522SAndroid Build Coastguard Worker   kAndroidVersionQ = 10,
123*288bf522SAndroid Build Coastguard Worker   kAndroidVersionR = 11,
124*288bf522SAndroid Build Coastguard Worker   kAndroidVersionS = 12,
125*288bf522SAndroid Build Coastguard Worker   kAndroidVersionT = 13,
126*288bf522SAndroid Build Coastguard Worker   kAndroidVersionU = 14,
127*288bf522SAndroid Build Coastguard Worker   kAndroidVersionV = 15,
128*288bf522SAndroid Build Coastguard Worker };
129*288bf522SAndroid Build Coastguard Worker 
130*288bf522SAndroid Build Coastguard Worker // Return 0 if no android version.
131*288bf522SAndroid Build Coastguard Worker int GetAndroidVersion();
132*288bf522SAndroid Build Coastguard Worker std::optional<std::pair<int, int>> GetKernelVersion();
133*288bf522SAndroid Build Coastguard Worker 
134*288bf522SAndroid Build Coastguard Worker std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
135*288bf522SAndroid Build Coastguard Worker 
136*288bf522SAndroid Build Coastguard Worker bool MappedFileOnlyExistInMemory(const char* filename);
137*288bf522SAndroid Build Coastguard Worker 
138*288bf522SAndroid Build Coastguard Worker std::string GetCompleteProcessName(pid_t pid);
139*288bf522SAndroid Build Coastguard Worker const char* GetTraceFsDir();
140*288bf522SAndroid Build Coastguard Worker 
141*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
GetSystemClock()142*288bf522SAndroid Build Coastguard Worker static inline uint64_t GetSystemClock() {
143*288bf522SAndroid Build Coastguard Worker   timespec ts;
144*288bf522SAndroid Build Coastguard Worker   // Assume clock_gettime() doesn't fail.
145*288bf522SAndroid Build Coastguard Worker   clock_gettime(CLOCK_MONOTONIC, &ts);
146*288bf522SAndroid Build Coastguard Worker   return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
147*288bf522SAndroid Build Coastguard Worker }
148*288bf522SAndroid Build Coastguard Worker 
149*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
150*288bf522SAndroid Build Coastguard Worker bool IsInAppUid();
151*288bf522SAndroid Build Coastguard Worker #endif
152*288bf522SAndroid Build Coastguard Worker #if !defined(__ANDROID__) && !defined(ANDROID_HOST_MUSL)
gettid()153*288bf522SAndroid Build Coastguard Worker static inline int gettid() {
154*288bf522SAndroid Build Coastguard Worker   return syscall(__NR_gettid);
155*288bf522SAndroid Build Coastguard Worker }
156*288bf522SAndroid Build Coastguard Worker #endif
157*288bf522SAndroid Build Coastguard Worker 
158*288bf522SAndroid Build Coastguard Worker struct CpuModel {
159*288bf522SAndroid Build Coastguard Worker   std::string arch;  // "arm", "riscv" or "x86"
160*288bf522SAndroid Build Coastguard Worker   struct {
161*288bf522SAndroid Build Coastguard Worker     uint32_t implementer = 0;
162*288bf522SAndroid Build Coastguard Worker     uint32_t partnum = 0;
163*288bf522SAndroid Build Coastguard Worker   } arm_data;
164*288bf522SAndroid Build Coastguard Worker   struct {
165*288bf522SAndroid Build Coastguard Worker     uint64_t mvendorid = 0;
166*288bf522SAndroid Build Coastguard Worker     uint64_t marchid = 0;
167*288bf522SAndroid Build Coastguard Worker     uint64_t mimpid = 0;
168*288bf522SAndroid Build Coastguard Worker   } riscv_data;
169*288bf522SAndroid Build Coastguard Worker   struct {
170*288bf522SAndroid Build Coastguard Worker     std::string vendor_id;
171*288bf522SAndroid Build Coastguard Worker   } x86_data;
172*288bf522SAndroid Build Coastguard Worker   std::vector<int> cpus;
173*288bf522SAndroid Build Coastguard Worker };
174*288bf522SAndroid Build Coastguard Worker 
175*288bf522SAndroid Build Coastguard Worker std::vector<CpuModel> GetCpuModels();
176*288bf522SAndroid Build Coastguard Worker 
177*288bf522SAndroid Build Coastguard Worker #endif  // defined(__linux__)
178*288bf522SAndroid Build Coastguard Worker 
179*288bf522SAndroid Build Coastguard Worker std::optional<uint32_t> GetProcessUid(pid_t pid);
180*288bf522SAndroid Build Coastguard Worker 
181*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
182*288bf522SAndroid Build Coastguard Worker 
183*288bf522SAndroid Build Coastguard Worker #endif  // SIMPLE_PERF_ENVIRONMENT_H_
184