diff options
| author | Alex Manuskin <amanusk@protonmail.com> | 2018-06-26 16:19:54 +0300 |
|---|---|---|
| committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-06-26 06:19:54 -0700 |
| commit | 196802dd34b7134c1fff99ad4755d0bc0e04cc98 (patch) | |
| tree | 6afb9396ae0231bef6df741b6b136f4a9f1e6b90 /psutil/arch/osx | |
| parent | d20b9939b985d871177bee2c1271a04a1810a100 (diff) | |
| download | psutil-196802dd34b7134c1fff99ad4755d0bc0e04cc98.tar.gz | |
Osx temps (#1284)
OSX: add temperatures() and fans()
Diffstat (limited to 'psutil/arch/osx')
| -rw-r--r-- | psutil/arch/osx/smc.c | 236 | ||||
| -rw-r--r-- | psutil/arch/osx/smc.h | 79 |
2 files changed, 315 insertions, 0 deletions
diff --git a/psutil/arch/osx/smc.c b/psutil/arch/osx/smc.c new file mode 100644 index 00000000..fb5668a1 --- /dev/null +++ b/psutil/arch/osx/smc.c @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Interface to SMC API, needed in order to collect sensors stats. + */ + +#include <stdio.h> +#include <string.h> +#include <IOKit/IOKitLib.h> + +#include "smc.h" + +UInt32 _strtoul(char *str, int size, int base) +{ + UInt32 total = 0; + int i; + + for (i = 0; i < size; i++) { + if (base == 16) + total += str[i] << (size - 1 - i) * 8; + else + total += (unsigned char) (str[i] << (size - 1 - i) * 8); + } + return total; +} + + +float _strtof(unsigned char *str, int size, int e) +{ + float total = 0; + int i; + + for (i = 0; i < size; i++) { + if (i == (size - 1)) + total += (str[i] & 0xff) >> e; + else + total += str[i] << (size - 1 - i) * (8 - e); + } + + total += (str[size-1] & 0x03) * 0.25; + + return total; +} + + +void _ultostr(char *str, UInt32 val) +{ + str[0] = '\0'; + sprintf(str, "%c%c%c%c", + (unsigned int) val >> 24, + (unsigned int) val >> 16, + (unsigned int) val >> 8, + (unsigned int) val); +} + + +kern_return_t SMCOpen(io_connect_t * pconn) +{ + kern_return_t result; + io_iterator_t iterator; + io_object_t device; + + CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC"); + result = IOServiceGetMatchingServices( + kIOMasterPortDefault, + matchingDictionary, + &iterator + ); + if (result != kIOReturnSuccess) { + return 1; + } + + device = IOIteratorNext(iterator); + IOObjectRelease(iterator); + if (device == 0) { + return 1; + } + + result = IOServiceOpen(device, mach_task_self(), 0, pconn); + IOObjectRelease(device); + if (result != kIOReturnSuccess) { + return 1; + } + + return kIOReturnSuccess; +} + + +kern_return_t SMCClose(io_connect_t conn) +{ + return IOServiceClose(conn); +} + + +kern_return_t SMCCall(io_connect_t conn, + int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure) +{ + size_t structureInputSize; + size_t structureOutputSize; + + structureInputSize = sizeof(SMCKeyData_t); + structureOutputSize = sizeof(SMCKeyData_t); + +#if MAC_OS_X_VERSION_10_5 + return IOConnectCallStructMethod( + conn, index, + inputStructure, + structureInputSize, + outputStructure, + &structureOutputSize); +#else + return IOConnectMethodStructureIStructureO( + conn, index, + structureInputSize, + &structureOutputSize, + inputStructure, + outputStructure); +#endif +} + + +kern_return_t SMCReadKey(io_connect_t conn, UInt32Char_t key, SMCVal_t *val) { + kern_return_t result; + SMCKeyData_t inputStructure; + SMCKeyData_t outputStructure; + + memset(&inputStructure, 0, sizeof(SMCKeyData_t)); + memset(&outputStructure, 0, sizeof(SMCKeyData_t)); + memset(val, 0, sizeof(SMCVal_t)); + + inputStructure.key = _strtoul(key, 4, 16); + inputStructure.data8 = SMC_CMD_READ_KEYINFO; + + result = SMCCall( + conn, + KERNEL_INDEX_SMC, + &inputStructure, + &outputStructure + ); + if (result != kIOReturnSuccess) + return result; + + val->dataSize = outputStructure.keyInfo.dataSize; + _ultostr(val->dataType, outputStructure.keyInfo.dataType); + inputStructure.keyInfo.dataSize = val->dataSize; + inputStructure.data8 = SMC_CMD_READ_BYTES; + + result = SMCCall( + conn, + KERNEL_INDEX_SMC, + &inputStructure, + &outputStructure + ); + if (result != kIOReturnSuccess) + return result; + + memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes)); + + return kIOReturnSuccess; +} + + +double SMCGetTemperature(char *key) { + io_connect_t conn; + SMCVal_t val; + kern_return_t result; + int intValue; + + result = SMCOpen(&conn); + if (result != kIOReturnSuccess) { + return 0.0; + } + + result = SMCReadKey(conn, key, &val); + if (result == kIOReturnSuccess) { + // read succeeded - check returned value + if (val.dataSize > 0) { + if (strcmp(val.dataType, DATATYPE_SP78) == 0) { + // convert sp78 value to temperature + intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1]; + SMCClose(conn); + return intValue / 256.0; + } + } + } + // read failed + SMCClose(conn); + return 0.0; +} + + +float SMCGetFanSpeed(int fanNum) +{ + SMCVal_t val; + kern_return_t result; + UInt32Char_t key; + io_connect_t conn; + + result = SMCOpen(&conn); + if (result != kIOReturnSuccess) { + return -1; + } + + sprintf(key, SMC_KEY_FAN_SPEED, fanNum); + result = SMCReadKey(conn, key, &val); + if (result != kIOReturnSuccess) { + SMCClose(conn); + return -1; + } + SMCClose(conn); + return _strtof((unsigned char *)val.bytes, val.dataSize, 2); +} + + + +int SMCGetFanNumber(char *key) +{ + SMCVal_t val; + kern_return_t result; + io_connect_t conn; + + result = SMCOpen(&conn); + if (result != kIOReturnSuccess) { + return 0; + } + + result = SMCReadKey(conn, key, &val); + if (result != kIOReturnSuccess) { + SMCClose(conn); + return 0; + } + SMCClose(conn); + return _strtoul((char *)val.bytes, val.dataSize, 10); +} diff --git a/psutil/arch/osx/smc.h b/psutil/arch/osx/smc.h new file mode 100644 index 00000000..acd399c2 --- /dev/null +++ b/psutil/arch/osx/smc.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __SMC_H__ +#define __SMC_H__ + +#define KERNEL_INDEX_SMC 2 + +#define SMC_CMD_READ_BYTES 5 +#define SMC_CMD_WRITE_BYTES 6 +#define SMC_CMD_READ_INDEX 8 +#define SMC_CMD_READ_KEYINFO 9 +#define SMC_CMD_READ_PLIMIT 11 +#define SMC_CMD_READ_VERS 12 + + +#define DATATYPE_FPE2 "fpe2" +#define DATATYPE_UINT8 "ui8 " +#define DATATYPE_UINT16 "ui16" +#define DATATYPE_UINT32 "ui32" +#define DATATYPE_SP78 "sp78" + +// Fans SMC key values +#define SMC_KEY_FAN_SPEED "F%dAc" +#define SMC_KEY_FAN_NUM "FNum" + +typedef struct { + char major; + char minor; + char build; + char reserved[1]; + UInt16 release; +} SMCKeyData_vers_t; + +typedef struct { + UInt16 version; + UInt16 length; + UInt32 cpuPLimit; + UInt32 gpuPLimit; + UInt32 memPLimit; +} SMCKeyData_pLimitData_t; + +typedef struct { + UInt32 dataSize; + UInt32 dataType; + char dataAttributes; +} SMCKeyData_keyInfo_t; + +typedef char SMCBytes_t[32]; + +typedef struct { + UInt32 key; + SMCKeyData_vers_t vers; + SMCKeyData_pLimitData_t pLimitData; + SMCKeyData_keyInfo_t keyInfo; + char result; + char status; + char data8; + UInt32 data32; + SMCBytes_t bytes; +} SMCKeyData_t; + +typedef char UInt32Char_t[5]; + +typedef struct { + UInt32Char_t key; + UInt32 dataSize; + UInt32Char_t dataType; + SMCBytes_t bytes; +} SMCVal_t; + +double SMCGetTemperature(char *key); +int SMCGetFanNumber(char *key); +float SMCGetFanSpeed(int fanNum); + +#endif |
