Lines 17-41
Link Here
|
17 |
#ifndef INCLUDE_PERFETTO_EXT_BASE_UTILS_H_ |
17 |
#ifndef INCLUDE_PERFETTO_EXT_BASE_UTILS_H_ |
18 |
#define INCLUDE_PERFETTO_EXT_BASE_UTILS_H_ |
18 |
#define INCLUDE_PERFETTO_EXT_BASE_UTILS_H_ |
19 |
|
19 |
|
20 |
#include "perfetto/base/build_config.h" |
|
|
21 |
#include "perfetto/base/compiler.h" |
22 |
|
23 |
#include <errno.h> |
20 |
#include <errno.h> |
24 |
#include <stddef.h> |
21 |
#include <stddef.h> |
|
|
22 |
#include <stdint.h> |
25 |
#include <stdlib.h> |
23 |
#include <stdlib.h> |
26 |
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
|
|
27 |
#include <sys/types.h> |
28 |
#endif |
29 |
|
30 |
#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ |
31 |
PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
32 |
#include <unistd.h> // For getpagesize(). |
33 |
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) |
34 |
#include <mach/vm_page_size.h> |
35 |
#endif |
36 |
|
24 |
|
37 |
#include <atomic> |
25 |
#include <atomic> |
|
|
26 |
#include <functional> |
27 |
#include <memory> |
28 |
#include <string> |
38 |
|
29 |
|
|
|
30 |
#include "perfetto/base/build_config.h" |
31 |
#include "perfetto/base/compiler.h" |
32 |
#include "perfetto/ext/base/sys_types.h" |
33 |
|
34 |
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
35 |
// Even if Windows has errno.h, the all syscall-restart behavior does not apply. |
36 |
// Trying to handle EINTR can cause more harm than good if errno is left stale. |
37 |
// Chromium does the same. |
38 |
#define PERFETTO_EINTR(x) (x) |
39 |
#else |
39 |
#define PERFETTO_EINTR(x) \ |
40 |
#define PERFETTO_EINTR(x) \ |
40 |
([&] { \ |
41 |
([&] { \ |
41 |
decltype(x) eintr_wrapper_result; \ |
42 |
decltype(x) eintr_wrapper_result; \ |
Lines 44-67
Link Here
|
44 |
} while (eintr_wrapper_result == -1 && errno == EINTR); \ |
45 |
} while (eintr_wrapper_result == -1 && errno == EINTR); \ |
45 |
return eintr_wrapper_result; \ |
46 |
return eintr_wrapper_result; \ |
46 |
}()) |
47 |
}()) |
47 |
|
|
|
48 |
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
49 |
// TODO(brucedawson) - create a ::perfetto::base::IOSize to replace this. |
50 |
#if defined(_WIN64) |
51 |
using ssize_t = __int64; |
52 |
#else |
53 |
using ssize_t = long; |
54 |
#endif |
55 |
#endif |
48 |
#endif |
56 |
|
49 |
|
57 |
namespace perfetto { |
50 |
namespace perfetto { |
58 |
namespace base { |
51 |
namespace base { |
59 |
|
52 |
|
60 |
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
|
|
61 |
constexpr uid_t kInvalidUid = static_cast<uid_t>(-1); |
62 |
constexpr pid_t kInvalidPid = static_cast<pid_t>(-1); |
63 |
#endif |
64 |
|
65 |
// Do not add new usages of kPageSize, consider using GetSysPageSize() below. |
53 |
// Do not add new usages of kPageSize, consider using GetSysPageSize() below. |
66 |
// TODO(primiano): over time the semantic of kPageSize became too ambiguous. |
54 |
// TODO(primiano): over time the semantic of kPageSize became too ambiguous. |
67 |
// Strictly speaking, this constant is incorrect on some new devices where the |
55 |
// Strictly speaking, this constant is incorrect on some new devices where the |
Lines 72-101
Link Here
|
72 |
|
60 |
|
73 |
// Returns the system's page size. Use this when dealing with mmap, madvise and |
61 |
// Returns the system's page size. Use this when dealing with mmap, madvise and |
74 |
// similar mm-related syscalls. |
62 |
// similar mm-related syscalls. |
75 |
inline uint32_t GetSysPageSize() { |
63 |
uint32_t GetSysPageSize(); |
76 |
ignore_result(kPageSize); // Just to keep the amalgamated build happy. |
|
|
77 |
#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ |
78 |
PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
79 |
static std::atomic<uint32_t> page_size{0}; |
80 |
// This function might be called in hot paths. Avoid calling getpagesize() all |
81 |
// the times, in many implementations getpagesize() calls sysconf() which is |
82 |
// not cheap. |
83 |
uint32_t cached_value = page_size.load(std::memory_order_relaxed); |
84 |
if (PERFETTO_UNLIKELY(cached_value == 0)) { |
85 |
cached_value = static_cast<uint32_t>(getpagesize()); |
86 |
page_size.store(cached_value, std::memory_order_relaxed); |
87 |
} |
88 |
return cached_value; |
89 |
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) |
90 |
return static_cast<uint32_t>(vm_page_size); |
91 |
#else |
92 |
return 4096; |
93 |
#endif |
94 |
} |
95 |
|
64 |
|
96 |
template <typename T> |
65 |
template <typename T, size_t TSize> |
97 |
constexpr size_t ArraySize(const T& array) { |
66 |
constexpr size_t ArraySize(const T (&)[TSize]) { |
98 |
return sizeof(array) / sizeof(array[0]); |
67 |
return TSize; |
99 |
} |
68 |
} |
100 |
|
69 |
|
101 |
// Function object which invokes 'free' on its parameter, which must be |
70 |
// Function object which invokes 'free' on its parameter, which must be |
Lines 109-116
Link Here
|
109 |
|
78 |
|
110 |
template <typename T> |
79 |
template <typename T> |
111 |
constexpr T AssumeLittleEndian(T value) { |
80 |
constexpr T AssumeLittleEndian(T value) { |
112 |
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, |
|
|
113 |
"Unimplemented on big-endian archs"); |
114 |
return value; |
81 |
return value; |
115 |
} |
82 |
} |
116 |
|
83 |
|
Lines 125-130
Link Here
|
125 |
return err == EAGAIN || err == EWOULDBLOCK; |
92 |
return err == EAGAIN || err == EWOULDBLOCK; |
126 |
} |
93 |
} |
127 |
|
94 |
|
|
|
95 |
// setenv(2)-equivalent. Deals with Windows vs Posix discrepancies. |
96 |
void SetEnv(const std::string& key, const std::string& value); |
97 |
|
98 |
// unsetenv(2)-equivalent. Deals with Windows vs Posix discrepancies. |
99 |
void UnsetEnv(const std::string& key); |
100 |
|
101 |
// Calls mallopt(M_PURGE, 0) on Android. Does nothing on other platforms. |
102 |
// This forces the allocator to release freed memory. This is used to work |
103 |
// around various Scudo inefficiencies. See b/170217718. |
104 |
void MaybeReleaseAllocatorMemToOS(); |
105 |
|
106 |
// geteuid() on POSIX OSes, returns 0 on Windows (See comment in utils.cc). |
107 |
uid_t GetCurrentUserId(); |
108 |
|
109 |
// Forks the process. |
110 |
// Parent: prints the PID of the child, calls |parent_cb| and exits from the |
111 |
// process with its return value. |
112 |
// Child: redirects stdio onto /dev/null, chdirs into / and returns. |
113 |
void Daemonize(std::function<int()> parent_cb); |
114 |
|
115 |
// Returns the path of the current executable, e.g. /foo/bar/exe. |
116 |
std::string GetCurExecutablePath(); |
117 |
|
118 |
// Returns the directory where the current executable lives in, e.g. /foo/bar. |
119 |
// This is independent of cwd(). |
120 |
std::string GetCurExecutableDir(); |
121 |
|
122 |
// Memory returned by AlignedAlloc() must be freed via AlignedFree() not just |
123 |
// free. It makes a difference on Windows where _aligned_malloc() and |
124 |
// _aligned_free() must be paired. |
125 |
// Prefer using the AlignedAllocTyped() below which takes care of the pairing. |
126 |
void* AlignedAlloc(size_t alignment, size_t size); |
127 |
void AlignedFree(void*); |
128 |
|
129 |
// A RAII version of the above, which takes care of pairing Aligned{Alloc,Free}. |
130 |
template <typename T> |
131 |
struct AlignedDeleter { |
132 |
inline void operator()(T* ptr) const { AlignedFree(ptr); } |
133 |
}; |
134 |
|
135 |
// The remove_extent<T> here and below is to allow defining unique_ptr<T[]>. |
136 |
// As per https://en.cppreference.com/w/cpp/memory/unique_ptr the Deleter takes |
137 |
// always a T*, not a T[]*. |
138 |
template <typename T> |
139 |
using AlignedUniquePtr = |
140 |
std::unique_ptr<T, AlignedDeleter<typename std::remove_extent<T>::type>>; |
141 |
|
142 |
template <typename T> |
143 |
AlignedUniquePtr<T> AlignedAllocTyped(size_t n_membs) { |
144 |
using TU = typename std::remove_extent<T>::type; |
145 |
return AlignedUniquePtr<T>( |
146 |
static_cast<TU*>(AlignedAlloc(alignof(TU), sizeof(TU) * n_membs))); |
147 |
} |
148 |
|
149 |
// A RAII wrapper to invoke a function when leaving a function/scope. |
150 |
template <typename Func> |
151 |
class OnScopeExitWrapper { |
152 |
public: |
153 |
explicit OnScopeExitWrapper(Func f) : f_(std::move(f)), active_(true) {} |
154 |
OnScopeExitWrapper(OnScopeExitWrapper&& other) noexcept |
155 |
: f_(std::move(other.f_)), active_(other.active_) { |
156 |
other.active_ = false; |
157 |
} |
158 |
~OnScopeExitWrapper() { |
159 |
if (active_) |
160 |
f_(); |
161 |
} |
162 |
|
163 |
private: |
164 |
Func f_; |
165 |
bool active_; |
166 |
}; |
167 |
|
168 |
template <typename Func> |
169 |
PERFETTO_WARN_UNUSED_RESULT OnScopeExitWrapper<Func> OnScopeExit(Func f) { |
170 |
return OnScopeExitWrapper<Func>(std::move(f)); |
171 |
} |
172 |
|
173 |
// Returns a xxd-style hex dump (hex + ascii chars) of the input data. |
174 |
std::string HexDump(const void* data, size_t len, size_t bytes_per_line = 16); |
175 |
inline std::string HexDump(const std::string& data, |
176 |
size_t bytes_per_line = 16) { |
177 |
return HexDump(data.data(), data.size(), bytes_per_line); |
178 |
} |
179 |
|
128 |
} // namespace base |
180 |
} // namespace base |
129 |
} // namespace perfetto |
181 |
} // namespace perfetto |
130 |
|
182 |
|