2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_UTILS_H_
18 #define SIMPLE_PERF_UTILS_H_
26 #include <android-base/logging.h>
27 #include <android-base/macros.h>
28 #include <ziparchive/zip_archive.h>
30 static inline uint64_t Align(uint64_t value, uint64_t alignment) {
31 return (value + alignment - 1) & ~(alignment - 1);
35 #define CLOSE_ON_EXEC_MODE ""
37 #define CLOSE_ON_EXEC_MODE "e"
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 {
44 explicit OneTimeFreeAllocator(size_t unit_size = 8192u)
45 : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
48 ~OneTimeFreeAllocator() {
53 const char* AllocateString(const std::string& s);
56 const size_t unit_size_;
57 std::vector<char*> v_;
64 static FileHelper OpenReadOnly(const std::string& filename);
65 static FileHelper OpenWriteOnly(const std::string& filename);
67 FileHelper(FileHelper&& other) {
74 explicit operator bool() const {
83 explicit FileHelper(int fd) : fd_(fd) {}
86 DISALLOW_COPY_AND_ASSIGN(FileHelper);
91 ArchiveHelper(int fd, const std::string& debug_filename);
94 explicit operator bool() const {
97 ZipArchiveHandle &archive_handle() {
102 ZipArchiveHandle handle_;
105 DISALLOW_COPY_AND_ASSIGN(ArchiveHelper);
109 void MoveFromBinaryFormat(T& data, const char*& p) {
110 static_assert(std::is_standard_layout<T>::value, "not standard layout");
111 memcpy(&data, p, sizeof(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);
124 void MoveToBinaryFormat(const T& data, char*& p) {
125 static_assert(std::is_standard_layout<T>::value, "not standard layout");
126 memcpy(p, &data, sizeof(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);
138 void PrintIndented(size_t indent, const char* fmt, ...);
139 void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...);
141 bool IsPowerOfTwo(uint64_t value);
143 std::vector<std::string> GetEntriesInDir(const std::string& dirpath);
144 std::vector<std::string> GetSubDirs(const std::string& dirpath);
145 bool IsDir(const std::string& dirpath);
146 bool IsRegularFile(const std::string& filename);
147 uint64_t GetFileSize(const std::string& filename);
148 bool MkdirWithParents(const std::string& path);
150 bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data);
152 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity);
156 struct KernelSymbol {
160 const char* module; // If nullptr, the symbol is not in a kernel module.
163 bool ProcessKernelSymbols(std::string& symbol_data,
164 const std::function<bool(const KernelSymbol&)>& callback);
166 size_t GetPageSize();
168 uint64_t ConvertBytesToValue(const char* bytes, uint32_t size);
170 timeval SecondToTimeval(double time_in_sec);
172 std::string GetSimpleperfVersion();
174 #endif // SIMPLE_PERF_UTILS_H_