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 30static 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. 42class 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 62class 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 89class 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 108template <class T> 109void 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 115template <class T> 116void MoveFromBinaryFormat(T& data, char*& p) { 117 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 118 memcpy(&data, p, sizeof(T)); 119 p += sizeof(T); 120} 121 122template <class T> 123void MoveFromBinaryFormat(T* data_p, size_t n, const char*& p) { 124 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 125 size_t size = n * sizeof(T); 126 memcpy(data_p, p, size); 127 p += size; 128} 129 130template <class T> 131void MoveToBinaryFormat(const T& data, char*& p) { 132 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 133 memcpy(p, &data, sizeof(T)); 134 p += sizeof(T); 135} 136 137template <class T> 138void MoveToBinaryFormat(const T* data_p, size_t n, char*& p) { 139 static_assert(std::is_standard_layout<T>::value, "not standard layout"); 140 size_t size = n * sizeof(T); 141 memcpy(p, data_p, size); 142 p += size; 143} 144 145void PrintIndented(size_t indent, const char* fmt, ...); 146void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...); 147 148bool IsPowerOfTwo(uint64_t value); 149 150std::vector<std::string> GetEntriesInDir(const std::string& dirpath); 151std::vector<std::string> GetSubDirs(const std::string& dirpath); 152bool IsDir(const std::string& dirpath); 153bool IsRegularFile(const std::string& filename); 154uint64_t GetFileSize(const std::string& filename); 155bool MkdirWithParents(const std::string& path); 156 157bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data); 158 159bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity); 160std::string GetLogSeverityName(); 161 162bool IsRoot(); 163 164struct KernelSymbol { 165 uint64_t addr; 166 char type; 167 const char* name; 168 const char* module; // If nullptr, the symbol is not in a kernel module. 169}; 170 171bool ProcessKernelSymbols(std::string& symbol_data, 172 const std::function<bool(const KernelSymbol&)>& callback); 173 174size_t GetPageSize(); 175 176uint64_t ConvertBytesToValue(const char* bytes, uint32_t size); 177 178timeval SecondToTimeval(double time_in_sec); 179 180std::string GetSimpleperfVersion(); 181 182#endif // SIMPLE_PERF_UTILS_H_ 183