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
89 ArchType GetMachineArch();
90 void PrepareVdsoFile();
91
92 std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
93 void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner); // for testing only
94 bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
95 const std::vector<std::string>& args, size_t workload_args_size,
96 const std::string& output_filepath, bool need_tracepoint_events);
97 std::string GetAppType(const std::string& app_package_name);
98
99 void AllowMoreOpenedFiles();
100
101 class ScopedTempFiles {
102 public:
103 static std::unique_ptr<ScopedTempFiles> Create(const std::string& tmp_dir);
104 ~ScopedTempFiles();
105 // If delete_in_destructor = true, the temp file will be deleted in the destructor of
106 // ScopedTempFile. Otherwise, it should be deleted by the caller.
107 static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
108 static void RegisterTempFile(const std::string& path);
109
110 private:
111 ScopedTempFiles(const std::string& tmp_dir);
112
113 static std::string tmp_dir_;
114 static std::vector<std::string> files_to_delete_;
115 };
116
117 bool SignalIsIgnored(int signo);
118
119 enum {
120 kAndroidVersionP = 9,
121 kAndroidVersionQ = 10,
122 kAndroidVersionR = 11,
123 kAndroidVersionS = 12,
124 };
125
126 // Return 0 if no android version.
127 int GetAndroidVersion();
128 std::optional<std::pair<int, int>> GetKernelVersion();
129
130 std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
131
132 bool MappedFileOnlyExistInMemory(const char* filename);
133
134 std::string GetCompleteProcessName(pid_t pid);
135 const char* GetTraceFsDir();
136
137 #if defined(__linux__)
GetSystemClock()138 static inline uint64_t GetSystemClock() {
139 timespec ts;
140 // Assume clock_gettime() doesn't fail.
141 clock_gettime(CLOCK_MONOTONIC, &ts);
142 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
143 }
144
145 #if !defined(__ANDROID__)
gettid()146 static inline int gettid() {
147 return syscall(__NR_gettid);
148 }
149 #endif
150
151 std::optional<uid_t> GetProcessUid(pid_t pid);
152 #endif // defined(__linux__)
153
154 } // namespace simpleperf
155
156 #endif // SIMPLE_PERF_ENVIRONMENT_H_
157