blob: 8d3945f15bdd5dd5c38f4e328530f3f98b7ea724 [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 Cuib032de72015-06-17 21:15:09 -070029#include <set>
Yabin Cui323e9452015-04-20 18:07:17 -070030#include <string>
31#include <vector>
Yabin Cuif569b472015-04-30 09:43:26 -070032
Yabin Cui38b6a902017-12-06 14:15:48 -080033#include <android-base/test_utils.h>
34
Yabin Cui8f622512015-05-05 19:58:07 -070035#include "build_id.h"
Yabin Cui417df292016-11-16 12:26:13 -080036#include "perf_regs.h"
Yabin Cui323e9452015-04-20 18:07:17 -070037
38std::vector<int> GetOnlineCpus();
Yabin Cuicb4c17e2015-10-26 16:15:29 -070039std::vector<int> GetCpusFromString(const std::string& s);
Yabin Cui9759e1b2015-04-28 15:54:13 -070040
Yabin Cui7d59bb42015-05-04 20:27:57 -070041struct KernelMmap {
42 std::string name;
43 uint64_t start_addr;
44 uint64_t len;
Yabin Cui7d59bb42015-05-04 20:27:57 -070045 std::string filepath;
46};
47
Yabin Cui7134f382016-03-25 17:43:43 -070048void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
Yabin Cui7d59bb42015-05-04 20:27:57 -070049
Yabin Cui7d59bb42015-05-04 20:27:57 -070050struct ThreadMmap {
51 uint64_t start_addr;
52 uint64_t len;
53 uint64_t pgoff;
54 std::string name;
55 bool executable;
Yabin Cui4f79dc82018-05-09 18:25:25 -070056 ThreadMmap() {}
57 ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, bool executable)
58 : start_addr(start), len(len), pgoff(pgoff), name(name), executable(executable) {}
Yabin Cui7d59bb42015-05-04 20:27:57 -070059};
60
61bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
62
Yabin Cuibe6afa32015-06-25 13:23:55 -070063constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
Yabin Cui8f622512015-05-05 19:58:07 -070064
65bool GetKernelBuildId(BuildId* build_id);
66bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
67
Yabin Cui5f43fc42016-12-13 13:47:49 -080068bool IsThreadAlive(pid_t tid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070069std::vector<pid_t> GetAllProcesses();
Yabin Cuibc2a1022016-08-29 12:33:17 -070070std::vector<pid_t> GetThreadsInProcess(pid_t pid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070071bool GetProcessForThread(pid_t tid, pid_t* pid);
72bool GetThreadName(pid_t tid, std::string* name);
73
Yabin Cuib032de72015-06-17 21:15:09 -070074bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
75
Yabin Cuiebf79f32016-06-01 15:39:39 -070076bool CheckPerfEventLimit();
Yabin Cui0720d482017-03-06 17:05:50 -080077bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
Yabin Cuiaf698952016-07-08 11:27:48 -070078bool CheckKernelSymbolAddresses();
Yabin Cuif8974522017-07-17 14:36:37 -070079bool CanRecordRawData();
Yabin Cuiebf79f32016-06-01 15:39:39 -070080
Yabin Cui003b2452016-09-29 15:32:45 -070081#if defined(__linux__)
82static inline uint64_t GetSystemClock() {
83 timespec ts;
84 // Assume clock_gettime() doesn't fail.
85 clock_gettime(CLOCK_MONOTONIC, &ts);
86 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
87}
Yabin Cuib92bae82017-02-10 12:07:29 -080088
89#if !defined(__ANDROID__)
90static inline int gettid() {
91 return syscall(__NR_gettid);
92}
93#endif
Yabin Cui003b2452016-09-29 15:32:45 -070094#endif
95
Yabin Cui417df292016-11-16 12:26:13 -080096ArchType GetMachineArch();
Yabin Cui63a1c3d2017-05-19 12:57:44 -070097void PrepareVdsoFile();
Yabin Cui417df292016-11-16 12:26:13 -080098
Yabin Cui7cb6f292017-08-28 14:49:04 -070099std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
Yabin Cuia80f8f72017-07-12 15:50:20 -0700100bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
101 const std::vector<std::string>& args, size_t workload_args_size,
Yabin Cuif8974522017-07-17 14:36:37 -0700102 const std::string& output_filepath, bool need_tracepoint_events);
Yabin Cuia80f8f72017-07-12 15:50:20 -0700103
Yabin Cui616b3a02017-07-14 15:59:56 -0700104// Below two functions are only used in cts tests, to force stat/record cmd to run in app's context.
105void SetDefaultAppPackageName(const std::string& package_name);
106const std::string& GetDefaultAppPackageName();
Yabin Cui7c5fe4e2017-12-05 17:44:05 -0800107void AllowMoreOpenedFiles();
Yabin Cui616b3a02017-07-14 15:59:56 -0700108
Yabin Cuic68e66d2018-03-07 15:47:15 -0800109class ScopedTempFiles {
110 public:
111 ScopedTempFiles(const std::string& tmp_dir);
112 ~ScopedTempFiles();
113 // If delete_in_destructor = true, the temp file will be deleted in the destructor of
114 // ScopedTempFile. Otherwise, it should be deleted by the caller.
115 static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
116
117 private:
118 static std::string tmp_dir_;
119 static std::vector<std::string> files_to_delete_;
120};
Yabin Cui38b6a902017-12-06 14:15:48 -0800121
Josh Gao30535f22017-12-14 16:10:28 -0800122bool SignalIsIgnored(int signo);
Yabin Cui9e43e9f2018-03-09 15:57:13 -0800123// Return 0 if no android version.
124int GetAndroidVersion();
Josh Gao30535f22017-12-14 16:10:28 -0800125
Yabin Cui516a87c2018-03-26 17:34:00 -0700126constexpr int kAndroidVersionP = 9;
127
Yabin Cui323e9452015-04-20 18:07:17 -0700128#endif // SIMPLE_PERF_ENVIRONMENT_H_