File: user_tuning_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (120 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2022 The Chromium Authors
// 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/public/user_tuning/user_tuning_utils.h"

#include <optional>

#include "base/check.h"
#include "base/feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"
#include "chrome/browser/performance_manager/public/user_tuning/battery_saver_mode_manager.h"
#include "chrome/browser/performance_manager/public/user_tuning/user_performance_tuning_manager.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/performance_manager.h"

#if !BUILDFLAG(IS_ANDROID)
#include <algorithm>
#include <string>
#include <vector>

#include "chrome/browser/performance_manager/policies/cannot_discard_reason.h"
#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#endif

namespace performance_manager::user_tuning {

bool IsRefreshRateThrottled() {
#if BUILDFLAG(IS_ANDROID)
  return false;
#else
  // This can be false in unit tests.
  if (!BatterySaverModeManager::HasInstance()) {
    return false;
  }

  return BatterySaverModeManager::GetInstance()->IsBatterySaverActive();
#endif
}

bool IsBatterySaverModeManagedByOS() {
#if BUILDFLAG(IS_CHROMEOS)
  return ash::features::IsBatterySaverAvailable();
#else
  return false;
#endif
}

uint64_t GetDiscardedMemoryEstimateForPage(const PageNode* node) {
  DCHECK_ON_GRAPH_SEQUENCE(node->GetGraph());

  return node->EstimatePrivateFootprintSize();
}

std::vector<std::string> GetCannotDiscardReasonsForPageNode(
    const PageNode* page_node) {
#if BUILDFLAG(IS_ANDROID)
  // TODO(crbug.com/399740817): Enable PageDiscardingHelper after
  // WebContentsDiscard launch.
  return {"not implemented"};
#else
  auto* discarding_helper = policies::DiscardEligibilityPolicy::GetFromGraph(
      PerformanceManager::GetGraph());
  CHECK(discarding_helper);
  CHECK(page_node);

  std::vector<policies::CannotDiscardReason> cannot_discard_reasons;
  discarding_helper->CanDiscard(
      page_node, policies::DiscardEligibilityPolicy::DiscardReason::PROACTIVE,
      policies::kNonVisiblePagesUrgentProtectionTime, &cannot_discard_reasons);

  std::vector<std::string> results;
  results.reserve(cannot_discard_reasons.size());  // Reserve space
  std::transform(cannot_discard_reasons.begin(), cannot_discard_reasons.end(),
                 std::back_inserter(results),
                 policies::CannotDiscardReasonToString);
  return results;
#endif
}

void DiscardPage(const PageNode* page_node,
                 ::mojom::LifecycleUnitDiscardReason reason,
                 bool ignore_minimum_time_in_background) {
#if !BUILDFLAG(IS_ANDROID)
  auto* discarding_helper = policies::PageDiscardingHelper::GetFromGraph(
      PerformanceManager::GetGraph());
  CHECK(discarding_helper);
  CHECK(page_node);
  discarding_helper->ImmediatelyDiscardMultiplePages(
      {page_node}, reason,
      ignore_minimum_time_in_background
          ? base::TimeDelta()
          : policies::kNonVisiblePagesUrgentProtectionTime);
#endif
}

void DiscardAnyPage(::mojom::LifecycleUnitDiscardReason reason,
                    bool ignore_minimum_time_in_background) {
#if !BUILDFLAG(IS_ANDROID)
  auto* discarding_helper = policies::PageDiscardingHelper::GetFromGraph(
      PerformanceManager::GetGraph());
  CHECK(discarding_helper);
  discarding_helper->DiscardAPage(
      reason, ignore_minimum_time_in_background
                  ? base::TimeDelta()
                  : policies::kNonVisiblePagesUrgentProtectionTime);
#endif
}

}  //  namespace performance_manager::user_tuning