File: performance_log_source.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (115 lines) | stat: -rw-r--r-- 4,258 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
// 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/feedback/system_logs/log_sources/performance_log_source.h"

#include <string>
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/to_string.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/performance_manager/public/user_tuning/user_performance_tuning_manager.h"
#include "components/feedback/feedback_report.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/user_tuning/prefs.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"

namespace system_logs {

using performance_manager::user_tuning::prefs::BatterySaverModeState;
using performance_manager::user_tuning::prefs::kBatterySaverModeState;

namespace {
std::string BoolToString(bool value) {
  return base::ToString(value);
}

#if !BUILDFLAG(IS_CHROMEOS)
std::string BatterySaverModeStateToString(BatterySaverModeState state) {
  switch (state) {
    case BatterySaverModeState::kDisabled:
      return "disabled";
    case BatterySaverModeState::kEnabledBelowThreshold:
      return "enabled_threshold";
    case BatterySaverModeState::kEnabledOnBattery:
      return "enabled_battery_power";
    case BatterySaverModeState::kEnabled:
      return "enabled";
    default:
      return "unknown_battery_saver_mode_state";
  }
}
#endif  //  !BUILDFLAG(IS_CHROMEOS)

}  // namespace

PerformanceLogSource::PerformanceLogSource() : SystemLogsSource("Performance") {
  battery_saver_mode_manager_ =
      performance_manager::user_tuning::BatterySaverModeManager::GetInstance();
  tuning_manager_ = performance_manager::user_tuning::
      UserPerformanceTuningManager::GetInstance();
}

PerformanceLogSource::~PerformanceLogSource() = default;

void PerformanceLogSource::Fetch(SysLogsSourceCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!callback.is_null());

  auto response = std::make_unique<SystemLogsResponse>();
  CHECK(tuning_manager_);
  PopulatePerformanceSettingLogs(response.get());
#if !BUILDFLAG(IS_CHROMEOS)
  PopulateBatteryDetailLogs(response.get());
#endif
  std::move(callback).Run(std::move(response));
}

void PerformanceLogSource::PopulatePerformanceSettingLogs(
    SystemLogsResponse* response) {
  response->emplace("high_efficiency_mode_active",
                    BoolToString(tuning_manager_->IsMemorySaverModeActive()));

#if !BUILDFLAG(IS_CHROMEOS)
  // Battery and battery saver logs are not used on ChromeOS.
  PrefService* local_prefs = g_browser_process->local_state();
  int battery_saver_state = local_prefs->GetInteger(kBatterySaverModeState);
  bool is_battery_saver_active =
      battery_saver_mode_manager_->IsBatterySaverActive();
  bool is_battery_saver_disabled_for_session =
      battery_saver_mode_manager_->IsBatterySaverModeDisabledForSession();

  response->emplace(
      "battery_saver_state",
      BatterySaverModeStateToString(
          static_cast<BatterySaverModeState>(battery_saver_state)));
  response->emplace("battery_saver_mode_active",
                    BoolToString(is_battery_saver_active));
  response->emplace("battery_saver_disabled_for_session",
                    BoolToString(is_battery_saver_disabled_for_session));
#endif  //  !BUILDFLAG(IS_CHROMEOS)
}

#if !BUILDFLAG(IS_CHROMEOS)
void PerformanceLogSource::PopulateBatteryDetailLogs(
    SystemLogsResponse* response) {
  bool has_battery = battery_saver_mode_manager_->DeviceHasBattery();
  bool is_using_battery_power =
      battery_saver_mode_manager_->IsUsingBatteryPower();
  int battery_percentage =
      battery_saver_mode_manager_->SampledBatteryPercentage();

  response->emplace("device_has_battery", BoolToString(has_battery));
  response->emplace("device_using_battery_power",
                    BoolToString(is_using_battery_power));
  response->emplace("device_battery_percentage",
                    base::NumberToString(battery_percentage));
}
#endif  //  !BUILDFLAG(IS_CHROMEOS)

}  // namespace system_logs