Skip to content
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

Implement metric for free cpu memory #5116

Merged
merged 8 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ source_set("browser") {
"//cobalt/browser/h5vcc_system",
"//cobalt/browser/h5vcc_system/public/mojom",
"//cobalt/browser/migrate_storage_record",
"//cobalt/browser/performance",
"//cobalt/browser/performance/public/mojom",
"//cobalt/media/service",
"//cobalt/media/service/mojom",
"//cobalt/user_agent",
Expand Down
4 changes: 4 additions & 0 deletions cobalt/browser/cobalt_browser_interface_binders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "cobalt/browser/h5vcc_runtime/public/mojom/h5vcc_runtime.mojom.h"
#include "cobalt/browser/h5vcc_system/h5vcc_system_impl.h"
#include "cobalt/browser/h5vcc_system/public/mojom/h5vcc_system.mojom.h"
#include "cobalt/browser/performance/performance_impl.h"
#include "cobalt/browser/performance/public/mojom/performance.mojom.h"

#if BUILDFLAG(IS_ANDROIDTV)
#include "content/public/browser/render_frame_host.h"
Expand Down Expand Up @@ -63,6 +65,8 @@ void PopulateCobaltFrameBinders(
base::BindRepeating(&h5vcc_system::H5vccSystemImpl::Create));
binder_map->Add<h5vcc_runtime::mojom::H5vccRuntime>(
base::BindRepeating(&h5vcc_runtime::H5vccRuntimeImpl::Create));
binder_map->Add<performance::mojom::CobaltPerformance>(
base::BindRepeating(&performance::PerformanceImpl::Create));
}

} // namespace cobalt
27 changes: 27 additions & 0 deletions cobalt/browser/performance/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source_set("performance") {
sources = [
"performance_impl.cc",
"performance_impl.h",
]

deps = [
"//base",
"//cobalt/browser/performance/public/mojom",
"//content/public/browser",
"//mojo/public/cpp/bindings",
]
}
38 changes: 38 additions & 0 deletions cobalt/browser/performance/performance_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/browser/performance/performance_impl.h"

#include "base/system/sys_info.h"

namespace performance {

PerformanceImpl::PerformanceImpl(
content::RenderFrameHost& render_frame_host,
mojo::PendingReceiver<mojom::CobaltPerformance> receiver)
: content::DocumentService<mojom::CobaltPerformance>(render_frame_host,
std::move(receiver)) {}

void PerformanceImpl::Create(
content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<mojom::CobaltPerformance> receiver) {
new PerformanceImpl(*render_frame_host, std::move(receiver));
}

void PerformanceImpl::MeasureAvailableCpuMemory(
MeasureAvailableCpuMemoryCallback callback) {
std::move(callback).Run(base::SysInfo::AmountOfAvailablePhysicalMemory());
}

} // namespace performance
51 changes: 51 additions & 0 deletions cobalt/browser/performance/performance_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_BROWSER_PERFORMANCE_PERFORMANCE_H_
#define COBALT_BROWSER_PERFORMANCE_PERFORMANCE_H_

#include "cobalt/browser/performance/public/mojom/performance.mojom.h"
#include "content/public/browser/document_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"

namespace content {
class RenderFrameHost;
} // namespace content

namespace performance {

// Implements the H5vccSystem Mojo interface and extends
// DocumentService so that an object's lifetime is scoped to the corresponding
// document / RenderFrameHost (see DocumentService for details).
class PerformanceImpl
: public content::DocumentService<mojom::CobaltPerformance> {
public:
// Creates a PerformanceImpl. The PerformanceImpl is bound to the
// receiver and its lifetime is scoped to the render_frame_host.
static void Create(content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<mojom::CobaltPerformance> receiver);

PerformanceImpl(const PerformanceImpl&) = delete;
PerformanceImpl& operator=(const PerformanceImpl&) = delete;

void MeasureAvailableCpuMemory(MeasureAvailableCpuMemoryCallback) override;

private:
PerformanceImpl(content::RenderFrameHost& render_frame_host,
mojo::PendingReceiver<mojom::CobaltPerformance> receiver);
};

} // namespace performance

#endif // COBALT_BROWSER_PERFORMANCE_PERFORMANCE_H_
20 changes: 20 additions & 0 deletions cobalt/browser/performance/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//mojo/public/tools/bindings/mojom.gni")

mojom("mojom") {
generate_java = is_android
sources = [ "performance.mojom" ]
}
21 changes: 21 additions & 0 deletions cobalt/browser/performance/public/mojom/performance.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module performance.mojom;

interface CobaltPerformance {
// Get the amount of available memory on the device in bytes.
[Sync]
MeasureAvailableCpuMemory() => (uint64 bytes);
};
4 changes: 4 additions & 0 deletions third_party/blink/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ mojom("mojom_core") {
"//url/mojom:url_mojom_origin",
]

if (is_cobalt) {
public_deps += [ "//cobalt/browser/performance/public/mojom" ]
}

if (is_android) {
# Direct deps (instead of transitive deps) are necessary for java targets.
public_deps += [
Expand Down
6 changes: 6 additions & 0 deletions third_party/blink/renderer/bindings/idl_in_core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,12 @@ static_idl_files_in_core = get_path_info(
],
"abspath")

if (is_cobalt) {
static_idl_files_in_core += get_path_info(
[ "//third_party/blink/renderer/core/cobalt/performance/performance_extensions.idl" ],
"abspath")
}

# Statically-defined (not build-time-generated) IDL files in 'core' component.
# These IDL definitions are used only for testing.
static_idl_files_in_core_for_testing = get_path_info(
Expand Down
5 changes: 5 additions & 0 deletions third_party/blink/renderer/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ if (is_ios) {
}
if (is_cobalt) {
import("//starboard/build/buildflags.gni")
import("//third_party/blink/renderer/core/cobalt/build.gni")
}

visibility = [
Expand Down Expand Up @@ -304,6 +305,10 @@ component("core") {
rebase_path(blink_core_sources_xmlhttprequest, "", "xmlhttprequest")
sources += rebase_path(blink_core_sources_speech, "", "speech")

if (is_cobalt) {
sources += rebase_path(blink_core_sources_cobalt, "", "cobalt")
}

configs -= core_config_remove
configs += core_config_add
configs += [ "//v8:external_startup_data" ]
Expand Down
20 changes: 20 additions & 0 deletions third_party/blink/renderer/core/cobalt/build.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

assert(is_cobalt)

blink_core_sources_cobalt = [
"performance/performance_extensions.cc",
"performance/performance_extensions.h",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "third_party/blink/renderer/core/cobalt/performance/performance_extensions.h"

#include "cobalt/browser/performance/public/mojom/performance.mojom.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/timing/performance.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"

namespace blink {

uint64_t PerformanceExtensions::measureAvailableCpuMemory(
ScriptState* script_state,
const Performance&) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
DCHECK(execution_context);

HeapMojoRemote<performance::mojom::CobaltPerformance>
remote_performance_system(execution_context);
auto task_runner =
execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI);
execution_context->GetBrowserInterfaceBroker().GetInterface(
remote_performance_system.BindNewPipeAndPassReceiver(task_runner));

uint64_t free_memory = 0;
remote_performance_system->MeasureAvailableCpuMemory(&free_memory);
return free_memory;
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_COBALT_PERFORMANCE_PERFORMANCE_EXTENSIONS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_COBALT_PERFORMANCE_PERFORMANCE_EXTENSIONS_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace blink {

class Performance;
class ScriptState;

class CORE_EXPORT PerformanceExtensions final {
STATIC_ONLY(PerformanceExtensions);

public:
// Web-exposed interface:
static uint64_t measureAvailableCpuMemory(ScriptState*, const Performance&);
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_COBALT_PERFORMANCE_PERFORMANCE_EXTENSIONS_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Non standard interface used for Cobalt only.
[
ImplementedAs=PerformanceExtensions
] partial interface Performance {
// Returns the amount of available memory on the device in bytes, according
// to base::SysInfo::AmountOfAvailablePhysicalMemory().
[CallWith=ScriptState] unsigned long long measureAvailableCpuMemory();
};
Loading