stack_buffer.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_PROFILER_STACK_BUFFER_H_
  5. #define BASE_PROFILER_STACK_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include "base/base_export.h"
  10. #include "base/memory/aligned_memory.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. // This class contains a buffer for stack copies that can be shared across
  14. // multiple instances of StackSampler.
  15. class BASE_EXPORT StackBuffer {
  16. public:
  17. // The expected alignment of the stack on the current platform. Windows and
  18. // System V AMD64 ABIs on x86, x64, and ARM require the stack to be aligned
  19. // to twice the pointer size. Excepted from this requirement is code setting
  20. // up the stack during function calls (between pushing the return address
  21. // and the end of the function prologue). The profiler will sometimes
  22. // encounter this exceptional case for leaf frames.
  23. static constexpr size_t kPlatformStackAlignment = 2 * sizeof(uintptr_t);
  24. explicit StackBuffer(size_t buffer_size);
  25. StackBuffer(const StackBuffer&) = delete;
  26. StackBuffer& operator=(const StackBuffer&) = delete;
  27. ~StackBuffer();
  28. // Returns a kPlatformStackAlignment-aligned pointer to the stack buffer.
  29. uintptr_t* buffer() const {
  30. // Aligned during allocation.
  31. return buffer_.get();
  32. }
  33. // Size in bytes.
  34. size_t size() const { return size_; }
  35. #if BUILDFLAG(IS_CHROMEOS)
  36. // Tell the kernel that we no longer need the data currently in the upper
  37. // parts of the buffer and that the kernel may discard it to free up space.
  38. // Specifically, the bytes from |(uint8_t*)buffer + retained_bytes| to the
  39. // end of the buffer may be discarded, while the bytes from |buffer| to
  40. // |(uint8_t*)buffer + retained_bytes - 1| will not be affected.
  41. // The program can still write to that part of the buffer, but should not read
  42. // from that part of buffer until after the next write. (The contents of that
  43. // part of the buffer are undefined.)
  44. // After calling this function, there may be a page fault on the next write to
  45. // that area, so it should only be called when parts of the buffer were
  46. // written to and will probably not be written to again soon.
  47. void MarkUpperBufferContentsAsUnneeded(size_t retained_bytes);
  48. #endif
  49. private:
  50. // The size in bytes of the requested buffer allocation.
  51. const size_t size_;
  52. // The buffer to store the stack. Already aligned.
  53. const std::unique_ptr<uintptr_t, AlignedFreeDeleter> buffer_;
  54. };
  55. } // namespace base
  56. #endif // BASE_PROFILER_STACK_BUFFER_H_