Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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> |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 21 | #include <time.h> |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 22 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 23 | #include <string> |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 24 | #include <vector> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 25 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 27 | #include <android-base/macros.h> |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 28 | #include <ziparchive/zip_archive.h> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 29 | |
Yabin Cui | a7a0e50 | 2016-06-15 11:49:23 -0700 | [diff] [blame] | 30 | static inline uint64_t Align(uint64_t value, uint64_t alignment) { |
| 31 | return (value + alignment - 1) & ~(alignment - 1); |
| 32 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 33 | |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 34 | #ifdef _WIN32 |
| 35 | #define CLOSE_ON_EXEC_MODE "" |
| 36 | #else |
| 37 | #define CLOSE_ON_EXEC_MODE "e" |
| 38 | #endif |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 39 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 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: |
Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 44 | explicit OneTimeFreeAllocator(size_t unit_size = 8192u) |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 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 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 62 | class FileHelper { |
| 63 | public: |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 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 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 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: |
Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 83 | explicit FileHelper(int fd) : fd_(fd) {} |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 84 | int fd_; |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(FileHelper); |
| 87 | }; |
| 88 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 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 | |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 108 | template <class T> |
| 109 | void MoveFromBinaryFormat(T& data, const char*& p) { |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 110 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 111 | memcpy(&data, p, sizeof(T)); |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 112 | p += sizeof(T); |
| 113 | } |
| 114 | |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 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"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 126 | memcpy(p, &data, sizeof(T)); |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 138 | void PrintIndented(size_t indent, const char* fmt, ...); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 139 | void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 140 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 141 | bool IsPowerOfTwo(uint64_t value); |
| 142 | |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 143 | std::vector<std::string> GetEntriesInDir(const std::string& dirpath); |
| 144 | std::vector<std::string> GetSubDirs(const std::string& dirpath); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 145 | bool IsDir(const std::string& dirpath); |
Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 146 | bool IsRegularFile(const std::string& filename); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 147 | uint64_t GetFileSize(const std::string& filename); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 148 | bool MkdirWithParents(const std::string& path); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 149 | |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 150 | bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data); |
| 151 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 152 | bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity); |
| 153 | |
Yabin Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 154 | bool IsRoot(); |
| 155 | |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 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, |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 164 | const std::function<bool(const KernelSymbol&)>& callback); |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 165 | |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 166 | size_t GetPageSize(); |
| 167 | |
| 168 | uint64_t ConvertBytesToValue(const char* bytes, uint32_t size); |
| 169 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 170 | timeval SecondToTimeval(double time_in_sec); |
| 171 | |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 172 | std::string GetSimpleperfVersion(); |
| 173 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 174 | #endif // SIMPLE_PERF_UTILS_H_ |