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 | |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 40 | static inline uint64_t AlignDown(uint64_t value, uint64_t alignment) { |
| 41 | return value & ~(alignment - 1); |
| 42 | } |
| 43 | |
Yabin Cui | a7a0e50 | 2016-06-15 11:49:23 -0700 | [diff] [blame] | 44 | static inline uint64_t Align(uint64_t value, uint64_t alignment) { |
Evgeny Eltsin | 91dbae0 | 2020-08-27 15:46:09 +0200 | [diff] [blame] | 45 | return AlignDown(value + alignment - 1, alignment); |
Yabin Cui | a7a0e50 | 2016-06-15 11:49:23 -0700 | [diff] [blame] | 46 | } |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 47 | |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 48 | #ifdef _WIN32 |
| 49 | #define CLOSE_ON_EXEC_MODE "" |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 50 | #define OS_PATH_SEPARATOR '\\' |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 51 | #else |
| 52 | #define CLOSE_ON_EXEC_MODE "e" |
Yabin Cui | 1b9b1c1 | 2018-10-29 14:23:48 -0700 | [diff] [blame] | 53 | #define OS_PATH_SEPARATOR '/' |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 54 | #endif |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 55 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 56 | // OneTimeAllocator is used to allocate memory many times and free only once at the end. |
| 57 | // It reduces the cost to free each allocated memory. |
| 58 | class OneTimeFreeAllocator { |
| 59 | public: |
Chih-Hung Hsieh | 5674ed8 | 2016-07-12 11:35:16 -0700 | [diff] [blame] | 60 | explicit OneTimeFreeAllocator(size_t unit_size = 8192u) |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 61 | : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {} |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 62 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 63 | ~OneTimeFreeAllocator() { Clear(); } |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 64 | |
| 65 | void Clear(); |
Martin Stjernholm | 7c27cc2 | 2018-11-28 00:46:00 +0000 | [diff] [blame] | 66 | const char* AllocateString(std::string_view s); |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | const size_t unit_size_; |
| 70 | std::vector<char*> v_; |
| 71 | char* cur_; |
| 72 | char* end_; |
| 73 | }; |
| 74 | |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 75 | class LineReader { |
| 76 | public: |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 77 | explicit LineReader(std::string_view file_path) : ifs_(file_path) {} |
| 78 | // Return true if open file successfully. |
| 79 | bool Ok() const { return ifs_.good(); } |
| 80 | // If available, return next line content with new line, otherwise return nullptr. |
| 81 | std::string* ReadLine() { return (std::getline(ifs_, buf_)) ? &buf_ : nullptr; } |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 82 | |
| 83 | private: |
Yabin Cui | 40eef9e | 2021-04-13 13:08:31 -0700 | [diff] [blame] | 84 | std::ifstream ifs_; |
| 85 | std::string buf_; |
Thiébaud Weksteen | e7e750e | 2020-11-19 15:07:46 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 88 | class FileHelper { |
| 89 | public: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 90 | static android::base::unique_fd OpenReadOnly(const std::string& filename); |
| 91 | static android::base::unique_fd OpenWriteOnly(const std::string& filename); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 94 | class ArchiveHelper { |
| 95 | public: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 96 | static std::unique_ptr<ArchiveHelper> CreateInstance(const std::string& filename); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 97 | ~ArchiveHelper(); |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 98 | // Iterate each entry in the zip file. Break the iteration when callback returns false. |
| 99 | bool IterateEntries(const std::function<bool(ZipEntry&, const std::string&)>& callback); |
| 100 | bool FindEntry(const std::string& name, ZipEntry* entry); |
| 101 | bool GetEntryData(ZipEntry& entry, std::vector<uint8_t>* data); |
| 102 | int GetFd(); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 103 | |
| 104 | private: |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 105 | ArchiveHelper(ZipArchiveHandle handle, const std::string& filename) |
| 106 | : handle_(handle), filename_(filename) {} |
| 107 | |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 108 | ZipArchiveHandle handle_; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 109 | std::string filename_; |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(ArchiveHelper); |
| 112 | }; |
| 113 | |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 114 | template <class T> |
| 115 | void MoveFromBinaryFormat(T& data, const char*& p) { |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 116 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 117 | memcpy(&data, p, sizeof(T)); |
Yabin Cui | d713f95 | 2015-06-23 18:50:36 -0700 | [diff] [blame] | 118 | p += sizeof(T); |
| 119 | } |
| 120 | |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 121 | template <class T> |
Yabin Cui | 3d4aa26 | 2017-11-01 15:58:55 -0700 | [diff] [blame] | 122 | void MoveFromBinaryFormat(T& data, char*& p) { |
| 123 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 124 | memcpy(&data, p, sizeof(T)); |
| 125 | p += sizeof(T); |
| 126 | } |
| 127 | |
| 128 | template <class T> |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 129 | void MoveFromBinaryFormat(T* data_p, size_t n, const char*& p) { |
| 130 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 131 | size_t size = n * sizeof(T); |
| 132 | memcpy(data_p, p, size); |
| 133 | p += size; |
| 134 | } |
| 135 | |
| 136 | template <class T> |
| 137 | void MoveToBinaryFormat(const T& data, char*& p) { |
| 138 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
Yabin Cui | c24dd76 | 2016-11-10 15:25:15 -0800 | [diff] [blame] | 139 | memcpy(p, &data, sizeof(T)); |
Yabin Cui | 2597ef0 | 2016-10-19 11:28:48 -0700 | [diff] [blame] | 140 | p += sizeof(T); |
| 141 | } |
| 142 | |
| 143 | template <class T> |
| 144 | void MoveToBinaryFormat(const T* data_p, size_t n, char*& p) { |
| 145 | static_assert(std::is_standard_layout<T>::value, "not standard layout"); |
| 146 | size_t size = n * sizeof(T); |
| 147 | memcpy(p, data_p, size); |
| 148 | p += size; |
| 149 | } |
| 150 | |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 151 | void PrintIndented(size_t indent, const char* fmt, ...); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 152 | void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...); |
Yabin Cui | 7d59bb4 | 2015-05-04 20:27:57 -0700 | [diff] [blame] | 153 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 154 | bool IsPowerOfTwo(uint64_t value); |
| 155 | |
Yabin Cui | 0786586 | 2016-08-22 13:39:19 -0700 | [diff] [blame] | 156 | std::vector<std::string> GetEntriesInDir(const std::string& dirpath); |
| 157 | std::vector<std::string> GetSubDirs(const std::string& dirpath); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 158 | bool IsDir(const std::string& dirpath); |
Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 159 | bool IsRegularFile(const std::string& filename); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 160 | uint64_t GetFileSize(const std::string& filename); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 161 | bool MkdirWithParents(const std::string& path); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 162 | |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 163 | bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data); |
| 164 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 165 | bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity); |
Yabin Cui | 750b373 | 2017-12-18 17:46:36 -0800 | [diff] [blame] | 166 | std::string GetLogSeverityName(); |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 167 | |
Yabin Cui | 5633586 | 2016-04-18 13:43:20 -0700 | [diff] [blame] | 168 | bool IsRoot(); |
| 169 | |
Yabin Cui | 4f41df6 | 2016-06-01 17:29:06 -0700 | [diff] [blame] | 170 | size_t GetPageSize(); |
| 171 | |
| 172 | uint64_t ConvertBytesToValue(const char* bytes, uint32_t size); |
| 173 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 174 | timeval SecondToTimeval(double time_in_sec); |
| 175 | |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 176 | std::string GetSimpleperfVersion(); |
| 177 | |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 178 | std::optional<std::set<int>> GetCpusFromString(const std::string& s); |
| 179 | std::optional<std::set<pid_t>> GetTidsFromString(const std::string& s, bool check_if_exists); |
Namhyung Kim | a5f1d42 | 2019-12-17 17:15:02 +0900 | [diff] [blame] | 180 | |
Yabin Cui | a89a374 | 2021-02-11 13:14:54 -0800 | [diff] [blame] | 181 | template <typename T> |
| 182 | std::optional<std::set<T>> ParseUintVector(const std::string& s) { |
| 183 | std::set<T> result; |
| 184 | T value; |
| 185 | for (const auto& p : android::base::Split(s, ",")) { |
| 186 | if (!android::base::ParseUint(p.c_str(), &value, std::numeric_limits<T>::max())) { |
| 187 | LOG(ERROR) << "Invalid Uint '" << p << "' in " << s; |
| 188 | return std::nullopt; |
| 189 | } |
| 190 | result.insert(value); |
| 191 | } |
| 192 | return result; |
| 193 | } |
| 194 | |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 195 | // from boost::hash_combine |
| 196 | template <typename T> |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 197 | static inline void HashCombine(size_t& seed, const T& val) { |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 198 | seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 199 | } |
| 200 | |
Yabin Cui | 1cb8580 | 2021-11-12 17:56:57 -0800 | [diff] [blame] | 201 | size_t SafeStrlen(const char* s, const char* end); |
| 202 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 203 | } // namespace simpleperf |
Yabin Cui | fad7bbe | 2019-09-18 16:05:51 -0700 | [diff] [blame] | 204 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 205 | #endif // SIMPLE_PERF_UTILS_H_ |