blob: 8aaa71925583e9ac1625003df760537d156c03a1 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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
Dan Albert918e4b72015-08-11 15:59:43 -070020#include <sys/types.h>
Yabin Cui003b2452016-09-29 15:32:45 -070021#include <time.h>
Dan Albert918e4b72015-08-11 15:59:43 -070022
Yabin Cuib92bae82017-02-10 12:07:29 -080023#if defined(__linux__)
24#include <sys/syscall.h>
25#include <unistd.h>
26#endif
27
Yabin Cui7d59bb42015-05-04 20:27:57 -070028#include <functional>
Yabin Cui554f3bb2021-05-05 13:34:59 -070029#include <memory>
Yabin Cuid703bb32021-01-05 16:45:52 -080030#include <optional>
Yabin Cuib032de72015-06-17 21:15:09 -070031#include <set>
Yabin Cui323e9452015-04-20 18:07:17 -070032#include <string>
Yabin Cuid703bb32021-01-05 16:45:52 -080033#include <utility>
Yabin Cui323e9452015-04-20 18:07:17 -070034#include <vector>
Yabin Cuif569b472015-04-30 09:43:26 -070035
Mark Salyzyn499a0172018-11-12 12:58:06 -080036#include <android-base/file.h>
Yabin Cui38b6a902017-12-06 14:15:48 -080037
Yabin Cui8f622512015-05-05 19:58:07 -070038#include "build_id.h"
Yabin Cui417df292016-11-16 12:26:13 -080039#include "perf_regs.h"
Yabin Cui323e9452015-04-20 18:07:17 -070040
Yabin Cuifaa7b922021-01-11 17:35:57 -080041namespace simpleperf {
42
Yabin Cui323e9452015-04-20 18:07:17 -070043std::vector<int> GetOnlineCpus();
Yabin Cui9759e1b2015-04-28 15:54:13 -070044
Yabin Cui7d59bb42015-05-04 20:27:57 -070045struct KernelMmap {
46 std::string name;
47 uint64_t start_addr;
48 uint64_t len;
Yabin Cui7d59bb42015-05-04 20:27:57 -070049 std::string filepath;
50};
51
Yabin Cui7134f382016-03-25 17:43:43 -070052void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
Yabin Cui7d59bb42015-05-04 20:27:57 -070053
Yabin Cui7d59bb42015-05-04 20:27:57 -070054struct ThreadMmap {
55 uint64_t start_addr;
56 uint64_t len;
57 uint64_t pgoff;
58 std::string name;
Yabin Cui2db05b42018-07-16 14:04:49 -070059 uint32_t prot;
Yabin Cui4f79dc82018-05-09 18:25:25 -070060 ThreadMmap() {}
Yabin Cui2db05b42018-07-16 14:04:49 -070061 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) {}
Yabin Cui7d59bb42015-05-04 20:27:57 -070063};
64
65bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
66
Yabin Cuibe6afa32015-06-25 13:23:55 -070067constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
Yabin Cui8f622512015-05-05 19:58:07 -070068
69bool GetKernelBuildId(BuildId* build_id);
Yabin Cuiea105c82020-07-22 13:01:31 -070070bool GetModuleBuildId(const std::string& module_name, BuildId* build_id,
71 const std::string& sysfs_dir = "/sys");
Yabin Cui8f622512015-05-05 19:58:07 -070072
Yabin Cui5f43fc42016-12-13 13:47:49 -080073bool IsThreadAlive(pid_t tid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070074std::vector<pid_t> GetAllProcesses();
Yabin Cuibc2a1022016-08-29 12:33:17 -070075std::vector<pid_t> GetThreadsInProcess(pid_t pid);
Yabin Cuidc2708c2020-01-10 15:33:11 -080076bool ReadThreadNameAndPid(pid_t tid, std::string* comm, pid_t* pid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070077bool GetProcessForThread(pid_t tid, pid_t* pid);
78bool GetThreadName(pid_t tid, std::string* name);
Yabin Cuiebf79f32016-06-01 15:39:39 -070079bool CheckPerfEventLimit();
Yabin Cui6e173a42018-08-13 15:58:25 -070080bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb);
Yabin Cui0720d482017-03-06 17:05:50 -080081bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
Yabin Cui6e173a42018-08-13 15:58:25 -070082bool SetMaxSampleFrequency(uint64_t max_sample_freq);
83bool GetCpuTimeMaxPercent(size_t* percent);
84bool SetCpuTimeMaxPercent(size_t percent);
85bool GetPerfEventMlockKb(uint64_t* mlock_kb);
86bool SetPerfEventMlockKb(uint64_t mlock_kb);
Yabin Cuif8974522017-07-17 14:36:37 -070087bool CanRecordRawData();
Yi Kongd5f247b2024-07-08 16:59:51 +090088uint64_t GetMemorySize();
Yabin Cuiebf79f32016-06-01 15:39:39 -070089
Yabin Cui417df292016-11-16 12:26:13 -080090ArchType GetMachineArch();
Yabin Cui63a1c3d2017-05-19 12:57:44 -070091void PrepareVdsoFile();
Yabin Cui417df292016-11-16 12:26:13 -080092
Yabin Cui7cb6f292017-08-28 14:49:04 -070093std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
Yabin Cui94c148d2020-02-18 14:32:33 -080094void SetRunInAppToolForTesting(bool run_as, bool simpleperf_app_runner); // for testing only
Yabin Cuia80f8f72017-07-12 15:50:20 -070095bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
96 const std::vector<std::string>& args, size_t workload_args_size,
Yabin Cuif8974522017-07-17 14:36:37 -070097 const std::string& output_filepath, bool need_tracepoint_events);
Yabin Cuidb19d6d2021-05-12 17:36:02 -070098std::string GetAppType(const std::string& app_package_name);
Yabin Cuia80f8f72017-07-12 15:50:20 -070099
Yabin Cui7c5fe4e2017-12-05 17:44:05 -0800100void AllowMoreOpenedFiles();
Yabin Cui616b3a02017-07-14 15:59:56 -0700101
Yabin Cuic68e66d2018-03-07 15:47:15 -0800102class ScopedTempFiles {
103 public:
Yabin Cui554f3bb2021-05-05 13:34:59 -0700104 static std::unique_ptr<ScopedTempFiles> Create(const std::string& tmp_dir);
Yabin Cuic68e66d2018-03-07 15:47:15 -0800105 ~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);
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700109 static void RegisterTempFile(const std::string& path);
Yabin Cuic68e66d2018-03-07 15:47:15 -0800110
111 private:
Yabin Cui554f3bb2021-05-05 13:34:59 -0700112 ScopedTempFiles(const std::string& tmp_dir);
113
Yabin Cuic68e66d2018-03-07 15:47:15 -0800114 static std::string tmp_dir_;
115 static std::vector<std::string> files_to_delete_;
116};
Yabin Cui38b6a902017-12-06 14:15:48 -0800117
Josh Gao30535f22017-12-14 16:10:28 -0800118bool SignalIsIgnored(int signo);
Yabin Cui8729bc62021-05-11 11:29:38 -0700119
120enum {
121 kAndroidVersionP = 9,
122 kAndroidVersionQ = 10,
123 kAndroidVersionR = 11,
124 kAndroidVersionS = 12,
125};
126
Yabin Cui9e43e9f2018-03-09 15:57:13 -0800127// Return 0 if no android version.
128int GetAndroidVersion();
Yabin Cuid703bb32021-01-05 16:45:52 -0800129std::optional<std::pair<int, int>> GetKernelVersion();
Josh Gao30535f22017-12-14 16:10:28 -0800130
Yabin Cui8faa6152018-06-07 15:52:57 -0700131std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
132
Yabin Cui2db05b42018-07-16 14:04:49 -0700133bool MappedFileOnlyExistInMemory(const char* filename);
134
Yabin Cui38335f42018-07-17 16:06:21 -0700135std::string GetCompleteProcessName(pid_t pid);
Yabin Cuia22efeb2020-01-29 13:27:33 -0800136const char* GetTraceFsDir();
137
Yabin Cuia89a3742021-02-11 13:14:54 -0800138#if defined(__linux__)
139static 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
Yabin Cui3485af92021-10-27 10:50:53 -0700146#if defined(__ANDROID__)
147bool IsInAppUid();
Colin Crossc13a62b2021-12-15 14:56:47 -0800148#endif
149#if !defined(__ANDROID__) && !defined(ANDROID_HOST_MUSL)
Yabin Cuia89a3742021-02-11 13:14:54 -0800150static inline int gettid() {
151 return syscall(__NR_gettid);
152}
153#endif
154
Yabin Cui433d6f52024-01-09 15:29:09 -0800155struct ARMCpuModel {
156 uint32_t implementer = 0;
157 uint32_t partnum = 0;
158 std::vector<int> cpus;
159};
160
161std::vector<ARMCpuModel> GetARMCpuModels();
162
Yabin Cuia89a3742021-02-11 13:14:54 -0800163#endif // defined(__linux__)
164
Yabin Cui5ea3bc12022-01-05 11:50:00 -0800165std::optional<uint32_t> GetProcessUid(pid_t pid);
166
Yabin Cuifaa7b922021-01-11 17:35:57 -0800167} // namespace simpleperf
168
Yabin Cui323e9452015-04-20 18:07:17 -0700169#endif // SIMPLE_PERF_ENVIRONMENT_H_