From 5190ee9f96419e132bc71215495730791b7aae4a Mon Sep 17 00:00:00 2001 From: Drew Thomas Date: Wed, 21 Aug 2024 09:32:22 -0700 Subject: [PATCH] Add `performance.memory.jsHeapSizeLimit` support (#3928) This value, in combination with the existing `totalJSHeapSize` and `usedJSHeapSize` values, can help describe memory usage of a webpage. See the MDN reference: https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#value b/355487905 --- cobalt/dom/memory_info.cc | 14 ++++++++++++++ cobalt/dom/memory_info.h | 2 ++ cobalt/dom/memory_info.idl | 1 + cobalt/script/javascript_engine.h | 1 + cobalt/script/v8c/v8c_engine.cc | 3 ++- 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cobalt/dom/memory_info.cc b/cobalt/dom/memory_info.cc index d6875450f7bd0..1c6619e6cc760 100644 --- a/cobalt/dom/memory_info.cc +++ b/cobalt/dom/memory_info.cc @@ -61,6 +61,20 @@ uint32 MemoryInfo::used_js_heap_size( .used_heap_size); } +uint32 MemoryInfo::js_heap_size_limit( + script::EnvironmentSettings* environment_settings) const { + if (!environment_settings) { + return 0u; + } + return static_cast( + base::polymorphic_downcast( + environment_settings) + ->context() + ->javascript_engine() + ->GetHeapStatistics() + .heap_size_limit); +} + uint32 MemoryInfo::media_source_size_limit( script::EnvironmentSettings* environment_settings) const { auto memory_info = GetDecoderBufferMemoryInfo(environment_settings); diff --git a/cobalt/dom/memory_info.h b/cobalt/dom/memory_info.h index a6e4dcfd50b2a..5d1dd57d10ffd 100644 --- a/cobalt/dom/memory_info.h +++ b/cobalt/dom/memory_info.h @@ -33,6 +33,8 @@ class MemoryInfo : public script::Wrappable { script::EnvironmentSettings* environment_settings) const; uint32 used_js_heap_size( script::EnvironmentSettings* environment_settings) const; + uint32 js_heap_size_limit( + script::EnvironmentSettings* environment_settings) const; uint32 media_source_size_limit( script::EnvironmentSettings* environment_settings) const; uint32 total_media_source_size( diff --git a/cobalt/dom/memory_info.idl b/cobalt/dom/memory_info.idl index 4308390a6b80b..16257fae9076e 100644 --- a/cobalt/dom/memory_info.idl +++ b/cobalt/dom/memory_info.idl @@ -20,6 +20,7 @@ ] interface MemoryInfo { [CallWith=EnvironmentSettings] readonly attribute unsigned long totalJSHeapSize; [CallWith=EnvironmentSettings] readonly attribute unsigned long usedJSHeapSize; + [CallWith=EnvironmentSettings] readonly attribute unsigned long jsHeapSizeLimit; [CallWith=EnvironmentSettings] readonly attribute unsigned long mediaSourceSizeLimit; [CallWith=EnvironmentSettings] readonly attribute unsigned long totalMediaSourceSize; diff --git a/cobalt/script/javascript_engine.h b/cobalt/script/javascript_engine.h index 55124a79294c6..a7807a7e499b4 100644 --- a/cobalt/script/javascript_engine.h +++ b/cobalt/script/javascript_engine.h @@ -31,6 +31,7 @@ class GlobalEnvironment; struct HeapStatistics { size_t total_heap_size; size_t used_heap_size; + size_t heap_size_limit; }; class JavaScriptEngine { diff --git a/cobalt/script/v8c/v8c_engine.cc b/cobalt/script/v8c/v8c_engine.cc index 2caa5fa61be21..89e7a135167f7 100644 --- a/cobalt/script/v8c/v8c_engine.cc +++ b/cobalt/script/v8c/v8c_engine.cc @@ -206,7 +206,8 @@ HeapStatistics V8cEngine::GetHeapStatistics() { v8::HeapStatistics v8_heap_statistics; isolate_->GetHeapStatistics(&v8_heap_statistics); return {v8_heap_statistics.total_heap_size(), - v8_heap_statistics.used_heap_size()}; + v8_heap_statistics.used_heap_size(), + v8_heap_statistics.heap_size_limit()}; } void V8cEngine::UpdateDateTimeConfiguration() {