File: power_metrics.h

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 (88 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef CHROME_BROWSER_METRICS_POWER_POWER_METRICS_H_
#define CHROME_BROWSER_METRICS_POWER_POWER_METRICS_H_

#include <optional>
#include <vector>

#include "base/power_monitor/battery_level_provider.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/metrics/power/process_monitor.h"

// Report aggregated process metrics to histograms with |suffixes|.
void ReportAggregatedProcessMetricsHistograms(
    const ProcessMonitor::Metrics& aggregated_process_metrics,
    const std::vector<const char*>& suffixes);

// Any change to this enum should be reflected in the corresponding enums.xml
// and ukm.xml
enum class BatteryDischargeMode {
  kDischarging = 0,
  kPluggedIn = 1,
  kStateChanged = 2,
  kRetrievalError = 3,
  kNoBattery = 4,
  kBatteryLevelIncreased = 5,
  // kInvalidInterval = 6,  No longer used.
  kMacFullyCharged = 7,
  kMultipleBatteries = 8,
  kFullChargedCapacityIsZero = 9,
  kInsufficientResolution = 10,
  kMaxValue = kInsufficientResolution,
};

struct BatteryDischarge {
  BatteryDischargeMode mode;
  // Discharge rate in milliwatts.
  std::optional<int64_t> rate_milliwatts;
#if BUILDFLAG(IS_WIN)
  // Discharge rate in milliwatts, if the client's battery discharge granularity
  // is at most 17 mWh. Calculated using the used capacity instead of the
  // current capacity.
  std::optional<int64_t> rate_milliwatts_with_precise_granularity;
#endif  // BUILDFLAG(IS_WIN)
  // Discharge rate in hundredth of a percent per minute.
  std::optional<int64_t> rate_relative;
};

// Returns the discharge rate in milliwatts.
int64_t CalculateDischargeRateMilliwatts(
    const base::BatteryLevelProvider::BatteryState& previous_battery_state,
    const base::BatteryLevelProvider::BatteryState& new_battery_state,
    base::TimeDelta interval_duration);

// Returns the discharge rate in one hundredth of a percent of full capacity per
// minute.
int64_t CalculateDischargeRateRelative(
    const base::BatteryLevelProvider::BatteryState& previous_battery_state,
    const base::BatteryLevelProvider::BatteryState& new_battery_state,
    base::TimeDelta interval_duration);

// Computes and returns the battery discharge mode and rate during the interval.
// If the discharge rate isn't valid, the returned rate is nullopt and the
// reason is indicated per BatteryDischargeMode.
BatteryDischarge GetBatteryDischargeDuringInterval(
    const std::optional<base::BatteryLevelProvider::BatteryState>&
        previous_battery_state,
    const std::optional<base::BatteryLevelProvider::BatteryState>&
        new_battery_state,
    base::TimeDelta interval_duration);

// Report battery metrics to histograms with |scenario_suffixes|.
void ReportBatteryHistograms(base::TimeDelta interval_duration,
                             BatteryDischarge battery_discharge,
                             bool is_initial_interval,
                             const std::vector<const char*>& scenario_suffixes);

#if BUILDFLAG(IS_WIN)
// Report battery metrics captured over a >10 minutes interval.
void ReportBatteryHistogramsTenMinutesInterval(
    base::TimeDelta interval_duration,
    BatteryDischarge battery_discharge);
#endif  // BUILDFLAG(IS_WIN)

#endif  // CHROME_BROWSER_METRICS_POWER_POWER_METRICS_H_