environment.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef SIMPLE_PERF_ENVIRONMENT_H_
  17. #define SIMPLE_PERF_ENVIRONMENT_H_
  18. #include <sys/types.h>
  19. #include <time.h>
  20. #if defined(__linux__)
  21. #include <sys/syscall.h>
  22. #include <unistd.h>
  23. #endif
  24. #include <functional>
  25. #include <set>
  26. #include <string>
  27. #include <vector>
  28. #include <android-base/file.h>
  29. #include "build_id.h"
  30. #include "perf_regs.h"
  31. std::vector<int> GetOnlineCpus();
  32. std::vector<int> GetCpusFromString(const std::string& s);
  33. struct KernelMmap {
  34. std::string name;
  35. uint64_t start_addr;
  36. uint64_t len;
  37. std::string filepath;
  38. };
  39. void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps);
  40. struct ThreadMmap {
  41. uint64_t start_addr;
  42. uint64_t len;
  43. uint64_t pgoff;
  44. std::string name;
  45. uint32_t prot;
  46. ThreadMmap() {}
  47. ThreadMmap(uint64_t start, uint64_t len, uint64_t pgoff, const char* name, uint32_t prot)
  48. : start_addr(start), len(len), pgoff(pgoff), name(name), prot(prot) {}
  49. };
  50. bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
  51. constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
  52. bool GetKernelBuildId(BuildId* build_id);
  53. bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
  54. bool IsThreadAlive(pid_t tid);
  55. std::vector<pid_t> GetAllProcesses();
  56. std::vector<pid_t> GetThreadsInProcess(pid_t pid);
  57. bool GetProcessForThread(pid_t tid, pid_t* pid);
  58. bool GetThreadName(pid_t tid, std::string* name);
  59. bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
  60. bool CheckPerfEventLimit();
  61. bool SetPerfEventLimits(uint64_t sample_freq, size_t cpu_percent, uint64_t mlock_kb);
  62. bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
  63. bool SetMaxSampleFrequency(uint64_t max_sample_freq);
  64. bool GetCpuTimeMaxPercent(size_t* percent);
  65. bool SetCpuTimeMaxPercent(size_t percent);
  66. bool GetPerfEventMlockKb(uint64_t* mlock_kb);
  67. bool SetPerfEventMlockKb(uint64_t mlock_kb);
  68. bool CheckKernelSymbolAddresses();
  69. bool CanRecordRawData();
  70. #if defined(__linux__)
  71. static inline uint64_t GetSystemClock() {
  72. timespec ts;
  73. // Assume clock_gettime() doesn't fail.
  74. clock_gettime(CLOCK_MONOTONIC, &ts);
  75. return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  76. }
  77. #if !defined(__ANDROID__)
  78. static inline int gettid() {
  79. return syscall(__NR_gettid);
  80. }
  81. #endif
  82. #endif
  83. ArchType GetMachineArch();
  84. void PrepareVdsoFile();
  85. std::set<pid_t> WaitForAppProcesses(const std::string& package_name);
  86. bool IsAppDebuggable(const std::string& package_name);
  87. bool RunInAppContext(const std::string& app_package_name, const std::string& cmd,
  88. const std::vector<std::string>& args, size_t workload_args_size,
  89. const std::string& output_filepath, bool need_tracepoint_events);
  90. void AllowMoreOpenedFiles();
  91. class ScopedTempFiles {
  92. public:
  93. ScopedTempFiles(const std::string& tmp_dir);
  94. ~ScopedTempFiles();
  95. // If delete_in_destructor = true, the temp file will be deleted in the destructor of
  96. // ScopedTempFile. Otherwise, it should be deleted by the caller.
  97. static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
  98. private:
  99. static std::string tmp_dir_;
  100. static std::vector<std::string> files_to_delete_;
  101. };
  102. bool SignalIsIgnored(int signo);
  103. // Return 0 if no android version.
  104. int GetAndroidVersion();
  105. constexpr int kAndroidVersionP = 9;
  106. std::string GetHardwareFromCpuInfo(const std::string& cpu_info);
  107. bool MappedFileOnlyExistInMemory(const char* filename);
  108. std::string GetCompleteProcessName(pid_t pid);
  109. #endif // SIMPLE_PERF_ENVIRONMENT_H_