blob: 775fd5c40b39a1c08f825f0efbd9585d2480c486 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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>
Yabin Cui3e4c5952016-07-26 15:03:27 -070021#include <time.h>
Yabin Cuicc2e59e2015-08-21 14:23:43 -070022
Yabin Cui323e9452015-04-20 18:07:17 -070023#include <string>
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include <vector>
Yabin Cui67d3abd2015-04-16 15:26:31 -070025
Yabin Cui8f680f62016-03-18 18:47:43 -070026#include <android-base/logging.h>
Yabin Cuib1a885b2016-02-14 19:18:02 -080027#include <android-base/macros.h>
Yabin Cuibe7ec662016-03-02 13:56:28 -080028#include <ziparchive/zip_archive.h>
Yabin Cuib1a885b2016-02-14 19:18:02 -080029
Yabin Cuia7a0e502016-06-15 11:49:23 -070030static inline uint64_t Align(uint64_t value, uint64_t alignment) {
31 return (value + alignment - 1) & ~(alignment - 1);
32}
Yabin Cui67d3abd2015-04-16 15:26:31 -070033
Yabin Cuiffaa9122016-01-15 15:25:48 -080034#ifdef _WIN32
35#define CLOSE_ON_EXEC_MODE ""
36#else
37#define CLOSE_ON_EXEC_MODE "e"
38#endif
Yabin Cui621a5332015-06-15 16:17:20 -070039
Yabin Cuicc2e59e2015-08-21 14:23:43 -070040// 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.
42class OneTimeFreeAllocator {
43 public:
Chih-Hung Hsieh5674ed82016-07-12 11:35:16 -070044 explicit OneTimeFreeAllocator(size_t unit_size = 8192u)
Yabin Cuicc2e59e2015-08-21 14:23:43 -070045 : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
46 }
47
48 ~OneTimeFreeAllocator() {
49 Clear();
50 }
51
52 void Clear();
53 const char* AllocateString(const std::string& s);
54
55 private:
56 const size_t unit_size_;
57 std::vector<char*> v_;
58 char* cur_;
59 char* end_;
60};
61
Yabin Cuib1a885b2016-02-14 19:18:02 -080062class FileHelper {
63 public:
Yabin Cuibe7ec662016-03-02 13:56:28 -080064 static FileHelper OpenReadOnly(const std::string& filename);
65 static FileHelper OpenWriteOnly(const std::string& filename);
66
67 FileHelper(FileHelper&& other) {
68 fd_ = other.fd_;
69 other.fd_ = -1;
70 }
71
Yabin Cuib1a885b2016-02-14 19:18:02 -080072 ~FileHelper();
73
74 explicit operator bool() const {
75 return fd_ != -1;
76 }
77
78 int fd() const {
79 return fd_;
80 }
81
82 private:
Chih-Hung Hsieh5674ed82016-07-12 11:35:16 -070083 explicit FileHelper(int fd) : fd_(fd) {}
Yabin Cuib1a885b2016-02-14 19:18:02 -080084 int fd_;
85
86 DISALLOW_COPY_AND_ASSIGN(FileHelper);
87};
88
Yabin Cuibe7ec662016-03-02 13:56:28 -080089class ArchiveHelper {
90 public:
91 ArchiveHelper(int fd, const std::string& debug_filename);
92 ~ArchiveHelper();
93
94 explicit operator bool() const {
95 return valid_;
96 }
97 ZipArchiveHandle &archive_handle() {
98 return handle_;
99 }
100
101 private:
102 ZipArchiveHandle handle_;
103 bool valid_;
104
105 DISALLOW_COPY_AND_ASSIGN(ArchiveHelper);
106};
107
Yabin Cuid713f952015-06-23 18:50:36 -0700108template <class T>
109void MoveFromBinaryFormat(T& data, const char*& p) {
Yabin Cui2597ef02016-10-19 11:28:48 -0700110 static_assert(std::is_standard_layout<T>::value, "not standard layout");
Yabin Cuic24dd762016-11-10 15:25:15 -0800111 memcpy(&data, p, sizeof(T));
Yabin Cuid713f952015-06-23 18:50:36 -0700112 p += sizeof(T);
113}
114
Yabin Cui2597ef02016-10-19 11:28:48 -0700115template <class T>
116void 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);
120 p += size;
121}
122
123template <class T>
124void MoveToBinaryFormat(const T& data, char*& p) {
125 static_assert(std::is_standard_layout<T>::value, "not standard layout");
Yabin Cuic24dd762016-11-10 15:25:15 -0800126 memcpy(p, &data, sizeof(T));
Yabin Cui2597ef02016-10-19 11:28:48 -0700127 p += sizeof(T);
128}
129
130template <class T>
131void 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);
135 p += size;
136}
137
Yabin Cui7d59bb42015-05-04 20:27:57 -0700138void PrintIndented(size_t indent, const char* fmt, ...);
Yabin Cui767dd172016-06-02 21:02:43 -0700139void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700140
Yabin Cui9759e1b2015-04-28 15:54:13 -0700141bool IsPowerOfTwo(uint64_t value);
142
Yabin Cui07865862016-08-22 13:39:19 -0700143std::vector<std::string> GetEntriesInDir(const std::string& dirpath);
144std::vector<std::string> GetSubDirs(const std::string& dirpath);
Yabin Cuib032de72015-06-17 21:15:09 -0700145bool IsDir(const std::string& dirpath);
Yabin Cui797116b2015-12-08 17:43:15 -0800146bool IsRegularFile(const std::string& filename);
Yabin Cuib1a885b2016-02-14 19:18:02 -0800147uint64_t GetFileSize(const std::string& filename);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800148bool MkdirWithParents(const std::string& path);
Yabin Cui323e9452015-04-20 18:07:17 -0700149
Yabin Cui05400532016-03-17 21:18:53 -0700150bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data);
151
Yabin Cui8f680f62016-03-18 18:47:43 -0700152bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity);
153
Yabin Cui56335862016-04-18 13:43:20 -0700154bool IsRoot();
155
Yabin Cuib4212972016-05-25 14:08:05 -0700156struct KernelSymbol {
157 uint64_t addr;
158 char type;
159 const char* name;
160 const char* module; // If nullptr, the symbol is not in a kernel module.
161};
162
163bool ProcessKernelSymbols(std::string& symbol_data,
Yabin Cui3e4c5952016-07-26 15:03:27 -0700164 const std::function<bool(const KernelSymbol&)>& callback);
Yabin Cuib4212972016-05-25 14:08:05 -0700165
Yabin Cui4f41df62016-06-01 17:29:06 -0700166size_t GetPageSize();
167
168uint64_t ConvertBytesToValue(const char* bytes, uint32_t size);
169
Yabin Cui3e4c5952016-07-26 15:03:27 -0700170timeval SecondToTimeval(double time_in_sec);
171
Yabin Cuidf6333c2017-05-03 16:34:02 -0700172std::string GetSimpleperfVersion();
173
Yabin Cui67d3abd2015-04-16 15:26:31 -0700174#endif // SIMPLE_PERF_UTILS_H_