btop/src/osx/smc.cpp
aristocratos bd5d697830 Squashed commit of the following:
commit c296ac13cd
Merge: 9a1e760 091c30a
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Aug 26 19:29:57 2023 +0200

    Merge pull request #590 from nobounce/dangling-reference-config

    Convert parameters and config keys to std::string_view

commit 9a1e760a66
Merge: 9c8af4d 22e64ca
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Aug 26 19:20:18 2023 +0200

    Merge pull request #602 from jfouquart/main

    Fix getting zfs pool name with '.' char in freebsd

commit 9c8af4df43
Merge: 8a49d8c 2217cbe
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Aug 26 19:18:55 2023 +0200

    Merge pull request #601 from joske/cleanup

    [macos] don't check /sys on macos

commit 8a49d8cf45
Merge: 1556388 008fcd8
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Aug 26 19:18:07 2023 +0200

    Merge pull request #600 from joske/makefile

    [macos/freebsd] support gcc13

commit 1556388c83
Merge: 1b126f5 d17e1a2
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Aug 26 19:14:00 2023 +0200

    Merge pull request #599 from joske/main

    [macos] fix temp sensor on system with many cores

commit d17e1a2dac
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Fri Aug 25 16:18:39 2023 +0200

    fix some warnings

commit 4d8aa6b118
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Fri Aug 25 15:52:58 2023 +0200

    fix core check

commit 22e64caaff
Author: Jonathan Fouquart <jfouquart@hotmail.fr>
Date:   Fri Aug 25 09:37:49 2023 +0200

    Fix getting zfs pool name with '.' char in freebsd

commit 2217cbe143
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Wed Aug 23 16:01:04 2023 +0200

    [macos] don't check /sys on macos

commit 008fcd889e
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Wed Aug 23 16:05:00 2023 +0200

    also add g++13

commit 0fdca5eb03
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Wed Aug 23 15:54:07 2023 +0200

    support gcc13

commit dcbdb7360d
Author: Jos Dehaes <jos.dehaes@gmail.com>
Date:   Wed Aug 23 15:46:47 2023 +0200

    [macos] fix temp sensor on system with many cores

commit 1b126f55e3
Author: aristocratos <gnmjpl@gmail.com>
Date:   Fri Aug 4 01:08:27 2023 +0200

    Update Makefile for partial static compilation on freebsd

commit c8ec6bbb00
Author: aristocratos <gnmjpl@gmail.com>
Date:   Thu Aug 3 23:08:33 2023 +0200

    Fix freebsd nullptr changes and makefile for gcc12 and newer

commit 8a33aab588
Merge: 94e5c02 e4abcef
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sun Jul 30 13:21:48 2023 +0200

    Merge pull request #539 from nobounce/replace-NULL-nullptr

    Modernize using nullptr.

commit 94e5c02d11
Author: aristocratos <gnmjpl@gmail.com>
Date:   Thu Jul 27 20:51:21 2023 +0200

    Better text editing

commit 091c30ab2b
Author: nobounce <steffen.winter@proton.me>
Date:   Thu Jul 27 14:17:54 2023 +0200

    Convert parameters and config keys to std::string_view

    Using std::string_view instead of std::string& silences a new warning
    from GCC 13, -Wdangling-reference

    Also switch return type of `getI` from int& to int, trivial types are
    cheaper to copy by value

commit e4abcefbf9
Author: nobounce <steffen.winter@proton.me>
Date:   Wed Jul 26 16:19:17 2023 +0200

    Use nullptr instead of NULL.

    See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
    TLDR: NULL is of type int and relies on proper implicit pointer
    conversion which may lead to issues when using overloaded functions

    It is also considered a 'best practise' for modern C++ and
    conveys the programmers intention more precisly.

commit d53307f14c
Author: nobounce <steffen.winter@proton.me>
Date:   Sun Jul 23 19:53:36 2023 +0200

    Fix path to Linux CI file in itself

    The CI file has a list of dependent files including itself. The path was
    not updated when the CI was split into different files

commit 594f42b9eb
Merge: aca2e4b 53d6eba
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Wed Jul 26 15:38:01 2023 +0200

    Merge pull request #584 from nobounce/nb/fix-ci-path

    Fix path to Linux CI file in itself

commit aca2e4be75
Author: aristocratos <gnmjpl@gmail.com>
Date:   Wed Jul 26 14:38:48 2023 +0200

    Fix whitespace indent -> tab indent

commit 33faa01910
Author: aristocratos <gnmjpl@gmail.com>
Date:   Wed Jul 26 14:34:15 2023 +0200

    Revert fmt submodule to static fmt folder in include

commit 53d6ebabc0
Author: nobounce <steffen.winter@proton.me>
Date:   Sun Jul 23 19:53:36 2023 +0200

    Fix path to Linux CI file in itself

    The CI file has a list of dependent files including itself. The path was
    not updated when the CI was split into different files
2023-08-26 20:29:43 +02:00

155 lines
4.7 KiB
C++

/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
indent = tab
tab-size = 4
*/
#include "smc.hpp"
static constexpr size_t MaxIndexCount = sizeof("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") - 1;
static constexpr const char *KeyIndexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static 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;
}
static void _ultostr(char *str, UInt32 val) {
str[0] = '\0';
snprintf(str, 5, "%c%c%c%c",
(unsigned int)val >> 24,
(unsigned int)val >> 16,
(unsigned int)val >> 8,
(unsigned int)val);
}
namespace Cpu {
SMCConnection::SMCConnection() {
CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
result = IOServiceGetMatchingServices(0, matchingDictionary, &iterator);
if (result != kIOReturnSuccess) {
throw std::runtime_error("failed to get AppleSMC");
}
device = IOIteratorNext(iterator);
IOObjectRelease(iterator);
if (device == 0) {
throw std::runtime_error("failed to get SMC device");
}
result = IOServiceOpen(device, mach_task_self(), 0, &conn);
IOObjectRelease(device);
if (result != kIOReturnSuccess) {
throw std::runtime_error("failed to get SMC connection");
}
}
SMCConnection::~SMCConnection() {
IOServiceClose(conn);
}
long long SMCConnection::getSMCTemp(char *key) {
SMCVal_t val;
kern_return_t result;
result = SMCReadKey(key, &val);
if (result == kIOReturnSuccess) {
if (val.dataSize > 0) {
if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
// convert sp78 value to temperature
int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1];
return static_cast<long long>(intValue / 256.0);
}
}
}
return -1;
}
// core means physical core in SMC, while in core map it's cpu threads :-/ Only an issue on hackintosh?
// this means we can only get the T per physical core
// another issue with the SMC API is that the key is always 4 chars -> what with systems with more than 9 physical cores?
// no Mac models with more than 18 threads are released, so no problem so far
// according to VirtualSMC docs (hackintosh fake SMC) the enumeration follows with alphabetic chars - not implemented yet here (nor in VirtualSMC)
long long SMCConnection::getTemp(int core) {
char key[] = SMC_KEY_CPU_TEMP;
if (core >= 0) {
if ((size_t)core > MaxIndexCount) {
return -1;
}
snprintf(key, 5, "TC%1cc", KeyIndexes[core]);
}
long long result = getSMCTemp(key);
if (result == -1) {
// try again with C
snprintf(key, 5, "TC%1dC", KeyIndexes[core]);
result = getSMCTemp(key);
}
return result;
}
kern_return_t SMCConnection::SMCReadKey(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(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(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
if (result != kIOReturnSuccess)
return result;
memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes));
return kIOReturnSuccess;
}
kern_return_t SMCConnection::SMCCall(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure) {
size_t structureInputSize;
size_t structureOutputSize;
structureInputSize = sizeof(SMCKeyData_t);
structureOutputSize = sizeof(SMCKeyData_t);
return IOConnectCallStructMethod(conn, index,
// inputStructure
inputStructure, structureInputSize,
// ouputStructure
outputStructure, &structureOutputSize);
}
} // namespace Cpu