1 // Copyright 2021 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.
5 #include "components/power_metrics/m1_sensors_mac.h"
7 #include <CoreFoundation/CoreFoundation.h>
8 #import <Foundation/Foundation.h>
9 #import <IOKit/hid/IOHIDDeviceKeys.h>
10 #import <IOKit/hidsystem/IOHIDServiceClient.h>
14 #include "base/apple/bridging.h"
15 #include "base/apple/foundation_util.h"
16 #include "base/memory/ptr_util.h"
17 #include "components/power_metrics/m1_sensors_internal_types_mac.h"
21 extern IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef);
22 extern int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client,
23 CFDictionaryRef match);
24 extern CFTypeRef IOHIDServiceClientCopyEvent(IOHIDServiceClientRef,
28 extern double IOHIDEventGetFloatValue(CFTypeRef, int32_t);
31 namespace power_metrics {
35 absl::optional<double> GetEventFloatValue(IOHIDServiceClientRef service,
37 base::apple::ScopedCFTypeRef<CFTypeRef> event(
38 IOHIDServiceClientCopyEvent(service, event_type, 0, 0));
41 return IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(event_type));
46 M1SensorsReader::TemperaturesCelsius::TemperaturesCelsius() = default;
47 M1SensorsReader::TemperaturesCelsius::TemperaturesCelsius(
48 const TemperaturesCelsius&) noexcept = default;
49 M1SensorsReader::TemperaturesCelsius::~TemperaturesCelsius() = default;
51 M1SensorsReader::~M1SensorsReader() = default;
54 std::unique_ptr<M1SensorsReader> M1SensorsReader::Create() {
55 base::apple::ScopedCFTypeRef<IOHIDEventSystemClientRef> system(
56 IOHIDEventSystemClientCreate(kCFAllocatorDefault));
61 NSDictionary* filter = @{
62 @kIOHIDPrimaryUsagePageKey : @(kHIDPage_AppleVendor),
63 @kIOHIDPrimaryUsageKey : @(kHIDUsage_AppleVendor_TemperatureSensor),
65 IOHIDEventSystemClientSetMatching(system, base::apple::NSToCFPtrCast(filter));
67 return base::WrapUnique(new M1SensorsReader(std::move(system)));
70 M1SensorsReader::TemperaturesCelsius M1SensorsReader::ReadTemperatures() {
71 base::apple::ScopedCFTypeRef<CFArrayRef> services(
72 IOHIDEventSystemClientCopyServices(system_.get()));
74 // There are multiple temperature sensors on P-Cores and E-Cores. Count and
75 // sum values to compute average later.
76 int num_p_core_temp = 0;
77 int num_e_core_temp = 0;
78 double sum_p_core_temp = 0;
79 double sum_e_core_temp = 0;
81 for (CFIndex i = 0; i < CFArrayGetCount(services); ++i) {
82 IOHIDServiceClientRef service =
83 (IOHIDServiceClientRef)CFArrayGetValueAtIndex(services, i);
85 base::apple::ScopedCFTypeRef<CFStringRef> product(
86 base::apple::CFCast<CFStringRef>(
87 IOHIDServiceClientCopyProperty(service, CFSTR(kIOHIDProductKey))));
92 if (CFStringHasPrefix(product, CFSTR("pACC MTR Temp Sensor"))) {
93 absl::optional<double> temp =
94 GetEventFloatValue(service, kIOHIDEventTypeTemperature);
95 if (temp.has_value()) {
97 sum_p_core_temp += temp.value();
101 if (CFStringHasPrefix(product, CFSTR("eACC MTR Temp Sensor"))) {
102 absl::optional<double> temp =
103 GetEventFloatValue(service, kIOHIDEventTypeTemperature);
104 if (temp.has_value()) {
105 num_e_core_temp += 1;
106 sum_e_core_temp += temp.value();
111 TemperaturesCelsius temperatures;
112 if (num_p_core_temp > 0)
113 temperatures.p_cores = sum_p_core_temp / num_p_core_temp;
114 if (num_e_core_temp > 0)
115 temperatures.e_cores = sum_e_core_temp / num_e_core_temp;
120 M1SensorsReader::M1SensorsReader(
121 base::apple::ScopedCFTypeRef<IOHIDEventSystemClientRef> system)
122 : system_(std::move(system)) {}
124 } // namespace power_metrics