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