File: chrome_responsiveness_calculator_delegate.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,061 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 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/metrics/usage_scenario/chrome_responsiveness_calculator_delegate.h"

#include "base/check.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/system/sys_info.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario_data_store.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario_tracker.h"

namespace {

bool IsChromeUsedInScenario(Scenario scenario) {
  // Exclude all scenarios in which Chrome is not being used. Defined as either
  // visible to the user, playing audio, or capturing video.
  switch (scenario) {
    case Scenario::kAllTabsHiddenNoVideoCaptureOrAudio:
    case Scenario::kZeroWindow:
      return false;

    case Scenario::kAllTabsHiddenAudio:
    case Scenario::kAllTabsHiddenVideoCapture:
    case Scenario::kAudio:
    case Scenario::kEmbeddedVideoNoNavigation:
    case Scenario::kEmbeddedVideoWithNavigation:
    case Scenario::kFullscreenVideo:
    case Scenario::kInteraction:
    case Scenario::kNavigation:
    case Scenario::kPassive:
    case Scenario::kVideoCapture:
      return true;

    case Scenario::kAllTabsHiddenNoVideoCaptureOrAudioRecent:
    case Scenario::kZeroWindowRecent:
      // Short scenario only.
      NOTREACHED();
  }
}

#if BUILDFLAG(IS_CHROMEOS)
// Returns true if available memory is less than 5.7% of total memory. It's
// based on ChromeOS stable 7 day aggregation ending May 20th 2024 from
// Memory.Experimental.AvailableMemoryPercent 10 percentile.
bool IsLowMemory() {
  auto available_bytes = base::SysInfo::AmountOfAvailablePhysicalMemory();
  auto total_bytes = base::SysInfo::AmountOfPhysicalMemory();
  return (available_bytes * 1000 / total_bytes) < 57;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

const char* GetSuffixForExtensionCount(size_t extension_count) {
  if (extension_count >= 16) {
    return "16";
  }
  if (extension_count >= 8) {
    return "8";
  }
  if (extension_count >= 4) {
    return "4";
  }
  if (extension_count >= 2) {
    return "2";
  }
  if (extension_count == 1) {
    return "1";
  }
  return "0";
}

}  // namespace

// static
std::unique_ptr<ChromeResponsivenessCalculatorDelegate>
ChromeResponsivenessCalculatorDelegate::Create() {
  // The instance will create its own data store if one is not provided.
  return base::WrapUnique(new ChromeResponsivenessCalculatorDelegate(
      /*usage_scenario_data_store=*/nullptr));
}

// static
std::unique_ptr<ChromeResponsivenessCalculatorDelegate>
ChromeResponsivenessCalculatorDelegate::CreateForTesting(
    UsageScenarioDataStore* usage_scenario_data_store) {
  return base::WrapUnique(
      new ChromeResponsivenessCalculatorDelegate(usage_scenario_data_store));
}

ChromeResponsivenessCalculatorDelegate::
    ~ChromeResponsivenessCalculatorDelegate() = default;

void ChromeResponsivenessCalculatorDelegate::OnMeasurementIntervalEnded() {
  auto interval_data = usage_scenario_data_store_->ResetIntervalData();
  interval_scenario_params_ = GetLongIntervalScenario(interval_data);
  extensions_with_content_scripts_in_interval_ =
      interval_data.num_extensions_with_content_scripts;
}

void ChromeResponsivenessCalculatorDelegate::OnResponsivenessEmitted(
    int num_congested_slices,
    int min,
    int exclusive_max,
    size_t buckets) {
  CHECK(interval_scenario_params_);
  base::UmaHistogramCustomCounts(
      base::StrCat({"Browser.MainThreadsCongestion",
                    interval_scenario_params_->histogram_suffix}),
      num_congested_slices, min, exclusive_max, buckets);

  if (IsChromeUsedInScenario(interval_scenario_params_->scenario)) {
    base::UmaHistogramCustomCounts("Browser.MainThreadsCongestion.Used",
                                   num_congested_slices, min, exclusive_max,
                                   buckets);
  }
  if (extensions_with_content_scripts_in_interval_.has_value()) {
    base::UmaHistogramCustomCounts(
        base::StrCat(
            {"Browser.MainThreadsCongestion.ExtensionContentScripts.",
             GetSuffixForExtensionCount(
                 extensions_with_content_scripts_in_interval_.value())}),
        num_congested_slices, min, exclusive_max, buckets);
  }
#if BUILDFLAG(IS_CHROMEOS)
  if (IsLowMemory()) {
    base::UmaHistogramCustomCounts("Browser.MainThreadsCongestion.LowMemory",
                                   num_congested_slices, min, exclusive_max,
                                   buckets);
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
}

ChromeResponsivenessCalculatorDelegate::ChromeResponsivenessCalculatorDelegate(
    UsageScenarioDataStore* usage_scenario_data_store)
    : usage_scenario_data_store_(usage_scenario_data_store) {
  if (!usage_scenario_data_store_) {
    usage_scenario_tracker_ = std::make_unique<UsageScenarioTracker>();
    usage_scenario_data_store_ = usage_scenario_tracker_->data_store();
  }
}