Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev -> main #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ int main(int argc, char** argv) {
std::cout << "----------------------------------- CPU -----------------------------------" << std::endl;
std::cout << "CPU model:\t\t\t" << cpu.modelName() << std::endl;
std::cout << "CPU vendor:\t\t\t" << cpu.vendor() << std::endl;
std::cout << "CPU physical cores:\t" << cpu.numCores() << std::endl;
std::cout << "CPU physical cores:\t" << cpu.numPhysicalCores() << std::endl;
std::cout << "CPU logical cores:\t" << cpu.numLogicalCores() << std::endl;
std::cout << "CPU max clock s.:\t" << cpu.maxClockSpeedMHz() << std::endl;
std::cout << "CPU clock speed:\t"<< cpu.regularClockSpeedMHz() << std::endl;
std::cout << "CPU cache size:\t\t" << cpu.cacheSize() << std::endl;
std::cout << "CPU cache size:\t\t" << cpu.cacheSizeBytes() << std::endl;

hwinfo::OS os;
std::cout << "----------------------------------- OS ------------------------------------" << std::endl;
Expand Down
45 changes: 43 additions & 2 deletions include/hwinfo/WMIwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
namespace hwinfo::wmi {

template<typename T>
bool queryWMI(const std::string& WMIClass, std::string field, std::vector<T> &value, const std::string& serverName = "ROOT\\CIMV2") {
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;
Expand Down Expand Up @@ -108,7 +111,7 @@ bool queryWMI(const std::string& WMIClass, std::string field, std::vector<T> &va
} else if (std::is_same<T, unsigned long long>::value) {
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);
Expand All @@ -126,7 +129,45 @@ bool queryWMI(const std::string& WMIClass, std::string field, std::vector<T> &va
return true;
}

template<typename T>
T *AdvanceBytes(T *p, SIZE_T cb) {
return reinterpret_cast<T *>(reinterpret_cast<BYTE *>(p) + cb);
}

class EnumLogicalProcessorInformation {
public:
explicit EnumLogicalProcessorInformation(LOGICAL_PROCESSOR_RELATIONSHIP Relationship)
: m_pinfoBase(nullptr), m_pinfoCurrent(nullptr), m_cbRemaining(0) {
DWORD cb = 0;
if (GetLogicalProcessorInformationEx(Relationship, nullptr, &cb)) return;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
m_pinfoBase = reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>
(LocalAlloc(LMEM_FIXED, cb));
if (!m_pinfoBase) return;
if (!GetLogicalProcessorInformationEx(Relationship, m_pinfoBase, &cb)) return;
m_pinfoCurrent = m_pinfoBase;
m_cbRemaining = cb;
}
~EnumLogicalProcessorInformation() { LocalFree(m_pinfoBase); }
void MoveNext() {
if (m_pinfoCurrent) {
m_cbRemaining -= m_pinfoCurrent->Size;
if (m_cbRemaining) {
m_pinfoCurrent = AdvanceBytes(m_pinfoCurrent, m_pinfoCurrent->Size);
} else {
m_pinfoCurrent = nullptr;
}
}
}
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *Current() { return m_pinfoCurrent; }
private:
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoBase;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoCurrent;
DWORD m_cbRemaining;
};

} // namespace hwinfo::wmi

#else
#error "This part of the software is Windows specific"
#endif
Expand Down
24 changes: 6 additions & 18 deletions include/hwinfo/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class CPU {

std::string modelName() { return _modelName; }
std::string vendor() { return _vendor; }
[[nodiscard]] int cacheSize() const { return _cacheSize; }
[[nodiscard]] int numCores() const { return _numCores; }
[[nodiscard]] int cacheSizeBytes() const { return _cacheSizeBytes; }
[[nodiscard]] int numPhysicalCores() const { return _numPhysicalCores; }
[[nodiscard]] int numLogicalCores() const { return _numLogicalCores; }
[[nodiscard]] int maxClockSpeedMHz() const { return _maxClockSpeedMHz; }
[[nodiscard]] int regularClockSpeedMHz() const { return _regularClockSpeedMHz; }
Expand All @@ -38,22 +38,22 @@ class CPU {

static std::string getModelName();
static std::string getVendor();
static int getNumCores();
static int getNumPhysicalCores();
static int getNumLogicalCores();
static int getMaxClockSpeedMHz();
static int getRegularClockSpeedMHz();
static int getMinClockSpeedMHz();
static int getCacheSize();
static int getCacheSizeBytes();
private:

std::string _modelName;
int _numCores = -1;
int _numPhysicalCores = -1;
int _numLogicalCores = -1;
int _maxClockSpeedMHz = -1;
int _regularClockSpeedMHz = -1;
int _minClockSpeedMHz = -1;
std::string _vendor;
int _cacheSize = -1;
int _cacheSizeBytes = -1;
int _numSMT = -1;
bool _isHTT = false;
bool _isSSE = false;
Expand All @@ -63,18 +63,6 @@ class CPU {
bool _isSSE42 = false;
bool _isAVX = false;
bool _isAVX2 = false;

// bit positions for data extraction
static const uint32_t SSE_POS = 0x02000000;
static const uint32_t SSE2_POS = 0x04000000;
static const uint32_t SSE3_POS = 0x00000001;
static const uint32_t SSE41_POS = 0x00080000;
static const uint32_t SSE42_POS = 0x00100000;
static const uint32_t AVX_POS = 0x10000000;
static const uint32_t AVX2_POS = 0x00000020;
static const uint32_t LVL_NUM = 0x000000ff;
static const uint32_t LVL_TYPE = 0x0000ff00;
static const uint32_t LVL_CORES = 0x0000ffff;
};

} // namespace hwinfo
Expand Down
49 changes: 31 additions & 18 deletions include/hwinfo/cpuid.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
// Copyright Leon Freist
// Author Leon Freist <[email protected]>

#if defined(__x86_64__) || defined(__x86_64) || defined(_M_IX86)

#ifndef HWINFO_CPUID_H_
#define HWINFO_CPUID_H_

namespace hwinfo {
#pragma once

#include <string>

#define MAX_INTEL_TOP_LVL 4

class CPUID {
public:
explicit CPUID(unsigned funcId, unsigned subFuncId) {
#define SSE_POS 0x02000000
#define SSE2_POS 0x04000000
#define SSE3_POS 0x00000001
#define SSE41_POS 0x00080000
#define SSE42_POS 0x00100000
#define AVX_POS 0x10000000
#define AVX2_POS 0x00000020
#define LVL_NUM 0x000000ff
#define LVL_TYPE 0x0000ff00
#define LVL_CORES 0x0000ffff

namespace hwinfo::cpuid {

/**
* cpuid wrapper for Linux, MacOS and Windows
* @param func_id
* @param sub_func_id
* @param regs
*/
void cpuid(unsigned func_id, unsigned sub_func_id, uint32_t regs[4]) {
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
__cpuidex((int*) _regs, (int) funcId, (int) subFuncId);
__cpuidex((int*) regs, static_cast<int>(func_id), static_cast<int>(sub_func_id));
#else
asm volatile ("cpuid" :"=a" (_regs[0]), "=b" (_regs[1]), "=c" (_regs[2]), "=d" (_regs[3]) : "a" (funcId), "c" (subFuncId));
asm volatile ("cpuid" :"=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) : "a" (func_id), "c" (sub_func_id));
#endif
}

[[nodiscard]] const uint32_t &EAX() const { return _regs[0]; }
[[nodiscard]] const uint32_t &EBX() const { return _regs[1]; }
[[nodiscard]] const uint32_t &ECX() const {return _regs[2]; }
[[nodiscard]] const uint32_t &EDX() const {return _regs[3]; }

private:
uint32_t _regs[4]{};
};
}

} // hwinfo
} // namespace hwinfo::cpuid

#endif //HWINFO_CPUID_H_
#endif //HWINFO_CPUID_H_
#endif // check x86 arch
Loading