Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91d50dcbae | ||
|
|
36c7ea2319 | ||
|
|
fda8bf9e49 | ||
|
|
feceb2412d | ||
|
|
f9093b1d99 | ||
|
|
a14f0b925e | ||
|
|
b47ffd72ca | ||
|
|
c2fa0c05ed | ||
|
|
67bfa9f681 | ||
|
|
4973a67ca9 |
@@ -9,18 +9,24 @@
|
||||
/* Begin PBXBuildFile section */
|
||||
160334CC765E091A7CBE21AA /* MMCPULoadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE92B1A8D7FDBB6499448E1 /* MMCPULoadTests.swift */; };
|
||||
1879A3DCF6E305D7F2CD472E /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */; };
|
||||
1B15F48F6B19E9EA4556ECD5 /* MMStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78325870C7CD8312AECA2236 /* MMStorageTests.swift */; };
|
||||
1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */; };
|
||||
3C0AC14F803C813287D8A663 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 007F48A5A599AB4E8D216D16 /* IOKit.framework */; };
|
||||
4419FE7554489B23976E2970 /* MMCPULoadProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */; };
|
||||
6201FD599FED061F6C736E8B /* MMMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 099DCF7EF03A58ADA40A94A1 /* MMMemoryTests.swift */; };
|
||||
655366D7066478A02EDA91BB /* MMFanTelemetryProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = C9ED8F75E082D4F8AC2FF2F5 /* MMFanTelemetryProvider.m */; };
|
||||
87818E46CEA5145A8215D0E3 /* MMCPUThermalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F2BD3777A674DC4EB442890 /* MMCPUThermalTests.swift */; };
|
||||
87A9E18362486D0D385BBC4A /* MMCPUThermalProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 62C377EFD025D6A6B5563E77 /* MMCPUThermalProvider.m */; };
|
||||
88387CDAE2AC1CC74EF89B57 /* MacMonitorApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADEA6BD38BA8D887B1DB64A9 /* MacMonitorApp.swift */; };
|
||||
9BCE74C5454F81EF80492E75 /* MacMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */; };
|
||||
A7609CE56D49EC3419987468 /* SystemTelemetryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B4E94FB2886C60D5548A2A /* SystemTelemetryStore.swift */; };
|
||||
A9FCAEC0BF27A419DB148C4A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E142AE61F9B87757997870DD /* ContentView.swift */; };
|
||||
B9C2D6574E808B517BA34C83 /* MMMemoryTelemetryProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 991AEEAF262F3C6BE930BC77 /* MMMemoryTelemetryProvider.m */; };
|
||||
CD91FC663B19DA1FEA3D49D7 /* MMAppleSMCClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 5555436CD3CB8A73F5D6504F /* MMAppleSMCClient.m */; };
|
||||
E6F2F695B8D3E7045967C082 /* MMFanTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 336CEE35A745F3C7E9F70F43 /* MMFanTests.swift */; };
|
||||
EAFAF5A10502D4ACC64F4720 /* MMTelemetryCoordinator.m in Sources */ = {isa = PBXBuildFile; fileRef = F9F9AD1268F906761032AF4D /* MMTelemetryCoordinator.m */; };
|
||||
EC9FA3E4A119277B89E73F8D /* MMSMCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 940D608A1AF80F75584E744E /* MMSMCTests.swift */; };
|
||||
F2F7A141F2452EC1B4FB45D8 /* MMStorageTelemetryProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 6DCBCEA450103E2404761B78 /* MMStorageTelemetryProvider.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
@@ -37,23 +43,32 @@
|
||||
007F48A5A599AB4E8D216D16 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
|
||||
0130BD1E18972DF9E2955334 /* MMMemoryTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMMemoryTelemetryProvider.h; sourceTree = "<group>"; };
|
||||
099DCF7EF03A58ADA40A94A1 /* MMMemoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMMemoryTests.swift; sourceTree = "<group>"; };
|
||||
0F2BD3777A674DC4EB442890 /* MMCPUThermalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMCPUThermalTests.swift; sourceTree = "<group>"; };
|
||||
1AD9EEE571CD904BBCD1C96B /* MMSMCParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMSMCParser.h; sourceTree = "<group>"; };
|
||||
2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
|
||||
2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorTests.swift; sourceTree = "<group>"; };
|
||||
32340166F6100A08105308D3 /* MMTelemetryCoordinator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryCoordinator.h; sourceTree = "<group>"; };
|
||||
336CEE35A745F3C7E9F70F43 /* MMFanTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMFanTests.swift; sourceTree = "<group>"; };
|
||||
375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMSMCParser.m; sourceTree = "<group>"; };
|
||||
3CFC5B905E428A6B854E0AB4 /* MMFanTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMFanTelemetryProvider.h; sourceTree = "<group>"; };
|
||||
46DE3AE12DAE26C4FE58034C /* MMStorageTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMStorageTelemetryProvider.h; sourceTree = "<group>"; };
|
||||
4CE92B1A8D7FDBB6499448E1 /* MMCPULoadTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMCPULoadTests.swift; sourceTree = "<group>"; };
|
||||
5555436CD3CB8A73F5D6504F /* MMAppleSMCClient.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMAppleSMCClient.m; sourceTree = "<group>"; };
|
||||
5635101F862DE680856BDE6E /* MMTelemetryDomain.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryDomain.h; sourceTree = "<group>"; };
|
||||
56E285A0A1746AE659981F22 /* MacMonitorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MacMonitorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
57845310A3D4EC67E48A3B11 /* MMCPULoadProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMCPULoadProvider.h; sourceTree = "<group>"; };
|
||||
62C377EFD025D6A6B5563E77 /* MMCPUThermalProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMCPUThermalProvider.m; sourceTree = "<group>"; };
|
||||
6D0E5CAF25DFA9A1D2A698A0 /* MMTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryProvider.h; sourceTree = "<group>"; };
|
||||
6DCBCEA450103E2404761B78 /* MMStorageTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMStorageTelemetryProvider.m; sourceTree = "<group>"; };
|
||||
726005CB5A6C86E7D80D3CA0 /* MMAppleSMCClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMAppleSMCClient.h; sourceTree = "<group>"; };
|
||||
78325870C7CD8312AECA2236 /* MMStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMStorageTests.swift; sourceTree = "<group>"; };
|
||||
8FBB436F3D9472194D8C6EDD /* MMSMCDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMSMCDefines.h; sourceTree = "<group>"; };
|
||||
940D608A1AF80F75584E744E /* MMSMCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMSMCTests.swift; sourceTree = "<group>"; };
|
||||
991AEEAF262F3C6BE930BC77 /* MMMemoryTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMMemoryTelemetryProvider.m; sourceTree = "<group>"; };
|
||||
ADEA6BD38BA8D887B1DB64A9 /* MacMonitorApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorApp.swift; sourceTree = "<group>"; };
|
||||
C9ED8F75E082D4F8AC2FF2F5 /* MMFanTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMFanTelemetryProvider.m; sourceTree = "<group>"; };
|
||||
D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMCPULoadProvider.m; sourceTree = "<group>"; };
|
||||
D73CCC7B5E6BF1825EA68E2B /* MMCPUThermalProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMCPUThermalProvider.h; sourceTree = "<group>"; };
|
||||
E142AE61F9B87757997870DD /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||
E4B4E94FB2886C60D5548A2A /* SystemTelemetryStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemTelemetryStore.swift; sourceTree = "<group>"; };
|
||||
F5EC59DCDA5FABDA409CDBCB /* MacMonitor.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MacMonitor.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -136,7 +151,10 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
78383CED205BCA772701AE48 /* CPU */,
|
||||
AD2995435E44FA3E1790A4F3 /* Fan */,
|
||||
EE2F5EFC9DC33CEDA79A131F /* Memory */,
|
||||
B6EB3F6545276C40212C2992 /* Storage */,
|
||||
DBDC37231C01F0A2D4A7FC73 /* Thermal */,
|
||||
);
|
||||
path = Telemetry;
|
||||
sourceTree = "<group>";
|
||||
@@ -149,6 +167,24 @@
|
||||
path = Hardware;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
AD2995435E44FA3E1790A4F3 /* Fan */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
3CFC5B905E428A6B854E0AB4 /* MMFanTelemetryProvider.h */,
|
||||
C9ED8F75E082D4F8AC2FF2F5 /* MMFanTelemetryProvider.m */,
|
||||
);
|
||||
path = Fan;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
B6EB3F6545276C40212C2992 /* Storage */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
46DE3AE12DAE26C4FE58034C /* MMStorageTelemetryProvider.h */,
|
||||
6DCBCEA450103E2404761B78 /* MMStorageTelemetryProvider.m */,
|
||||
);
|
||||
path = Storage;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C80C3EEE8F912F4B214102E3 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -163,12 +199,24 @@
|
||||
children = (
|
||||
2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */,
|
||||
4CE92B1A8D7FDBB6499448E1 /* MMCPULoadTests.swift */,
|
||||
0F2BD3777A674DC4EB442890 /* MMCPUThermalTests.swift */,
|
||||
336CEE35A745F3C7E9F70F43 /* MMFanTests.swift */,
|
||||
099DCF7EF03A58ADA40A94A1 /* MMMemoryTests.swift */,
|
||||
940D608A1AF80F75584E744E /* MMSMCTests.swift */,
|
||||
78325870C7CD8312AECA2236 /* MMStorageTests.swift */,
|
||||
);
|
||||
path = Tests;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
DBDC37231C01F0A2D4A7FC73 /* Thermal */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D73CCC7B5E6BF1825EA68E2B /* MMCPUThermalProvider.h */,
|
||||
62C377EFD025D6A6B5563E77 /* MMCPUThermalProvider.m */,
|
||||
);
|
||||
path = Thermal;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E012FBB26EF9E35A2FAF87E2 /* AppleSMC */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -275,8 +323,11 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
160334CC765E091A7CBE21AA /* MMCPULoadTests.swift in Sources */,
|
||||
87818E46CEA5145A8215D0E3 /* MMCPUThermalTests.swift in Sources */,
|
||||
E6F2F695B8D3E7045967C082 /* MMFanTests.swift in Sources */,
|
||||
6201FD599FED061F6C736E8B /* MMMemoryTests.swift in Sources */,
|
||||
EC9FA3E4A119277B89E73F8D /* MMSMCTests.swift in Sources */,
|
||||
1B15F48F6B19E9EA4556ECD5 /* MMStorageTests.swift in Sources */,
|
||||
9BCE74C5454F81EF80492E75 /* MacMonitorTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -288,8 +339,11 @@
|
||||
A9FCAEC0BF27A419DB148C4A /* ContentView.swift in Sources */,
|
||||
CD91FC663B19DA1FEA3D49D7 /* MMAppleSMCClient.m in Sources */,
|
||||
4419FE7554489B23976E2970 /* MMCPULoadProvider.m in Sources */,
|
||||
87A9E18362486D0D385BBC4A /* MMCPUThermalProvider.m in Sources */,
|
||||
655366D7066478A02EDA91BB /* MMFanTelemetryProvider.m in Sources */,
|
||||
B9C2D6574E808B517BA34C83 /* MMMemoryTelemetryProvider.m in Sources */,
|
||||
1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */,
|
||||
F2F7A141F2452EC1B4FB45D8 /* MMStorageTelemetryProvider.m in Sources */,
|
||||
EAFAF5A10502D4ACC64F4720 /* MMTelemetryCoordinator.m in Sources */,
|
||||
88387CDAE2AC1CC74EF89B57 /* MacMonitorApp.swift in Sources */,
|
||||
A7609CE56D49EC3419987468 /* SystemTelemetryStore.swift in Sources */,
|
||||
|
||||
@@ -14,5 +14,10 @@
|
||||
// Telemetry Providers
|
||||
#import "MMCPULoadProvider.h"
|
||||
#import "MMMemoryTelemetryProvider.h"
|
||||
#import "MMStorageTelemetryProvider.h"
|
||||
#import "MMCPUThermalProvider.h"
|
||||
#import "MMFanTelemetryProvider.h"
|
||||
#import "MMComponentThermalProvider.h"
|
||||
#import "MMPowerTelemetryProvider.h"
|
||||
|
||||
#endif /* MacMonitor_Bridging_Header_h */
|
||||
|
||||
@@ -40,6 +40,13 @@
|
||||
(uint32_t)bytes[3];
|
||||
}
|
||||
|
||||
static int hexDigitValue(unichar c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
+ (nullable NSNumber *)decodeValueWithDataType:(NSString *)dataType bytes:(const uint8_t *)bytes size:(NSUInteger)size {
|
||||
if (!bytes || size == 0) return nil;
|
||||
|
||||
@@ -49,7 +56,7 @@
|
||||
if (size >= 2) return @([self decodeSP78:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"fpe2"]) {
|
||||
if (size >= 2) return @([self decodeFPE2:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"flt"]) {
|
||||
} else if ([trimmedType isEqualToString:@"flt"] || [trimmedType isEqualToString:@"ioft"]) {
|
||||
if (size >= 4) return @([self decodeFLT:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"ui8"]) {
|
||||
if (size >= 1) return @([self decodeUI8:bytes]);
|
||||
@@ -59,6 +66,18 @@
|
||||
if (size >= 4) return @([self decodeUI32:bytes]);
|
||||
} else if ([trimmedType isEqualToString:@"flag"]) {
|
||||
if (size >= 1) return @(bytes[0] != 0);
|
||||
} else if (trimmedType.length == 4 && [trimmedType hasPrefix:@"sp"] && size >= 2) {
|
||||
int fracBits = hexDigitValue([trimmedType characterAtIndex:3]);
|
||||
if (fracBits >= 0 && fracBits <= 15) {
|
||||
int16_t raw = (int16_t)(((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1]);
|
||||
return @((float)raw / (float)(1 << fracBits));
|
||||
}
|
||||
} else if (trimmedType.length == 4 && [trimmedType hasPrefix:@"fp"] && size >= 2) {
|
||||
int fracBits = hexDigitValue([trimmedType characterAtIndex:3]);
|
||||
if (fracBits >= 0 && fracBits <= 15) {
|
||||
uint16_t raw = ((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1];
|
||||
return @((float)raw / (float)(1 << fracBits));
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef MMFanTelemetryProvider_h
|
||||
#define MMFanTelemetryProvider_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MMTelemetryProvider.h"
|
||||
#import "MMAppleSMCClient.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMFanTelemetryProvider
|
||||
* Samples multi-fan telemetry from AppleSMC: current RPM, min RPM, max RPM, target RPM,
|
||||
* and fan utilization percentages.
|
||||
*/
|
||||
@interface MMFanTelemetryProvider : NSObject <MMTelemetryProvider>
|
||||
|
||||
@property (nonatomic, strong, readonly) MMAppleSMCClient *smcClient;
|
||||
@property (nonatomic, readonly) NSInteger fanCount;
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client;
|
||||
|
||||
/**
|
||||
* Static calculator method for deterministic unit testing.
|
||||
*/
|
||||
+ (NSDictionary<NSString *, id> *)calculateFanMetricsFromFanList:(NSArray<NSDictionary<NSString *, NSNumber *> *> *)fanList;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMFanTelemetryProvider_h */
|
||||
@@ -0,0 +1,147 @@
|
||||
#import "MMFanTelemetryProvider.h"
|
||||
|
||||
@interface MMFanTelemetryProvider () {
|
||||
MMAppleSMCClient *_smcClient;
|
||||
NSInteger _probedFanCount;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MMFanTelemetryProvider
|
||||
|
||||
- (instancetype)init {
|
||||
return [self initWithSMCClient:[MMAppleSMCClient sharedClient]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_smcClient = client;
|
||||
_probedFanCount = -1;
|
||||
[self probeFans];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)probeFans {
|
||||
if (!_smcClient.isAvailable) return;
|
||||
|
||||
NSNumber *num = [_smcClient readNumericValueForKey:@"FNum" error:nil];
|
||||
if (num) {
|
||||
_probedFanCount = [num integerValue];
|
||||
} else {
|
||||
// Fallback probe F0Ac
|
||||
NSNumber *f0 = [_smcClient readNumericValueForKey:@"F0Ac" error:nil];
|
||||
if (f0 && [f0 doubleValue] >= 0.0) {
|
||||
_probedFanCount = 1;
|
||||
NSNumber *f1 = [_smcClient readNumericValueForKey:@"F1Ac" error:nil];
|
||||
if (f1 && [f1 doubleValue] >= 0.0) {
|
||||
_probedFanCount = 2;
|
||||
}
|
||||
} else {
|
||||
_probedFanCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (MMTelemetryDomain)domain {
|
||||
return MMTelemetryDomainFan;
|
||||
}
|
||||
|
||||
- (NSString *)providerIdentifier {
|
||||
return @"com.i3omb.macmonitor.telemetry.fan";
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return _smcClient.isAvailable;
|
||||
}
|
||||
|
||||
- (MMAppleSMCClient *)smcClient {
|
||||
return _smcClient;
|
||||
}
|
||||
|
||||
- (NSInteger)fanCount {
|
||||
return (_probedFanCount >= 0) ? _probedFanCount : 0;
|
||||
}
|
||||
|
||||
+ (NSDictionary<NSString *, id> *)calculateFanMetricsFromFanList:(NSArray<NSDictionary<NSString *, NSNumber *> *> *)fanList {
|
||||
if (!fanList || fanList.count == 0) {
|
||||
return @{
|
||||
@"fanCount": @(0),
|
||||
@"fans": @[],
|
||||
@"averageRPM": @(0.0),
|
||||
@"peakRPM": @(0.0)
|
||||
};
|
||||
}
|
||||
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *resultList = [NSMutableArray arrayWithCapacity:fanList.count];
|
||||
double sumRPM = 0.0;
|
||||
double peakRPM = 0.0;
|
||||
|
||||
for (NSUInteger i = 0; i < fanList.count; i++) {
|
||||
NSDictionary<NSString *, NSNumber *> *raw = fanList[i];
|
||||
double current = [raw[@"currentRPM"] doubleValue];
|
||||
double min = [raw[@"minRPM"] doubleValue];
|
||||
double max = [raw[@"maxRPM"] doubleValue];
|
||||
double target = [raw[@"targetRPM"] doubleValue];
|
||||
|
||||
double util = 0.0;
|
||||
if (max > min && current >= min) {
|
||||
util = ((current - min) / (max - min)) * 100.0;
|
||||
if (util < 0.0) util = 0.0;
|
||||
if (util > 100.0) util = 100.0;
|
||||
}
|
||||
|
||||
if (current > peakRPM) peakRPM = current;
|
||||
sumRPM += current;
|
||||
|
||||
[resultList addObject:@{
|
||||
@"fanIndex": @(i),
|
||||
@"currentRPM": @(current),
|
||||
@"minRPM": @(min),
|
||||
@"maxRPM": @(max),
|
||||
@"targetRPM": @(target),
|
||||
@"utilizationPercent": @(util)
|
||||
}];
|
||||
}
|
||||
|
||||
double avgRPM = fanList.count > 0 ? (sumRPM / fanList.count) : 0.0;
|
||||
|
||||
return @{
|
||||
@"fanCount": @(fanList.count),
|
||||
@"fans": [resultList copy],
|
||||
@"averageRPM": @(avgRPM),
|
||||
@"peakRPM": @(peakRPM)
|
||||
};
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
||||
if (_probedFanCount < 0 && _smcClient.isAvailable) {
|
||||
[self probeFans];
|
||||
}
|
||||
|
||||
NSInteger count = [self fanCount];
|
||||
NSMutableArray<NSDictionary<NSString *, NSNumber *> *> *rawList = [NSMutableArray arrayWithCapacity:count];
|
||||
|
||||
for (NSInteger i = 0; i < count; i++) {
|
||||
NSString *acKey = [NSString stringWithFormat:@"F%ldAc", (long)i];
|
||||
NSString *mnKey = [NSString stringWithFormat:@"F%ldMn", (long)i];
|
||||
NSString *mxKey = [NSString stringWithFormat:@"F%ldMx", (long)i];
|
||||
NSString *tgKey = [NSString stringWithFormat:@"F%ldTg", (long)i];
|
||||
|
||||
NSNumber *ac = [_smcClient readNumericValueForKey:acKey error:nil] ?: @(0.0);
|
||||
NSNumber *mn = [_smcClient readNumericValueForKey:mnKey error:nil] ?: @(0.0);
|
||||
NSNumber *mx = [_smcClient readNumericValueForKey:mxKey error:nil] ?: @(6000.0);
|
||||
NSNumber *tg = [_smcClient readNumericValueForKey:tgKey error:nil] ?: ac;
|
||||
|
||||
[rawList addObject:@{
|
||||
@"currentRPM": ac,
|
||||
@"minRPM": mn,
|
||||
@"maxRPM": mx,
|
||||
@"targetRPM": tg
|
||||
}];
|
||||
}
|
||||
|
||||
return [MMFanTelemetryProvider calculateFanMetricsFromFanList:rawList];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef MMPowerTelemetryProvider_h
|
||||
#define MMPowerTelemetryProvider_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MMTelemetryProvider.h"
|
||||
#import "MMAppleSMCClient.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMPowerTelemetryProvider
|
||||
* Samples system power draw, CPU/GPU package wattage, rail voltages, and currents
|
||||
* via AppleSMC and AppleSmartBattery IOKit interfaces.
|
||||
*/
|
||||
@interface MMPowerTelemetryProvider : NSObject <MMTelemetryProvider>
|
||||
|
||||
@property (nonatomic, strong, readonly) MMAppleSMCClient *smcClient;
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *discoveredPowerKeys;
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *discoveredVoltageKeys;
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *discoveredCurrentKeys;
|
||||
|
||||
- (instancetype)init;
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client;
|
||||
|
||||
/**
|
||||
* Friendly name for power, voltage, and current SMC keys.
|
||||
*/
|
||||
+ (NSString *)humanReadableNameForKey:(NSString *)key;
|
||||
|
||||
/**
|
||||
* Unit of measurement for SMC key ("W", "V", "A").
|
||||
*/
|
||||
+ (NSString *)unitForKey:(NSString *)key;
|
||||
|
||||
/**
|
||||
* Pure calculator method for deterministic unit testing.
|
||||
*/
|
||||
+ (NSDictionary<NSString *, id> *)calculatePowerMetricsWithReadings:(NSDictionary<NSString *, NSNumber *> *)readings
|
||||
batteryInfo:(nullable NSDictionary<NSString *, id> *)batteryInfo;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMPowerTelemetryProvider_h */
|
||||
@@ -0,0 +1,314 @@
|
||||
#import "MMPowerTelemetryProvider.h"
|
||||
#import <IOKit/IOKitLib.h>
|
||||
#import <IOKit/ps/IOPowerSources.h>
|
||||
#import <IOKit/ps/IOPSKeys.h>
|
||||
|
||||
@interface MMPowerTelemetryProvider () {
|
||||
MMAppleSMCClient *_smcClient;
|
||||
NSMutableArray<NSString *> *_discoveredPowerKeys;
|
||||
NSMutableArray<NSString *> *_discoveredVoltageKeys;
|
||||
NSMutableArray<NSString *> *_discoveredCurrentKeys;
|
||||
BOOL _probed;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MMPowerTelemetryProvider
|
||||
|
||||
- (instancetype)init {
|
||||
return [self initWithSMCClient:[MMAppleSMCClient sharedClient]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_smcClient = client;
|
||||
_discoveredPowerKeys = [NSMutableArray array];
|
||||
_discoveredVoltageKeys = [NSMutableArray array];
|
||||
_discoveredCurrentKeys = [NSMutableArray array];
|
||||
[self probeSensors];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)probeSensors {
|
||||
if (!_smcClient.isAvailable) return;
|
||||
|
||||
NSArray<NSString *> *powerCandidates = @[
|
||||
@"PSTR", @"PDTR", @"PCPR", @"PCTR", @"PC0C", @"PCPT",
|
||||
@"PG0R", @"PGTR", @"PMSR", @"PM0R", @"PCPC"
|
||||
];
|
||||
|
||||
NSArray<NSString *> *voltageCandidates = @[
|
||||
@"VC0C", @"VD0R", @"VN0C", @"VM0R", @"VP0R"
|
||||
];
|
||||
|
||||
NSArray<NSString *> *currentCandidates = @[
|
||||
@"IC0C", @"ID0R", @"IG0C", @"IM0R"
|
||||
];
|
||||
|
||||
[_discoveredPowerKeys removeAllObjects];
|
||||
for (NSString *key in powerCandidates) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 5000.0) {
|
||||
[_discoveredPowerKeys addObject:key];
|
||||
}
|
||||
}
|
||||
|
||||
[_discoveredVoltageKeys removeAllObjects];
|
||||
for (NSString *key in voltageCandidates) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val && [val doubleValue] > 0.0) {
|
||||
[_discoveredVoltageKeys addObject:key];
|
||||
}
|
||||
}
|
||||
|
||||
[_discoveredCurrentKeys removeAllObjects];
|
||||
for (NSString *key in currentCandidates) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val && [val doubleValue] > 0.0) {
|
||||
[_discoveredCurrentKeys addObject:key];
|
||||
}
|
||||
}
|
||||
|
||||
_probed = YES;
|
||||
}
|
||||
|
||||
- (MMTelemetryDomain)domain {
|
||||
return MMTelemetryDomainPower;
|
||||
}
|
||||
|
||||
- (NSString *)providerIdentifier {
|
||||
return @"com.i3omb.macmonitor.telemetry.power";
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return _smcClient.isAvailable;
|
||||
}
|
||||
|
||||
- (MMAppleSMCClient *)smcClient {
|
||||
return _smcClient;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)discoveredPowerKeys {
|
||||
return [_discoveredPowerKeys copy];
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)discoveredVoltageKeys {
|
||||
return [_discoveredVoltageKeys copy];
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)discoveredCurrentKeys {
|
||||
return [_discoveredCurrentKeys copy];
|
||||
}
|
||||
|
||||
+ (NSString *)unitForKey:(NSString *)key {
|
||||
if ([key hasPrefix:@"P"]) return @"W";
|
||||
if ([key hasPrefix:@"V"]) return @"V";
|
||||
if ([key hasPrefix:@"I"]) return @"A";
|
||||
return @"";
|
||||
}
|
||||
|
||||
+ (NSString *)humanReadableNameForKey:(NSString *)key {
|
||||
static NSDictionary<NSString *, NSString *> *names = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
names = @{
|
||||
@"PSTR": @"Total System Power",
|
||||
@"PDTR": @"DC-In Total Power",
|
||||
@"PCPR": @"CPU Package Power",
|
||||
@"PCTR": @"CPU Total Power",
|
||||
@"PC0C": @"CPU Core Power",
|
||||
@"PCPT": @"CPU Package Total Power",
|
||||
@"PG0R": @"GPU 0 Power",
|
||||
@"PGTR": @"GPU Total Power",
|
||||
@"PMSR": @"Memory Subsystem Power",
|
||||
@"PM0R": @"Memory Rail Power",
|
||||
@"PCPC": @"Power Supply In / AC",
|
||||
@"VC0C": @"CPU Core Voltage",
|
||||
@"VD0R": @"DC-In Rail Voltage",
|
||||
@"VN0C": @"PCH Rail Voltage",
|
||||
@"VM0R": @"Memory Rail Voltage",
|
||||
@"VP0R": @"Power Supply Voltage",
|
||||
@"IC0C": @"CPU Core Current",
|
||||
@"ID0R": @"DC-In Rail Current",
|
||||
@"IG0C": @"GPU Rail Current",
|
||||
@"IM0R": @"Memory Rail Current"
|
||||
};
|
||||
});
|
||||
|
||||
NSString *match = names[key];
|
||||
return match ?: key;
|
||||
}
|
||||
|
||||
+ (NSDictionary<NSString *, id> *)calculatePowerMetricsWithReadings:(NSDictionary<NSString *, NSNumber *> *)readings
|
||||
batteryInfo:(nullable NSDictionary<NSString *, id> *)batteryInfo {
|
||||
// Determine system total power
|
||||
double systemTotal = 0.0;
|
||||
if (readings[@"PSTR"]) {
|
||||
systemTotal = [readings[@"PSTR"] doubleValue];
|
||||
} else if (readings[@"PDTR"]) {
|
||||
systemTotal = [readings[@"PDTR"] doubleValue];
|
||||
} else if (batteryInfo[@"batteryWatts"]) {
|
||||
systemTotal = [batteryInfo[@"batteryWatts"] doubleValue];
|
||||
}
|
||||
|
||||
// CPU wattage
|
||||
double cpuWatts = 0.0;
|
||||
if (readings[@"PCPR"]) {
|
||||
cpuWatts = [readings[@"PCPR"] doubleValue];
|
||||
} else if (readings[@"PCTR"]) {
|
||||
cpuWatts = [readings[@"PCTR"] doubleValue];
|
||||
} else if (readings[@"PC0C"]) {
|
||||
cpuWatts = [readings[@"PC0C"] doubleValue];
|
||||
} else if (readings[@"PCPT"]) {
|
||||
cpuWatts = [readings[@"PCPT"] doubleValue];
|
||||
}
|
||||
|
||||
// Voltage & Current
|
||||
double cpuVoltage = readings[@"VC0C"] ? [readings[@"VC0C"] doubleValue] : 0.0;
|
||||
double cpuCurrent = readings[@"IC0C"] ? [readings[@"IC0C"] doubleValue] : 0.0;
|
||||
|
||||
// Fallback: calculate cpuWatts from V * I if SMC didn't provide direct wattage
|
||||
if (cpuWatts == 0.0 && cpuVoltage > 0.0 && cpuCurrent > 0.0) {
|
||||
cpuWatts = cpuVoltage * cpuCurrent;
|
||||
}
|
||||
|
||||
// GPU wattage
|
||||
double gpuWatts = 0.0;
|
||||
if (readings[@"PG0R"]) {
|
||||
gpuWatts = [readings[@"PG0R"] doubleValue];
|
||||
} else if (readings[@"PGTR"]) {
|
||||
gpuWatts = [readings[@"PGTR"] doubleValue];
|
||||
}
|
||||
|
||||
// Memory wattage
|
||||
double memoryWatts = 0.0;
|
||||
if (readings[@"PMSR"]) {
|
||||
memoryWatts = [readings[@"PMSR"] doubleValue];
|
||||
} else if (readings[@"PM0R"]) {
|
||||
memoryWatts = [readings[@"PM0R"] doubleValue];
|
||||
}
|
||||
|
||||
// Collect all individual sensor entries
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *sensors = [NSMutableArray arrayWithCapacity:readings.count];
|
||||
NSArray<NSString *> *sortedKeys = [readings.allKeys sortedArrayUsingSelector:@selector(compare:)];
|
||||
for (NSString *k in sortedKeys) {
|
||||
double val = [readings[k] doubleValue];
|
||||
[sensors addObject:@{
|
||||
@"key": k,
|
||||
@"name": [self humanReadableNameForKey:k],
|
||||
@"value": @(val),
|
||||
@"unit": [self unitForKey:k]
|
||||
}];
|
||||
}
|
||||
|
||||
NSString *powerSource = batteryInfo[@"powerSource"] ?: @"AC Power";
|
||||
BOOL isCharging = [batteryInfo[@"isCharging"] boolValue];
|
||||
double batteryLevel = [batteryInfo[@"batteryLevel"] doubleValue];
|
||||
|
||||
return @{
|
||||
@"systemTotalWatts": @(systemTotal),
|
||||
@"cpuWatts": @(cpuWatts),
|
||||
@"gpuWatts": @(gpuWatts),
|
||||
@"memoryWatts": @(memoryWatts),
|
||||
@"cpuVoltage": @(cpuVoltage),
|
||||
@"cpuCurrent": @(cpuCurrent),
|
||||
@"powerSource": powerSource,
|
||||
@"isCharging": @(isCharging),
|
||||
@"batteryLevel": @(batteryLevel),
|
||||
@"sensors": sensors
|
||||
};
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleBatteryInfo {
|
||||
CFTypeRef psInfo = IOPSCopyPowerSourcesInfo();
|
||||
if (!psInfo) return nil;
|
||||
|
||||
CFArrayRef psList = IOPSCopyPowerSourcesList(psInfo);
|
||||
if (!psList) {
|
||||
CFRelease(psInfo);
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString *stateStr = @"AC Power";
|
||||
BOOL isCharging = NO;
|
||||
double capacity = 100.0;
|
||||
|
||||
if (CFArrayGetCount(psList) > 0) {
|
||||
CFTypeRef ps = CFArrayGetValueAtIndex(psList, 0);
|
||||
CFDictionaryRef desc = IOPSGetPowerSourceDescription(psInfo, ps);
|
||||
if (desc) {
|
||||
NSDictionary *dict = (__bridge NSDictionary *)desc;
|
||||
stateStr = dict[@"Power Source State"] ?: @"AC Power";
|
||||
isCharging = [dict[@"Is Charging"] boolValue];
|
||||
if (dict[@"Current Capacity"]) {
|
||||
capacity = [dict[@"Current Capacity"] doubleValue];
|
||||
}
|
||||
}
|
||||
}
|
||||
CFRelease(psList);
|
||||
CFRelease(psInfo);
|
||||
|
||||
// Sample AppleSmartBattery for instantaneous voltage and current
|
||||
double batteryWatts = 0.0;
|
||||
double batteryVoltage = 0.0;
|
||||
double batteryAmperage = 0.0;
|
||||
|
||||
io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("AppleSmartBattery"));
|
||||
if (service != IO_OBJECT_NULL) {
|
||||
CFMutableDictionaryRef props = NULL;
|
||||
if (IORegistryEntryCreateCFProperties(service, &props, kCFAllocatorDefault, 0) == KERN_SUCCESS && props) {
|
||||
NSDictionary *dict = (__bridge NSDictionary *)props;
|
||||
double v_mV = [dict[@"Voltage"] doubleValue];
|
||||
double a_mA = dict[@"InstantAmperage"] ? [dict[@"InstantAmperage"] doubleValue] : [dict[@"Amperage"] doubleValue];
|
||||
|
||||
batteryVoltage = v_mV / 1000.0;
|
||||
batteryAmperage = fabs(a_mA) / 1000.0;
|
||||
batteryWatts = batteryVoltage * batteryAmperage;
|
||||
|
||||
CFRelease(props);
|
||||
}
|
||||
IOObjectRelease(service);
|
||||
}
|
||||
|
||||
return @{
|
||||
@"powerSource": stateStr,
|
||||
@"isCharging": @(isCharging),
|
||||
@"batteryLevel": @(capacity),
|
||||
@"batteryVoltage": @(batteryVoltage),
|
||||
@"batteryAmperage": @(batteryAmperage),
|
||||
@"batteryWatts": @(batteryWatts)
|
||||
};
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
||||
if (!_probed && _smcClient.isAvailable) {
|
||||
[self probeSensors];
|
||||
}
|
||||
|
||||
NSMutableDictionary<NSString *, NSNumber *> *readings = [NSMutableDictionary dictionary];
|
||||
|
||||
NSMutableArray<NSString *> *allKeys = [NSMutableArray arrayWithArray:_discoveredPowerKeys];
|
||||
[allKeys addObjectsFromArray:_discoveredVoltageKeys];
|
||||
[allKeys addObjectsFromArray:_discoveredCurrentKeys];
|
||||
|
||||
for (NSString *key in allKeys) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val) {
|
||||
double v = [val doubleValue];
|
||||
// Normalize mV or mA if values appear raw
|
||||
if ([key hasPrefix:@"V"] && v > 100.0) {
|
||||
v /= 1000.0;
|
||||
} else if ([key hasPrefix:@"I"] && v > 500.0) {
|
||||
v /= 1000.0;
|
||||
}
|
||||
readings[key] = @(v);
|
||||
}
|
||||
}
|
||||
|
||||
NSDictionary<NSString *, id> *batteryInfo = [self sampleBatteryInfo];
|
||||
return [MMPowerTelemetryProvider calculatePowerMetricsWithReadings:readings
|
||||
batteryInfo:batteryInfo];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef MMStorageTelemetryProvider_h
|
||||
#define MMStorageTelemetryProvider_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <sys/mount.h>
|
||||
#import "MMTelemetryProvider.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMStorageTelemetryProvider
|
||||
* Samples mounted disk volumes, APFS containers, used/free storage capacity,
|
||||
* and mount filesystem attributes.
|
||||
*/
|
||||
@interface MMStorageTelemetryProvider : NSObject <MMTelemetryProvider>
|
||||
|
||||
/**
|
||||
* Static parser method for converting statfs structures into volume dictionaries.
|
||||
*/
|
||||
+ (NSArray<NSDictionary<NSString *, id> *> *)parseStatFSBuffer:(const struct statfs *)buffer count:(int)count;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMStorageTelemetryProvider_h */
|
||||
@@ -0,0 +1,108 @@
|
||||
#import "MMStorageTelemetryProvider.h"
|
||||
|
||||
@implementation MMStorageTelemetryProvider
|
||||
|
||||
- (MMTelemetryDomain)domain {
|
||||
return MMTelemetryDomainStorage;
|
||||
}
|
||||
|
||||
- (NSString *)providerIdentifier {
|
||||
return @"com.i3omb.macmonitor.telemetry.storage";
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (NSArray<NSDictionary<NSString *, id> *> *)parseStatFSBuffer:(const struct statfs *)buffer count:(int)count {
|
||||
if (!buffer || count <= 0) return @[];
|
||||
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *volumes = [NSMutableArray arrayWithCapacity:count];
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
const struct statfs *mnt = &buffer[i];
|
||||
|
||||
// Only include local mounts
|
||||
if (!(mnt->f_flags & MNT_LOCAL)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
NSString *mountPoint = [NSString stringWithUTF8String:mnt->f_mntonname];
|
||||
NSString *deviceName = [NSString stringWithUTF8String:mnt->f_mntfromname];
|
||||
NSString *fsType = [NSString stringWithUTF8String:mnt->f_fstypename];
|
||||
|
||||
// Exclude virtual/internal devfs, autodefs, and synthetic system roots
|
||||
if ([fsType isEqualToString:@"devfs"] ||
|
||||
[fsType isEqualToString:@"autofs"] ||
|
||||
[mountPoint hasPrefix:@"/System/Volumes/Data/"] ||
|
||||
[deviceName containsString:@"com.apple.os.update"]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint64_t blockSize = (uint64_t)mnt->f_bsize;
|
||||
uint64_t totalBytes = (uint64_t)mnt->f_blocks * blockSize;
|
||||
uint64_t freeBytes = (uint64_t)mnt->f_bfree * blockSize;
|
||||
uint64_t availBytes = (uint64_t)mnt->f_bavail * blockSize;
|
||||
uint64_t usedBytes = (totalBytes >= freeBytes) ? (totalBytes - freeBytes) : 0;
|
||||
|
||||
if (totalBytes == 0) continue;
|
||||
|
||||
double usedPercent = ((double)usedBytes / (double)totalBytes) * 100.0;
|
||||
if (usedPercent < 0.0) usedPercent = 0.0;
|
||||
if (usedPercent > 100.0) usedPercent = 100.0;
|
||||
|
||||
NSString *volumeName = [mountPoint lastPathComponent];
|
||||
if ([mountPoint isEqualToString:@"/"]) {
|
||||
volumeName = @"Macintosh HD";
|
||||
}
|
||||
|
||||
BOOL isReadOnly = (mnt->f_flags & MNT_RDONLY) != 0;
|
||||
|
||||
[volumes addObject:@{
|
||||
@"volumeName": volumeName,
|
||||
@"mountPoint": mountPoint,
|
||||
@"devicePath": deviceName,
|
||||
@"fsType": fsType,
|
||||
@"totalBytes": @(totalBytes),
|
||||
@"usedBytes": @(usedBytes),
|
||||
@"freeBytes": @(freeBytes),
|
||||
@"availableBytes": @(availBytes),
|
||||
@"usedPercent": @(usedPercent),
|
||||
@"isReadOnly": @(isReadOnly)
|
||||
}];
|
||||
}
|
||||
|
||||
return [volumes copy];
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
||||
struct statfs *mntbuf = NULL;
|
||||
int count = getmntinfo(&mntbuf, MNT_WAIT);
|
||||
|
||||
if (count <= 0 || mntbuf == NULL) {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.Storage"
|
||||
code:-1
|
||||
userInfo:@{NSLocalizedDescriptionKey: @"getmntinfo returned no mounted filesystems"}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSArray *volumes = [MMStorageTelemetryProvider parseStatFSBuffer:mntbuf count:count];
|
||||
|
||||
uint64_t totalStorage = 0;
|
||||
uint64_t totalUsed = 0;
|
||||
for (NSDictionary *v in volumes) {
|
||||
totalStorage += [v[@"totalBytes"] unsignedLongLongValue];
|
||||
totalUsed += [v[@"usedBytes"] unsignedLongLongValue];
|
||||
}
|
||||
|
||||
return @{
|
||||
@"volumes": volumes,
|
||||
@"volumeCount": @(volumes.count),
|
||||
@"aggregateTotalBytes": @(totalStorage),
|
||||
@"aggregateUsedBytes": @(totalUsed)
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef MMCPUThermalProvider_h
|
||||
#define MMCPUThermalProvider_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MMTelemetryProvider.h"
|
||||
#import "MMAppleSMCClient.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMCPUThermalProvider
|
||||
* Samples real-time CPU die, proximity, and per-core temperature sensors from AppleSMC
|
||||
* alongside OS thermal pressure states.
|
||||
*/
|
||||
@interface MMCPUThermalProvider : NSObject <MMTelemetryProvider>
|
||||
|
||||
@property (nonatomic, strong, readonly) MMAppleSMCClient *smcClient;
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *discoveredCoreKeys;
|
||||
@property (nonatomic, readonly, nullable) NSString *packageKey;
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client;
|
||||
|
||||
/**
|
||||
* Static calculator method for deterministic unit testing.
|
||||
*/
|
||||
+ (NSDictionary<NSString *, id> *)calculateThermalMetricsWithPackageTemp:(nullable NSNumber *)pkgTemp
|
||||
coreTemps:(NSDictionary<NSString *, NSNumber *> *)coreDict
|
||||
thermalState:(NSProcessInfoThermalState)state;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMCPUThermalProvider_h */
|
||||
@@ -0,0 +1,151 @@
|
||||
#import "MMCPUThermalProvider.h"
|
||||
#import <sys/sysctl.h>
|
||||
|
||||
@interface MMCPUThermalProvider () {
|
||||
MMAppleSMCClient *_smcClient;
|
||||
NSMutableArray<NSString *> *_discoveredCoreKeys;
|
||||
NSString *_packageKey;
|
||||
BOOL _probed;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MMCPUThermalProvider
|
||||
|
||||
- (instancetype)init {
|
||||
return [self initWithSMCClient:[MMAppleSMCClient sharedClient]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_smcClient = client;
|
||||
_discoveredCoreKeys = [NSMutableArray array];
|
||||
[self probeSensors];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)probeSensors {
|
||||
if (!_smcClient.isAvailable) return;
|
||||
|
||||
// Probe package temperature key
|
||||
NSArray<NSString *> *packageCandidates = @[@"TC0P", @"TC0D", @"TC0H", @"TCXC"];
|
||||
for (NSString *candidate in packageCandidates) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:candidate error:nil];
|
||||
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 125.0) {
|
||||
_packageKey = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Probe core keys (TC0C, TC1C, etc.)
|
||||
int coreCount = 1;
|
||||
size_t size = sizeof(coreCount);
|
||||
sysctlbyname("hw.physicalcpu", &coreCount, &size, NULL, 0);
|
||||
if (coreCount <= 0) coreCount = 4;
|
||||
|
||||
for (int i = 0; i < coreCount && i < 32; i++) {
|
||||
NSString *key = [NSString stringWithFormat:@"TC%dC", i];
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 125.0) {
|
||||
[_discoveredCoreKeys addObject:key];
|
||||
}
|
||||
}
|
||||
|
||||
_probed = YES;
|
||||
}
|
||||
|
||||
- (MMTelemetryDomain)domain {
|
||||
return MMTelemetryDomainThermal;
|
||||
}
|
||||
|
||||
- (NSString *)providerIdentifier {
|
||||
return @"com.i3omb.macmonitor.telemetry.thermal.cpu";
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return _smcClient.isAvailable;
|
||||
}
|
||||
|
||||
- (MMAppleSMCClient *)smcClient {
|
||||
return _smcClient;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)discoveredCoreKeys {
|
||||
return [_discoveredCoreKeys copy];
|
||||
}
|
||||
|
||||
- (NSString *)packageKey {
|
||||
return _packageKey;
|
||||
}
|
||||
|
||||
+ (NSDictionary<NSString *, id> *)calculateThermalMetricsWithPackageTemp:(nullable NSNumber *)pkgTemp
|
||||
coreTemps:(NSDictionary<NSString *, NSNumber *> *)coreDict
|
||||
thermalState:(NSProcessInfoThermalState)state {
|
||||
double peak = pkgTemp ? [pkgTemp doubleValue] : 0.0;
|
||||
double sum = 0.0;
|
||||
int count = 0;
|
||||
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *coresList = [NSMutableArray arrayWithCapacity:coreDict.count];
|
||||
NSArray<NSString *> *sortedKeys = [coreDict.allKeys sortedArrayUsingSelector:@selector(compare:)];
|
||||
|
||||
for (NSUInteger idx = 0; idx < sortedKeys.count; idx++) {
|
||||
NSString *k = sortedKeys[idx];
|
||||
double temp = [coreDict[k] doubleValue];
|
||||
if (temp > peak) peak = temp;
|
||||
sum += temp;
|
||||
count++;
|
||||
|
||||
[coresList addObject:@{
|
||||
@"coreIndex": @(idx),
|
||||
@"key": k,
|
||||
@"temperature": @(temp)
|
||||
}];
|
||||
}
|
||||
|
||||
double avg = (count > 0) ? (sum / count) : (pkgTemp ? [pkgTemp doubleValue] : 0.0);
|
||||
|
||||
NSString *stateStr = @"Nominal";
|
||||
switch (state) {
|
||||
case NSProcessInfoThermalStateNominal: stateStr = @"Nominal"; break;
|
||||
case NSProcessInfoThermalStateFair: stateStr = @"Fair"; break;
|
||||
case NSProcessInfoThermalStateSerious: stateStr = @"Serious"; break;
|
||||
case NSProcessInfoThermalStateCritical: stateStr = @"Critical"; break;
|
||||
}
|
||||
|
||||
return @{
|
||||
@"packageTemperature": pkgTemp ?: @(0.0),
|
||||
@"averageCoreTemperature": @(avg),
|
||||
@"peakCoreTemperature": @(peak),
|
||||
@"coreTemperatures": coresList,
|
||||
@"thermalPressureState": stateStr,
|
||||
@"isThrottling": @(state >= NSProcessInfoThermalStateSerious)
|
||||
};
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
||||
if (!_probed && _smcClient.isAvailable) {
|
||||
[self probeSensors];
|
||||
}
|
||||
|
||||
NSNumber *pkgTemp = nil;
|
||||
if (_packageKey) {
|
||||
pkgTemp = [_smcClient readNumericValueForKey:_packageKey error:nil];
|
||||
}
|
||||
|
||||
NSMutableDictionary<NSString *, NSNumber *> *coreTemps = [NSMutableDictionary dictionary];
|
||||
for (NSString *coreKey in _discoveredCoreKeys) {
|
||||
NSNumber *t = [_smcClient readNumericValueForKey:coreKey error:nil];
|
||||
if (t) {
|
||||
coreTemps[coreKey] = t;
|
||||
}
|
||||
}
|
||||
|
||||
NSProcessInfoThermalState thermalState = [NSProcessInfo processInfo].thermalState;
|
||||
|
||||
return [MMCPUThermalProvider calculateThermalMetricsWithPackageTemp:pkgTemp
|
||||
coreTemps:coreTemps
|
||||
thermalState:thermalState];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef MMComponentThermalProvider_h
|
||||
#define MMComponentThermalProvider_h
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MMTelemetryProvider.h"
|
||||
#import "MMAppleSMCClient.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* MMComponentThermalProvider
|
||||
* Samples motherboard, PCH chipset, heatsink, memory, and enclosure ambient temperatures
|
||||
* via AppleSMC.
|
||||
*/
|
||||
@interface MMComponentThermalProvider : NSObject <MMTelemetryProvider>
|
||||
|
||||
@property (nonatomic, strong, readonly) MMAppleSMCClient *smcClient;
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *discoveredKeys;
|
||||
|
||||
- (instancetype)init;
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client;
|
||||
|
||||
/**
|
||||
* Categorize SMC temperature key into component domain (e.g., PCH / Chipset, Heatsink, Memory, etc.)
|
||||
*/
|
||||
+ (NSString *)categoryForKey:(NSString *)key;
|
||||
|
||||
/**
|
||||
* Human-readable friendly name for known component SMC keys.
|
||||
*/
|
||||
+ (NSString *)humanReadableNameForKey:(NSString *)key;
|
||||
|
||||
/**
|
||||
* Static calculator method for deterministic unit testing.
|
||||
*/
|
||||
+ (NSDictionary<NSString *, id> *)calculateComponentMetricsFromDictionary:(NSDictionary<NSString *, NSNumber *> *)readings;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif /* MMComponentThermalProvider_h */
|
||||
@@ -0,0 +1,174 @@
|
||||
#import "MMComponentThermalProvider.h"
|
||||
|
||||
@interface MMComponentThermalProvider () {
|
||||
MMAppleSMCClient *_smcClient;
|
||||
NSMutableArray<NSString *> *_discoveredKeys;
|
||||
BOOL _probed;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MMComponentThermalProvider
|
||||
|
||||
- (instancetype)init {
|
||||
return [self initWithSMCClient:[MMAppleSMCClient sharedClient]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_smcClient = client;
|
||||
_discoveredKeys = [NSMutableArray array];
|
||||
[self probeSensors];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)probeSensors {
|
||||
if (!_smcClient.isAvailable) return;
|
||||
|
||||
NSArray<NSString *> *candidates = @[
|
||||
@"TPCD", @"TP0P", @"TP0D",
|
||||
@"Th0H", @"Th1H", @"Th2H", @"Th3H",
|
||||
@"TM0P", @"TM1P", @"TM2P", @"TM3P", @"TM0S", @"TM1S",
|
||||
@"Tp0P", @"Tp1P", @"Tp2P", @"Tp0C",
|
||||
@"TA0P", @"TA1P", @"TA0S", @"TA1S",
|
||||
@"TTLD", @"TT1D", @"TB0T", @"TB1T"
|
||||
];
|
||||
|
||||
[_discoveredKeys removeAllObjects];
|
||||
for (NSString *key in candidates) {
|
||||
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 125.0) {
|
||||
[_discoveredKeys addObject:key];
|
||||
}
|
||||
}
|
||||
|
||||
_probed = YES;
|
||||
}
|
||||
|
||||
- (MMTelemetryDomain)domain {
|
||||
return MMTelemetryDomainThermal;
|
||||
}
|
||||
|
||||
- (NSString *)providerIdentifier {
|
||||
return @"com.i3omb.macmonitor.telemetry.thermal.components";
|
||||
}
|
||||
|
||||
- (BOOL)isAvailable {
|
||||
return _smcClient.isAvailable;
|
||||
}
|
||||
|
||||
- (MMAppleSMCClient *)smcClient {
|
||||
return _smcClient;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)discoveredKeys {
|
||||
return [_discoveredKeys copy];
|
||||
}
|
||||
|
||||
+ (NSString *)categoryForKey:(NSString *)key {
|
||||
if ([key hasPrefix:@"TP"]) return @"PCH / Chipset";
|
||||
if ([key hasPrefix:@"Th"]) return @"Heatsink";
|
||||
if ([key hasPrefix:@"TM"]) return @"Memory";
|
||||
if ([key hasPrefix:@"Tp"]) return @"Power Supply";
|
||||
if ([key hasPrefix:@"TA"]) return @"Ambient / Enclosure";
|
||||
if ([key hasPrefix:@"TT"] || [key hasPrefix:@"TB"] || [key hasPrefix:@"TI"]) return @"Thunderbolt / IO";
|
||||
return @"Other";
|
||||
}
|
||||
|
||||
+ (NSString *)humanReadableNameForKey:(NSString *)key {
|
||||
static NSDictionary<NSString *, NSString *> *names = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
names = @{
|
||||
@"TPCD": @"Platform Controller Hub (Die)",
|
||||
@"TP0P": @"Platform Controller Hub (Proximity)",
|
||||
@"TP0D": @"Platform Controller Hub (Die Alternate)",
|
||||
@"Th0H": @"Main Heatsink A",
|
||||
@"Th1H": @"Main Heatsink B",
|
||||
@"Th2H": @"Main Heatsink C",
|
||||
@"Th3H": @"Main Heatsink D",
|
||||
@"TM0P": @"Memory Bank 0 Proximity",
|
||||
@"TM1P": @"Memory Bank 1 Proximity",
|
||||
@"TM2P": @"Memory Bank 2 Proximity",
|
||||
@"TM3P": @"Memory Bank 3 Proximity",
|
||||
@"TM0S": @"Memory Module 0",
|
||||
@"TM1S": @"Memory Module 1",
|
||||
@"Tp0P": @"Power Supply Primary",
|
||||
@"Tp1P": @"Power Supply Secondary",
|
||||
@"Tp2P": @"Power Supply Tertiary",
|
||||
@"Tp0C": @"Power Supply Core",
|
||||
@"TA0P": @"Ambient Air Intake",
|
||||
@"TA1P": @"Ambient Air Exhaust",
|
||||
@"TA0S": @"Ambient Air Enclosure",
|
||||
@"TA1S": @"Ambient Air Secondary",
|
||||
@"TTLD": @"Thunderbolt Controller Die",
|
||||
@"TT1D": @"Thunderbolt Controller Secondary",
|
||||
@"TB0T": @"Thunderbolt Port 0",
|
||||
@"TB1T": @"Thunderbolt Port 1"
|
||||
};
|
||||
});
|
||||
|
||||
NSString *match = names[key];
|
||||
return match ?: key;
|
||||
}
|
||||
|
||||
+ (NSDictionary<NSString *, id> *)calculateComponentMetricsFromDictionary:(NSDictionary<NSString *, NSNumber *> *)readings {
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *components = [NSMutableArray arrayWithCapacity:readings.count];
|
||||
NSMutableDictionary<NSString *, NSMutableArray<NSDictionary<NSString *, id> *> *> *categorized = [NSMutableDictionary dictionary];
|
||||
|
||||
NSArray<NSString *> *sortedKeys = [readings.allKeys sortedArrayUsingSelector:@selector(compare:)];
|
||||
double peak = 0.0;
|
||||
double sum = 0.0;
|
||||
|
||||
for (NSString *k in sortedKeys) {
|
||||
double temp = [readings[k] doubleValue];
|
||||
if (temp > peak) peak = temp;
|
||||
sum += temp;
|
||||
|
||||
NSString *cat = [self categoryForKey:k];
|
||||
NSString *name = [self humanReadableNameForKey:k];
|
||||
|
||||
NSDictionary<NSString *, id> *entry = @{
|
||||
@"key": k,
|
||||
@"name": name,
|
||||
@"category": cat,
|
||||
@"temperature": @(temp)
|
||||
};
|
||||
|
||||
[components addObject:entry];
|
||||
|
||||
if (!categorized[cat]) {
|
||||
categorized[cat] = [NSMutableArray array];
|
||||
}
|
||||
[categorized[cat] addObject:entry];
|
||||
}
|
||||
|
||||
double avg = readings.count > 0 ? (sum / (double)readings.count) : 0.0;
|
||||
|
||||
return @{
|
||||
@"components": components,
|
||||
@"categorized": categorized,
|
||||
@"sensorCount": @(readings.count),
|
||||
@"peakTemperature": @(peak),
|
||||
@"averageTemperature": @(avg)
|
||||
};
|
||||
}
|
||||
|
||||
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
||||
if (!_probed && _smcClient.isAvailable) {
|
||||
[self probeSensors];
|
||||
}
|
||||
|
||||
NSMutableDictionary<NSString *, NSNumber *> *readings = [NSMutableDictionary dictionary];
|
||||
for (NSString *key in _discoveredKeys) {
|
||||
NSNumber *temp = [_smcClient readNumericValueForKey:key error:nil];
|
||||
if (temp) {
|
||||
readings[key] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return [MMComponentThermalProvider calculateComponentMetricsFromDictionary:readings];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,54 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMCPUThermalTests: XCTestCase {
|
||||
|
||||
func testThermalMetricsCalculation() {
|
||||
let pkgTemp = NSNumber(value: 55.0)
|
||||
let coreDict: [String: NSNumber] = [
|
||||
"TC0C": NSNumber(value: 52.0),
|
||||
"TC1C": NSNumber(value: 56.0),
|
||||
"TC2C": NSNumber(value: 54.0),
|
||||
"TC3C": NSNumber(value: 58.0)
|
||||
]
|
||||
|
||||
let metrics = MMCPUThermalProvider.calculateThermalMetrics(
|
||||
withPackageTemp: pkgTemp,
|
||||
coreTemps: coreDict,
|
||||
thermalState: .nominal
|
||||
)
|
||||
|
||||
let peak = metrics["peakCoreTemperature"] as? Double ?? 0
|
||||
let avg = metrics["averageCoreTemperature"] as? Double ?? 0
|
||||
let state = metrics["thermalPressureState"] as? String ?? ""
|
||||
let isThrottling = metrics["isThrottling"] as? Bool ?? true
|
||||
|
||||
XCTAssertEqual(peak, 58.0, accuracy: 0.1)
|
||||
XCTAssertEqual(avg, 55.0, accuracy: 0.1)
|
||||
XCTAssertEqual(state, "Nominal")
|
||||
XCTAssertFalse(isThrottling)
|
||||
}
|
||||
|
||||
func testThermalThrottlingDetection() {
|
||||
let metrics = MMCPUThermalProvider.calculateThermalMetrics(
|
||||
withPackageTemp: NSNumber(value: 99.0),
|
||||
coreTemps: [:],
|
||||
thermalState: .serious
|
||||
)
|
||||
|
||||
let isThrottling = metrics["isThrottling"] as? Bool ?? false
|
||||
let state = metrics["thermalPressureState"] as? String ?? ""
|
||||
|
||||
XCTAssertTrue(isThrottling)
|
||||
XCTAssertEqual(state, "Serious")
|
||||
}
|
||||
|
||||
func testLiveCPUThermalProvider() throws {
|
||||
let provider = MMCPUThermalProvider()
|
||||
XCTAssertEqual(provider.domain, .thermal)
|
||||
|
||||
let sample = try provider.sampleTelemetry()
|
||||
XCTAssertNotNil(sample)
|
||||
XCTAssertNotNil(sample["thermalPressureState"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMComponentThermalTests: XCTestCase {
|
||||
|
||||
func testCategoryClassification() {
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TPCD"), "PCH / Chipset")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TP0P"), "PCH / Chipset")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "Th0H"), "Heatsink")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "Th1H"), "Heatsink")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TM0P"), "Memory")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TM1S"), "Memory")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "Tp0P"), "Power Supply")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TA0P"), "Ambient / Enclosure")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TTLD"), "Thunderbolt / IO")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "TB0T"), "Thunderbolt / IO")
|
||||
XCTAssertEqual(MMComponentThermalProvider.category(forKey: "ZZ99"), "Other")
|
||||
}
|
||||
|
||||
func testHumanReadableNames() {
|
||||
XCTAssertEqual(MMComponentThermalProvider.humanReadableName(forKey: "TPCD"), "Platform Controller Hub (Die)")
|
||||
XCTAssertEqual(MMComponentThermalProvider.humanReadableName(forKey: "Th0H"), "Main Heatsink A")
|
||||
XCTAssertEqual(MMComponentThermalProvider.humanReadableName(forKey: "TA0P"), "Ambient Air Intake")
|
||||
XCTAssertEqual(MMComponentThermalProvider.humanReadableName(forKey: "CUSTOM"), "CUSTOM")
|
||||
}
|
||||
|
||||
func testComponentMetricsCalculation() {
|
||||
let readings: [String: NSNumber] = [
|
||||
"TPCD": NSNumber(value: 65.5),
|
||||
"Th0H": NSNumber(value: 48.0),
|
||||
"TM0P": NSNumber(value: 42.0),
|
||||
"TA0P": NSNumber(value: 28.5)
|
||||
]
|
||||
|
||||
let metrics = MMComponentThermalProvider.calculateComponentMetrics(from: readings)
|
||||
|
||||
let peak = metrics["peakTemperature"] as? Double ?? 0
|
||||
let avg = metrics["averageTemperature"] as? Double ?? 0
|
||||
let count = metrics["sensorCount"] as? Int ?? 0
|
||||
let components = metrics["components"] as? [[String: Any]] ?? []
|
||||
let categorized = metrics["categorized"] as? [String: [[String: Any]]] ?? [:]
|
||||
|
||||
XCTAssertEqual(peak, 65.5, accuracy: 0.1)
|
||||
XCTAssertEqual(avg, (65.5 + 48.0 + 42.0 + 28.5) / 4.0, accuracy: 0.1)
|
||||
XCTAssertEqual(count, 4)
|
||||
XCTAssertEqual(components.count, 4)
|
||||
|
||||
let pchList = categorized["PCH / Chipset"]
|
||||
XCTAssertNotNil(pchList)
|
||||
let firstPchName = pchList?.first?["name"] as? String
|
||||
XCTAssertEqual(firstPchName, "Platform Controller Hub (Die)")
|
||||
XCTAssertEqual(categorized["Heatsink"]?.count, 1)
|
||||
XCTAssertEqual(categorized["Memory"]?.count, 1)
|
||||
XCTAssertEqual(categorized["Ambient / Enclosure"]?.count, 1)
|
||||
}
|
||||
|
||||
func testEmptyReadingsCalculation() {
|
||||
let metrics = MMComponentThermalProvider.calculateComponentMetrics(from: [:])
|
||||
|
||||
let peak = metrics["peakTemperature"] as? Double ?? -1
|
||||
let avg = metrics["averageTemperature"] as? Double ?? -1
|
||||
let count = metrics["sensorCount"] as? Int ?? -1
|
||||
|
||||
XCTAssertEqual(peak, 0.0)
|
||||
XCTAssertEqual(avg, 0.0)
|
||||
XCTAssertEqual(count, 0)
|
||||
}
|
||||
|
||||
func testLiveComponentThermalProvider() throws {
|
||||
let provider = MMComponentThermalProvider()
|
||||
XCTAssertEqual(provider.domain, .thermal)
|
||||
XCTAssertEqual(provider.providerIdentifier, "com.i3omb.macmonitor.telemetry.thermal.components")
|
||||
|
||||
let sample = try provider.sampleTelemetry()
|
||||
XCTAssertNotNil(sample)
|
||||
XCTAssertNotNil(sample["sensorCount"])
|
||||
XCTAssertNotNil(sample["components"])
|
||||
XCTAssertNotNil(sample["categorized"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMFanTests: XCTestCase {
|
||||
|
||||
func testFanUtilizationCalculation() {
|
||||
let fanList: [[String: NSNumber]] = [
|
||||
[
|
||||
"currentRPM": NSNumber(value: 2000.0),
|
||||
"minRPM": NSNumber(value: 1200.0),
|
||||
"maxRPM": NSNumber(value: 5000.0),
|
||||
"targetRPM": NSNumber(value: 2000.0)
|
||||
],
|
||||
[
|
||||
"currentRPM": NSNumber(value: 2500.0),
|
||||
"minRPM": NSNumber(value: 1200.0),
|
||||
"maxRPM": NSNumber(value: 5000.0),
|
||||
"targetRPM": NSNumber(value: 2500.0)
|
||||
]
|
||||
]
|
||||
|
||||
let metrics = MMFanTelemetryProvider.calculateFanMetrics(fromFanList: fanList)
|
||||
|
||||
let count = metrics["fanCount"] as? Int ?? 0
|
||||
let avgRPM = metrics["averageRPM"] as? Double ?? 0
|
||||
let peakRPM = metrics["peakRPM"] as? Double ?? 0
|
||||
let fans = metrics["fans"] as? [[String: Any]] ?? []
|
||||
|
||||
XCTAssertEqual(count, 2)
|
||||
XCTAssertEqual(avgRPM, 2250.0, accuracy: 0.1)
|
||||
XCTAssertEqual(peakRPM, 2500.0, accuracy: 0.1)
|
||||
XCTAssertEqual(fans.count, 2)
|
||||
|
||||
let fan0 = fans[0]
|
||||
let util0 = fan0["utilizationPercent"] as? Double ?? 0
|
||||
// (2000 - 1200) / 3800 * 100 = 21.05%
|
||||
XCTAssertEqual(util0, 21.05, accuracy: 0.1)
|
||||
}
|
||||
|
||||
func testLiveFanProvider() throws {
|
||||
let provider = MMFanTelemetryProvider()
|
||||
XCTAssertEqual(provider.domain, .fan)
|
||||
|
||||
let sample = try provider.sampleTelemetry()
|
||||
XCTAssertNotNil(sample)
|
||||
XCTAssertNotNil(sample["fanCount"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMPowerTests: XCTestCase {
|
||||
|
||||
func testUnitsAndNames() {
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.unit(forKey: "PSTR"), "W")
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.unit(forKey: "VC0C"), "V")
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.unit(forKey: "IC0C"), "A")
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.unit(forKey: "UNKNOWN"), "")
|
||||
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.humanReadableName(forKey: "PSTR"), "Total System Power")
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.humanReadableName(forKey: "VC0C"), "CPU Core Voltage")
|
||||
XCTAssertEqual(MMPowerTelemetryProvider.humanReadableName(forKey: "IC0C"), "CPU Core Current")
|
||||
}
|
||||
|
||||
func testPowerMetricsCalculationFromSMC() {
|
||||
let readings: [String: NSNumber] = [
|
||||
"PSTR": NSNumber(value: 45.2),
|
||||
"PCPR": NSNumber(value: 28.4),
|
||||
"PG0R": NSNumber(value: 8.5),
|
||||
"PMSR": NSNumber(value: 3.1),
|
||||
"VC0C": NSNumber(value: 1.15),
|
||||
"IC0C": NSNumber(value: 24.5)
|
||||
]
|
||||
|
||||
let batteryInfo: [String: Any] = [
|
||||
"powerSource": "AC Power",
|
||||
"isCharging": false,
|
||||
"batteryLevel": 100.0,
|
||||
"batteryWatts": 0.0
|
||||
]
|
||||
|
||||
let metrics = MMPowerTelemetryProvider.calculatePowerMetrics(
|
||||
withReadings: readings,
|
||||
batteryInfo: batteryInfo
|
||||
)
|
||||
|
||||
let systemWatts = metrics["systemTotalWatts"] as? Double ?? 0
|
||||
let cpuWatts = metrics["cpuWatts"] as? Double ?? 0
|
||||
let gpuWatts = metrics["gpuWatts"] as? Double ?? 0
|
||||
let memWatts = metrics["memoryWatts"] as? Double ?? 0
|
||||
let cpuV = metrics["cpuVoltage"] as? Double ?? 0
|
||||
let cpuI = metrics["cpuCurrent"] as? Double ?? 0
|
||||
let powerSource = metrics["powerSource"] as? String ?? ""
|
||||
let sensors = metrics["sensors"] as? [[String: Any]] ?? []
|
||||
|
||||
XCTAssertEqual(systemWatts, 45.2, accuracy: 0.1)
|
||||
XCTAssertEqual(cpuWatts, 28.4, accuracy: 0.1)
|
||||
XCTAssertEqual(gpuWatts, 8.5, accuracy: 0.1)
|
||||
XCTAssertEqual(memWatts, 3.1, accuracy: 0.1)
|
||||
XCTAssertEqual(cpuV, 1.15, accuracy: 0.01)
|
||||
XCTAssertEqual(cpuI, 24.5, accuracy: 0.1)
|
||||
XCTAssertEqual(powerSource, "AC Power")
|
||||
XCTAssertEqual(sensors.count, 6)
|
||||
}
|
||||
|
||||
func testBatteryFallbackCalculation() {
|
||||
// No PSTR, no PCPR, but VC0C and IC0C are present
|
||||
let readings: [String: NSNumber] = [
|
||||
"VC0C": NSNumber(value: 1.2),
|
||||
"IC0C": NSNumber(value: 10.0)
|
||||
]
|
||||
|
||||
let batteryInfo: [String: Any] = [
|
||||
"powerSource": "Battery Power",
|
||||
"isCharging": false,
|
||||
"batteryLevel": 75.0,
|
||||
"batteryWatts": 18.5
|
||||
]
|
||||
|
||||
let metrics = MMPowerTelemetryProvider.calculatePowerMetrics(
|
||||
withReadings: readings,
|
||||
batteryInfo: batteryInfo
|
||||
)
|
||||
|
||||
let systemWatts = metrics["systemTotalWatts"] as? Double ?? 0
|
||||
let cpuWatts = metrics["cpuWatts"] as? Double ?? 0
|
||||
let powerSource = metrics["powerSource"] as? String ?? ""
|
||||
let isCharging = metrics["isCharging"] as? Bool ?? true
|
||||
|
||||
// System watts falls back to batteryWatts (18.5)
|
||||
XCTAssertEqual(systemWatts, 18.5, accuracy: 0.1)
|
||||
// CPU watts calculated from V * I = 1.2 * 10.0 = 12.0 W
|
||||
XCTAssertEqual(cpuWatts, 12.0, accuracy: 0.1)
|
||||
XCTAssertEqual(powerSource, "Battery Power")
|
||||
XCTAssertFalse(isCharging)
|
||||
}
|
||||
|
||||
func testLivePowerProvider() throws {
|
||||
let provider = MMPowerTelemetryProvider()
|
||||
XCTAssertEqual(provider.domain, .power)
|
||||
XCTAssertEqual(provider.providerIdentifier, "com.i3omb.macmonitor.telemetry.power")
|
||||
|
||||
let sample = try provider.sampleTelemetry()
|
||||
XCTAssertNotNil(sample)
|
||||
XCTAssertNotNil(sample["systemTotalWatts"])
|
||||
XCTAssertNotNil(sample["powerSource"])
|
||||
XCTAssertNotNil(sample["sensors"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import XCTest
|
||||
@testable import MacMonitor
|
||||
|
||||
final class MMStorageTests: XCTestCase {
|
||||
|
||||
func testStatFSBufferParsing() {
|
||||
var stat = statfs()
|
||||
stat.f_bsize = 4096
|
||||
stat.f_blocks = 125_000_000 // 500 GB
|
||||
stat.f_bfree = 50_000_000 // 200 GB
|
||||
stat.f_bavail = 45_000_000
|
||||
stat.f_flags = UInt32(MNT_LOCAL)
|
||||
|
||||
let mountPath = "/Volumes/External"
|
||||
let devicePath = "/dev/disk2s1"
|
||||
let fsType = "apfs"
|
||||
|
||||
_ = mountPath.withCString { mntPtr in
|
||||
strncpy(&stat.f_mntonname.0, mntPtr, Int(MNAMELEN))
|
||||
}
|
||||
_ = devicePath.withCString { devPtr in
|
||||
strncpy(&stat.f_mntfromname.0, devPtr, Int(MNAMELEN))
|
||||
}
|
||||
_ = fsType.withCString { fsPtr in
|
||||
strncpy(&stat.f_fstypename.0, fsPtr, Int(MFSTYPENAMELEN))
|
||||
}
|
||||
|
||||
withUnsafePointer(to: stat) { ptr in
|
||||
let volumes = MMStorageTelemetryProvider.parseStatFSBuffer(ptr, count: 1)
|
||||
XCTAssertEqual(volumes.count, 1)
|
||||
|
||||
let vol = volumes.first!
|
||||
XCTAssertEqual(vol["volumeName"] as? String, "External")
|
||||
XCTAssertEqual(vol["mountPoint"] as? String, "/Volumes/External")
|
||||
XCTAssertEqual(vol["devicePath"] as? String, "/dev/disk2s1")
|
||||
XCTAssertEqual(vol["fsType"] as? String, "apfs")
|
||||
|
||||
let total = vol["totalBytes"] as? UInt64 ?? 0
|
||||
let free = vol["freeBytes"] as? UInt64 ?? 0
|
||||
let used = vol["usedBytes"] as? UInt64 ?? 0
|
||||
let pct = vol["usedPercent"] as? Double ?? 0
|
||||
|
||||
XCTAssertEqual(total, 512_000_000_000)
|
||||
XCTAssertEqual(free, 204_800_000_000)
|
||||
XCTAssertEqual(used, 307_200_000_000)
|
||||
XCTAssertEqual(pct, 60.0, accuracy: 0.1)
|
||||
}
|
||||
}
|
||||
|
||||
func testLiveStorageProvider() throws {
|
||||
let provider = MMStorageTelemetryProvider()
|
||||
XCTAssertTrue(provider.isAvailable)
|
||||
XCTAssertEqual(provider.domain, .storage)
|
||||
|
||||
let sample = try provider.sampleTelemetry()
|
||||
let volumes = sample["volumes"] as? [[String: Any]]
|
||||
XCTAssertNotNil(volumes)
|
||||
XCTAssertGreaterThan(volumes?.count ?? 0, 0)
|
||||
|
||||
let firstVol = volumes?.first
|
||||
XCTAssertNotNil(firstVol?["mountPoint"])
|
||||
XCTAssertGreaterThan(firstVol?["totalBytes"] as? UInt64 ?? 0, 0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user