mirror of
https://github.com/xmrig/xmrig.git
synced 2025-02-02 19:26:38 +00:00
More flexible API handling for mining backends.
This commit is contained in:
parent
36da54b8ce
commit
f9f04e4b2e
4 changed files with 54 additions and 48 deletions
|
@ -37,6 +37,7 @@ namespace xmrig {
|
|||
|
||||
class Algorithm;
|
||||
class Hashrate;
|
||||
class IApiRequest;
|
||||
class IWorker;
|
||||
class Job;
|
||||
class String;
|
||||
|
@ -61,6 +62,7 @@ public:
|
|||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
virtual rapidjson::Value toJSON(rapidjson::Document &doc) const = 0;
|
||||
virtual void handleRequest(IApiRequest &request) = 0;
|
||||
# endif
|
||||
};
|
||||
|
||||
|
|
|
@ -43,6 +43,11 @@
|
|||
#include "rapidjson/document.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_API
|
||||
# include "base/api/interfaces/IApiRequest.h"
|
||||
#endif
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
|
@ -117,6 +122,38 @@ public:
|
|||
}
|
||||
|
||||
|
||||
rapidjson::Value hugePages(int version, rapidjson::Document &doc)
|
||||
{
|
||||
std::pair<unsigned, unsigned> pages(0, 0);
|
||||
|
||||
# ifdef XMRIG_ALGO_RANDOMX
|
||||
if (algo.family() == Algorithm::RANDOM_X) {
|
||||
pages = Rx::hugePages();
|
||||
}
|
||||
# endif
|
||||
|
||||
mutex.lock();
|
||||
|
||||
pages.first += status.hugePages;
|
||||
pages.second += status.pages;
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
rapidjson::Value hugepages;
|
||||
|
||||
if (version > 1) {
|
||||
hugepages.SetArray();
|
||||
hugepages.PushBack(pages.first, doc.GetAllocator());
|
||||
hugepages.PushBack(pages.second, doc.GetAllocator());
|
||||
}
|
||||
else {
|
||||
hugepages = pages.first == pages.second;
|
||||
}
|
||||
|
||||
return hugepages;
|
||||
}
|
||||
|
||||
|
||||
Algorithm algo;
|
||||
Controller *controller;
|
||||
LaunchStatus status;
|
||||
|
@ -143,25 +180,6 @@ xmrig::CpuBackend::~CpuBackend()
|
|||
}
|
||||
|
||||
|
||||
std::pair<unsigned, unsigned> xmrig::CpuBackend::hugePages() const
|
||||
{
|
||||
std::pair<unsigned, unsigned> pages(0, 0);
|
||||
|
||||
# ifdef XMRIG_ALGO_RANDOMX
|
||||
if (d_ptr->algo.family() == Algorithm::RANDOM_X) {
|
||||
pages = Rx::hugePages();
|
||||
}
|
||||
# endif
|
||||
|
||||
std::lock_guard<std::mutex> lock(d_ptr->mutex);
|
||||
|
||||
pages.first += d_ptr->status.hugePages;
|
||||
pages.second += d_ptr->status.pages;
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
|
||||
bool xmrig::CpuBackend::isEnabled() const
|
||||
{
|
||||
return d_ptr->controller->config()->cpu().isEnabled();
|
||||
|
@ -319,13 +337,7 @@ rapidjson::Value xmrig::CpuBackend::toJSON(rapidjson::Document &doc) const
|
|||
out.AddMember("asm", false, allocator);
|
||||
# endif
|
||||
|
||||
const auto pages = hugePages();
|
||||
|
||||
rapidjson::Value hugepages(rapidjson::kArrayType);
|
||||
hugepages.PushBack(pages.first, allocator);
|
||||
hugepages.PushBack(pages.second, allocator);
|
||||
|
||||
out.AddMember("hugepages", hugepages, allocator);
|
||||
out.AddMember("hugepages", d_ptr->hugePages(2, doc), allocator);
|
||||
out.AddMember("memory", static_cast<uint64_t>(d_ptr->algo.isValid() ? (d_ptr->ways() * d_ptr->algo.l3()) : 0), allocator);
|
||||
|
||||
if (d_ptr->threads.empty() || !hashrate()) {
|
||||
|
@ -357,4 +369,12 @@ rapidjson::Value xmrig::CpuBackend::toJSON(rapidjson::Document &doc) const
|
|||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::CpuBackend::handleRequest(IApiRequest &request)
|
||||
{
|
||||
if (request.type() == IApiRequest::REQ_SUMMARY) {
|
||||
request.reply().AddMember("hugepages", d_ptr->hugePages(request.version(), request.doc()), request.doc().GetAllocator());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -46,8 +46,6 @@ public:
|
|||
CpuBackend(Controller *controller);
|
||||
~CpuBackend() override;
|
||||
|
||||
std::pair<unsigned, unsigned> hugePages() const;
|
||||
|
||||
protected:
|
||||
bool isEnabled() const override;
|
||||
bool isEnabled(const Algorithm &algorithm) const override;
|
||||
|
@ -63,6 +61,7 @@ protected:
|
|||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
rapidjson::Value toJSON(rapidjson::Document &doc) const override;
|
||||
void handleRequest(IApiRequest &request) override;
|
||||
# endif
|
||||
|
||||
private:
|
||||
|
|
|
@ -135,7 +135,7 @@ public:
|
|||
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
void getMiner(rapidjson::Value &reply, rapidjson::Document &doc, int version) const
|
||||
void getMiner(rapidjson::Value &reply, rapidjson::Document &doc, int) const
|
||||
{
|
||||
using namespace rapidjson;
|
||||
auto &allocator = doc.GetAllocator();
|
||||
|
@ -144,25 +144,6 @@ public:
|
|||
reply.AddMember("kind", APP_KIND, allocator);
|
||||
reply.AddMember("ua", StringRef(Platform::userAgent()), allocator);
|
||||
reply.AddMember("cpu", Cpu::toJSON(doc), allocator);
|
||||
|
||||
Value hugepages;
|
||||
|
||||
if (!backends.empty() && backends.front()->type() == "cpu") {
|
||||
const auto pages = static_cast<CpuBackend *>(backends.front())->hugePages();
|
||||
if (version > 1) {
|
||||
hugepages.SetArray();
|
||||
hugepages.PushBack(pages.first, allocator);
|
||||
hugepages.PushBack(pages.second, allocator);
|
||||
}
|
||||
else {
|
||||
hugepages = pages.first == pages.second;
|
||||
}
|
||||
}
|
||||
else {
|
||||
hugepages = false;
|
||||
}
|
||||
|
||||
reply.AddMember("hugepages", hugepages, allocator);
|
||||
reply.AddMember("donate_level", controller->config()->pools().donateLevel(), allocator);
|
||||
|
||||
Value algo(kArrayType);
|
||||
|
@ -489,5 +470,9 @@ void xmrig::Miner::onRequest(IApiRequest &request)
|
|||
setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
for (IBackend *backend : d_ptr->backends) {
|
||||
backend->handleRequest(request);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue