File indexing completed on 2024-05-05 05:36:40
0001 /* 0002 SPDX-FileCopyrightText: 2019 Jonah Brüchert <jbb@kaidan.im> 0003 SPDX-FileCopyrightText: 2012-2019 Harald Sitter <sitter@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "hardwareinfo.h" 0009 0010 #include <KCoreAddons> 0011 #include <KFormat> 0012 0013 #include <solid/device.h> 0014 #include <solid/processor.h> 0015 0016 #include <KLocalizedString> 0017 0018 #ifdef Q_OS_LINUX 0019 #include <sys/sysinfo.h> 0020 #elif defined(Q_OS_FREEBSD) 0021 #include <sys/sysctl.h> 0022 #include <sys/types.h> 0023 #endif 0024 0025 HardwareInfo::HardwareInfo(QObject *parent) 0026 : QObject(parent) 0027 { 0028 } 0029 0030 int HardwareInfo::processorCount() const 0031 { 0032 return Solid::Device::listFromType(Solid::DeviceInterface::Processor).count(); 0033 } 0034 0035 QString HardwareInfo::processors() const 0036 { 0037 const auto list = Solid::Device::listFromType(Solid::DeviceInterface::Processor); 0038 0039 // Format processor string 0040 // Group by processor name 0041 QMap<QString, int> processorMap; 0042 for (const auto &device : list) { 0043 const QString name = device.product(); 0044 auto it = processorMap.find(name); 0045 if (it == processorMap.end()) { 0046 processorMap.insert(name, 1); 0047 } else { 0048 ++it.value(); 0049 } 0050 } 0051 // Create a formatted list of grouped processors 0052 QStringList names; 0053 names.reserve(processorMap.count()); 0054 for (auto it = processorMap.constBegin(); it != processorMap.constEnd(); ++it) { 0055 const int count = it.value(); 0056 QString name = it.key(); 0057 name.replace(QStringLiteral("(TM)"), QChar(8482)); 0058 name.replace(QStringLiteral("(R)"), QChar(174)); 0059 name = name.simplified(); 0060 names.append(QStringLiteral("%1 × %2").arg(count).arg(name)); 0061 } 0062 0063 const QString processorLabel = names.join(QLatin1String(", ")); 0064 0065 return processorLabel; 0066 } 0067 0068 QString HardwareInfo::memory() const 0069 { 0070 qlonglong totalRam = -1; 0071 #ifdef Q_OS_LINUX 0072 struct sysinfo info { 0073 }; 0074 if (sysinfo(&info) == 0) 0075 // manpage "sizes are given as multiples of mem_unit bytes" 0076 totalRam = qlonglong(info.totalram) * info.mem_unit; 0077 #elif defined(Q_OS_FREEBSD) 0078 /* Stuff for sysctl */ 0079 size_t len; 0080 0081 unsigned long memory; 0082 len = sizeof(memory); 0083 sysctlbyname("hw.physmem", &memory, &len, NULL, 0); 0084 0085 totalRam = memory; 0086 #endif 0087 0088 return KFormat().formatByteSize(totalRam); 0089 }