blob: 2f4d58b91f5783407f4660155f107cd8732a9d9f [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 Cui8f622512015-05-05 19:58:07 -070033#include "build_id.h"
Yabin Cui417df292016-11-16 12:26:13 -080034#include "perf_regs.h"
Yabin Cui323e9452015-04-20 18:07:17 -070035
36std::vector<int> GetOnlineCpus();
Yabin Cuicb4c17e2015-10-26 16:15:29 -070037std::vector<int> GetCpusFromString(const std::string& s);
Yabin Cui9759e1b2015-04-28 15:54:13 -070038
Yabin Cui7d59bb42015-05-04 20:27:57 -070039struct KernelMmap {
40 std::string name;
41 uint64_t start_addr;
42 uint64_t len;
Yabin Cui7d59bb42015-05-04 20:27:57 -070043 std::string filepath;
44};
45
Yabin Cui7134f382016-03-25 17:43:43 -070046void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
Yabin Cui7d59bb42015-05-04 20:27:57 -070047
Yabin Cui7d59bb42015-05-04 20:27:57 -070048struct 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
Yabin Cuibe6afa32015-06-25 13:23:55 -070058constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
Yabin Cui8f622512015-05-05 19:58:07 -070059
60bool GetKernelBuildId(BuildId* build_id);
61bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
62
Yabin Cui5f43fc42016-12-13 13:47:49 -080063bool IsThreadAlive(pid_t tid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070064std::vector<pid_t> GetAllProcesses();
Yabin Cuibc2a1022016-08-29 12:33:17 -070065std::vector<pid_t> GetThreadsInProcess(pid_t pid);
Yabin Cui0a7a06d2016-10-28 13:27:41 -070066bool GetProcessForThread(pid_t tid, pid_t* pid);
67bool GetThreadName(pid_t tid, std::string* name);
68
Yabin Cuib032de72015-06-17 21:15:09 -070069bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
70
Yabin Cuiebf79f32016-06-01 15:39:39 -070071bool CheckPerfEventLimit();
Yabin Cui0720d482017-03-06 17:05:50 -080072bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
Yabin Cuib8915492016-06-22 12:43:09 -070073bool CheckSampleFrequency(uint64_t sample_freq);
Yabin Cuiaf698952016-07-08 11:27:48 -070074bool CheckKernelSymbolAddresses();
Yabin Cuiebf79f32016-06-01 15:39:39 -070075
Yabin Cui003b2452016-09-29 15:32:45 -070076#if defined(__linux__)
77static inline uint64_t GetSystemClock() {
78 timespec ts;
79 // Assume clock_gettime() doesn't fail.
80 clock_gettime(CLOCK_MONOTONIC, &ts);
81 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
82}
Yabin Cuib92bae82017-02-10 12:07:29 -080083
84#if !defined(__ANDROID__)
85static inline int gettid() {
86 return syscall(__NR_gettid);
87}
88#endif
Yabin Cui003b2452016-09-29 15:32:45 -070089#endif
90
Yabin Cui417df292016-11-16 12:26:13 -080091ArchType GetMachineArch();
Yabin Cui63a1c3d2017-05-19 12:57:44 -070092void PrepareVdsoFile();
Yabin Cui417df292016-11-16 12:26:13 -080093
Yabin Cui323e9452015-04-20 18:07:17 -070094#endif // SIMPLE_PERF_ENVIRONMENT_H_