-
Notifications
You must be signed in to change notification settings - Fork 196
cobalt/metrics: Add interfaces and stubs for granular memory tracking. #10017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2026 The Chromium Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_DETAILED_METRICS_DELEGATE_H_ | ||
| #define SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_DETAILED_METRICS_DELEGATE_H_ | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "base/component_export.h" | ||
| #include "base/containers/flat_map.h" | ||
|
|
||
| #include "base/memory/weak_ptr.h" | ||
| #include "third_party/abseil-cpp/absl/strings/string_view.h" | ||
|
|
||
| namespace memory_instrumentation { | ||
|
|
||
| // Optimized structure for smaps metrics. | ||
| struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION) | ||
| SmapsMetrics { | ||
| uint64_t rss_kb = 0; | ||
| uint64_t pss_kb = 0; | ||
| uint64_t private_dirty_kb = 0; | ||
| uint64_t private_clean_kb = 0; | ||
| uint64_t shared_dirty_kb = 0; | ||
| uint64_t shared_clean_kb = 0; | ||
| uint64_t swap_kb = 0; | ||
| uint64_t swap_pss_kb = 0; | ||
| }; | ||
|
|
||
| // Interface for project-specific detailed memory metrics collection. | ||
| class COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION) | ||
| DetailedMetricsDelegate { | ||
| public: | ||
| virtual ~DetailedMetricsDelegate() = default; | ||
|
|
||
| // Called for each parsed entry. |name| is transient and must be copied if | ||
| // needed. Implementations must be thread-safe. | ||
| virtual void OnSmapsEntry(absl::string_view name, | ||
| const SmapsMetrics& metrics) = 0; | ||
|
|
||
| // Swaps the internal results map with an empty one and returns it via |stats|. | ||
| virtual void GetAndResetStats(base::flat_map<std::string, uint64_t>& stats) = 0; | ||
|
|
||
| virtual base::WeakPtr<DetailedMetricsDelegate> GetWeakPtr() = 0; | ||
| }; | ||
|
|
||
| } // namespace memory_instrumentation | ||
|
|
||
| #endif // SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_DETAILED_METRICS_DELEGATE_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright 2017 The Chromium Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_OS_METRICS_H_ | ||
| #define SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_OS_METRICS_H_ | ||
|
|
||
|
|
@@ -52,7 +53,8 @@ class COMPONENT_EXPORT( | |
| // the current process is used | ||
| static bool FillOSMemoryDump(base::ProcessHandle handle, | ||
| const MemDumpFlagSet& flags, | ||
| mojom::RawOSMemDump* dump); | ||
| mojom::RawOSMemDump* dump, | ||
| class DetailedMetricsDelegate* delegate = nullptr); | ||
| #if BUILDFLAG(IS_APPLE) | ||
| static bool FillOSMemoryDump(base::ProcessHandle handle, | ||
| const MemDumpFlagSet& flags, | ||
|
|
@@ -64,13 +66,31 @@ class COMPONENT_EXPORT( | |
| mojom::RawOSMemDump*); | ||
| static std::vector<mojom::VmRegionPtr> GetProcessMemoryMaps( | ||
| base::ProcessHandle); | ||
| static std::vector<mojom::VmRegionPtr> GetProcessMemoryMaps( | ||
| const std::string& smaps_content); | ||
|
|
||
| #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) | ||
| static void SetProcSmapsForTesting(FILE*); | ||
| #if BUILDFLAG(IS_COBALT) | ||
| static void SetSmapsRollupForTesting(FILE*); | ||
|
|
||
| // Set the delegate for detailed metrics collection. This must be called | ||
| // before FillOSMemoryDump with MEM_DUMP_DETAILED_STATS. | ||
| static void SetDetailedMetricsDelegate( | ||
| class DetailedMetricsDelegate* delegate); | ||
|
Comment on lines
+79
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The addition of a static References
|
||
| #endif | ||
| #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || | ||
| // BUILDFLAG(IS_ANDROID) | ||
|
|
||
| private: | ||
| #if BUILDFLAG(IS_COBALT) | ||
| static bool FillDetailedMetrics(base::ProcessHandle handle, | ||
| const MemDumpFlagSet& flags, | ||
| mojom::RawOSMemDump* dump, | ||
| class DetailedMetricsDelegate* delegate); | ||
| static bool ReadDetailedMetricsFile(base::ProcessHandle handle, | ||
| class DetailedMetricsDelegate* delegate); | ||
| #endif | ||
| FRIEND_TEST_ALL_PREFIXES(OSMetricsTest, ParseProcSmaps); | ||
| FRIEND_TEST_ALL_PREFIXES(OSMetricsTest, TestWinModuleReading); | ||
| FRIEND_TEST_ALL_PREFIXES(OSMetricsTest, TestMachOReading); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning a raw pointer to the delegate from
GetDetailedMetricsDelegate()is potentially unsafe, even with the use ofdetailed_metrics_delegate_lock_. Once the lock is released, the caller holds a pointer that could be nullified or the underlying object destroyed by another thread (e.g., via a call toSetDetailedMetricsDelegate). Consider a safer access pattern, such as passing the delegate directly to the methods that require it, or using a callback/observer pattern if the lifetime cannot be strictly guaranteed.