Skip to content

Commit

Permalink
Fix build on MacOS (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shieldine committed Jun 13, 2024
1 parent 3d234f4 commit 845649e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 159 deletions.
127 changes: 68 additions & 59 deletions src/apple/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,55 @@

#include <mach/mach.h>
#include <mach/mach_time.h>
#include <math.h>
#include <pthread.h>
#include <sys/sysctl.h>

#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

#include "hwinfo/cpu.h"
#include "hwinfo/cpuid.h"
#include "hwinfo/utils/stringutils.h"

namespace hwinfo {

// _____________________________________________________________________________________________________________________
int CPU::currentClockSpeed_kHz() {
int64_t getMaxClockSpeed_MHz(const int& core_id) {
long speed = 0;
size_t speed_size = sizeof(speed);
if (sysctlbyname("hw.cpufrequency", &speed, &speed_size, nullptr, 0) != 0) {
speed = -1;
}

// TODO: its khz, make mhz
return static_cast<int>(speed);
}

// _____________________________________________________________________________________________________________________
int64_t getRegularClockSpeed_MHz(const int& core_id) {
uint64_t frequency = 0;
size_t size = sizeof(frequency);
if (sysctlbyname("hw.cpufrequency", &frequency, &size, nullptr, 0) == 0) {
return static_cast<int>(frequency);
}

// TODO: its khz, make mhz
return -1;
}

// _____________________________________________________________________________________________________________________
int64_t getMinClockSpeed_MHz(const int& core_id) {
// TODO: implement
return -1;
}

// _____________________________________________________________________________________________________________________
std::string CPU::getVendor() {
std::vector<int64_t> CPU::currentClockSpeed_MHz() const {
// TODO: implement
std::vector<int64_t> res;
return res;
}

// _____________________________________________________________________________________________________________________
std::string getVendor() {
#if defined(HWINFO_X86)
std::string vendor;
uint32_t regs[4]{0};
Expand All @@ -44,7 +71,26 @@ std::string CPU::getVendor() {
}

// _____________________________________________________________________________________________________________________
std::string CPU::getModelName() {
double CPU::currentUtilisation() const {
// TODO: implement
return -1;
}

// _____________________________________________________________________________________________________________________
double CPU::threadUtilisation(int thread_index) const {
// TODO: implement
return -1;
}

// _____________________________________________________________________________________________________________________
std::vector<double> CPU::threadsUtilisation() const {
std::vector<double> thread_utiliy;
// TODO: implement
return thread_utiliy;
}

// _____________________________________________________________________________________________________________________
std::string getModelName() {
#if defined(HWINFO_X86)
std::string model;
uint32_t regs[4]{};
Expand Down Expand Up @@ -76,7 +122,7 @@ std::string CPU::getModelName() {
size_t size = 1024;
std::string model;
model.resize(size);
if (sysctlbyname("machdep.cpu.brand_string", model.data(), &size, NULL, 0) < 0) {
if (sysctlbyname("machdep.cpu.brand_string", model.data(), &size, nullptr, 0) < 0) {
model.resize(size);
return model;
}
Expand All @@ -85,7 +131,7 @@ std::string CPU::getModelName() {
}

// _____________________________________________________________________________________________________________________
int CPU::getNumPhysicalCores() {
int getNumPhysicalCores() {
#if defined(HWINFO_X86)
uint32_t regs[4]{};
std::string vendorId = getVendor();
Expand Down Expand Up @@ -139,7 +185,7 @@ int CPU::getNumPhysicalCores() {
}

// _____________________________________________________________________________________________________________________
int CPU::getNumLogicalCores() {
int getNumLogicalCores() {
#if defined(HWINFO_X86)
std::string vendorId = getVendor();
std::for_each(vendorId.begin(), vendorId.end(), [](char& in) { in = ::toupper(in); });
Expand Down Expand Up @@ -176,59 +222,22 @@ int CPU::getNumLogicalCores() {
}

// _____________________________________________________________________________________________________________________
int CPU::getMaxClockSpeed_kHz() {
long speed = 0;
size_t speed_size = sizeof(speed);
if (sysctlbyname("hw.cpufrequency", &speed, &speed_size, nullptr, 0) != 0) {
speed = -1;
}
return static_cast<int>(speed);
}
std::vector<CPU> getAllCPUs() {
std::vector<CPU> cpus;
CPU cpu;

// _____________________________________________________________________________________________________________________
int CPU::getRegularClockSpeed_kHz() {
uint64_t frequency = 0;
size_t size = sizeof(frequency);
if (sysctlbyname("hw.cpufrequency", &frequency, &size, nullptr, 0) == 0) {
return static_cast<int>(frequency);
}
return -1;
}

int CPU::getCacheSize_Bytes() { return -1; }

double CPU::currentUtility_Percentage() const { return -1.0; }

double CPU::currentThreadUtility_Percentage(const int& thread_index) const { return -1.0; }

std::vector<double> CPU::currentThreadsUtility_Percentage_MainThread() const { return std::vector<double>(); }

// double CPU::currentTemperature_Celsius() const {
// return -1.0;
// }
cpu._vendor = getVendor();
cpu._modelName = getModelName();
cpu._numPhysicalCores = getNumPhysicalCores();
cpu._numLogicalCores = getNumLogicalCores();
cpu._maxClockSpeed_MHz = getMaxClockSpeed_MHz(0);
cpu._regularClockSpeed_MHz = getRegularClockSpeed_MHz(0);

// =====================================================================================================================
// _____________________________________________________________________________________________________________________
// Helper function for linux: parses /proc/cpuinfo. socket_id == physical_id.
// _____________________________________________________________________________________________________________________
std::unique_ptr<CPU> getCPU(uint8_t socket_id) { return {}; }
cpus.push_back(cpu);

// ===== Socket ========================================================================================================
// _____________________________________________________________________________________________________________________
Socket::Socket(uint8_t id) : _id(id) {
auto cpu = getCPU(_id);
if (cpu != nullptr) {
_cpu = *cpu;
}
return cpus;
}

// _____________________________________________________________________________________________________________________
Socket::Socket(uint8_t id, const class CPU& cpu) : _id(id) { _cpu = cpu; }

// =====================================================================================================================
// _____________________________________________________________________________________________________________________
std::vector<Socket> getAllSockets() { return {}; }

} // namespace hwinfo

#endif // HWINFO_APPLE
25 changes: 3 additions & 22 deletions src/apple/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "hwinfo/platform.h"

#ifdef HWINFO_APPLE
#ifndef USE_OCL

#include <regex>
#include <string>
Expand All @@ -15,30 +14,12 @@
namespace hwinfo {

// _____________________________________________________________________________________________________________________
std::string GPU::getVendor() {
std::vector<GPU> getAllGPUs() {
std::vector<GPU> gpus{};
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string GPU::getName() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string GPU::getDriverVersion() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
int64_t GPU::getMemory_Bytes() {
// TODO: implement
return -1;
return gpus;
}

} // namespace hwinfo

#endif // USE_OCL
#endif // HWINFO_APPLE
26 changes: 5 additions & 21 deletions src/apple/mainboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@
namespace hwinfo {

// _____________________________________________________________________________________________________________________
std::string MainBoard::getVendor() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string MainBoard::getName() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string MainBoard::getVersion() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string MainBoard::getSerialNumber() {
// TODO: implement
return "<unknown>";
MainBoard::MainBoard() {
_vendor = "<unknown>";
_name = "<unknown>";
_version = "<unknown>";
_serialNumber = "<unknown>";
}

} // namespace hwinfo
Expand Down
43 changes: 13 additions & 30 deletions src/apple/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,32 @@
namespace hwinfo {

// _____________________________________________________________________________________________________________________
std::string OS::getFullName() {
OS::OS() {
size_t size = 1024;
std::string os_name;
os_name.resize(size);
if (sysctlbyname("kern.os", (void*)(os_name.data()), &size, nullptr, 0) == 0) {
os_name.resize(size); // trim the string to the actual size
return os_name;
}
return "macOS <unknown version>";
}

// _____________________________________________________________________________________________________________________
std::string OS::getName() {
size_t size = 1024;
std::string os_name;
os_name.resize(size);
if (sysctlbyname("kern.os", (void*)(os_name.data()), &size, nullptr, 0) == 0) {
if (sysctlbyname("kern.ostype", os_name.data(), &size, nullptr, 0) == 0) {
os_name.resize(size); // trim the string to the actual size
return os_name;
_name = os_name;
} else {
_name = "macOS";
}
return "macOS";
}

// _____________________________________________________________________________________________________________________
std::string OS::getVersion() {
size_t size = 1024;
std::string os_version;
os_version.resize(size);
if (sysctlbyname("kern.osrelease", (void*)(os_version.data()), &size, nullptr, 0) == 0) {
if (sysctlbyname("kern.osrelease", os_version.data(), &size, nullptr, 0) == 0) {
os_version.resize(size); // trim the string to the actual size
return os_version;
_version = os_version;
} else {
_version = "<unknown>";
}
return "<unknown version>";
}

// _____________________________________________________________________________________________________________________
std::string OS::getKernel() {
// TODO: implement
return "<unknown>";
}
_kernel = "<unknown>";

// _____________________________________________________________________________________________________________________
bool OS::getIs64bit() { return true; }
_64bit = true;
_32bit = !_64bit;
}

} // namespace hwinfo

Expand Down
42 changes: 15 additions & 27 deletions src/apple/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,39 @@

#ifdef HWINFO_APPLE

#include <hwinfo/ram.h>
#include <sys/sysctl.h>

#include <string>
#include <vector>

#include "hwinfo/ram.h"

namespace hwinfo {

// _____________________________________________________________________________________________________________________
std::string RAM::getVendor() {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
std::string RAM::getName() {
// TODO: implement
return "<unknown>";
Memory::Memory() {
// TODO: get information for actual memory modules (DIMM)
Module module;
module.vendor = "<unknown>";
module.name = "<unknown>";
module.serial_number = "<unknown>";
module.model = "<unknown>";
module.id = 0;
module.total_Bytes = -1;
module.frequency_Hz = -1;
_modules.push_back(module);
}

// _____________________________________________________________________________________________________________________
std::string RAM::getModel() {
int64_t Memory::free_Bytes() const {
// TODO: implement
return "<unknown>";
return -1;
}

// _____________________________________________________________________________________________________________________
std::string RAM::getSerialNumber() {
int64_t Memory::available_Bytes() const {
// TODO: implement
return "<unknown>";
}

// _____________________________________________________________________________________________________________________
int64_t RAM::getTotalSize_Bytes() {
int64_t memsize = 0;
size_t size = sizeof(memsize);
if (sysctlbyname("hw.memsize", &memsize, &size, nullptr, 0) == 0) {
return memsize;
}
return -1;
}

int64_t RAM::getAvailableMemory() { return -1; }

} // namespace hwinfo

#endif // HWINFO_APPLE

0 comments on commit 845649e

Please sign in to comment.