blob: 66a261fc52c07d704f59b8f9501285d3f932625f [file] [log] [blame]
Francois Doray9b7f7b52021-11-29 21:22:101// Copyright 2021 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// The System Management Controller (SMC) is a hardware component that controls
6// the power functions of Intel-based Macs. This file defines a class to read
7// known SMC keys.
8
9#ifndef COMPONENTS_POWER_METRICS_SMC_MAC_H_
10#define COMPONENTS_POWER_METRICS_SMC_MAC_H_
11
12#import <Foundation/Foundation.h>
13
14#include <memory>
15
16#include "base/mac/scoped_ioobject.h"
17#include "components/power_metrics/smc_internal_types_mac.h"
18#include "third_party/abseil-cpp/absl/types/optional.h"
19
20namespace power_metrics {
21
22class SMCReader {
23 public:
24 // Creates an SMC Reader. Returns nullptr in case of failure.
25 static std::unique_ptr<SMCReader> Create();
26
27 virtual ~SMCReader();
28
29 // Returns the power consumption of various hardware components in watts.
30 // Virtual for testing.
31 virtual absl::optional<double> ReadTotalPowerW();
32 virtual absl::optional<double> ReadCPUPackageCPUPowerW();
33 virtual absl::optional<double> ReadCPUPackageGPUPowerW();
34 virtual absl::optional<double> ReadGPU0PowerW();
35 virtual absl::optional<double> ReadGPU1PowerW();
36
37 protected:
38 explicit SMCReader(base::mac::ScopedIOObject<io_object_t> connect);
39
40 private:
41 class SMCKey {
42 public:
43 SMCKey(base::mac::ScopedIOObject<io_object_t> connect,
44 SMCKeyIdentifier key_identifier);
45 ~SMCKey();
46
47 bool Exists() const;
48 absl::optional<double> Read();
49
50 private:
51 bool CallSMCFunction(uint8_t function, SMCParamStruct* out);
52
53 base::mac::ScopedIOObject<io_object_t> connect_;
54 const SMCKeyIdentifier key_identifier_;
55 SMCKeyInfoData key_info_;
56 };
57
58 SMCKey total_power_key_;
59 SMCKey cpu_package_cpu_power_key_;
60 SMCKey cpu_package_gpu_power_key_;
61 SMCKey gpu0_power_key_;
62 SMCKey gpu1_power_key_;
63};
64
65} // namespace power_metrics
66
67#endif // COMPONENTS_POWER_METRICS_SMC_MAC_H_