blob: 76bdbce79d58d8310406507dc174de3b7b4dc3c3 [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;
56};
57
58bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
59
Yabin Cuibe6afa32015-06-25 13:23:55 -070060constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
Yabin Cui8f622512015-05-05 19:58:07 -070061
62bool GetKernelBuildId(BuildId* build_id);
63bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
64
Yabin Cui5f43fc42016-12-13 13:47:49 -080065bool IsThreadAlive(pid_t tid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070066std::vector<pid_t> GetAllProcesses();
Yabin Cuibc2a1022016-08-29 12:33:17 -070067std::vector<pid_t> GetThreadsInProcess(pid_t pid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070068bool GetProcessForThread(pid_t tid, pid_t* pid);
69bool GetThreadName(pid_t tid, std::string* name);
70
Yabin Cuib032de72015-06-17 21:15:09 -070071bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
72
Yabin Cuiebf79f32016-06-01 15:39:39 -070073bool CheckPerfEventLimit();
Yabin Cui0720d482017-03-06 17:05:50 -080074bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
Yabin Cuiaf698952016-07-08 11:27:48 -070075bool CheckKernelSymbolAddresses();
Yabin Cuif8974522017-07-17 14:36:37 -070076bool CanRecordRawData();
Yabin Cuiebf79f32016-06-01 15:39:39 -070077
Yabin Cui003b2452016-09-29 15:32:45 -070078#if defined(__linux__)
79static inline uint64_t GetSystemClock() {
80 timespec ts;
81 // Assume clock_gettime() doesn't fail.
82 clock_gettime(CLOCK_MONOTONIC, &ts);
83 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
84}
Yabin Cuib92bae82017-02-10 12:07:29 -080085
86#if !defined(__ANDROID__)
87static inline int gettid() {
88 return syscall(__NR_gettid);
89}
90#endif
Yabin Cui003b2452016-09-29 15:32:45 -070091#endif
92
Yabin Cui417df292016-11-16 12:26:13 -080093ArchType GetMachineArch();
Yabin Cui63a1c3d2017-05-19 12:57:44 -070094void PrepareVdsoFile();
Yabin Cui417df292016-11-16 12:26:13 -080095
Yabin Cui7cb6f292017-08-28 14:49:04 -070096std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
Yabin Cuia80f8f72017-07-12 15:50:20 -070097bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
98 const std::vector<std::string>& args, size_t workload_args_size,
Yabin Cuif8974522017-07-17 14:36:37 -070099 const std::string& output_filepath, bool need_tracepoint_events);
Yabin Cuia80f8f72017-07-12 15:50:20 -0700100
Yabin Cui616b3a02017-07-14 15:59:56 -0700101// Below two functions are only used in cts tests, to force stat/record cmd to run in app's context.
102void SetDefaultAppPackageName(const std::string& package_name);
103const std::string& GetDefaultAppPackageName();
Yabin Cui7c5fe4e2017-12-05 17:44:05 -0800104void AllowMoreOpenedFiles();
Yabin Cui616b3a02017-07-14 15:59:56 -0700105
Yabin Cuic68e66d2018-03-07 15:47:15 -0800106class ScopedTempFiles {
107 public:
108 ScopedTempFiles(const std::string& tmp_dir);
109 ~ScopedTempFiles();
110 // If delete_in_destructor = true, the temp file will be deleted in the destructor of
111 // ScopedTempFile. Otherwise, it should be deleted by the caller.
112 static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
113
114 private:
115 static std::string tmp_dir_;
116 static std::vector<std::string> files_to_delete_;
117};
Yabin Cui38b6a902017-12-06 14:15:48 -0800118
Josh Gao30535f22017-12-14 16:10:28 -0800119bool SignalIsIgnored(int signo);
Yabin Cui9e43e9f2018-03-09 15:57:13 -0800120// Return 0 if no android version.
121int GetAndroidVersion();
Josh Gao30535f22017-12-14 16:10:28 -0800122
Yabin Cui323e9452015-04-20 18:07:17 -0700123#endif // SIMPLE_PERF_ENVIRONMENT_H_