adding cpu and memory as well as json serialization with the 3rd party library

This commit is contained in:
amerhifi 2019-05-03 15:57:45 -07:00
parent 2762597e6a
commit 527cf8b3d3
7 changed files with 1082 additions and 1025 deletions

File diff suppressed because it is too large Load diff

View file

@ -48,7 +48,6 @@
#include <ThreadSafeValueCache.h>
#include <shared/ConicalViewFrustum.h>
#include <shared/FileLogger.h>
#include <RunningMarker.h>
#include "avatar/MyAvatar.h"
@ -148,7 +147,7 @@ public:
// Return an HTTP User-Agent string with OS and device information.
Q_INVOKABLE QString getUserAgent();
void initializePlatform();
void initializeGL();
void initializeDisplayPlugins();
void initializeRenderEngine();
@ -174,7 +173,6 @@ public:
void raise();
void showCursor(const Cursor::Icon& cursor);
void InitializePlatform();
bool isThrottleRendering() const;

View file

@ -1,3 +1,5 @@
set(TARGET_NAME platform)
setup_hifi_library()
link_hifi_libraries(shared)
link_hifi_libraries(shared)
target_json()

View file

@ -7,17 +7,87 @@
//
#include "WINPlatform.h"
#include <intrin.h>
#include <Windows.h>
#include <thread>
using namespace platform;
using namespace nlohmann;
bool WINInstance::enumerateProcessors() {
cpu cpu;
getCpuDetails(cpu);
cpu.numberOfCores = getNumLogicalCores();
_processors.push_back(cpu);
_memory.totalMb = getTotalSystemRamMb();
return true;
}
void WINInstance::getCpuDetails(cpu &cpu) {
int CPUInfo[4] = { -1 };
unsigned nExIds;
unsigned int i = 0;
char CPUBrandString[16];
char CPUModelString[16];
char CPUClockString[16];
// Get the information associated with each extended ID.
__cpuid(CPUInfo, 0x80000000);
nExIds = CPUInfo[0];
for (i = 0x80000000; i <= nExIds; ++i) {
__cpuid(CPUInfo, i);
// Interpret CPU brand string
if (i == 0x80000002) {
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
} else if (i == 0x80000003) {
memcpy(CPUModelString, CPUInfo, sizeof(CPUInfo));
} else if (i == 0x80000004) {
memcpy(CPUClockString, CPUInfo, sizeof(CPUInfo));
}
}
cpu.brand = CPUBrandString;
cpu.model = CPUModelString;
cpu.clockSpeed = CPUClockString;
}
unsigned int WINInstance::getNumLogicalCores() {
return std::thread::hardware_concurrency();
}
int WINInstance::getTotalSystemRamMb() {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
return statex.ullTotalPhys / 1024 / 1024;
}
std::string WINInstance::getProcessor(int index) {
std::string result;
if (index >= _processors.size())
return result;
return "Fake processor";
json j;
to_Json(j, _processors.at(index));
//serialize this
return j.dump();
}
void WINInstance::to_Json(json& result, const cpu& cpu) {
result["cpuBrand"] = cpu.brand;
result["cpuModel"] = cpu.model;
result["cpuClockSpeed"] = cpu.clockSpeed;
result["cpuNumberOfCores"] = cpu.numberOfCores;
}

View file

@ -8,14 +8,23 @@
#pragma once
#include "platform.h"
#include <nlohmann/json.hpp>
namespace platform {
using namespace nlohmann;
class WINInstance : public Instance {
public:
bool enumerateProcessors();
std::string getProcessor(int index);
};
public:
bool enumerateProcessors();
std::string getProcessor(int index);
private:
unsigned int getNumLogicalCores();
void getCpuDetails(cpu& cpu);
int getTotalSystemRamMb();
void to_Json(nlohmann::json& result, const cpu& cpu);
};
} // namespace platform

View file

@ -36,4 +36,12 @@ std::string platform::getProcessor(int index) {
bool platform::enumerateProcessors() {
return _instance->enumerateProcessors();
}
int platform::getTotalSystemRamMb() {
return _instance->getTotalSystemRamMb();
}
int platform::getProcessorCount() {
return _instance->getProcessorCount();
}

View file

@ -6,28 +6,42 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once
#include <string>
#include <vector>
namespace platform {
class Instance {
public:
std::string virtual getProcessor(int index) = 0;
bool virtual enumerateProcessors() = 0;
private:
std::vector<std::string> _processors;
};
struct cpu {
std::string brand;
std::string model;
int numberOfCores;
std::string clockSpeed;
};
static Instance *_instance;
struct memory {
int totalMb;
};
void create();
std::string getProcessor(int index);
bool enumerateProcessors();
}
class Instance {
public:
std::string virtual getProcessor(int index) = 0;
bool virtual enumerateProcessors() = 0;
int virtual getTotalSystemRamMb() = 0;
int getProcessorCount() {return _processors.size(); }
protected:
std::vector<cpu> _processors;
struct memory _memory;
};
static Instance* _instance;
//Platform level functions
void create();
std::string getProcessor(int index);
int getProcessorCount();
bool enumerateProcessors();
int getTotalSystemRamMb();
} // namespace platform