OSDN Git Service

simpleperf: avoid selinux denials.
[android-x86/system-extras.git] / simpleperf / utils.h
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 <string>
24 #include <vector>
25
26 #include <android-base/logging.h>
27 #include <android-base/macros.h>
28 #include <ziparchive/zip_archive.h>
29
30 static inline uint64_t Align(uint64_t value, uint64_t alignment) {
31   return (value + alignment - 1) & ~(alignment - 1);
32 }
33
34 #ifdef _WIN32
35 #define CLOSE_ON_EXEC_MODE ""
36 #else
37 #define CLOSE_ON_EXEC_MODE "e"
38 #endif
39
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 {
43  public:
44   explicit OneTimeFreeAllocator(size_t unit_size = 8192u)
45       : 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
62 class FileHelper {
63  public:
64   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
72   ~FileHelper();
73
74   explicit operator bool() const {
75     return fd_ != -1;
76   }
77
78   int fd() const {
79     return fd_;
80   }
81
82  private:
83   explicit FileHelper(int fd) : fd_(fd) {}
84   int fd_;
85
86   DISALLOW_COPY_AND_ASSIGN(FileHelper);
87 };
88
89 class 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
108 template <class T>
109 void MoveFromBinaryFormat(T& data, const char*& p) {
110   data = *reinterpret_cast<const T*>(p);
111   p += sizeof(T);
112 }
113
114 void PrintIndented(size_t indent, const char* fmt, ...);
115 void FprintIndented(FILE* fp, size_t indent, const char* fmt, ...);
116
117 bool IsPowerOfTwo(uint64_t value);
118
119 std::vector<std::string> GetEntriesInDir(const std::string& dirpath);
120 std::vector<std::string> GetSubDirs(const std::string& dirpath);
121 bool IsDir(const std::string& dirpath);
122 bool IsRegularFile(const std::string& filename);
123 uint64_t GetFileSize(const std::string& filename);
124 bool MkdirWithParents(const std::string& path);
125
126 bool XzDecompress(const std::string& compressed_data, std::string* decompressed_data);
127
128 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity);
129
130 bool IsRoot();
131
132 struct KernelSymbol {
133   uint64_t addr;
134   char type;
135   const char* name;
136   const char* module;  // If nullptr, the symbol is not in a kernel module.
137 };
138
139 bool ProcessKernelSymbols(std::string& symbol_data,
140                           const std::function<bool(const KernelSymbol&)>& callback);
141
142 size_t GetPageSize();
143
144 uint64_t ConvertBytesToValue(const char* bytes, uint32_t size);
145
146 timeval SecondToTimeval(double time_in_sec);
147
148 #endif  // SIMPLE_PERF_UTILS_H_