Changeset View
Changeset View
Standalone View
Standalone View
src/solid/devices/backends/iokit/iokitprocessor.cpp
- This file was copied to src/solid/devices/backends/iokit/iokitblock.cpp.
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright 2009 Harald Fernengel <harry@kdevelop.org> | 2 | Copyright 2009 Harald Fernengel <harry@kdevelop.org> | ||
3 | Copyright 2017 René J.V. Bertin <rjvbertin@gmail.com> | ||||
3 | 4 | | |||
4 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either | ||
7 | version 2.1 of the License, or (at your option) version 3, or any | 8 | version 2.1 of the License, or (at your option) version 3, or any | ||
8 | later version accepted by the membership of KDE e.V. (or its | 9 | later version accepted by the membership of KDE e.V. (or its | ||
9 | successor approved by the membership of KDE e.V.), which shall | 10 | successor approved by the membership of KDE e.V.), which shall | ||
10 | act as a proxy defined in Section 6 of version 3 of the license. | 11 | act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | 12 | | |||
12 | This library is distributed in the hope that it will be useful, | 13 | This library is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | Lesser General Public License for more details. | 16 | Lesser General Public License for more details. | ||
16 | 17 | | |||
17 | You should have received a copy of the GNU Lesser General Public | 18 | You should have received a copy of the GNU Lesser General Public | ||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 20 | */ | ||
20 | 21 | | |||
21 | #include "iokitprocessor.h" | 22 | #include "iokitprocessor.h" | ||
22 | #include "iokitdevice.h" | 23 | #include "iokitdevice.h" | ||
23 | 24 | | |||
24 | #include <QtCore/qdebug.h> | 25 | #include <QtCore/qdebug.h> | ||
25 | 26 | | |||
27 | #include <sys/types.h> | ||||
28 | #include <sys/sysctl.h> | ||||
29 | #include <errno.h> | ||||
30 | | ||||
31 | #include "../shared/cpufeatures.h" | ||||
32 | | ||||
33 | // from cfhelper.cpp | ||||
34 | extern bool q_sysctlbyname(const char *name, QString &result); | ||||
35 | | ||||
26 | using namespace Solid::Backends::IOKit; | 36 | using namespace Solid::Backends::IOKit; | ||
27 | 37 | | |||
28 | Processor::Processor(IOKitDevice *device) | 38 | Processor::Processor(IOKitDevice *device) | ||
29 | : DeviceInterface(device) | 39 | : DeviceInterface(device) | ||
30 | { | 40 | { | ||
31 | //IOKitDevice parent(device->parentUdi()); | 41 | //IOKitDevice parent(device->parentUdi()); | ||
32 | } | 42 | } | ||
33 | 43 | | |||
34 | Processor::~Processor() | 44 | Processor::~Processor() | ||
35 | { | 45 | { | ||
36 | 46 | | |||
37 | } | 47 | } | ||
38 | 48 | | |||
39 | int Processor::number() const | 49 | int Processor::number() const | ||
40 | { | 50 | { | ||
41 | return m_device->property(QLatin1String("IOCPUNumber")).toInt(); | 51 | return m_device->property(QLatin1String("IOCPUNumber")).toInt(); | ||
42 | } | 52 | } | ||
43 | 53 | | |||
44 | int Processor::maxSpeed() const | 54 | int Processor::maxSpeed() const | ||
45 | { | 55 | { | ||
46 | return 0; // TODO | 56 | uint64_t freq = 0; | ||
57 | size_t size = sizeof(freq); | ||||
58 | | ||||
59 | if (sysctlbyname("hw.cpufrequency", &freq, &size, nullptr, 0) < 0) { | ||||
60 | qWarning() << "sysctl error reading hw.cpufrequency:" << strerror(errno); | ||||
61 | return 0; | ||||
62 | } else { | ||||
63 | return int(freq / 1000000); | ||||
64 | } | ||||
47 | } | 65 | } | ||
48 | 66 | | |||
49 | bool Processor::canChangeFrequency() const | 67 | bool Processor::canChangeFrequency() const | ||
50 | { | 68 | { | ||
51 | return false; // TODO | 69 | uint64_t minFreq = 0, maxFreq = 0; | ||
70 | size_t size = sizeof(uint64_t); | ||||
71 | | ||||
72 | if (sysctlbyname("hw.cpufrequency_min", &minFreq, &size, nullptr, 0) == 0 | ||||
73 | && sysctlbyname("hw.cpufrequency_max", &maxFreq, &size, nullptr, 0) == 0) { | ||||
74 | return maxFreq > minFreq; | ||||
75 | } | ||||
76 | return false; | ||||
52 | } | 77 | } | ||
53 | 78 | | |||
54 | Solid::Processor::InstructionSets Processor::instructionSets() const | 79 | Solid::Processor::InstructionSets Processor::instructionSets() const | ||
55 | { | 80 | { | ||
56 | return 0; // TODO | 81 | // use sysctlbyname() and "machdep.cpu.features" + "machdep.cpu.extfeatures" | ||
82 | static Solid::Processor::InstructionSets cpuextensions = Solid::Backends::Shared::cpuFeatures(); | ||||
83 | | ||||
84 | return cpuextensions; | ||||
kfunk: Remove `const` | |||||
85 | } | ||||
86 | | ||||
87 | QString Processor::vendor() | ||||
Way too much error prone new/delete logic on naked char arrays. Every invocation here is wrong (causing undefined behavior) due to the new[]/delete` mismatch mentioned above. Factor that out reading into a QString into a separate function, cf. https://github.com/trueos/lumina/blob/master/src-qt5/core/libLumina/LuminaOS-DragonFly.cpp#L29 and use it everywhere instead. kfunk: Way too much error prone `new`/`delete` logic on naked char arrays. Every invocation here is… | |||||
88 | { | ||||
89 | QString qVendor; | ||||
90 | q_sysctlbyname("machdep.cpu.vendor", qVendor); | ||||
91 | return qVendor; | ||||
57 | } | 92 | } | ||
58 | 93 | | |||
94 | QString Processor::product() | ||||
95 | { | ||||
96 | QString product; | ||||
97 | q_sysctlbyname("machdep.cpu.brand_string", product); | ||||
98 | return product; | ||||
99 | } | ||||
kfunk: Remove `const` |
Remove const