• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/file.h>
34 
35 #include "build_id.h"
36 #include "perf_regs.h"
37 
38 std::vector<int> GetOnlineCpus();
39 std::vector<int> GetCpusFromString(const std::string& s);
40 
41 struct KernelMmap {
42   std::string name;
43   uint64_t start_addr;
44   uint64_t len;
45   std::string filepath;
46 };
47 
48 void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
49 
50 struct ThreadMmap {
51   uint64_t start_addr;
52   uint64_t len;
53   uint64_t pgoff;
54   std::string name;
55   uint32_t prot;
ThreadMmapThreadMmap56   ThreadMmap() {}
ThreadMmapThreadMmap57   ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot)
58       : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {}
59 };
60 
61 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
62 
63 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
64 
65 bool GetKernelBuildId(BuildId* build_id);
66 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
67 
68 bool IsThreadAlive(pid_t tid);
69 std::vector<pid_t> GetAllProcesses();
70 std::vector<pid_t> GetThreadsInProcess(pid_t pid);
71 bool GetProcessForThread(pid_t tid, pid_t* pid);
72 bool GetThreadName(pid_t tid, std::string* name);
73 
74 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
75 
76 bool CheckPerfEventLimit();
77 bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb);
78 bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
79 bool SetMaxSampleFrequency(uint64_t max_sample_freq);
80 bool GetCpuTimeMaxPercent(size_t* percent);
81 bool SetCpuTimeMaxPercent(size_t percent);
82 bool GetPerfEventMlockKb(uint64_t* mlock_kb);
83 bool SetPerfEventMlockKb(uint64_t mlock_kb);
84 bool CheckKernelSymbolAddresses();
85 bool CanRecordRawData();
86 
87 #if defined(__linux__)
GetSystemClock()88 static inline uint64_t GetSystemClock() {
89   timespec ts;
90   // Assume clock_gettime() doesn't fail.
91   clock_gettime(CLOCK_MONOTONIC, &ts);
92   return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
93 }
94 
95 #if !defined(__ANDROID__)
gettid()96 static inline int gettid() {
97   return syscall(__NR_gettid);
98 }
99 #endif
100 #endif
101 
102 ArchType GetMachineArch();
103 void PrepareVdsoFile();
104 
105 std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
106 bool IsAppDebuggable(const std::string& package_name);
107 bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
108                      const std::vector<std::string>& args, size_t workload_args_size,
109                      const std::string& output_filepath, bool need_tracepoint_events);
110 
111 void AllowMoreOpenedFiles();
112 
113 class ScopedTempFiles {
114  public:
115   ScopedTempFiles(const std::string& tmp_dir);
116   ~ScopedTempFiles();
117   // If delete_in_destructor = true, the temp file will be deleted in the destructor of
118   // ScopedTempFile. Otherwise, it should be deleted by the caller.
119   static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
120 
121  private:
122   static std::string tmp_dir_;
123   static std::vector<std::string> files_to_delete_;
124 };
125 
126 bool SignalIsIgnored(int signo);
127 // Return 0 if no android version.
128 int GetAndroidVersion();
129 
130 constexpr int kAndroidVersionP = 9;
131 
132 std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
133 
134 bool MappedFileOnlyExistInMemory(const char* filename);
135 
136 std::string GetCompleteProcessName(pid_t pid);
137 
138 #endif  // SIMPLE_PERF_ENVIRONMENT_H_
139