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