Home | History | Annotate | Download | only in simpleperf
      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/test_utils.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   bool executable;
     56 };
     57 
     58 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
     59 
     60 constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
     61 
     62 bool GetKernelBuildId(BuildId* build_id);
     63 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
     64 
     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);
     70 
     71 bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
     72 
     73 bool CheckPerfEventLimit();
     74 bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
     75 bool CheckKernelSymbolAddresses();
     76 bool CanRecordRawData();
     77 
     78 #if defined(__linux__)
     79 static inline uint64_t GetSystemClock() {
     80   timespec ts;
     81   // Assume clock_gettime() doesn't fail.
     82   clock_gettime(CLOCK_MONOTONIC, &ts);
     83   return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
     84 }
     85 
     86 #if !defined(__ANDROID__)
     87 static inline int gettid() {
     88   return syscall(__NR_gettid);
     89 }
     90 #endif
     91 #endif
     92 
     93 ArchType GetMachineArch();
     94 void PrepareVdsoFile();
     95 
     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);
    100 
    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();
    105 
    106 class ScopedTempFiles {
    107  public:
    108   ScopedTempFiles(const std::string& tmp_dir);
    109   ~ScopedTempFiles();
    110   // If delete_in_destructor = true, the temp file will be deleted in the destructor of
    111   // ScopedTempFile. Otherwise, it should be deleted by the caller.
    112   static std::unique_ptr<TemporaryFile> CreateTempFile(bool delete_in_destructor = true);
    113 
    114  private:
    115   static std::string tmp_dir_;
    116   static std::vector<std::string> files_to_delete_;
    117 };
    118 
    119 bool SignalIsIgnored(int signo);
    120 
    121 #endif  // SIMPLE_PERF_ENVIRONMENT_H_
    122