Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 1 | // Copyright 2023 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 | #ifndef BASE_METRICS_HISTOGRAM_SHARED_MEMORY_H_ |
| 6 | #define BASE_METRICS_HISTOGRAM_SHARED_MEMORY_H_ |
| 7 | |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 8 | #include <optional> |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 9 | #include <string_view> |
| 10 | |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 11 | #include "base/base_export.h" |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 12 | #include "base/command_line.h" |
| 13 | #include "base/feature_list.h" |
Gyuyoung Kim | 40cc667c | 2025-04-15 14:07:26 | [diff] [blame] | 14 | #include "base/memory/shared_memory_switch.h" |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 15 | #include "base/memory/unsafe_shared_memory_region.h" |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 16 | #include "base/metrics/persistent_memory_allocator.h" |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 17 | #include "base/process/launch.h" |
| 18 | #include "build/build_config.h" |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 19 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 20 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) |
| 21 | #include "base/files/platform_file.h" |
| 22 | #include "base/posix/global_descriptors.h" |
| 23 | #endif |
| 24 | |
| 25 | #if !BUILDFLAG(USE_BLINK) |
| 26 | #error "This is only intended for platforms that use blink." |
| 27 | #endif |
| 28 | |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 29 | namespace base { |
| 30 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 31 | BASE_EXPORT BASE_DECLARE_FEATURE(kPassHistogramSharedMemoryOnLaunch); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 32 | |
| 33 | // Helper structure to create and return a shared memory region and a histogram |
| 34 | // allocator over top of it. Once returned it is expected that the caller will |
| 35 | // move both the memory regions and the allocator out of the struct and into |
| 36 | // it's own appropriate state variables. Note that the memory region must |
| 37 | // outlive the allocator. |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 38 | struct BASE_EXPORT HistogramSharedMemory { |
| 39 | HistogramSharedMemory() = delete; |
| 40 | ~HistogramSharedMemory() = delete; |
| 41 | HistogramSharedMemory(HistogramSharedMemory&) = delete; |
| 42 | HistogramSharedMemory(HistogramSharedMemory&&) = delete; |
| 43 | HistogramSharedMemory& operator=(HistogramSharedMemory&) = delete; |
| 44 | HistogramSharedMemory& operator=(HistogramSharedMemory&&) = delete; |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 45 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 46 | // Configuration with which to create a histogram shared memory region and |
| 47 | // allocator. Note the expectation that this be initialized with static |
| 48 | // data for the allocator name (i.e., a string literal or static constant |
| 49 | // character array). |
| 50 | struct BASE_EXPORT Config { |
| 51 | const int process_type; // See: content/public/common/process_type.h |
| 52 | const std::string_view allocator_name; |
| 53 | const size_t memory_size_bytes; |
| 54 | }; |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 55 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 56 | // Temporary structure used to return the shared memory region and allocator |
| 57 | // created by the |Create| factory function. The caller is expected to move |
| 58 | // the returned values out of this struct. |
| 59 | struct BASE_EXPORT SharedMemory { |
| 60 | UnsafeSharedMemoryRegion region; |
| 61 | std::unique_ptr<PersistentMemoryAllocator> allocator; |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 62 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 63 | SharedMemory(UnsafeSharedMemoryRegion, |
| 64 | std::unique_ptr<PersistentMemoryAllocator>); |
| 65 | ~SharedMemory(); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 66 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 67 | // Movable |
| 68 | SharedMemory(SharedMemory&&); |
| 69 | SharedMemory& operator=(SharedMemory&&); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 70 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 71 | // Not copyable |
| 72 | SharedMemory(SharedMemory&) = delete; |
| 73 | SharedMemory& operator=(SharedMemory&) = delete; |
| 74 | }; |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 75 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 76 | // Factory to initialize a shared |memory_region| and |allocator| for |
| 77 | // |process_id| based on |config|. On success, returns true and updates |
| 78 | // the values of |memory_region| and |allocator|. On failure, returns false |
| 79 | // and |memory_region| and |allocator| are unchanged. |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 80 | static std::optional<SharedMemory> Create(int process_id, |
| 81 | const Config& config); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 82 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 83 | #if BUILDFLAG(IS_APPLE) |
| 84 | // Exposed for testing. |
Gyuyoung Kim | 40cc667c | 2025-04-15 14:07:26 | [diff] [blame] | 85 | static const shared_memory::SharedMemoryMachPortRendezvousKey kRendezvousKey; |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 86 | #endif |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 87 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 88 | // Returns true if passing the shared memory handle via command-line arguments |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 89 | // is enabled. |process_type| values should come from content:::ProcessType. |
| 90 | static bool PassOnCommandLineIsEnabled(int process_type); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 91 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 92 | // Updates the launch parameters to share |unsafe_memory_region| to a |
| 93 | // child process that is about to be launched. This should be called in the |
| 94 | // parent process as a part of setting up the launch conditions of the child. |
| 95 | // This call will update the |command_line| and |launch_options|. On posix, |
| 96 | // where we prefer to use a zygote instead of using the launch_options to |
| 97 | // launch a new process, the platform |descriptor_to_share| is returned. The |
| 98 | // caller is expected to transmit the descriptor to the launch flow for the |
| 99 | // zygote. |
| 100 | static void AddToLaunchParameters( |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 101 | const UnsafeSharedMemoryRegion& unsafe_memory_region, |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 102 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) |
| 103 | GlobalDescriptors::Key descriptor_key, |
| 104 | ScopedFD& descriptor_to_share, |
| 105 | #endif |
| 106 | CommandLine* command_line, |
| 107 | LaunchOptions* launch_options); |
| 108 | |
| 109 | // Initialize the (global) histogram shared memory from the launch parameters. |
| 110 | // This should be called in the child process before any histogram samples are |
| 111 | // recorded. |
| 112 | static void InitFromLaunchParameters(const CommandLine& command_line); |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace base |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 116 | |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 117 | #endif // BASE_METRICS_HISTOGRAM_SHARED_MEMORY_H_ |