blob: fc0eccaeec16215ee18a2ffa4e369407f31bbd82 [file] [log] [blame]
Roger McFarlanec92516502023-05-15 20:05:121// 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 Sonzognie5fff99c2024-02-21 15:58:248#include <optional>
Roger McFarlanecba0e902024-01-24 17:51:379#include <string_view>
10
Roger McFarlanec92516502023-05-15 20:05:1211#include "base/base_export.h"
Roger McFarlanecba0e902024-01-24 17:51:3712#include "base/command_line.h"
13#include "base/feature_list.h"
Gyuyoung Kim40cc667c2025-04-15 14:07:2614#include "base/memory/shared_memory_switch.h"
Roger McFarlanecba0e902024-01-24 17:51:3715#include "base/memory/unsafe_shared_memory_region.h"
Roger McFarlanec92516502023-05-15 20:05:1216#include "base/metrics/persistent_memory_allocator.h"
Roger McFarlanecba0e902024-01-24 17:51:3717#include "base/process/launch.h"
18#include "build/build_config.h"
Roger McFarlanec92516502023-05-15 20:05:1219
Roger McFarlanecba0e902024-01-24 17:51:3720#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 McFarlanec92516502023-05-15 20:05:1229namespace base {
30
Roger McFarlanecba0e902024-01-24 17:51:3731BASE_EXPORT BASE_DECLARE_FEATURE(kPassHistogramSharedMemoryOnLaunch);
Roger McFarlanec92516502023-05-15 20:05:1232
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 McFarlanecba0e902024-01-24 17:51:3738struct 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 McFarlanec92516502023-05-15 20:05:1245
Roger McFarlanecba0e902024-01-24 17:51:3746 // 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 McFarlanec92516502023-05-15 20:05:1255
Roger McFarlanecba0e902024-01-24 17:51:3756 // 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 McFarlanec92516502023-05-15 20:05:1262
Roger McFarlanecba0e902024-01-24 17:51:3763 SharedMemory(UnsafeSharedMemoryRegion,
64 std::unique_ptr<PersistentMemoryAllocator>);
65 ~SharedMemory();
Roger McFarlanec92516502023-05-15 20:05:1266
Roger McFarlanecba0e902024-01-24 17:51:3767 // Movable
68 SharedMemory(SharedMemory&&);
69 SharedMemory& operator=(SharedMemory&&);
Roger McFarlanec92516502023-05-15 20:05:1270
Roger McFarlanecba0e902024-01-24 17:51:3771 // Not copyable
72 SharedMemory(SharedMemory&) = delete;
73 SharedMemory& operator=(SharedMemory&) = delete;
74 };
Roger McFarlanec92516502023-05-15 20:05:1275
Roger McFarlanecba0e902024-01-24 17:51:3776 // 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 Sonzognie5fff99c2024-02-21 15:58:2480 static std::optional<SharedMemory> Create(int process_id,
81 const Config& config);
Roger McFarlanec92516502023-05-15 20:05:1282
Roger McFarlanecba0e902024-01-24 17:51:3783#if BUILDFLAG(IS_APPLE)
84 // Exposed for testing.
Gyuyoung Kim40cc667c2025-04-15 14:07:2685 static const shared_memory::SharedMemoryMachPortRendezvousKey kRendezvousKey;
Roger McFarlanecba0e902024-01-24 17:51:3786#endif
Roger McFarlanec92516502023-05-15 20:05:1287
Roger McFarlanecba0e902024-01-24 17:51:3788 // Returns true if passing the shared memory handle via command-line arguments
Roger McFarlane2f246882025-01-22 16:12:1489 // is enabled. |process_type| values should come from content:::ProcessType.
90 static bool PassOnCommandLineIsEnabled(int process_type);
Roger McFarlanec92516502023-05-15 20:05:1291
Roger McFarlanecba0e902024-01-24 17:51:3792 // 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 McFarlane2f246882025-01-22 16:12:14101 const UnsafeSharedMemoryRegion& unsafe_memory_region,
Roger McFarlanecba0e902024-01-24 17:51:37102#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 McFarlanec92516502023-05-15 20:05:12113};
114
115} // namespace base
Roger McFarlanecba0e902024-01-24 17:51:37116
Roger McFarlanec92516502023-05-15 20:05:12117#endif // BASE_METRICS_HISTOGRAM_SHARED_MEMORY_H_