File: performance_controls_metrics.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 (270 lines) | stat: -rw-r--r-- 10,168 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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/ui/performance_controls/performance_controls_metrics.h"

#include <memory>

#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/performance_manager/public/user_tuning/performance_detection_manager.h"
#include "chrome/browser/ui/performance_controls/performance_intervention_button_controller.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/daily_event.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/user_tuning/prefs.h"
#include "components/prefs/pref_service.h"

namespace {

using performance_manager::user_tuning::PerformanceDetectionManager;

// The interval at which the DailyEvent::CheckInterval function should be
// called.
constexpr base::TimeDelta kDailyEventIntervalTimeDelta = base::Minutes(30);

std::string GetDetectionResourceTypeString(
    PerformanceDetectionManager::ResourceType resource_type) {
  switch (resource_type) {
    case PerformanceDetectionManager::ResourceType::kCpu:
      return "Cpu";
    case PerformanceDetectionManager::ResourceType::kMemory:
      return "Memory";
    case PerformanceDetectionManager::ResourceType::kNetwork:
      return "Network";
  }
}

class DailyEventObserver : public metrics::DailyEvent::Observer {
 public:
  explicit DailyEventObserver(PrefService* pref_service)
      : pref_service_(pref_service) {}

  ~DailyEventObserver() override = default;

  void OnDailyEvent(metrics::DailyEvent::IntervalType type) override {
    base::UmaHistogramCounts100(
        base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                      GetDetectionResourceTypeString(
                          PerformanceDetectionManager::ResourceType::kCpu),
                      ".MessageShownCount"}),
        GetAndResetPref(
            prefs::kPerformanceInterventionBackgroundCpuMessageCount));

    base::UmaHistogramCounts100(
        base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                      GetDetectionResourceTypeString(
                          PerformanceDetectionManager::ResourceType::kCpu),
                      ".RateLimitedCount"}),
        GetAndResetPref(
            prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount));

    PrefService* const pref_service = g_browser_process->local_state();
    if (base::FeatureList::IsEnabled(
            performance_manager::features::
                kPerformanceInterventionNotificationImprovements) &&
        !pref_service
             ->GetList(performance_manager::user_tuning::prefs::
                           kPerformanceInterventionNotificationAcceptHistory)
             .empty()) {
      base::UmaHistogramPercentage(
          "PerformanceControls.Intervention.DailyAcceptancePercentage",
          PerformanceInterventionButtonController::GetAcceptancePercentage());
    }
  }

  int GetAndResetPref(std::string pref_name) {
    const int previous_count = pref_service_->GetInteger(pref_name);
    pref_service_->ClearPref(pref_name);
    return previous_count;
  }

 private:
  raw_ptr<PrefService> pref_service_ = nullptr;
};

CpuInterventionHealthChange GetHealthChange(
    PerformanceDetectionManager::HealthLevel before_discard,
    PerformanceDetectionManager::HealthLevel after_discard) {
  // Add one to the max health level since it uses zero based indexing.
  const int num_health_levels =
      static_cast<int>(PerformanceDetectionManager::HealthLevel::kMaxValue) + 1;
  const int health_change =
      num_health_levels * static_cast<int>(before_discard) +
      static_cast<int>(after_discard);
  CHECK_LE(health_change,
           static_cast<int>(CpuInterventionHealthChange::kMaxValue));
  return static_cast<CpuInterventionHealthChange>(health_change);
}

}  // namespace

PerformanceInterventionMetricsReporter::PerformanceInterventionMetricsReporter(
    PrefService* pref_service)
    : daily_event_(std::make_unique<metrics::DailyEvent>(
          pref_service,
          prefs::kPerformanceInterventionDailySample,
          std::string())) {
  daily_event_->AddObserver(std::make_unique<DailyEventObserver>(pref_service));
  daily_event_->CheckInterval();
  daily_event_timer_.Start(FROM_HERE, kDailyEventIntervalTimeDelta,
                           daily_event_.get(),
                           &metrics::DailyEvent::CheckInterval);
}

PerformanceInterventionMetricsReporter::
    ~PerformanceInterventionMetricsReporter() = default;

void PerformanceInterventionMetricsReporter::RegisterLocalStatePrefs(
    PrefRegistrySimple* registry) {
  metrics::DailyEvent::RegisterPref(registry,
                                    prefs::kPerformanceInterventionDailySample);
  registry->RegisterIntegerPref(
      prefs::kPerformanceInterventionBackgroundCpuMessageCount, 0);
  registry->RegisterIntegerPref(
      prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount, 0);
}

void RecordBatterySaverBubbleAction(BatterySaverBubbleActionType type) {
  base::UmaHistogramEnumeration("PerformanceControls.BatterySaver.BubbleAction",
                                type);
}

void RecordBatterySaverIPHOpenSettings(bool success) {
  base::UmaHistogramBoolean("PerformanceControls.BatterySaver.IPHOpenSettings",
                            success);
}

void RecordMemorySaverBubbleAction(MemorySaverBubbleActionType type) {
  base::UmaHistogramEnumeration("PerformanceControls.MemorySaver.BubbleAction",
                                type);
}

void RecordMemorySaverIPHEnableMode(bool success) {
  base::UmaHistogramBoolean("PerformanceControls.MemorySaver.IPHEnableMode",
                            success);
}

void RecordMemorySaverChipState(MemorySaverChipState state) {
  base::UmaHistogramEnumeration("PerformanceControls.MemorySaver.ChipState",
                                state);
}

void RecordInterventionMessageCount(
    PerformanceDetectionManager::ResourceType resource_type,
    PrefService* pref_service) {
  const int previous_count = pref_service->GetInteger(
      prefs::kPerformanceInterventionBackgroundCpuMessageCount);
  pref_service->SetInteger(
      prefs::kPerformanceInterventionBackgroundCpuMessageCount,
      previous_count + 1);
}

void RecordInterventionRateLimitedCount(
    PerformanceDetectionManager::ResourceType resource_type,
    PrefService* pref_service) {
  const int previous_count = pref_service->GetInteger(
      prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount);
  pref_service->SetInteger(
      prefs::kPerformanceInterventionBackgroundCpuRateLimitedCount,
      previous_count + 1);
}

void RecordInterventionTriggerResult(
    PerformanceDetectionManager::ResourceType resource_type,
    InterventionMessageTriggerResult reason) {
  base::UmaHistogramEnumeration(
      base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                    GetDetectionResourceTypeString(resource_type),
                    ".MessageTriggerResult"}),
      reason);
}

void RecordInterventionToolbarButtonClicked() {
  base::RecordComputedAction("PerformanceIntervention.Toolbar.OpenDialog");
}

void RecordInterventionBubbleClosedReason(
    PerformanceDetectionManager::ResourceType resource_type,
    InterventionBubbleActionType type) {
  base::UmaHistogramEnumeration(
      base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                    GetDetectionResourceTypeString(resource_type),
                    ".BubbleAction"}),
      type);
}

void RecordCpuHealthStatusAfterDiscard(
    base::TimeDelta time_after_discard,
    PerformanceDetectionManager::HealthLevel health_level) {
  std::string time = std::string();
  if (time_after_discard == base::Minutes(1)) {
    time = "1Min";
  } else if (time_after_discard == base::Minutes(2)) {
    time = "2Min";
  } else {
    CHECK_EQ(time_after_discard, base::Minutes(4));
    time = "4Min";
  }

  base::UmaHistogramEnumeration(
      base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                    GetDetectionResourceTypeString(
                        PerformanceDetectionManager::ResourceType::kCpu),
                    ".HealthStatusAfterDiscard.", time}),
      health_level);
}

void RecordCpuUsageBeforeDiscard(int cpu_usage) {
  base::UmaHistogramPercentage(
      "PerformanceControls.Intervention.DiscardedTabsPercentageUsage",
      cpu_usage);
}

void RecordSuggestedTabShownCount(int count) {
  base::UmaHistogramCustomCounts(
      "PerformanceControls.Intervention.SuggestedTabShownCount", count, 0, 10,
      10);
}

void RecordTabRemovedFromTabList(int count_after_removal) {
  base::RecordComputedAction(
      "PerformanceIntervention.Dialog.RemovedSuggestion");
  base::UmaHistogramCustomCounts(
      "PerformanceControls.Intervention.Dialog.TabsAfterRemovalCount",
      count_after_removal, 0, 10, 10);
}

void RecordNumberOfDiscardedTabs(int count) {
  base::UmaHistogramCustomCounts(
      "PerformanceControls.Intervention.DiscardedTabCount", count, 0, 10, 10);
}

void RecordCpuHealthStatusChange(
    base::TimeDelta time_after_discard,
    PerformanceDetectionManager::HealthLevel before_discard,
    PerformanceDetectionManager::HealthLevel after_discard) {
  std::string time = std::string();
  if (time_after_discard == base::Minutes(1)) {
    time = "1Min";
  } else if (time_after_discard == base::Minutes(2)) {
    time = "2Min";
  } else {
    CHECK_EQ(time_after_discard, base::Minutes(4));
    time = "4Min";
  }

  base::UmaHistogramEnumeration(
      base::StrCat({"PerformanceControls.Intervention.BackgroundTab.",
                    GetDetectionResourceTypeString(
                        PerformanceDetectionManager::ResourceType::kCpu),
                    ".HealthStatusChange.", time}),
      GetHealthChange(before_discard, after_discard));
}