xref: /aosp_15_r20/external/cronet/base/profiler/stack_buffer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/profiler/stack_buffer.h"
6 
7 #include <bit>
8 
9 #if BUILDFLAG(IS_CHROMEOS)
10 #include <sys/mman.h>
11 
12 #include <ostream>
13 
14 #include "base/bits.h"
15 #include "base/check.h"
16 #include "base/check_op.h"
17 #include "base/memory/page_size.h"
18 #endif  // #if BUILDFLAG(IS_CHROMEOS)
19 
20 namespace base {
21 
22 constexpr size_t StackBuffer::kPlatformStackAlignment;
23 
24 #if BUILDFLAG(IS_CHROMEOS)
25 
MarkUpperBufferContentsAsUnneeded(size_t retained_bytes)26 void StackBuffer::MarkUpperBufferContentsAsUnneeded(size_t retained_bytes) {
27   // Round up to the next multiple of the page size. madvise needs the
28   // starting address to be page aligned. Since buffer_.get() is
29   // already page aligned, we just need to round up the retained bytes.
30   size_t actual_retained_bytes = bits::AlignUp(retained_bytes, GetPageSize());
31 
32   // Avoid passing a negative discard_size to madvise(). Doing so would randomly
33   // discard large amounts of memory causing weird crashes.
34   CHECK_LE(actual_retained_bytes, size_);
35 
36   uint8_t* start_of_discard =
37       reinterpret_cast<uint8_t*>(buffer_.get()) + actual_retained_bytes;
38   size_t discard_size = size_ - actual_retained_bytes;
39   int result = madvise(start_of_discard, discard_size, MADV_DONTNEED);
40 
41   DPCHECK(result == 0) << "madvise failed: ";
42 }
43 
44 #endif  // #if BUILDFLAG(IS_CHROMEOS)
45 
StackBuffer(size_t buffer_size)46 StackBuffer::StackBuffer(size_t buffer_size)
47 #if BUILDFLAG(IS_CHROMEOS)
48     // On ChromeOS, we have 8MB of stack space per thread; however, we normally
49     // only use a small fraction of that. To avoid blowing our memory budget,
50     // we use madvise(MADV_DONTNEED) to let the kernel discard the memory in the
51     // 8MB buffer except when we are actively using it. For madvise() to work,
52     // we need |buffer_| to be aligned to a page boundary.
53     //
54     // We also need the |size_| to be a multiple of the page size so that we
55     // don't pass partial pages to madvise(). This isn't documented but the
56     // program will consistently crash otherwise.
57     : size_(bits::AlignUp(buffer_size, GetPageSize())),
58       buffer_(static_cast<uintptr_t*>(AlignedAlloc(size_, GetPageSize()))) {
59   // Our (very large) buffer may already have data written to it & thus have
60   // backing pages. Tell the kernel we don't need the current contents.
61   MarkUpperBufferContentsAsUnneeded(0);
62 }
63 #else   // #if BUILDFLAG(IS_CHROMEOS)
64     : size_(buffer_size),
65       buffer_(static_cast<uintptr_t*>(
66           AlignedAlloc(size_, kPlatformStackAlignment))) {
67   static_assert(std::has_single_bit(kPlatformStackAlignment));
68 }
69 #endif  // !#if BUILDFLAG(IS_CHROMEOS)
70 
71 StackBuffer::~StackBuffer() = default;
72 
73 }  // namespace base
74