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 <set>
30#include <string>
31#include <vector>
32
33#include "build_id.h"
34#include "perf_regs.h"
35
36std::vector<int> GetOnlineCpus();
37std::vector<int> GetCpusFromString(const std::string& s);
38
39struct KernelMmap {
40  std::string name;
41  uint64_t start_addr;
42  uint64_t len;
43  std::string filepath;
44};
45
46void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
47
48struct ThreadMmap {
49  uint64_t start_addr;
50  uint64_t len;
51  uint64_t pgoff;
52  std::string name;
53  bool executable;
54};
55
56bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
57
58constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
59
60bool GetKernelBuildId(BuildId* build_id);
61bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
62
63bool IsThreadAlive(pid_t tid);
64std::vector<pid_t> GetAllProcesses();
65std::vector<pid_t> GetThreadsInProcess(pid_t pid);
66bool GetProcessForThread(pid_t tid, pid_t* pid);
67bool GetThreadName(pid_t tid, std::string* name);
68
69bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
70
71bool CheckPerfEventLimit();
72bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
73bool CheckSampleFrequency(uint64_t sample_freq);
74bool CheckKernelSymbolAddresses();
75bool CanRecordRawData();
76
77#if defined(__linux__)
78static inline uint64_t GetSystemClock() {
79  timespec ts;
80  // Assume clock_gettime() doesn't fail.
81  clock_gettime(CLOCK_MONOTONIC, &ts);
82  return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
83}
84
85#if !defined(__ANDROID__)
86static inline int gettid() {
87  return syscall(__NR_gettid);
88}
89#endif
90#endif
91
92ArchType GetMachineArch();
93void PrepareVdsoFile();
94
95int WaitForAppProcess(const std::string& package_name);
96bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
97                     const std::vector<std::string>& args, size_t workload_args_size,
98                     const std::string& output_filepath, bool need_tracepoint_events);
99
100// Below two functions are only used in cts tests, to force stat/record cmd to run in app's context.
101void SetDefaultAppPackageName(const std::string& package_name);
102const std::string& GetDefaultAppPackageName();
103
104#endif  // SIMPLE_PERF_ENVIRONMENT_H_
105