Commit b759260c authored by Hailey Wang's avatar Hailey Wang Committed by Commit Bot

[PM] Add calculation of maximum simultaneous load

Add calculation logic for maximum simultaneous load inside
BackgroundTabLoadingPolicy.

This will be used for restricting the number of background tabs
loading in a future CL.

Bug: 1059341
Change-Id: I7ecd751fafb202ffda09472a2121f72a02a22cb3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2120267
Commit-Queue: Hailey Wang <haileywang@google.com>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarSébastien Marchand <sebmarchand@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755396}
parent 924e43f8
...@@ -3371,6 +3371,8 @@ jumbo_static_library("browser") { ...@@ -3371,6 +3371,8 @@ jumbo_static_library("browser") {
"page_load_metrics/observers/session_restore_page_load_metrics_observer.h", "page_load_metrics/observers/session_restore_page_load_metrics_observer.h",
"performance_manager/graph/policies/background_tab_loading_policy.cc", "performance_manager/graph/policies/background_tab_loading_policy.cc",
"performance_manager/graph/policies/background_tab_loading_policy.h", "performance_manager/graph/policies/background_tab_loading_policy.h",
"performance_manager/graph/policies/background_tab_loading_policy_helpers.cc",
"performance_manager/graph/policies/background_tab_loading_policy_helpers.h",
"performance_manager/graph/policies/urgent_page_discarding_policy.cc", "performance_manager/graph/policies/urgent_page_discarding_policy.cc",
"performance_manager/graph/policies/urgent_page_discarding_policy.h", "performance_manager/graph/policies/urgent_page_discarding_policy.h",
"performance_manager/mechanisms/page_discarder.cc", "performance_manager/mechanisms/page_discarder.cc",
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy.h" #include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy.h"
#include "base/system/sys_info.h"
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy_helpers.h"
#include "chrome/browser/performance_manager/mechanisms/page_loader.h" #include "chrome/browser/performance_manager/mechanisms/page_loader.h"
#include "components/performance_manager/public/decorators/tab_properties_decorator.h" #include "components/performance_manager/public/decorators/tab_properties_decorator.h"
#include "components/performance_manager/public/graph/policies/background_tab_loading_policy.h" #include "components/performance_manager/public/graph/policies/background_tab_loading_policy.h"
...@@ -14,7 +16,21 @@ namespace performance_manager { ...@@ -14,7 +16,21 @@ namespace performance_manager {
namespace policies { namespace policies {
namespace { namespace {
// Pointer to the instance of itself.
BackgroundTabLoadingPolicy* g_background_tab_loading_policy = nullptr; BackgroundTabLoadingPolicy* g_background_tab_loading_policy = nullptr;
// Lower bound for the maximum number of tabs to load simultaneously.
uint32_t kMinSimultaneousTabLoads = 1;
// Upper bound for the maximum number of tabs to load simultaneously.
// Setting to zero means no upper bound is applied.
uint32_t kMaxSimultaneousTabLoads = 4;
// The number of CPU cores required per permitted simultaneous tab
// load. Setting to zero means no CPU core limit applies.
uint32_t kCoresPerSimultaneousTabLoad = 2;
} // namespace } // namespace
void ScheduleLoadForRestoredTabs( void ScheduleLoadForRestoredTabs(
...@@ -48,6 +64,9 @@ BackgroundTabLoadingPolicy::BackgroundTabLoadingPolicy() ...@@ -48,6 +64,9 @@ BackgroundTabLoadingPolicy::BackgroundTabLoadingPolicy()
: page_loader_(std::make_unique<mechanism::PageLoader>()) { : page_loader_(std::make_unique<mechanism::PageLoader>()) {
DCHECK(!g_background_tab_loading_policy); DCHECK(!g_background_tab_loading_policy);
g_background_tab_loading_policy = this; g_background_tab_loading_policy = this;
simultaneous_tab_loads_ = CalculateMaxSimultaneousTabLoads(
kMinSimultaneousTabLoads, kMaxSimultaneousTabLoads,
kCoresPerSimultaneousTabLoad, base::SysInfo::NumberOfProcessors());
} }
BackgroundTabLoadingPolicy::~BackgroundTabLoadingPolicy() { BackgroundTabLoadingPolicy::~BackgroundTabLoadingPolicy() {
......
...@@ -69,6 +69,10 @@ class BackgroundTabLoadingPolicy : public GraphOwned, ...@@ -69,6 +69,10 @@ class BackgroundTabLoadingPolicy : public GraphOwned,
// The set of PageNodes that are currently loading. // The set of PageNodes that are currently loading.
std::vector<const PageNode*> page_nodes_loading_; std::vector<const PageNode*> page_nodes_loading_;
// The number of simultaneous tab loads that are permitted by policy. This
// is computed based on the number of cores on the machine.
size_t simultaneous_tab_loads_;
}; };
} // namespace policies } // namespace policies
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy_helpers.h"
#include "base/logging.h"
namespace performance_manager {
namespace policies {
size_t CalculateMaxSimultaneousTabLoads(size_t lower_bound,
size_t upper_bound,
size_t cores_per_load,
size_t num_cores) {
DCHECK(upper_bound == 0 || lower_bound <= upper_bound);
DCHECK(num_cores > 0);
size_t loads = 0;
// Setting |cores_per_load| == 0 means that no per-core limit is applied.
if (cores_per_load == 0) {
loads = std::numeric_limits<size_t>::max();
} else {
loads = num_cores / cores_per_load;
}
// If |upper_bound| isn't zero then apply the maximum that it implies.
if (upper_bound != 0)
loads = std::min(loads, upper_bound);
loads = std::max(loads, lower_bound);
return loads;
}
} // namespace policies
} // namespace performance_manager
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_GRAPH_POLICIES_BACKGROUND_TAB_LOADING_POLICY_HELPERS_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_GRAPH_POLICIES_BACKGROUND_TAB_LOADING_POLICY_HELPERS_H_
#include <stddef.h>
namespace performance_manager {
namespace policies {
// Helper function for BackgroundTabLoadingPolicy to compute the number of
// tabs that can load simultaneously.
size_t CalculateMaxSimultaneousTabLoads(size_t lower_bound,
size_t upper_bound,
size_t cores_per_load,
size_t num_cores);
} // namespace policies
} // namespace performance_manager
#endif // CHROME_BROWSER_PERFORMANCE_MANAGER_GRAPH_POLICIES_BACKGROUND_TAB_LOADING_POLICY_HELPERS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_manager {
namespace policies {
namespace {
class BackgroundTabLoadingPolicyHelpersTest : public ::testing::Test {};
} // namespace
TEST_F(BackgroundTabLoadingPolicyHelpersTest,
CalculateMaxSimultaneousTabLoads) {
// Test the lower bound is enforced.
EXPECT_EQ(10u, CalculateMaxSimultaneousTabLoads(
10 /* lower_bound */, 20 /* upper_bound */,
1 /* cores_per_load */, 1 /* cores */));
// Test the upper bound is enforced.
EXPECT_EQ(20u, CalculateMaxSimultaneousTabLoads(
10 /* lower_bound */, 20 /* upper_bound */,
1 /* cores_per_load */, 30 /* cores */));
// Test the per-core calculation is correct.
EXPECT_EQ(15u, CalculateMaxSimultaneousTabLoads(
10 /* lower_bound */, 20 /* upper_bound */,
1 /* cores_per_load */, 15 /* cores */));
EXPECT_EQ(15u, CalculateMaxSimultaneousTabLoads(
10 /* lower_bound */, 20 /* upper_bound */,
2 /* cores_per_load */, 30 /* cores */));
// If no per-core is specified then upper_bound is returned.
EXPECT_EQ(5u, CalculateMaxSimultaneousTabLoads(
1 /* lower_bound */, 5 /* upper_bound */,
0 /* cores_per_load */, 10 /* cores */));
// If no per-core and no upper_bound is applied, then "upper_bound" is
// returned.
EXPECT_EQ(
std::numeric_limits<size_t>::max(),
CalculateMaxSimultaneousTabLoads(3 /* lower_bound */, 0 /* upper_bound */,
0 /* cores_per_load */, 4 /* cores */));
}
} // namespace policies
} // namespace performance_manager
...@@ -3992,6 +3992,7 @@ test("unit_tests") { ...@@ -3992,6 +3992,7 @@ test("unit_tests") {
"../browser/performance_manager/graph/policies/urgent_page_discarding_policy_unittest.cc", "../browser/performance_manager/graph/policies/urgent_page_discarding_policy_unittest.cc",
# Background tab loading from performance_manager isn't supported on Android. # Background tab loading from performance_manager isn't supported on Android.
"../browser/performance_manager/graph/policies/background_tab_loading_policy_helpers_unittest.cc",
"../browser/performance_manager/graph/policies/background_tab_loading_policy_unittest.cc", "../browser/performance_manager/graph/policies/background_tab_loading_policy_unittest.cc",
# Android does not use the Message Center notification system. # Android does not use the Message Center notification system.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment