blob: 6f3ef09dd8859810262d467685f7d5b531a1f125 [file] [log] [blame]
Shiyi Zou5b493b3e2023-04-19 13:54:441// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_POWER_METRICS_ENERGY_METRICS_PROVIDER_LINUX_H_
6#define COMPONENTS_POWER_METRICS_ENERGY_METRICS_PROVIDER_LINUX_H_
7
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "base/files/scoped_file.h"
13#include "components/power_metrics/energy_metrics_provider.h"
14
15namespace power_metrics {
16
17// EnergyMetricsProviderLinux can only work on platforms with Intel Processor
18// with RAPL interface. It also requires at least Linux 3.14 and
19// /proc/sys/kernel/perf_event_paranoid < 1, which grants permission to read
20// perf event.
21class EnergyMetricsProviderLinux : public EnergyMetricsProvider {
22 public:
23 // A PowerEvent corresponds to a metric, which includes the metric type, the
24 // scale from ticks to joules and the file descriptor for reading perf data.
25 struct PowerEvent {
26 std::string metric_type;
27 double scale;
28 base::ScopedFD fd;
29
30 PowerEvent(std::string metric_type, double scale, base::ScopedFD fd);
31 ~PowerEvent();
32
33 PowerEvent(PowerEvent&& other);
34 PowerEvent& operator=(PowerEvent&& other);
35 };
36
37 // Factory method for production instances.
38 static std::unique_ptr<EnergyMetricsProviderLinux> Create();
39
40 EnergyMetricsProviderLinux(const EnergyMetricsProviderLinux&) = delete;
41 EnergyMetricsProviderLinux& operator=(const EnergyMetricsProviderLinux&) =
42 delete;
43
44 ~EnergyMetricsProviderLinux() override;
45
46 // EnergyMetricsProvider implementation.
Arthur Sonzogni40594412024-02-26 15:49:4347 std::optional<EnergyMetrics> CaptureMetrics() override;
Shiyi Zou5b493b3e2023-04-19 13:54:4448
49 private:
50 EnergyMetricsProviderLinux();
51
52 bool Initialize();
53
54 // Used to derive energy consumption data via perf event.
55 std::vector<PowerEvent> events_;
56 bool is_initialized_ = false;
57};
58
59} // namespace power_metrics
60
61#endif // COMPONENTS_POWER_METRICS_ENERGY_METRICS_PROVIDER_LINUX_H_