Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 915059 | Differences between
and this patch

Collapse All | Expand All

(-)qtwebengine-5.15.10_p20230815-orig/src/3rdparty/chromium/third_party/perfetto/include/perfetto/ext/base/utils.h (-51 / +103 lines)
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
(-) (+121 lines)
Added Link Here
1
/*
2
 * Copyright (C) 2020 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
#include "perfetto/ext/base/utils.h"
18
19
#include <string>
20
21
#include "perfetto/base/build_config.h"
22
#include "perfetto/base/logging.h"
23
#include "perfetto/ext/base/file_utils.h"
24
#include "perfetto/ext/base/pipe.h"
25
#include "perfetto/ext/base/string_utils.h"
26
27
#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
28
    PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
29
    PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) ||   \
30
    PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
31
#include <limits.h>
32
#include <stdlib.h>  // For _exit()
33
#include <unistd.h>  // For getpagesize() and geteuid() & fork()
34
#endif
35
36
#if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
37
#include <mach-o/dyld.h>
38
#include <mach/vm_page_size.h>
39
#endif
40
41
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
42
#include <Windows.h>
43
#include <io.h>
44
#include <malloc.h>  // For _aligned_malloc().
45
#endif
46
47
#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
48
#include <dlfcn.h>
49
#include <malloc.h>
50
51
#ifdef M_PURGE
52
#define PERFETTO_M_PURGE M_PURGE
53
#else
54
// Only available in in-tree builds and on newer SDKs.
55
#define PERFETTO_M_PURGE -101
56
#endif  // M_PURGE
57
58
#ifdef M_PURGE_ALL
59
#define PERFETTO_M_PURGE_ALL M_PURGE_ALL
60
#else
61
// Only available in in-tree builds and on newer SDKs.
62
#define PERFETTO_M_PURGE_ALL -104
63
#endif  // M_PURGE
64
65
namespace {
66
extern "C" {
67
using MalloptType = int (*)(int, int);
68
}
69
}  // namespace
70
#endif  // OS_ANDROID
71
72
namespace perfetto {
73
namespace base {
74
75
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
96
void* AlignedAlloc(size_t alignment, size_t size) {
97
  void* res = nullptr;
98
  alignment = AlignUp<sizeof(void*)>(alignment);  // At least pointer size.
99
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
100
  // Window's _aligned_malloc() has a nearly identically signature to Unix's
101
  // aligned_alloc() but its arguments are obviously swapped.
102
  res = _aligned_malloc(size, alignment);
103
#else
104
  // aligned_alloc() has been introduced in Android only in API 28.
105
  // Also NaCl and Fuchsia seems to have only posix_memalign().
106
  ignore_result(posix_memalign(&res, alignment, size));
107
#endif
108
  PERFETTO_CHECK(res);
109
  return res;
110
}
111
112
void AlignedFree(void* ptr) {
113
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
114
  _aligned_free(ptr);  // MSDN says it is fine to pass nullptr.
115
#else
116
  free(ptr);
117
#endif
118
}
119
120
}  // namespace base
121
}  // namespace perfetto
(-)qtwebengine-5.15.10_p20230815-orig/src/3rdparty/chromium/third_party/perfetto/src/base/BUILD.gn (+1 lines)
Lines 34-39 Link Here
34
    "subprocess.cc",
34
    "subprocess.cc",
35
    "thread_checker.cc",
35
    "thread_checker.cc",
36
    "time.cc",
36
    "time.cc",
37
    "utils.cc",
37
    "uuid.cc",
38
    "uuid.cc",
38
    "virtual_destructors.cc",
39
    "virtual_destructors.cc",
39
    "waitable_event.cc",
40
    "waitable_event.cc",

Return to bug 915059