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_
25 #include <android-base/logging.h>
26 #include <android-base/macros.h>
27 #include <ziparchive/zip_archive.h>
29 #define ALIGN(value, alignment) (((value) + (alignment)-1) & ~((alignment)-1))
32 #define CLOSE_ON_EXEC_MODE ""
34 #define CLOSE_ON_EXEC_MODE "e"
37 // OneTimeAllocator is used to allocate memory many times and free only once at the end.
38 // It reduces the cost to free each allocated memory.
39 class OneTimeFreeAllocator {
41 OneTimeFreeAllocator(size_t unit_size = 8192u)
42 : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
45 ~OneTimeFreeAllocator() {
50 const char* AllocateString(const std::string& s);
53 const size_t unit_size_;
54 std::vector<char*> v_;
61 static FileHelper OpenReadOnly(const std::string& filename);
62 static FileHelper OpenWriteOnly(const std::string& filename);
64 FileHelper(FileHelper&& other) {
71 explicit operator bool() const {
80 FileHelper(int fd) : fd_(fd) {}
83 DISALLOW_COPY_AND_ASSIGN(FileHelper);
88 ArchiveHelper(int fd, const std::string& debug_filename);
91 explicit operator bool() const {
94 ZipArchiveHandle &archive_handle() {
99 ZipArchiveHandle handle_;
102 DISALLOW_COPY_AND_ASSIGN(ArchiveHelper);
106 void MoveFromBinaryFormat(T& data, const char*& p) {
107 data = *reinterpret_cast<const T*>(p);
111 void PrintIndented(size_t indent, const char* fmt, ...);
113 bool IsPowerOfTwo(uint64_t value);
115 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
116 std::vector<std::string>* subdirs);
117 bool IsDir(const std::string& dirpath);
118 bool IsRegularFile(const std::string& filename);
119 uint64_t GetFileSize(const std::string& filename);
120 bool MkdirWithParents(const std::string& path);
122 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity);
125 #endif // SIMPLE_PERF_UTILS_H_