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> |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 22 | #include <time.h> |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 23 | |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 24 | #include <fstream> |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 25 | #include <functional> |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 26 | #include <optional> |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 27 | #include <set> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 28 | #include <string> |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 29 | #include <vector> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 30 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 32 | #include <android-base/macros.h> |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 33 | #include <android-base/parseint.h> |
| 34 | #include <android-base/strings.h> |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 36 | #include <ziparchive/zip_archive.h> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 37 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 38 | namespace simpleperf { |
| 39 | |
Yabin Cui | cfdc783 | 2023-03-02 15:35:01 -0800 | [diff] [blame] | 40 | static constexpr size_t kKilobyte = 1024; |
| 41 | static constexpr size_t kMegabyte = 1024 * kKilobyte; |
| 42 | static constexpr uint64_t kGigabyte = 1024 * kMegabyte; |
| 43 | |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 44 | static inline uint64_t AlignDown(uint64_t value, uint64_t alignment) { |
| 45 | return value & ~(alignment - 1); |
| 46 | } |
| 47 | |
Yabin Cui | a7a0e50 | 2016-06-15 11:49:23 -0700 | [diff] [blame] | 48 | static inline uint64_t Align(uint64_t value, uint64_t alignment) { |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 49 | return AlignDown(value + alignment - 1, alignment); |
Yabin Cui | a7a0e50 | 2016-06-15 11:49:23 -0700 | [diff] [blame] | 50 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 51 | |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 52 | #ifdef _WIN32 |
| 53 | #define CLOSE_ON_EXEC_MODE "" |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 54 | #define OS_PATH_SEPARATOR '\\' |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 55 | #else |
| 56 | #define CLOSE_ON_EXEC_MODE "e" |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 57 | #define OS_PATH_SEPARATOR '/' |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 58 | #endif |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 59 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 60 | // OneTimeAllocator is used to allocate memory many times and free only once at the end. |
| 61 | // It reduces the cost to free each allocated memory. |
| 62 | class OneTimeFreeAllocator { |
| 63 | public: |
Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 64 | explicit OneTimeFreeAllocator(size_t unit_size = 8192u) |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 65 | : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {} |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 66 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 67 | ~OneTimeFreeAllocator() { Clear(); } |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 68 | |
| 69 | void Clear(); |
Martin Stjernholm | 7c27cc2 | 2018-11-28 00:46:00 +0000 | [diff] [blame] | 70 | const char* AllocateString(std::string_view s); |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | const size_t unit_size_; |
| 74 | std::vector<char*> v_; |
| 75 | char* cur_; |
| 76 | char* end_; |
| 77 | }; |
| 78 | |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 79 | class LineReader { |
| 80 | public: |
Yi Kong | 73a5021 | 2024-05-24 10:07:18 +0900 | [diff] [blame] | 81 | explicit LineReader(std::string_view file_path) : ifs_(std::string(file_path).c_str()) {} |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 82 | // Return true if open file successfully. |
| 83 | bool Ok() const { return ifs_.good(); } |
| 84 | // If available, return next line content with new line, otherwise return nullptr. |
| 85 | std::string* ReadLine() { return (std::getline(ifs_, buf_)) ? &buf_ : nullptr; } |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 86 | |
| 87 | private: |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 88 | std::ifstream ifs_; |
| 89 | std::string buf_; |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 92 | class FileHelper { |
| 93 | public: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 94 | static android::base::unique_fd OpenReadOnly(const std::string& filename); |
| 95 | static android::base::unique_fd OpenWriteOnly(const std::string& filename); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 98 | class ArchiveHelper { |
| 99 | public: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 100 | static std::unique_ptr<ArchiveHelper> CreateInstance(const std::string& filename); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 101 | ~ArchiveHelper(); |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 102 | // Iterate each entry in the zip file. Break the iteration when callback returns false. |
| 103 | bool IterateEntries(const std::function<bool(ZipEntry&, const std::string&)>& callback); |
| 104 | bool FindEntry(const std::string& name, ZipEntry* entry); |
| 105 | bool GetEntryData(ZipEntry& entry, std::vector<uint8_t>* data); |
| 106 | int GetFd(); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 107 | |
| 108 | private: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 109 | ArchiveHelper(ZipArchiveHandle handle, const std::string& filename) |
| 110 | : handle_(handle), filename_(filename) {} |
| 111 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 112 | ZipArchiveHandle handle_; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 113 | std::string filename_; |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(ArchiveHelper); |
| 116 | }; |
| 117 | |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 118 | template <class T> |
| 119 | void MoveFromBinaryFormat(T& data, const char*& p) { |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 120 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 121 | memcpy(&data, p, sizeof(T)); |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 122 | p += sizeof(T); |
| 123 | } |
| 124 | |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 125 | template <class T> |
Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 126 | void MoveFromBinaryFormat(T& data, char*& p) { |
| 127 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 128 | memcpy(&data, p, sizeof(T)); |
| 129 | p += sizeof(T); |
| 130 | } |
| 131 | |
| 132 | template <class T> |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 133 | void MoveFromBinaryFormat(T* data_p, size_t n, const char*& p) { |
| 134 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 135 | size_t size = n * sizeof(T); |
| 136 | memcpy(data_p, p, size); |
| 137 | p += size; |
| 138 | } |
| 139 | |
| 140 | template <class T> |
| 141 | void MoveToBinaryFormat(const T& data, char*& p) { |
| 142 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 143 | memcpy(p, &data, sizeof(T)); |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 144 | p += sizeof(T); |
| 145 | } |
| 146 | |
| 147 | template <class T> |
| 148 | void MoveToBinaryFormat(const T* data_p, size_t n, char*& p) { |
| 149 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 150 | size_t size = n * sizeof(T); |
| 151 | memcpy(p, data_p, size); |
| 152 | p += size; |
| 153 | } |
| 154 | |
Yabin Cui | 540932c | 2022-09-13 15:52:33 -0700 | [diff] [blame] | 155 | // Read info from binary data. |
| 156 | struct BinaryReader { |
| 157 | public: |
| 158 | BinaryReader(const char* head, size_t size) : head(head), end(head + size), error(false) {} |
| 159 | |
| 160 | size_t LeftSize() const { return end - head; } |
| 161 | |
| 162 | bool CheckLeftSize(size_t size) { |
| 163 | if (UNLIKELY(error)) { |
| 164 | return false; |
| 165 | } |
| 166 | if (UNLIKELY(LeftSize() < size)) { |
| 167 | error = true; |
| 168 | return false; |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
Yabin Cui | a9734e3 | 2022-12-12 12:03:11 -0800 | [diff] [blame] | 173 | void Move(size_t size) { |
| 174 | if (CheckLeftSize(size)) { |
| 175 | head += size; |
| 176 | } |
| 177 | } |
| 178 | |
Yabin Cui | 540932c | 2022-09-13 15:52:33 -0700 | [diff] [blame] | 179 | template <class T> |
| 180 | void Read(T& data) { |
| 181 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 182 | if (UNLIKELY(error)) { |
| 183 | return; |
| 184 | } |
| 185 | if (UNLIKELY(LeftSize() < sizeof(T))) { |
| 186 | error = true; |
| 187 | } else { |
| 188 | memcpy(&data, head, sizeof(T)); |
| 189 | head += sizeof(T); |
| 190 | } |
| 191 | } |
| 192 | |
Yabin Cui | de7d8eb | 2022-09-14 10:53:34 -0700 | [diff] [blame] | 193 | template <class T> |
| 194 | void Read(T* data_p, size_t n) { |
| 195 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 196 | if (UNLIKELY(error)) { |
| 197 | return; |
| 198 | } |
| 199 | size_t size; |
| 200 | if (UNLIKELY(__builtin_mul_overflow(n, sizeof(T), &size) || LeftSize() < size)) { |
| 201 | error = true; |
| 202 | } else { |
| 203 | memcpy(data_p, head, size); |
| 204 | head += size; |
| 205 | } |
| 206 | } |
| 207 | |
Yabin Cui | 540932c | 2022-09-13 15:52:33 -0700 | [diff] [blame] | 208 | // Read a string ending with '\0'. |
| 209 | std::string ReadString() { |
| 210 | if (UNLIKELY(error)) { |
| 211 | return ""; |
| 212 | } |
| 213 | std::string result; |
| 214 | while (head < end && *head != '\0') { |
| 215 | result.push_back(*head++); |
| 216 | } |
| 217 | if (LIKELY(head < end && *head == '\0')) { |
| 218 | head++; |
| 219 | return result; |
| 220 | } |
| 221 | error = true; |
| 222 | return ""; |
| 223 | } |
| 224 | |
| 225 | const char* head; |
| 226 | const char* end; |
| 227 | bool error; |
| 228 | }; |
| 229 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 230 | void PrintIndented(size_t indent, const char* fmt, ...); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 231 | void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 232 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 233 | bool IsPowerOfTwo(uint64_t value); |
| 234 | |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 235 | std::vector<std::string> GetEntriesInDir(const std::string& dirpath); |
| 236 | std::vector<std::string> GetSubDirs(const std::string& dirpath); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 237 | bool IsDir(const std::string& dirpath); |
Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 238 | bool IsRegularFile(const std::string& filename); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 239 | uint64_t GetFileSize(const std::string& filename); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 240 | bool MkdirWithParents(const std::string& path); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 241 | |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 242 | bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data); |
| 243 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 244 | bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity); |
Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 245 | std::string GetLogSeverityName(); |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 246 | |
Yabin Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 247 | bool IsRoot(); |
| 248 | |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 249 | size_t GetPageSize(); |
| 250 | |
| 251 | uint64_t ConvertBytesToValue(const char* bytes, uint32_t size); |
| 252 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 253 | timeval SecondToTimeval(double time_in_sec); |
| 254 | |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 255 | std::string GetSimpleperfVersion(); |
| 256 | |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 257 | std::optional<std::set<int>> GetCpusFromString(const std::string& s); |
| 258 | std::optional<std::set<pid_t>> GetTidsFromString(const std::string& s, bool check_if_exists); |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 259 | std::optional<std::set<pid_t>> GetPidsFromStrings(const std::vector<std::string>& strs, |
| 260 | bool check_if_exists, |
| 261 | bool support_progress_name_regex); |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 262 | |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 263 | template <typename T> |
| 264 | std::optional<std::set<T>> ParseUintVector(const std::string& s) { |
| 265 | std::set<T> result; |
| 266 | T value; |
| 267 | for (const auto& p : android::base::Split(s, ",")) { |
| 268 | if (!android::base::ParseUint(p.c_str(), &value, std::numeric_limits<T>::max())) { |
| 269 | LOG(ERROR) << "Invalid Uint '" << p << "' in " << s; |
| 270 | return std::nullopt; |
| 271 | } |
| 272 | result.insert(value); |
| 273 | } |
| 274 | return result; |
| 275 | } |
| 276 | |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 277 | // from boost::hash_combine |
| 278 | template <typename T> |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 279 | static inline void HashCombine(size_t& seed, const T& val) { |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 280 | seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 281 | } |
| 282 | |
Yabin Cui | 1cb8580 | 2021-11-12 17:56:57 -0800 | [diff] [blame] | 283 | size_t SafeStrlen(const char* s, const char* end); |
| 284 | |
Yabin Cui | 65b8fab | 2023-01-31 09:50:53 -0800 | [diff] [blame] | 285 | struct OverflowResult { |
| 286 | bool overflow = false; |
| 287 | uint64_t value = 0; |
| 288 | }; |
| 289 | |
| 290 | OverflowResult SafeAdd(uint64_t a, uint64_t b); |
Yabin Cui | f5ff15d | 2023-04-28 17:42:02 -0700 | [diff] [blame] | 291 | void OverflowSafeAdd(uint64_t& dest, uint64_t add); |
Yabin Cui | 65b8fab | 2023-01-31 09:50:53 -0800 | [diff] [blame] | 292 | |
Yabin Cui | bb9a0d1 | 2023-08-31 15:53:31 -0700 | [diff] [blame] | 293 | std::string ReadableCount(uint64_t count); |
Yabin Cui | 0913622 | 2024-07-16 10:30:21 -0700 | [diff] [blame] | 294 | std::string ReadableBytes(uint64_t bytes); |
Yabin Cui | bb9a0d1 | 2023-08-31 15:53:31 -0700 | [diff] [blame] | 295 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 296 | } // namespace simpleperf |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 297 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 298 | #endif // SIMPLE_PERF_UTILS_H_ |