2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_ENVIRONMENT_H_
18 #define SIMPLE_PERF_ENVIRONMENT_H_
20 #include <sys/types.h>
23 #if defined(__linux__)
24 #include <sys/syscall.h>
33 #include <android-base/test_utils.h>
36 #include "perf_regs.h"
38 std::vector<int> GetOnlineCpus();
39 std::vector<int> GetCpusFromString(const std::string& s);
48 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
58 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
60 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
62 bool GetKernelBuildId(BuildId* build_id);
63 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
65 bool IsThreadAlive(pid_t tid);
66 std::vector<pid_t> GetAllProcesses();
67 std::vector<pid_t> GetThreadsInProcess(pid_t pid);
68 bool GetProcessForThread(pid_t tid, pid_t* pid);
69 bool GetThreadName(pid_t tid, std::string* name);
71 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
73 bool CheckPerfEventLimit();
74 bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
75 bool CheckKernelSymbolAddresses();
76 bool CanRecordRawData();
78 #if defined(__linux__)
79 static inline uint64_t GetSystemClock() {
81 // Assume clock_gettime() doesn't fail.
82 clock_gettime(CLOCK_MONOTONIC, &ts);
83 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
86 #if !defined(__ANDROID__)
87 static inline int gettid() {
88 return syscall(__NR_gettid);
93 ArchType GetMachineArch();
94 void PrepareVdsoFile();
96 std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
97 bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
98 const std::vector<std::string>& args, size_t workload_args_size,
99 const std::string& output_filepath, bool need_tracepoint_events);
101 // Below two functions are only used in cts tests, to force stat/record cmd to run in app's context.
102 void SetDefaultAppPackageName(const std::string& package_name);
103 const std::string& GetDefaultAppPackageName();
104 void AllowMoreOpenedFiles();
106 void SetTempDirectoryUsedInRecording(const std::string& tmp_dir);
107 std::unique_ptr<TemporaryFile> CreateTempFileUsedInRecording();
109 bool SignalIsIgnored(int signo);
111 #endif // SIMPLE_PERF_ENVIRONMENT_H_