Skip to content

Commit

Permalink
base/sampling_heap_profiler: Use BUILDFLAG for OS checking
Browse files Browse the repository at this point in the history
Use BUILDFLAG(IS_XXX) instead of defined(OS_XXX).

Generated by `os_buildflag_migration.py` (https://crrev.com/c/3311983).

[email protected]

Bug: 1234043
Test: No functionality change
Change-Id: I871b52a9f4fc00dda84018b5dc86ed6c1349dd7e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3390833
Reviewed-by: Nico Weber <[email protected]>
Commit-Queue: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#959596}
  • Loading branch information
xhwang-chromium authored and Chromium LUCI CQ committed Jan 15, 2022
1 parent 1a8409e commit 33df695
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
14 changes: 7 additions & 7 deletions base/sampling_heap_profiler/poisson_allocation_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "base/ranges/algorithm.h"
#include "build/build_config.h"

#if defined(OS_APPLE) || defined(OS_ANDROID)
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_ANDROID)
#include <pthread.h>
#endif

Expand All @@ -29,7 +29,7 @@ using allocator::AllocatorDispatch;

namespace {

#if defined(OS_APPLE) || defined(OS_ANDROID)
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_ANDROID)

// The macOS implementation of libmalloc sometimes calls malloc recursively,
// delegating allocations between zones. That causes our hooks being called
Expand Down Expand Up @@ -321,7 +321,7 @@ AllocatorDispatch g_allocator_dispatch = {&AllocFn,

#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)

#if BUILDFLAG(USE_PARTITION_ALLOC) && !defined(OS_NACL)
#if BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(IS_NACL)

void PartitionAllocHook(void* address, size_t size, const char* type) {
PoissonAllocationSampler::RecordAlloc(
Expand All @@ -332,7 +332,7 @@ void PartitionFreeHook(void* address) {
PoissonAllocationSampler::RecordFree(address);
}

#endif // BUILDFLAG(USE_PARTITION_ALLOC) && !defined(OS_NACL)
#endif // BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(IS_NACL)

void InstallStandardAllocatorHooks() {
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
Expand All @@ -343,18 +343,18 @@ void InstallStandardAllocatorHooks() {
// happen for tests.
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)

#if BUILDFLAG(USE_PARTITION_ALLOC) && !defined(OS_NACL)
#if BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(IS_NACL)
PartitionAllocHooks::SetObserverHooks(&PartitionAllocHook,
&PartitionFreeHook);
#endif // BUILDFLAG(USE_PARTITION_ALLOC) && !defined(OS_NACL)
#endif // BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(IS_NACL)
}

void RemoveStandardAllocatorHooksForTesting() {
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
allocator::RemoveAllocatorDispatchForTesting(
&g_allocator_dispatch); // IN-TEST
#endif
#if BUILDFLAG(USE_PARTITION_ALLOC) && !defined(OS_NACL)
#if BUILDFLAG(USE_PARTITION_ALLOC) && !BUILDFLAG(IS_NACL)
PartitionAllocHooks::SetObserverHooks(nullptr, nullptr);
#endif
}
Expand Down
19 changes: 10 additions & 9 deletions base/sampling_heap_profiler/sampling_heap_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
#include "base/trace_event/heap_profiler_allocation_context_tracker.h" // no-presubmit-check
#include "build/build_config.h"

#if defined(OS_APPLE)
#if BUILDFLAG(IS_APPLE)
#include <pthread.h>
#endif

#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID)
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
#include <sys/prctl.h>
#endif

#if defined(OS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
defined(OFFICIAL_BUILD)
#include "base/trace_event/cfi_backtrace_android.h" // no-presubmit-check
#endif
Expand All @@ -53,18 +53,19 @@ const char* GetAndLeakThreadName() {
// 64 on macOS, see PlatformThread::SetName in platform_thread_mac.mm.
constexpr size_t kBufferLen = 64;
char name[kBufferLen];
#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID)
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
// If the thread name is not set, try to get it from prctl. Thread name might
// not be set in cases where the thread started before heap profiling was
// enabled.
int err = prctl(PR_GET_NAME, name);
if (!err)
return strdup(name);
#elif defined(OS_APPLE)
#elif BUILDFLAG(IS_APPLE)
int err = pthread_getname_np(pthread_self(), name, kBufferLen);
if (err == 0 && *name != '\0')
return strdup(name);
#endif // defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID)
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
// BUILDFLAG(IS_ANDROID)

// Use tid if we don't have a thread name.
snprintf(name, sizeof(name), "Thread %lu",
Expand All @@ -81,7 +82,7 @@ const char* UpdateAndGetThreadName(const char* name) {
return thread_name;
}

#if defined(OS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
defined(OFFICIAL_BUILD)
// Checks whether unwinding from this function works.
bool HasDefaultUnwindTables() {
Expand Down Expand Up @@ -110,7 +111,7 @@ SamplingHeapProfiler::~SamplingHeapProfiler() {
}

uint32_t SamplingHeapProfiler::Start() {
#if defined(OS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
defined(OFFICIAL_BUILD)
if (!trace_event::CFIBacktraceAndroid::GetInitializedInstance()
->can_unwind_stack_frames()) {
Expand Down Expand Up @@ -162,7 +163,7 @@ void** SamplingHeapProfiler::CaptureStackTrace(void** frames,
size_t* count) {
// Skip top frames as they correspond to the profiler itself.
size_t skip_frames = 3;
#if defined(OS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(CAN_UNWIND_WITH_CFI_TABLE) && \
defined(OFFICIAL_BUILD)
size_t frame_count = 0;
if (use_default_unwinder_) {
Expand Down
2 changes: 1 addition & 1 deletion base/sampling_heap_profiler/sampling_heap_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class BASE_EXPORT SamplingHeapProfiler
// Whether it should record thread names.
std::atomic<bool> record_thread_names_{false};

#if defined(OS_ANDROID)
#if BUILDFLAG(IS_ANDROID)
// Whether to use CFI unwinder or default unwinder.
std::atomic<bool> use_default_unwinder_{false};
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace base {
class SamplingHeapProfilerTest : public ::testing::Test {
public:
void SetUp() override {
#if defined(OS_APPLE)
#if BUILDFLAG(IS_APPLE)
allocator::InitializeAllocatorShim();
#endif
SamplingHeapProfiler::Init();
Expand Down Expand Up @@ -118,7 +118,7 @@ TEST_F(SamplingHeapProfilerTest, IntervalRandomizationSanity) {
EXPECT_NEAR(1000, mean_samples, 100); // 10% tolerance.
}

#if defined(OS_IOS)
#if BUILDFLAG(IS_IOS)
// iOS devices generally have ~4GB of RAM with no swap and therefore need a
// lower allocation limit here.
const int kNumberOfAllocations = 1000;
Expand Down Expand Up @@ -225,7 +225,7 @@ TEST_F(SamplingHeapProfilerTest, DISABLED_SequentialLargeSmallStats) {
// Platform TLS: alloc+free[ns]: 22.184 alloc[ns]: 8.910 free[ns]: 13.274
// thread_local: alloc+free[ns]: 18.353 alloc[ns]: 5.021 free[ns]: 13.331
// TODO(crbug.com/1117342) Disabled on Mac
#if defined(OS_MAC)
#if BUILDFLAG(IS_MAC)
#define MAYBE_MANUAL_SamplerMicroBenchmark DISABLED_MANUAL_SamplerMicroBenchmark
#else
#define MAYBE_MANUAL_SamplerMicroBenchmark MANUAL_SamplerMicroBenchmark
Expand Down Expand Up @@ -292,7 +292,7 @@ TEST_F(SamplingHeapProfilerTest, StartStop) {
}

// TODO(crbug.com/1116543): Test is crashing on Mac.
#if defined(OS_MAC)
#if BUILDFLAG(IS_MAC)
#define MAYBE_ConcurrentStartStop DISABLED_ConcurrentStartStop
#else
#define MAYBE_ConcurrentStartStop ConcurrentStartStop
Expand Down

0 comments on commit 33df695

Please sign in to comment.