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_UTILS_H_ 18 #define SIMPLE_PERF_UTILS_H_ 19 20 #include <stddef.h> 21 #include <time.h> 22 23 #include <string> 24 #include <vector> 25 26 #include <android-base/logging.h> 27 #include <android-base/macros.h> 28 #include <ziparchive/zip_archive.h> 29 30 static inline uint64_t Align(uint64_t value, uint64_t alignment) { 31 return (value + alignment - 1) & ~(alignment - 1); 32 } 33 34 #ifdef _WIN32 35 #define CLOSE_ON_EXEC_MODE "" 36 #else 37 #define CLOSE_ON_EXEC_MODE "e" 38 #endif 39 40 // OneTimeAllocator is used to allocate memory many times and free only once at the end. 41 // It reduces the cost to free each allocated memory. 42 class OneTimeFreeAllocator { 43 public: 44 explicit OneTimeFreeAllocator(size_t unit_size = 8192u) 45 : unit_size_(unit_size), cur_(nullptr), end_(nullptr) { 46 } 47 48 ~OneTimeFreeAllocator() { 49 Clear(); 50 } 51 52 void Clear(); 53 const char* AllocateString(const std::string& s); 54 55 private: 56 const size_t unit_size_; 57 std::vector<char*> v_; 58 char* cur_; 59 char* end_; 60 }; 61 62 class FileHelper { 63 public: 64 static FileHelper OpenReadOnly(const std::string& filename); 65 static FileHelper OpenWriteOnly(const std::string& filename); 66 67 FileHelper(FileHelper&& other) { 68 fd_ = other.fd_; 69 other.fd_ = -1; 70 } 71 72 ~FileHelper(); 73 74 explicit operator bool() const { 75 return fd_ != -1; 76 } 77 78 int fd() const { 79 return fd_; 80 } 81 82 private: 83 explicit FileHelper(int fd) : fd_(fd) {} 84 int fd_; 85 86 DISALLOW_COPY_AND_ASSIGN(FileHelper); 87 }; 88 89 class ArchiveHelper { 90 public: 91 ArchiveHelper(int fd, const std::string& debug_filename); 92 ~ArchiveHelper(); 93 94 explicit operator bool() const { 95 return valid_; 96 } 97 ZipArchiveHandle &archive_handle() { 98 return handle_; 99 } 100 101 private: 102 ZipArchiveHandle handle_; 103 bool valid_; 104 105 DISALLOW_COPY_AND_ASSIGN(ArchiveHelper); 106 }; 107 108 template <class T> 109 void MoveFromBinaryFormat(T& data, const char*& p) { 110 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 111 memcpy(&data, p, sizeof(T)); 112 p += sizeof(T); 113 } 114 115 template <class T> 116 void MoveFromBinaryFormat(T* data_p, size_t n, const char*& p) { 117 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 118 size_t size = n * sizeof(T); 119 memcpy(data_p, p, size); 120 p += size; 121 } 122 123 template <class T> 124 void MoveToBinaryFormat(const T& data, char*& p) { 125 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 126 memcpy(p, &data, sizeof(T)); 127 p += sizeof(T); 128 } 129 130 template <class T> 131 void MoveToBinaryFormat(const T* data_p, size_t n, char*& p) { 132 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 133 size_t size = n * sizeof(T); 134 memcpy(p, data_p, size); 135 p += size; 136 } 137 138 void PrintIndented(size_t indent, const char* fmt, ...); 139 void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...); 140 141 bool IsPowerOfTwo(uint64_t value); 142 143 std::vector<std::string> GetEntriesInDir(const std::string& dirpath); 144 std::vector<std::string> GetSubDirs(const std::string& dirpath); 145 bool IsDir(const std::string& dirpath); 146 bool IsRegularFile(const std::string& filename); 147 uint64_t GetFileSize(const std::string& filename); 148 bool MkdirWithParents(const std::string& path); 149 150 bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data); 151 152 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity); 153 154 bool IsRoot(); 155 156 struct KernelSymbol { 157 uint64_t addr; 158 char type; 159 const char* name; 160 const char* module; // If nullptr, the symbol is not in a kernel module. 161 }; 162 163 bool ProcessKernelSymbols(std::string& symbol_data, 164 const std::function<bool(const KernelSymbol&)>& callback); 165 166 size_t GetPageSize(); 167 168 uint64_t ConvertBytesToValue(const char* bytes, uint32_t size); 169 170 timeval SecondToTimeval(double time_in_sec); 171 172 #endif // SIMPLE_PERF_UTILS_H_ 173