format check

This commit is contained in:
lfreist
2022-11-28 10:18:13 +01:00
parent 5c4cb9f58f
commit d704fdfc73
43 changed files with 440 additions and 464 deletions

5
.clang-format Normal file
View File

@@ -0,0 +1,5 @@
---
BasedOnStyle: Google
DerivePointerAlignment: false
PointerAlignment: Left
ColumnLimit: 120

22
.github/workflows/format-check.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: code-style
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: sudo apt-get install -y clang-format-14
- name: Run clang-format
run: |
chmod +x ./format_check.sh
./code_style_check.sh
shell: bash

View File

@@ -6,6 +6,8 @@
[![Windows (MinGW)](https://github.com/lfreist/hwinfo/actions/workflows/build-windows-mingw.yml/badge.svg)](https://github.com/lfreist/hwinfo/actions/workflows/build-windows-mingw.yml)
[![Windows (Visual Studio)](https://github.com/lfreist/hwinfo/actions/workflows/build-windows-vs.yml/badge.svg)](https://github.com/lfreist/hwinfo/actions/workflows/build-windows-vs.yml)
[![clang format](https://github.com/lfreist/hwinfo/actions/workflows/format-check.yml/badge.svg)](https://github.com/lfreist/hwinfo/actions/workflows/format-check.yml)
# hwinfo
hwinfo provides an easy-to-use and modern C++ API for retrieving hardware information of your systems components such as

29
code_style_check.sh Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
printf "Checking sources for code style\n"
SOURCE_FILES=()
find ./src/ ./test/ ./include/ -regextype egrep -regex '.*\.(h|c)(pp|xx)?$' -print0 > sourcelist
while IFS= read -r -d $'\0'; do
SOURCE_FILES+=("$REPLY")
done < sourcelist
ERROR=0
for source in "${SOURCE_FILES[@]}"; do
clang-format-14 -output-replacements-xml "$source" | grep "<replacement " &> /dev/null
HAS_WRONG_FILES=$?
if [ $HAS_WRONG_FILES -ne 1 ]; then
printf "Checking %s: \x1b[31mFAILED!\x1b[m\n" "$source"
ERROR=1
else
printf "Checking %s: \x1b[32mPASSED\x1b[m\n" "$source"
fi
done
rm sourcelist
if [ $ERROR -eq 0 ]; then
printf "\x1b[32mCongratulations! All sources match the code style\x1b[m\n"
else
printf "\x1b[31mclang-format-14 discovered style issues in at least one file.\x1b[m\n"
exit 1
fi

View File

@@ -1,28 +1,25 @@
// Copyright Leon Freist
// Author Leon Freist <freist@informatik.uni-freiburg.de>
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
#pragma once
#include <string>
#include <vector>
#include <type_traits>
#include <WbemIdl.h>
#include <Windows.h>
#include <comdef.h>
#include <WbemIdl.h>
#include <ntddscsi.h>
#include <string>
#include <type_traits>
#include <vector>
#pragma comment(lib, "wbemuuid.lib")
namespace hwinfo::wmi {
template<typename T>
inline bool queryWMI(const std::string &WMIClass,
std::string field,
std::vector<T> &value,
const std::string &serverName = "ROOT\\CIMV2") {
template <typename T>
inline bool queryWMI(const std::string& WMIClass, std::string field, std::vector<T>& value,
const std::string& serverName = "ROOT\\CIMV2") {
std::string query("SELECT " + field + " FROM " + WMIClass);
HRESULT hres;
@@ -30,62 +27,45 @@ inline bool queryWMI(const std::string &WMIClass,
if (FAILED(hres)) {
return false;
}
hres = CoInitializeSecurity(nullptr,
-1,
nullptr,
nullptr,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
nullptr,
EOAC_NONE,
nullptr);
hres = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,
nullptr, EOAC_NONE, nullptr);
if (FAILED(hres)) {
CoUninitialize();
return false;
}
IWbemLocator *pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
IWbemLocator* pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return false;
}
IWbemServices *pSvc = nullptr;
IWbemServices* pSvc = nullptr;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &pSvc);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return false;
}
hres = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
nullptr,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
nullptr,
EOAC_NONE);
hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false;
}
IEnumWbemClassObject *pEnumerator = nullptr;
hres = pSvc->ExecQuery(bstr_t(L"WQL"),
bstr_t(std::wstring(query.begin(), query.end()).c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
nullptr,
&pEnumerator);
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(bstr_t(L"WQL"), bstr_t(std::wstring(query.begin(), query.end()).c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr, &pEnumerator);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false;
}
IWbemClassObject *pclsObj = nullptr;
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator) {
pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (!uReturn) {
@@ -96,19 +76,19 @@ inline bool queryWMI(const std::string &WMIClass,
pclsObj->Get(std::wstring(field.begin(), field.end()).c_str(), 0, &vtProp, nullptr, nullptr);
if (std::is_same<T, long>::value || std::is_same<T, int>::value) {
value.push_back((T) vtProp.intVal);
value.push_back((T)vtProp.intVal);
} else if (std::is_same<T, bool>::value) {
value.push_back((T) vtProp.boolVal);
value.push_back((T)vtProp.boolVal);
} else if (std::is_same<T, unsigned>::value) {
value.push_back((T) vtProp.uintVal);
value.push_back((T)vtProp.uintVal);
} else if (std::is_same<T, unsigned short>::value) {
value.push_back((T) vtProp.uiVal);
value.push_back((T)vtProp.uiVal);
} else if (std::is_same<T, long long>::value) {
value.push_back((T) vtProp.llVal);
value.push_back((T)vtProp.llVal);
} else if (std::is_same<T, unsigned long long>::value) {
value.push_back((T) vtProp.ullVal);
value.push_back((T)vtProp.ullVal);
} else {
value.push_back((T) ((bstr_t) vtProp.bstrVal).copy());
value.push_back((T)((bstr_t)vtProp.bstrVal).copy());
}
VariantClear(&vtProp);
@@ -121,8 +101,7 @@ inline bool queryWMI(const std::string &WMIClass,
pSvc->Release();
pLoc->Release();
if (pEnumerator)
pEnumerator->Release();
if (pEnumerator) pEnumerator->Release();
CoUninitialize();
return true;
}

View File

@@ -12,14 +12,15 @@ namespace hwinfo {
class Battery {
friend std::vector<Battery> getAllBatteries();
public:
explicit Battery(int8_t id = 0);
~Battery() = default;
std::string &vendor();
std::string &model();
std::string &serialNumber();
std::string &technology();
std::string& vendor();
std::string& model();
std::string& serialNumber();
std::string& technology();
uint32_t energyFull();
double capacity();
@@ -46,4 +47,3 @@ class Battery {
std::vector<Battery> getAllBatteries();
} // namespace hwinfo

View File

@@ -3,9 +3,9 @@
#pragma once
#include <optional>
#include <string>
#include <vector>
#include <optional>
namespace hwinfo {
@@ -24,6 +24,7 @@ struct InstructionSet {
class CPU {
friend std::optional<CPU> getCPU(uint8_t socket_id);
public:
CPU() = default;
CPU(int id);

View File

@@ -26,7 +26,6 @@
#define LVL_TYPE 0x0000ff00
#define LVL_CORES 0x0000ffff
namespace hwinfo::cpuid {
/**

View File

@@ -9,14 +9,15 @@
namespace hwinfo {
class Disk {
friend std::vector<Disk>getAllDisks();
friend std::vector<Disk> getAllDisks();
public:
Disk(std::string &vendor, std::string &model, std::string &serialNumber, int64_t size_Bytes);
Disk(std::string& vendor, std::string& model, std::string& serialNumber, int64_t size_Bytes);
~Disk() = default;
[[nodiscard]] const std::string &vendor() const;
[[nodiscard]] const std::string &model() const;
[[nodiscard]] const std::string &serialNumber() const;
[[nodiscard]] const std::string& vendor() const;
[[nodiscard]] const std::string& model() const;
[[nodiscard]] const std::string& serialNumber() const;
[[nodiscard]] int64_t size_Bytes() const;
private:

View File

@@ -3,10 +3,10 @@
#pragma once
#include "hwinfo/cpu.h"
#include "hwinfo/ram.h"
#include "hwinfo/os.h"
#include "hwinfo/mainboard.h"
#include "hwinfo/gpu.h"
#include "hwinfo/disk.h"
#include "hwinfo/battery.h"
#include "hwinfo/cpu.h"
#include "hwinfo/disk.h"
#include "hwinfo/gpu.h"
#include "hwinfo/mainboard.h"
#include "hwinfo/os.h"
#include "hwinfo/ram.h"

View File

@@ -13,16 +13,13 @@ namespace hwinfo {
class MainBoard {
public:
MainBoard() = default;
MainBoard(const std::string &vendor,
const std::string &product,
const std::string &version,
const std::string &serialNumber);
MainBoard(std::string vendor, std::string product, std::string version, std::string serialNumber);
~MainBoard() = default;
std::string &vendor();
std::string &name();
std::string &version();
std::string &serialNumber();
std::string& vendor();
std::string& name();
std::string& version();
std::string& serialNumber();
static std::string getVendor();
static std::string getName();

View File

@@ -6,7 +6,7 @@
#if defined(__APPLE__)
#define HWINFO_APPLE
#endif
#if defined(_WIN32) || defined (_WIN64) || defined(__CYGWIN__)
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
#define HWINFO_WINDOWS
#endif

View File

@@ -10,13 +10,13 @@ namespace hwinfo {
class RAM {
public:
RAM() = default;
RAM(std::string &vendor, std::string &name, std::string &model, std::string &serialNumber, int64_t size_Bytes);
RAM(std::string& vendor, std::string& name, std::string& model, std::string& serialNumber, int64_t size_Bytes);
~RAM() = default;
std::string &vendor();
std::string &name();
std::string &model();
std::string &serialNumber();
std::string& vendor();
std::string& name();
std::string& model();
std::string& serialNumber();
int64_t totalSize_Bytes();
static std::string getVendor();

View File

@@ -7,9 +7,9 @@
#include <vector>
#include "hwinfo/cpu.h"
#include "hwinfo/ram.h"
#include "hwinfo/gpu.h"
#include "hwinfo/disk.h"
#include "hwinfo/gpu.h"
#include "hwinfo/ram.h"
namespace hwinfo {

View File

@@ -11,34 +11,39 @@
* @param input
*/
inline void strip(std::string& input) {
if (input.empty()) { return; }
if (input.empty()) {
return;
}
// optimization for input size == 1
if (input.size() == 1) {
if (input[0] == ' ' || input[0] == '\t' || input[0] == '\n') {
input = "";
return;
}
else {
} else {
return;
}
}
size_t start_index = 0;
while (true) {
char c = input[start_index];
if (c != ' ' && c != '\t' && c != '\n') { break; }
if (c != ' ' && c != '\t' && c != '\n') {
break;
}
start_index++;
}
size_t end_index = input.size() - 1;
while (true) {
char c = input[end_index];
if (c != ' ' && c != '\t' && c != '\n') { break; }
if (c != ' ' && c != '\t' && c != '\n') {
break;
}
end_index--;
}
if (end_index <= start_index) {
input.assign("");
return;
}
input.assign(input.begin()+start_index, input.begin()+end_index+1);
input.assign(input.begin() + start_index, input.begin() + end_index + 1);
}
/**
@@ -66,10 +71,12 @@ inline unsigned count_substring(const std::string& input, const std::string& sub
inline std::vector<std::string> split(const std::string& input, const std::string& delimiter) {
std::vector<std::string> result;
size_t shift = 0;
while(true) {
while (true) {
size_t match = input.find(delimiter, shift);
result.emplace_back(input.substr(shift, match-shift));
if (match == std::string::npos) { break; }
result.emplace_back(input.substr(shift, match - shift));
if (match == std::string::npos) {
break;
}
shift = match + delimiter.size();
}
return result;
@@ -84,10 +91,12 @@ inline std::vector<std::string> split(const std::string& input, const std::strin
inline std::vector<std::string> split(const std::string& input, const char delimiter) {
std::vector<std::string> result;
size_t shift = 0;
while(true) {
while (true) {
size_t match = input.find(delimiter, shift);
if (match == std::string::npos) { break; }
result.emplace_back(input.substr(shift, match-shift));
if (match == std::string::npos) {
break;
}
result.emplace_back(input.substr(shift, match - shift));
shift = match + 1;
}
return result;
@@ -104,11 +113,15 @@ inline std::vector<std::string> split(const std::string& input, const char delim
inline std::string split_get_index(const std::string& input, const std::string& delimiter, int index) {
unsigned occ = count_substring(input, delimiter) + 1;
index = index < 0 ? static_cast<int>(occ + index) : index;
if (occ <= index) { return ""; }
if (occ <= index) {
return "";
}
std::string::size_type start_index = 0;
while (true) {
if (index == 0) { break; }
if (index == 0) {
break;
}
start_index = input.find(delimiter, start_index) + delimiter.size();
index--;
}
@@ -123,6 +136,4 @@ inline std::string split_get_index(const std::string& input, const std::string&
* Convert windows wstring to string
* @return
*/
inline std::string wstring_to_string() {
return "";
}
inline std::string wstring_to_string() { return ""; }

View File

@@ -4,10 +4,10 @@
#pragma once
#include <cstdio>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <memory>
#include <iostream>
inline std::string exec(std::string& command) {
std::string output;

View File

@@ -11,44 +11,28 @@ namespace hwinfo {
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
std::string Battery::getVendor() const {
return "<unknwon>";
}
std::string Battery::getVendor() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
std::string Battery::getModel() const {
return "<unknwon>";
}
std::string Battery::getModel() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
std::string Battery::getSerialNumber() const {
return "<unknwon>";
}
std::string Battery::getSerialNumber() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
std::string Battery::getTechnology() const {
return "<unknwon>";
}
std::string Battery::getTechnology() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
uint32_t Battery::getEnergyFull() const {
return 0;
}
uint32_t Battery::getEnergyFull() const { return 0; }
// _____________________________________________________________________________________________________________________
uint32_t Battery::energyNow() const {
return 0;
}
uint32_t Battery::energyNow() const { return 0; }
// _____________________________________________________________________________________________________________________
bool Battery::charging() const {
return false;
}
bool Battery::charging() const { return false; }
// _____________________________________________________________________________________________________________________
bool Battery::discharging() const {
return false;
}
bool Battery::discharging() const { return false; }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________

View File

@@ -5,20 +5,19 @@
#ifdef HWINFO_APPLE
#include <string>
#include <vector>
#include <algorithm>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/sysctl.h>
#include <math.h>
#include <pthread.h>
#include <sys/sysctl.h>
#include <algorithm>
#include <string>
#include <vector>
#include "hwinfo/cpu.h"
#include "hwinfo/cpuid.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
@@ -31,11 +30,11 @@ int CPU::currentClockSpeed_kHz() {
std::string CPU::getVendor() {
#if defined(HWINFO_X86)
std::string vendor;
uint32_t regs[4] {0};
uint32_t regs[4]{0};
cpuid::cpuid(0, 0, regs);
vendor += std::string((const char *) &regs[1], 4);
vendor += std::string((const char *) &regs[3], 4);
vendor += std::string((const char *) &regs[2], 4);
vendor += std::string((const char*)&regs[1], 4);
vendor += std::string((const char*)&regs[3], 4);
vendor += std::string((const char*)&regs[2], 4);
return vendor;
#else
// TODO: implement
@@ -47,25 +46,25 @@ std::string CPU::getVendor() {
std::string CPU::getModelName() {
#if defined(HWINFO_X86)
std::string model;
uint32_t regs[4] {};
uint32_t regs[4]{};
for (unsigned i = 0x80000002; i < 0x80000005; ++i) {
cpuid::cpuid(i, 0, regs);
for(auto c : std::string((const char*)&regs[0], 4)) {
for (auto c : std::string((const char*)&regs[0], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[1], 4)) {
for (auto c : std::string((const char*)&regs[1], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[2], 4)) {
for (auto c : std::string((const char*)&regs[2], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[3], 4)) {
for (auto c : std::string((const char*)&regs[3], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
@@ -74,9 +73,9 @@ std::string CPU::getModelName() {
return model;
#else
char* model_2[1024];
size_t size=sizeof(model_2);
size_t size = sizeof(model_2);
if (sysctlbyname("machdep.cpu.brand_string", model_2, &size, NULL, 0) < 0) {
perror("sysctl");
perror("sysctl");
}
return std::string(model);
#endif
@@ -85,19 +84,19 @@ std::string CPU::getModelName() {
// _____________________________________________________________________________________________________________________
int CPU::getNumPhysicalCores() {
#if defined(HWINFO_X86)
uint32_t regs[4] {};
uint32_t regs[4]{};
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 11) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x01) {
int numCores = getNumLogicalCores()/static_cast<int>(LVL_CORES & regs_2[1]);
int numCores = getNumLogicalCores() / static_cast<int>(LVL_CORES & regs_2[1]);
if (numCores > 0) {
return numCores;
}
@@ -105,9 +104,9 @@ int CPU::getNumPhysicalCores() {
}
} else {
if (HFS >= 4) {
uint32_t regs_3[4] {};
uint32_t regs_3[4]{};
cpuid::cpuid(4, 0, regs_3);
int numCores = getNumLogicalCores()/static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
int numCores = getNumLogicalCores() / static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
if (numCores > 0) {
return numCores;
}
@@ -115,7 +114,7 @@ int CPU::getNumPhysicalCores() {
}
} else if (vendorId.find("AMD") != std::string::npos) {
if (HFS > 0) {
uint32_t regs_4[4] {};
uint32_t regs_4[4]{};
cpuid::cpuid(0x80000000, 0, regs_4);
if (regs_4[0] >= 8) {
int numCores = 1 + (regs_4[2] & 0xff);
@@ -128,11 +127,11 @@ int CPU::getNumPhysicalCores() {
return -1;
#else
int physical = 0;
size_t physical_size = sizeof(physical);
if (sysctlbyname("hw.physicalcpu", &physical, &physical_size, nullptr, 0) != 0) {
return -1;
}
return physical;
size_t physical_size = sizeof(physical);
if (sysctlbyname("hw.physicalcpu", &physical, &physical_size, nullptr, 0) != 0) {
return -1;
}
return physical;
#endif
}
@@ -140,14 +139,14 @@ int CPU::getNumPhysicalCores() {
int CPU::getNumLogicalCores() {
#if defined(HWINFO_X86)
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
uint32_t regs[4] {};
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
uint32_t regs[4]{};
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 0xb) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x02) {
@@ -204,8 +203,8 @@ int CPU::getCacheSize_Bytes() {
if (line.starts_with("cache size")) {
try {
stream.close();
return std::stoi(line.substr(line.find(": ")+2, line.length()-3)) * 1000;
} catch (std::invalid_argument &e) {
return std::stoi(line.substr(line.find(": ") + 2, line.length() - 3)) * 1000;
} catch (std::invalid_argument& e) {
return -1;
}
}
@@ -215,23 +214,22 @@ int CPU::getCacheSize_Bytes() {
#elif defined(__APPLE__)
return -1;
#elif defined(_WIN32) || defined(_WIN64)
std::vector<int> cacheSize {};
std::vector<int> cacheSize{};
wmi::queryWMI("Win32_Processor", "L3CacheSize", cacheSize);
if (cacheSize.empty()) { return -1; }
if (cacheSize.empty()) {
return -1;
}
return cacheSize[0];
#else
return -1;
#endif
}
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
// Helper function for linux: parses /proc/cpuinfo. socket_id == physical_id.
// _____________________________________________________________________________________________________________________
std::optional<CPU> getCPU(uint8_t socket_id) {
return {};
}
std::optional<CPU> getCPU(uint8_t socket_id) { return {}; }
// ===== Socket ========================================================================================================
// _____________________________________________________________________________________________________________________
@@ -243,15 +241,11 @@ Socket::Socket(uint8_t id) : _id(id) {
}
// _____________________________________________________________________________________________________________________
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) {
_cpu = cpu;
}
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) { _cpu = cpu; }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
std::vector<Socket> getAllSockets() {
return {};
}
std::vector<Socket> getAllSockets() { return {}; }
} // namespace hwinfo

View File

@@ -5,9 +5,9 @@
#ifdef HWINFO_APPLE
#include <vector>
#include <string>
#include <regex>
#include <string>
#include <vector>
#include "hwinfo/gpu.h"

View File

@@ -7,7 +7,6 @@
#include "hwinfo/mainboard.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________

View File

@@ -5,11 +5,11 @@
#ifdef HWINFO_APPLE
#include <sys/sysctl.h>
#include <sstream>
#include <string>
#include <sys/sysctl.h>
#include "hwinfo/os.h"
namespace hwinfo {
@@ -54,9 +54,7 @@ std::string OS::getKernel() {
}
// _____________________________________________________________________________________________________________________
bool OS::getIs64bit() {
return true;
}
bool OS::getIs64bit() { return true; }
} // namespace hwinfo

View File

@@ -5,11 +5,11 @@
#ifdef HWINFO_APPLE
#include <sys/sysctl.h>
#include <string>
#include <vector>
#include <sys/sysctl.h>
#include "hwinfo/ram.h"
namespace hwinfo {

View File

@@ -7,12 +7,10 @@ namespace hwinfo {
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
Battery::Battery(int8_t id) {
_id = id;
}
Battery::Battery(int8_t id) { _id = id; }
// _____________________________________________________________________________________________________________________
std::string &Battery::vendor() {
std::string& Battery::vendor() {
if (_vendor.empty()) {
_vendor = getVendor();
}
@@ -20,7 +18,7 @@ std::string &Battery::vendor() {
}
// _____________________________________________________________________________________________________________________
std::string &Battery::model() {
std::string& Battery::model() {
if (_model.empty()) {
_model = getModel();
}
@@ -28,7 +26,7 @@ std::string &Battery::model() {
}
// _____________________________________________________________________________________________________________________
std::string &Battery::serialNumber() {
std::string& Battery::serialNumber() {
if (_serialNumber.empty()) {
_serialNumber = getSerialNumber();
}
@@ -36,7 +34,7 @@ std::string &Battery::serialNumber() {
}
// _____________________________________________________________________________________________________________________
std::string &Battery::technology() {
std::string& Battery::technology() {
if (_technology.empty()) {
_technology = getTechnology();
}
@@ -52,8 +50,6 @@ uint32_t Battery::energyFull() {
}
// _____________________________________________________________________________________________________________________
double Battery::capacity() {
return static_cast<double>(energyNow()) / energyFull();
}
double Battery::capacity() { return static_cast<double>(energyNow()) / energyFull(); }
} // namespace hwinfo

View File

@@ -1,20 +1,18 @@
// Copyright (c) Leon Freist <freist@informatik.uni-freiburg.de>
// This software is part of HWBenchmark
#include "hwinfo/cpu.h"
#include <string>
#include <vector>
#include "hwinfo/cpu.h"
#include "hwinfo/platform.h"
#include "hwinfo/cpuid.h"
#include "hwinfo/platform.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
CPU::CPU(int id) {
_id = id;
}
CPU::CPU(int id) { _id = id; }
// _____________________________________________________________________________________________________________________
std::string& CPU::modelName() {
@@ -73,22 +71,20 @@ int CPU::regularClockSpeed_kHz() {
}
// _____________________________________________________________________________________________________________________
InstructionSet &CPU::instructionSet() {
InstructionSet& CPU::instructionSet() {
if (!_instructionSet._init_) {
#if defined(HWINFO_X86)
uint32_t regs[4] {};
uint32_t regs[4]{};
cpuid::cpuid(1, 0, regs);
_instructionSet = InstructionSet {
static_cast<bool>(regs[3] & AVX_POS),
static_cast<bool>(regs[3] & SSE_POS),
static_cast<bool>(regs[3] & SSE2_POS),
static_cast<bool>(regs[2] & SSE3_POS),
static_cast<bool>(regs[2] & SSE41_POS),
static_cast<bool>(regs[2] & SSE42_POS),
static_cast<bool>(regs[2] & AVX_POS),
false,
true
};
_instructionSet = InstructionSet{static_cast<bool>(regs[3] & AVX_POS),
static_cast<bool>(regs[3] & SSE_POS),
static_cast<bool>(regs[3] & SSE2_POS),
static_cast<bool>(regs[2] & SSE3_POS),
static_cast<bool>(regs[2] & SSE41_POS),
static_cast<bool>(regs[2] & SSE42_POS),
static_cast<bool>(regs[2] & AVX_POS),
false,
true};
cpuid::cpuid(7, 0, regs);
_instructionSet._isAVX2 = static_cast<bool>(regs[1] & AVX2_POS);
#else
@@ -100,9 +96,6 @@ InstructionSet &CPU::instructionSet() {
// ===== Socket ========================================================================================================
// _____________________________________________________________________________________________________________________
CPU &Socket::CPU() {
return _cpu;
}
CPU& Socket::CPU() { return _cpu; }
} // namespace hwinfo

View File

@@ -2,34 +2,27 @@
// Author Leon Freist <freist@informatik.uni-freiburg.de>
#include "hwinfo/disk.h"
#include "hwinfo/utils/stringutils.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
Disk::Disk(std::string &vendor, std::string &model, std::string &serialNumber, int64_t size_Bytes)
: _vendor(vendor), _model(model), _serialNumber(serialNumber) {
Disk::Disk(std::string& vendor, std::string& model, std::string& serialNumber, int64_t size_Bytes)
: _vendor(vendor), _model(model), _serialNumber(serialNumber) {
_size_Bytes = size_Bytes;
}
// _____________________________________________________________________________________________________________________
const std::string &Disk::vendor() const {
return _vendor;
}
const std::string& Disk::vendor() const { return _vendor; }
// _____________________________________________________________________________________________________________________
const std::string &Disk::model() const {
return _model;
}
const std::string& Disk::model() const { return _model; }
// _____________________________________________________________________________________________________________________
const std::string &Disk::serialNumber() const {
return _serialNumber;
}
const std::string& Disk::serialNumber() const { return _serialNumber; }
// _____________________________________________________________________________________________________________________
int64_t Disk::size_Bytes() const {
return _size_Bytes;
}
int64_t Disk::size_Bytes() const { return _size_Bytes; }
} // namespace hwinfo

View File

@@ -1,9 +1,9 @@
// Copyright Leon Freist
// Author Leon Freist <freist@informatik.uni-freiburg.de>
#include <vector>
#include <string>
#include <regex>
#include <string>
#include <vector>
#if defined(unix) || defined(__unix) || defined(__unix__)
#include "hwinfo/utils/subprocess.h"
@@ -18,13 +18,13 @@
namespace hwinfo {
// _____________________________________________________________________________________________________________________
GPU::GPU(const std::string& vendor, const std::string &name, const std::string &driverVersion, int64_t memory_Bytes)
: _vendor(vendor), _name(name), _driverVersion(driverVersion) {
GPU::GPU(const std::string& vendor, const std::string& name, const std::string& driverVersion, int64_t memory_Bytes)
: _vendor(vendor), _name(name), _driverVersion(driverVersion) {
_memory_Bytes = memory_Bytes;
}
// _____________________________________________________________________________________________________________________
std::string &GPU::vendor() {
std::string& GPU::vendor() {
if (_vendor.empty()) {
_vendor = getVendor();
}
@@ -32,7 +32,7 @@ std::string &GPU::vendor() {
}
// _____________________________________________________________________________________________________________________
std::string &GPU::name() {
std::string& GPU::name() {
if (_name.empty()) {
_name = getName();
}
@@ -40,7 +40,7 @@ std::string &GPU::name() {
}
// _____________________________________________________________________________________________________________________
std::string &GPU::driverVersion() {
std::string& GPU::driverVersion() {
if (_driverVersion.empty()) {
_driverVersion = getDriverVersion();
}

View File

@@ -5,8 +5,8 @@
#ifdef HWINFO_UNIX
#include <fstream>
#include <filesystem>
#include <fstream>
#include "hwinfo/battery.h"
@@ -114,9 +114,7 @@ bool Battery::charging() const {
}
// _____________________________________________________________________________________________________________________
bool Battery::discharging() const {
return !charging();
}
bool Battery::discharging() const { return !charging(); }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________

View File

@@ -5,17 +5,16 @@
#ifdef HWINFO_UNIX
#include <string>
#include <vector>
#include <algorithm>
#include <optional>
#include <fstream>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "hwinfo/cpu.h"
#include "hwinfo/utils/stringutils.h"
#if defined(HWINFO_X86)
#include "hwinfo/cpuid.h"
#endif
@@ -33,7 +32,7 @@ int CPU::currentClockSpeed_kHz() {
stream.close();
try {
return std::stoi(line);
} catch (std::invalid_argument &e) {
} catch (std::invalid_argument& e) {
return -1;
}
}
@@ -42,11 +41,11 @@ int CPU::currentClockSpeed_kHz() {
std::string CPU::getVendor() {
#if defined(HWINFO_X86)
std::string vendor;
uint32_t regs[4] {0};
uint32_t regs[4]{0};
cpuid::cpuid(0, 0, regs);
vendor += std::string((const char *) &regs[1], 4);
vendor += std::string((const char *) &regs[3], 4);
vendor += std::string((const char *) &regs[2], 4);
vendor += std::string((const char*)&regs[1], 4);
vendor += std::string((const char*)&regs[3], 4);
vendor += std::string((const char*)&regs[2], 4);
return vendor;
#else
std::string line;
@@ -57,7 +56,7 @@ std::string CPU::getVendor() {
while (getline(stream, line)) {
if (line.starts_with("vendor_id")) {
stream.close();
return line.substr(line.find(": ")+2, line.length());
return line.substr(line.find(": ") + 2, line.length());
}
}
return "<unknown>";
@@ -69,25 +68,25 @@ std::string CPU::getVendor() {
std::string CPU::getModelName() {
#if defined(HWINFO_X86)
std::string model;
uint32_t regs[4] {};
uint32_t regs[4]{};
for (unsigned i = 0x80000002; i < 0x80000005; ++i) {
cpuid::cpuid(i, 0, regs);
for(auto c : std::string((const char*)&regs[0], 4)) {
for (auto c : std::string((const char*)&regs[0], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[1], 4)) {
for (auto c : std::string((const char*)&regs[1], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[2], 4)) {
for (auto c : std::string((const char*)&regs[2], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[3], 4)) {
for (auto c : std::string((const char*)&regs[3], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
@@ -103,7 +102,7 @@ std::string CPU::getModelName() {
while (getline(stream, line)) {
if (line.starts_with("model name")) {
stream.close();
return line.substr(line.find(": ")+2, line.length());
return line.substr(line.find(": ") + 2, line.length());
}
}
return "<unknown>";
@@ -114,19 +113,19 @@ std::string CPU::getModelName() {
// _____________________________________________________________________________________________________________________
int CPU::getNumPhysicalCores() {
#if defined(HWINFO_X86)
uint32_t regs[4] {};
uint32_t regs[4]{};
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 11) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x01) {
int numCores = getNumLogicalCores()/static_cast<int>(LVL_CORES & regs_2[1]);
int numCores = getNumLogicalCores() / static_cast<int>(LVL_CORES & regs_2[1]);
if (numCores > 0) {
return numCores;
}
@@ -134,9 +133,9 @@ int CPU::getNumPhysicalCores() {
}
} else {
if (HFS >= 4) {
uint32_t regs_3[4] {};
uint32_t regs_3[4]{};
cpuid::cpuid(4, 0, regs_3);
int numCores = getNumLogicalCores()/static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
int numCores = getNumLogicalCores() / static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
if (numCores > 0) {
return numCores;
}
@@ -144,7 +143,7 @@ int CPU::getNumPhysicalCores() {
}
} else if (vendorId.find("AMD") != std::string::npos) {
if (HFS > 0) {
uint32_t regs_4[4] {};
uint32_t regs_4[4]{};
cpuid::cpuid(0x80000000, 0, regs_4);
if (regs_4[0] >= 8) {
int numCores = 1 + (regs_4[2] & 0xff);
@@ -169,14 +168,14 @@ int CPU::getNumPhysicalCores() {
int CPU::getNumLogicalCores() {
#if defined(HWINFO_X86)
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
uint32_t regs[4] {};
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
uint32_t regs[4]{};
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 0xb) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x02) {
@@ -212,7 +211,7 @@ int CPU::getMaxClockSpeed_kHz() {
stream.close();
try {
return std::stoi(line);
} catch (std::invalid_argument &e) {
} catch (std::invalid_argument& e) {
return -1;
}
}
@@ -228,8 +227,8 @@ int CPU::getRegularClockSpeed_kHz() {
if (line.starts_with("cpu MHz")) {
try {
stream.close();
return static_cast<int>(std::stof(line.substr(line.find(": ")+2, line.length()))) * 1000;
} catch (std::invalid_argument &e) {
return static_cast<int>(std::stof(line.substr(line.find(": ") + 2, line.length()))) * 1000;
} catch (std::invalid_argument& e) {
return -1;
}
}
@@ -248,8 +247,8 @@ int CPU::getCacheSize_Bytes() {
if (line.starts_with("cache size")) {
try {
stream.close();
return std::stoi(line.substr(line.find(": ")+2, line.length()-3)) * 1000;
} catch (std::invalid_argument &e) {
return std::stoi(line.substr(line.find(": ") + 2, line.length() - 3)) * 1000;
} catch (std::invalid_argument& e) {
return -1;
}
}
@@ -267,22 +266,23 @@ std::optional<CPU> getCPU(uint8_t socket_id) {
if (!cpuinfo.is_open()) {
return {};
}
std::string file((std::istreambuf_iterator<char>(cpuinfo)),
(std::istreambuf_iterator<char>()));
std::string file((std::istreambuf_iterator<char>(cpuinfo)), (std::istreambuf_iterator<char>()));
cpuinfo.close();
auto cpu_blocks_string = split(file, "\n\n");
std::map<const std::string, const std::string> cpu_block;
bool physical_id_found = false;
for (const auto &block: cpu_blocks_string) {
for (const auto& block : cpu_blocks_string) {
if (physical_id_found) {
break;
}
auto lines = split(block, '\n');
std::map<const std::string, const std::string> cpu_map;
bool add = true;
for (const auto &line: lines) {
for (const auto& line : lines) {
auto pair = split(line, "\t: ");
if (pair.size() != 2) { continue; }
if (pair.size() != 2) {
continue;
}
strip(pair[0]);
strip(pair[1]);
if (pair[0] == "physical id" && pair[1] != std::to_string(socket_id)) {
@@ -337,9 +337,7 @@ Socket::Socket(uint8_t id) : _id(id) {
}
// _____________________________________________________________________________________________________________________
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) {
_cpu = cpu;
}
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) { _cpu = cpu; }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________

View File

@@ -21,7 +21,7 @@ std::vector<Disk> getAllDisks() {
std::string vendor;
std::string model;
std::string serialNumber;
for (const auto& entry: std::filesystem::directory_iterator(base_path)) {
for (const auto& entry : std::filesystem::directory_iterator(base_path)) {
std::string path = entry.path().string() + "/device/";
if (!std::filesystem::exists(std::filesystem::path(path))) {
continue;
@@ -29,20 +29,23 @@ std::vector<Disk> getAllDisks() {
std::ifstream f(path + "vendor");
if (f) {
getline(f, vendor);
} else {
vendor = "<unknown>";
}
else { vendor = "<unknown>"; }
f.close();
f.open(path + "model");
if (f) {
getline(f, model);
} else {
vendor = "<unknown>";
}
else { vendor = "<unknown>"; }
f.close();
f.open(path + "serial");
if (f) {
getline(f, serialNumber);
} else {
serialNumber = "<unknown>";
}
else { serialNumber = "<unknown>"; }
f.close();
strip(vendor);
strip(model);

View File

@@ -1,3 +1,4 @@
// Copyright Leon Freist
// Author Leon Freist <freist@informatik.uni-freiburg.de>
@@ -5,13 +6,12 @@
#ifdef HWINFO_UNIX
#include <vector>
#include <string>
#include <regex>
#include "hwinfo/utils/subprocess.h"
#include <string>
#include <vector>
#include "hwinfo/gpu.h"
#include "hwinfo/utils/subprocess.h"
namespace hwinfo {
@@ -28,12 +28,16 @@ std::string GPU::getVendor() {
char prev = '\0';
bool add = false;
std::string tmp = match[0];
for (auto& c: tmp) {
if (c == '\n') { break; }
for (auto& c : tmp) {
if (c == '\n') {
break;
}
if (add) {
vendor += c;
}
if (prev == ':') { add = true; }
if (prev == ':') {
add = true;
}
prev = c;
}
}
@@ -52,12 +56,16 @@ std::string GPU::getName() {
if (std::regex_search(output.cbegin(), output.cend(), match, matcher)) {
bool add = false;
std::string tmp = match[0];
for (auto& c: tmp) {
if (c == ']') { break; }
for (auto& c : tmp) {
if (c == ']') {
break;
}
if (add) {
name += c;
}
if (c == '[') { add = true; }
if (c == '[') {
add = true;
}
}
}
return name;

View File

@@ -9,13 +9,12 @@
#include "hwinfo/mainboard.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
std::string MainBoard::getVendor() {
std::string manufacturer;
for (const auto& path: _candidates) {
for (const auto& path : _candidates) {
std::string full_path = path + "id/board_vendor";
std::ifstream f(full_path);
if (f) {
@@ -31,7 +30,7 @@ std::string MainBoard::getVendor() {
// _____________________________________________________________________________________________________________________
std::string MainBoard::getName() {
std::string name;
for (const auto& path: _candidates) {
for (const auto& path : _candidates) {
std::string full_path = path + "id/board_name";
std::ifstream f(full_path);
if (f) {
@@ -47,7 +46,7 @@ std::string MainBoard::getName() {
// _____________________________________________________________________________________________________________________
std::string MainBoard::getVersion() {
std::string version;
for (const auto& path: _candidates) {
for (const auto& path : _candidates) {
std::string full_path = path + "id/board_version";
std::ifstream f(full_path);
if (f) {
@@ -63,7 +62,7 @@ std::string MainBoard::getVersion() {
// _____________________________________________________________________________________________________________________
std::string MainBoard::getSerialNumber() {
std::string serialNumber;
for (const auto& path: _candidates) {
for (const auto& path : _candidates) {
std::string full_path = path + "id/board_serial";
std::ifstream f(full_path);
if (f) {

View File

@@ -5,13 +5,13 @@
#ifdef HWINFO_UNIX
#include <sstream>
#include <string>
#include <fstream>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <fstream>
#include <sstream>
#include <string>
#include "hwinfo/os.h"
namespace hwinfo {
@@ -25,9 +25,9 @@ std::string OS::getFullName() {
}
while (getline(stream, line)) {
if (line.starts_with("DISTRIB_DESCRIPTION")) {
line = line.substr(line.find('=')+1, line.length());
line = line.substr(line.find('=') + 1, line.length());
// remove \" at begin and end of the substring result
return {line.begin()+1, line.end()-1};
return {line.begin() + 1, line.end() - 1};
}
}
stream.close();
@@ -43,7 +43,7 @@ std::string OS::getName() {
}
while (getline(stream, line)) {
if (line.starts_with("DISTRIB_ID")) {
return line.substr(line.find('=')+1, line.length());
return line.substr(line.find('=') + 1, line.length());
}
}
stream.close();
@@ -59,7 +59,7 @@ std::string OS::getVersion() {
}
while (getline(stream, line)) {
if (line.starts_with("DISTRIB_RELEASE")) {
return line.substr(line.find('=')+1, line.length());
return line.substr(line.find('=') + 1, line.length());
}
}
stream.close();
@@ -77,7 +77,7 @@ std::string OS::getKernel() {
// _____________________________________________________________________________________________________________________
bool OS::getIs64bit() {
struct stat buffer{};
struct stat buffer {};
return (stat("/lib64/ld-linux-x86-64.so.2", &buffer) == 0);
}

View File

@@ -5,34 +5,26 @@
#ifdef HWINFO_UNIX
#include <unistd.h>
#include <string>
#include <vector>
#include <unistd.h>
#include "hwinfo/ram.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
std::string RAM::getVendor() {
return "<unknown>";
}
std::string RAM::getVendor() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
std::string RAM::getName() {
return "<unknown>";
}
std::string RAM::getName() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
std::string RAM::getModel() {
return "<unknown>";
}
std::string RAM::getModel() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
std::string RAM::getSerialNumber() {
return "<unknown>";
}
std::string RAM::getSerialNumber() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
int64_t RAM::getTotalSize_Bytes() {

View File

@@ -1,22 +1,21 @@
// Copyright (c) Leon Freist <freist@informatik.uni-freiburg.de>
// This software is part of HWBenchmark
#include <fstream>
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/mainboard.h"
#include <fstream>
#include <utility>
#include "hwinfo/WMIwrapper.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
MainBoard::MainBoard(const std::string &vendor,
const std::string &product,
const std::string &version,
const std::string &serialNumber)
: _vendor(vendor), _name(product), _version(version), _serialNumber(serialNumber) {
}
MainBoard::MainBoard(std::string vendor, std::string product, std::string version, std::string serialNumber)
: _vendor(std::move(vendor)),
_name(std::move(product)),
_version(std::move(version)),
_serialNumber(std::move(serialNumber)) {}
// _____________________________________________________________________________________________________________________
std::string& MainBoard::vendor() {

View File

@@ -1,10 +1,10 @@
// Copyright (c) Leon Freist <freist@informatik.uni-freiburg.de>
// This software is part of HWBenchmark
#include <string>
#include "hwinfo/os.h"
#include <string>
namespace hwinfo {
// _____________________________________________________________________________________________________________________
@@ -48,29 +48,19 @@ std::string OS::kernel() {
}
// _____________________________________________________________________________________________________________________
bool OS::is32bit() const {
return _32bit;
}
bool OS::is32bit() const { return _32bit; }
// _____________________________________________________________________________________________________________________
bool OS::is64bit() const {
return _64bit;
}
bool OS::is64bit() const { return _64bit; }
// _____________________________________________________________________________________________________________________
bool OS::isBigEndian() const {
return _bigEndian;
}
bool OS::isBigEndian() const { return _bigEndian; }
// _____________________________________________________________________________________________________________________
bool OS::isLittleEndian() const {
return _littleEndian;
}
bool OS::isLittleEndian() const { return _littleEndian; }
// _____________________________________________________________________________________________________________________
bool OS::getIs32bit() {
return !getIs64bit();
}
bool OS::getIs32bit() { return !getIs64bit(); }
// _____________________________________________________________________________________________________________________
bool OS::getIsBigEndian() {

View File

@@ -10,6 +10,7 @@
#include <sys/sysctl.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#include "hwinfo/WMIwrapper.h"
#endif
#include "hwinfo/ram.h"
@@ -17,17 +18,13 @@
namespace hwinfo {
// _____________________________________________________________________________________________________________________
RAM::RAM(std::string &vendor,
std::string &name,
std::string &model,
std::string &serialNumber,
int64_t size_Bytes)
: _vendor(vendor), _name(name), _model(model), _serialNumber(serialNumber) {
RAM::RAM(std::string& vendor, std::string& name, std::string& model, std::string& serialNumber, int64_t size_Bytes)
: _vendor(vendor), _name(name), _model(model), _serialNumber(serialNumber) {
_totalSize_Bytes = size_Bytes;
}
// _____________________________________________________________________________________________________________________
std::string &RAM::vendor() {
std::string& RAM::vendor() {
if (_vendor.empty()) {
_vendor = getVendor();
}
@@ -35,7 +32,7 @@ std::string &RAM::vendor() {
}
// _____________________________________________________________________________________________________________________
std::string &RAM::name() {
std::string& RAM::name() {
if (_name.empty()) {
_name = getName();
}
@@ -43,7 +40,7 @@ std::string &RAM::name() {
}
// _____________________________________________________________________________________________________________________
std::string &RAM::model() {
std::string& RAM::model() {
if (_model.empty()) {
_model = getModel();
}
@@ -51,7 +48,7 @@ std::string &RAM::model() {
}
// _____________________________________________________________________________________________________________________
std::string &RAM::serialNumber() {
std::string& RAM::serialNumber() {
if (_serialNumber.empty()) {
_serialNumber = getSerialNumber();
}

View File

@@ -2,69 +2,56 @@
// Author Leon Freist <freist@informatik.uni-freiburg.de>
#include <iostream>
#include "hwinfo/platform.h"
#ifdef HWINFO_WINDOWS
#include "hwinfo/battery.h"
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/battery.h"
namespace hwinfo {
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
std::string Battery::getVendor() const {
return "<unknwon>";
}
std::string Battery::getVendor() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
std::string Battery::getModel() const {
return _model;
}
std::string Battery::getModel() const { return _model; }
// _____________________________________________________________________________________________________________________
std::string Battery::getSerialNumber() const {
return "<unknwon>";
}
std::string Battery::getSerialNumber() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
std::string Battery::getTechnology() const {
return "<unknwon>";
}
std::string Battery::getTechnology() const { return "<unknwon>"; }
// _____________________________________________________________________________________________________________________
uint32_t Battery::getEnergyFull() const {
return 0;
}
uint32_t Battery::getEnergyFull() const { return 0; }
// _____________________________________________________________________________________________________________________
uint32_t Battery::energyNow() const {
return 0;
}
uint32_t Battery::energyNow() const { return 0; }
// _____________________________________________________________________________________________________________________
bool Battery::charging() const {
return false;
}
bool Battery::charging() const { return false; }
// _____________________________________________________________________________________________________________________
bool Battery::discharging() const {
return false;
}
bool Battery::discharging() const { return false; }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
std::vector<Battery> getAllBatteries() {
std::vector<Battery> batteries;
std::vector<const wchar_t*> res {};
std::vector<const wchar_t*> res{};
wmi::queryWMI("Win32_Battery", "Name", res);
if (res.empty() || res.at(0) == nullptr) { return {}; }
if (res.empty() || res.at(0) == nullptr) {
return {};
}
std::cout << res.size() << std::endl;
int8_t counter = 0;
for (const auto &v: res) {
for (const auto& v : res) {
std::wstring tmp(v);
batteries.emplace_back(counter++);
batteries.back()._model = { tmp.begin(), tmp.end() };
batteries.back()._model = {tmp.begin(), tmp.end()};
}
res.clear();
return batteries;

View File

@@ -5,38 +5,38 @@
#ifdef HWINFO_WINDOWS
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/cpu.h"
#include "hwinfo/cpuid.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
int CPU::currentClockSpeed_kHz() {
std::vector<int64_t> speed {};
std::vector<int64_t> speed{};
wmi::queryWMI("Win32_Processor", "CurrentClockSpeed", speed);
if (speed.empty()) { return -1; }
if (speed.empty()) {
return -1;
}
return speed[0];
}
// _____________________________________________________________________________________________________________________
std::string CPU::getVendor() {
std::vector<const wchar_t*> vendor {};
std::vector<const wchar_t*> vendor{};
wmi::queryWMI("Win32_Processor", "Manufacturer", vendor);
if (vendor.empty()) {
#if defined(HWINFO_X86)
std::string v;
uint32_t regs[4] {0};
uint32_t regs[4]{0};
cpuid::cpuid(0, 0, regs);
v += std::string((const char *) &regs[1], 4);
v += std::string((const char *) &regs[3], 4);
v += std::string((const char *) &regs[2], 4);
v += std::string((const char*)&regs[1], 4);
v += std::string((const char*)&regs[3], 4);
v += std::string((const char*)&regs[2], 4);
return v;
#else
return "<unknown>";
@@ -49,30 +49,30 @@ std::string CPU::getVendor() {
// _____________________________________________________________________________________________________________________
std::string CPU::getModelName() {
std::vector<const wchar_t*> vendor {};
std::vector<const wchar_t*> vendor{};
wmi::queryWMI("Win32_Processor", "Name", vendor);
if (vendor.empty()) {
#if defined(HWINFO_X86)
std::string model;
uint32_t regs[4] {};
uint32_t regs[4]{};
for (unsigned i = 0x80000002; i < 0x80000005; ++i) {
cpuid::cpuid(i, 0, regs);
for(auto c : std::string((const char*)&regs[0], 4)) {
for (auto c : std::string((const char*)&regs[0], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[1], 4)) {
for (auto c : std::string((const char*)&regs[1], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[2], 4)) {
for (auto c : std::string((const char*)&regs[2], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
}
for(auto c : std::string((const char*)&regs[3], 4)) {
for (auto c : std::string((const char*)&regs[3], 4)) {
if (std::isalnum(c) || c == '(' || c == ')' || c == '@' || c == ' ' || c == '-' || c == '.') {
model += c;
}
@@ -89,23 +89,23 @@ std::string CPU::getModelName() {
// _____________________________________________________________________________________________________________________
int CPU::getNumPhysicalCores() {
std::vector<int> cores {};
std::vector<int> cores{};
wmi::queryWMI("Win32_Processor", "NumberOfCores", cores);
if (cores.empty()) {
#if defined(HWINFO_X86)
uint32_t regs[4] {};
uint32_t regs[4]{};
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 11) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x01) {
int numCores = getNumLogicalCores()/static_cast<int>(LVL_CORES & regs_2[1]);
int numCores = getNumLogicalCores() / static_cast<int>(LVL_CORES & regs_2[1]);
if (numCores > 0) {
return numCores;
}
@@ -113,9 +113,9 @@ int CPU::getNumPhysicalCores() {
}
} else {
if (HFS >= 4) {
uint32_t regs_3[4] {};
uint32_t regs_3[4]{};
cpuid::cpuid(4, 0, regs_3);
int numCores = getNumLogicalCores()/static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
int numCores = getNumLogicalCores() / static_cast<int>(1 + ((regs_3[0] >> 26) & 0x3f));
if (numCores > 0) {
return numCores;
}
@@ -123,7 +123,7 @@ int CPU::getNumPhysicalCores() {
}
} else if (vendorId.find("AMD") != std::string::npos) {
if (HFS > 0) {
uint32_t regs_4[4] {};
uint32_t regs_4[4]{};
cpuid::cpuid(0x80000000, 0, regs_4);
if (regs_4[0] >= 8) {
int numCores = 1 + (regs_4[2] & 0xff);
@@ -141,19 +141,19 @@ int CPU::getNumPhysicalCores() {
// _____________________________________________________________________________________________________________________
int CPU::getNumLogicalCores() {
std::vector<int> cores {};
std::vector<int> cores{};
wmi::queryWMI("Win32_Processor", "NumberOfLogicalProcessors", cores);
if (cores.empty()) {
#if defined(HWINFO_X86)
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char &in) { in = ::toupper(in); } );
uint32_t regs[4] {};
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
uint32_t regs[4]{};
cpuid::cpuid(0, 0, regs);
uint32_t HFS = regs[0];
if (vendorId.find("INTEL") != std::string::npos) {
if (HFS >= 0xb) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
uint32_t regs_2[4] {};
uint32_t regs_2[4]{};
cpuid::cpuid(0x0b, lvl, regs_2);
uint32_t currLevel = (LVL_TYPE & regs_2[2]) >> 8;
if (currLevel == 0x02) {
@@ -178,35 +178,38 @@ int CPU::getNumLogicalCores() {
// _____________________________________________________________________________________________________________________
int CPU::getMaxClockSpeed_kHz() {
std::vector<int64_t> speed {};
std::vector<int64_t> speed{};
wmi::queryWMI("Win32_Processor", "MaxClockSpeed", speed);
if (speed.empty()) { return -1; }
if (speed.empty()) {
return -1;
}
return speed[0] * 1000;
}
// _____________________________________________________________________________________________________________________
int CPU::getRegularClockSpeed_kHz() {
std::vector<int64_t> speed {};
std::vector<int64_t> speed{};
wmi::queryWMI("Win32_Processor", "CurrentClockSpeed", speed);
if (speed.empty()) { return -1; }
if (speed.empty()) {
return -1;
}
return speed[0] * 1000;
}
int CPU::getCacheSize_Bytes() {
std::vector<int64_t> cacheSize {};
std::vector<int64_t> cacheSize{};
wmi::queryWMI("Win32_Processor", "L3CacheSize", cacheSize);
if (cacheSize.empty()) { return -1; }
if (cacheSize.empty()) {
return -1;
}
return cacheSize[0];
}
// =====================================================================================================================
// _____________________________________________________________________________________________________________________
// Helper function for linux: parses /proc/cpuinfo. socket_id == physical_id.
// _____________________________________________________________________________________________________________________
std::optional<CPU> getCPU(uint8_t socket_id) {
return {};
}
std::optional<CPU> getCPU(uint8_t socket_id) { return {}; }
// ===== Socket ========================================================================================================
// _____________________________________________________________________________________________________________________
@@ -218,9 +221,7 @@ Socket::Socket(uint8_t id) : _id(id) {
}
// _____________________________________________________________________________________________________________________
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) {
_cpu = cpu;
}
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) { _cpu = cpu; }
// =====================================================================================================================
// _____________________________________________________________________________________________________________________

View File

@@ -7,22 +7,24 @@
#include <filesystem>
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/disk.h"
#include "hwinfo/utils/stringutils.h"
#include "hwinfo/WMIwrapper.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________
std::vector<Disk> getAllDisks() {
std::vector<Disk> disks;
std::vector<const wchar_t*> res {};
std::vector<const wchar_t*> res{};
wmi::queryWMI("Win32_DiskDrive", "Manufacturer", res);
if (res.empty()) { return {}; }
for (const auto &v: res) {
if (res.empty()) {
return {};
}
for (const auto& v : res) {
std::wstring tmp(v);
disks.push_back(Disk());
disks.back()._vendor = { tmp.begin(), tmp.end() };
disks.back()._vendor = {tmp.begin(), tmp.end()};
}
res.clear();
wmi::queryWMI("Win32_DiskDrive", "Model", res);
@@ -31,7 +33,7 @@ std::vector<Disk> getAllDisks() {
break;
}
std::wstring tmp(res[i]);
disks[i]._model = { tmp.begin(), tmp.end() };
disks[i]._model = {tmp.begin(), tmp.end()};
}
res.clear();
wmi::queryWMI("Win32_DiskDrive", "SerialNumber", res);
@@ -40,7 +42,7 @@ std::vector<Disk> getAllDisks() {
break;
}
std::wstring tmp(res[i]);
disks[i]._serialNumber = { tmp.begin(), tmp.end() };
disks[i]._serialNumber = {tmp.begin(), tmp.end()};
}
std::vector<uint64_t> res2;
// this returns a random same number for all disks...

View File

@@ -5,8 +5,8 @@
#ifdef HWINFO_WINDOWS
#include <vector>
#include <string>
#include <vector>
#include "hwinfo/WMIwrapper.h"
#pragma comment(lib, "wbemuuid.lib")

View File

@@ -10,7 +10,6 @@
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/mainboard.h"
namespace hwinfo {
// _____________________________________________________________________________________________________________________

View File

@@ -5,11 +5,11 @@
#ifdef HWINFO_WINDOWS
#include <sstream>
#include <string>
#include <Windows.h>
#include <winternl.h>
#include <sstream>
#include <string>
#define STATUS_SUCCESS 0x00000000
#include "hwinfo/os.h"
@@ -18,9 +18,15 @@ namespace hwinfo {
// _____________________________________________________________________________________________________________________
std::string OS::getFullName() {
static NTSTATUS(__stdcall *RtlGetVersion)(OUT PRTL_OSVERSIONINFOEXW lpVersionInformation) = (NTSTATUS(__stdcall*)(PRTL_OSVERSIONINFOEXW))GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlGetVersion");
static void(__stdcall *GetNativeSystemInfo)(OUT LPSYSTEM_INFO lpSystemInfo) = (void(__stdcall*)(LPSYSTEM_INFO))GetProcAddress(GetModuleHandle("kernel32.dll"), "GetNativeSystemInfo");
static BOOL(__stdcall *GetProductInfo)(IN DWORD dwOSMajorVersion, IN DWORD dwOSMinorVersion, IN DWORD dwSpMajorVersion, IN DWORD dwSpMinorVersion, OUT PDWORD pdwReturnedProductType) = (BOOL(__stdcall*)(DWORD, DWORD, DWORD, DWORD, PDWORD))GetProcAddress(GetModuleHandle("kernel32.dll"), "GetProductInfo");
static NTSTATUS(__stdcall * RtlGetVersion)(OUT PRTL_OSVERSIONINFOEXW lpVersionInformation) =
(NTSTATUS(__stdcall*)(PRTL_OSVERSIONINFOEXW))GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlGetVersion");
static void(__stdcall * GetNativeSystemInfo)(OUT LPSYSTEM_INFO lpSystemInfo) =
(void(__stdcall*)(LPSYSTEM_INFO))GetProcAddress(GetModuleHandle("kernel32.dll"), "GetNativeSystemInfo");
static BOOL(__stdcall * GetProductInfo)(IN DWORD dwOSMajorVersion, IN DWORD dwOSMinorVersion,
IN DWORD dwSpMajorVersion, IN DWORD dwSpMinorVersion,
OUT PDWORD pdwReturnedProductType) =
(BOOL(__stdcall*)(DWORD, DWORD, DWORD, DWORD, PDWORD))GetProcAddress(GetModuleHandle("kernel32.dll"),
"GetProductInfo");
OSVERSIONINFOEXW osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXW));
@@ -34,7 +40,7 @@ std::string OS::getFullName() {
} else {
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4996) // C4996: 'function': was declared deprecated
#pragma warning(disable : 4996) // C4996: 'function': was declared deprecated
#endif
BOOL bOsVersionInfoEx = GetVersionExW((OSVERSIONINFOW*)&osvi);
if (bOsVersionInfoEx == 0) {
@@ -83,8 +89,7 @@ std::string OS::getFullName() {
if (osvi.dwMinorVersion == 3) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
os << "Windows 8.1 ";
}
else {
} else {
os << "Windows Server 2012 R2 ";
}
} else if (osvi.dwMinorVersion == 2) {
@@ -177,17 +182,18 @@ std::string OS::getFullName() {
} else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER) {
os << "Windows Home Server";
} else if ((osvi.wProductType == VER_NT_WORKSTATION) &&
(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
os << "Windows XP Professional x64 Edition";
} else {
os << "Windows Server 2003, ";
} if (osvi.wProductType != VER_NT_WORKSTATION) {
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
if (osvi.wSuiteMask & VER_SUITE_DATACENTER) {
os << "Datacenter Edition for Itanium-based Systems";
} else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) {
os << "Enterprise Edition for Itanium-based Systems";
}
}
if (osvi.wProductType != VER_NT_WORKSTATION) {
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
if (osvi.wSuiteMask & VER_SUITE_DATACENTER) {
os << "Datacenter Edition for Itanium-based Systems";
} else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) {
os << "Enterprise Edition for Itanium-based Systems";
}
} else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
if (osvi.wSuiteMask & VER_SUITE_DATACENTER) {
os << "Datacenter x64 Edition";
@@ -236,19 +242,13 @@ std::string OS::getFullName() {
}
// _____________________________________________________________________________________________________________________
std::string OS::getName() {
return "Windows";
}
std::string OS::getName() { return "Windows"; }
// _____________________________________________________________________________________________________________________
std::string OS::getVersion() {
return "<unknown>";
}
std::string OS::getVersion() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
std::string OS::getKernel() {
return "<unknown>";
}
std::string OS::getKernel() { return "<unknown>"; }
// _____________________________________________________________________________________________________________________
bool OS::getIs64bit() {

View File

@@ -5,11 +5,11 @@
#ifdef HWINFO_WINDOWS
#include <Windows.h>
#include <string>
#include <vector>
#include <Windows.h>
#include "hwinfo/WMIwrapper.h"
#include "hwinfo/ram.h"