Added class CpuThreads.

This commit is contained in:
XMRig 2019-08-07 16:13:23 +07:00
parent 0adab95ce4
commit 96fd7545d1
12 changed files with 160 additions and 58 deletions

View file

@ -24,7 +24,7 @@
#include "backend/common/Threads.h"
#include "backend/cpu/CpuThread.h"
#include "backend/cpu/CpuThreads.h"
#include "rapidjson/document.h"
@ -38,9 +38,9 @@ static const char *kAsterisk = "*";
template <class T>
const std::vector<T> &xmrig::Threads<T>::get(const String &profileName) const
const T &xmrig::Threads<T>::get(const String &profileName) const
{
static std::vector<T> empty;
static T empty;
if (profileName.isNull() || !has(profileName)) {
return empty;
}
@ -56,16 +56,9 @@ size_t xmrig::Threads<T>::read(const rapidjson::Value &value)
for (auto &member : value.GetObject()) {
if (member.value.IsArray()) {
std::vector<T> threads;
T threads(member.value);
for (auto &v : member.value.GetArray()) {
T thread(v);
if (thread.isValid()) {
threads.push_back(std::move(thread));
}
}
if (!threads.empty()) {
if (!threads.isEmpty()) {
move(member.name.GetString(), std::move(threads));
}
@ -138,13 +131,7 @@ void xmrig::Threads<T>::toJSON(rapidjson::Value &out, rapidjson::Document &doc)
auto &allocator = doc.GetAllocator();
for (const auto &kv : m_profiles) {
Value arr(kArrayType);
for (const T &thread : kv.second) {
arr.PushBack(thread.toJSON(doc), allocator);
}
out.AddMember(kv.first.toJSON(), arr, allocator);
out.AddMember(kv.first.toJSON(), kv.second.toJSON(doc), allocator);
}
for (const Algorithm &algo : m_disabled) {
@ -159,6 +146,6 @@ void xmrig::Threads<T>::toJSON(rapidjson::Value &out, rapidjson::Document &doc)
namespace xmrig {
template class Threads<CpuThread>;
template class Threads<CpuThreads>;
} // namespace xmrig

View file

@ -45,18 +45,18 @@ public:
inline bool has(const char *profile) const { return m_profiles.count(profile) > 0; }
inline bool isDisabled(const Algorithm &algo) const { return m_disabled.count(algo) > 0; }
inline bool isExist(const Algorithm &algo) const { return isDisabled(algo) || m_aliases.count(algo) > 0 || has(algo.shortName()); }
inline const std::vector<T> &get(const Algorithm &algo, bool strict = false) const { return get(profileName(algo, strict)); }
inline const T &get(const Algorithm &algo, bool strict = false) const { return get(profileName(algo, strict)); }
inline void disable(const Algorithm &algo) { m_disabled.insert(algo); }
inline void move(const char *profile, std::vector<T> &&threads) { m_profiles.insert({ profile, threads }); }
inline void move(const char *profile, T &&threads) { m_profiles.insert({ profile, threads }); }
const std::vector<T> &get(const String &profileName) const;
const T &get(const String &profileName) const;
size_t read(const rapidjson::Value &value);
String profileName(const Algorithm &algorithm, bool strict = false) const;
void toJSON(rapidjson::Value &out, rapidjson::Document &doc) const;
private:
std::map<Algorithm, String> m_aliases;
std::map<String, std::vector<T> > m_profiles;
std::map<String, T> m_profiles;
std::set<Algorithm> m_disabled;
};

View file

@ -46,7 +46,7 @@
namespace xmrig {
extern template class Threads<CpuThread>;
extern template class Threads<CpuThreads>;
static const char *tag = CYAN_BG_BOLD(" cpu ");
@ -150,7 +150,7 @@ bool xmrig::CpuBackend::isEnabled() const
bool xmrig::CpuBackend::isEnabled(const Algorithm &algorithm) const
{
return !d_ptr->controller->config()->cpu().threads().get(algorithm).empty();
return !d_ptr->controller->config()->cpu().threads().get(algorithm).isEmpty();
}

View file

@ -62,7 +62,7 @@ static const char *kRx = "rx";
static const char *kRxWOW = "rx/wow";
#endif
extern template class Threads<CpuThread>;
extern template class Threads<CpuThreads>;
}
@ -103,15 +103,15 @@ rapidjson::Value xmrig::CpuConfig::toJSON(rapidjson::Document &doc) const
std::vector<xmrig::CpuLaunchData> xmrig::CpuConfig::get(const Miner *miner, const Algorithm &algorithm) const
{
std::vector<CpuLaunchData> out;
const std::vector<CpuThread> &threads = m_threads.get(algorithm);
const CpuThreads &threads = m_threads.get(algorithm);
if (threads.empty()) {
if (threads.isEmpty()) {
return out;
}
out.reserve(threads.size());
out.reserve(threads.count());
for (const CpuThread &thread : threads) {
for (const CpuThread &thread : threads.data()) {
out.push_back(CpuLaunchData(miner, algorithm, *this, thread));
}

View file

@ -28,7 +28,7 @@
#include "backend/common/Threads.h"
#include "backend/cpu/CpuLaunchData.h"
#include "backend/cpu/CpuThread.h"
#include "backend/cpu/CpuThreads.h"
#include "crypto/common/Assembly.h"
@ -51,12 +51,12 @@ public:
std::vector<CpuLaunchData> get(const Miner *miner, const Algorithm &algorithm) const;
void read(const rapidjson::Value &value);
inline bool isEnabled() const { return m_enabled; }
inline bool isHugePages() const { return m_hugePages; }
inline bool isShouldSave() const { return m_shouldSave; }
inline const Assembly &assembly() const { return m_assembly; }
inline const Threads<CpuThread> &threads() const { return m_threads; }
inline int priority() const { return m_priority; }
inline bool isEnabled() const { return m_enabled; }
inline bool isHugePages() const { return m_hugePages; }
inline bool isShouldSave() const { return m_shouldSave; }
inline const Assembly &assembly() const { return m_assembly; }
inline const Threads<CpuThreads> &threads() const { return m_threads; }
inline int priority() const { return m_priority; }
private:
void generate();
@ -70,7 +70,7 @@ private:
bool m_hugePages = true;
bool m_shouldSave = false;
int m_priority = -1;
Threads<CpuThread> m_threads;
Threads<CpuThreads> m_threads;
};

View file

@ -22,11 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CPUTHREADCONFIG_H
#define XMRIG_CPUTHREADCONFIG_H
#include <vector>
#ifndef XMRIG_CPUTHREAD_H
#define XMRIG_CPUTHREAD_H
#include "rapidjson/fwd.h"
@ -58,10 +55,7 @@ private:
};
typedef std::vector<CpuThread> CpuThreads;
} /* namespace xmrig */
#endif /* XMRIG_CPUTHREADCONFIG_H */
#endif /* XMRIG_CPUTHREAD_H */

View file

@ -0,0 +1,54 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "backend/cpu/CpuThreads.h"
#include "rapidjson/document.h"
xmrig::CpuThreads::CpuThreads(const rapidjson::Value &value)
{
if (value.IsArray()) {
for (auto &v : value.GetArray()) {
CpuThread thread(v);
if (thread.isValid()) {
add(std::move(thread));
}
}
}
}
rapidjson::Value xmrig::CpuThreads::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value array(kArrayType);
for (const CpuThread &thread : m_data) {
array.PushBack(thread.toJSON(doc), allocator);
}
return array;
}

View file

@ -0,0 +1,63 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CPUTHREADS_H
#define XMRIG_CPUTHREADS_H
#include <vector>
#include "backend/cpu/CpuThread.h"
namespace xmrig {
class CpuThreads
{
public:
inline CpuThreads() {}
inline CpuThreads(size_t count) : m_data(count) {}
CpuThreads(const rapidjson::Value &value);
inline bool isEmpty() const { return m_data.empty(); }
inline const std::vector<CpuThread> &data() const { return m_data; }
inline size_t count() const { return m_data.size(); }
inline void add(CpuThread &&thread) { m_data.push_back(thread); }
inline void add(int64_t affinity, int intensity = 1) { add(CpuThread(intensity, affinity)); }
inline void reserve(size_t capacity) { m_data.reserve(capacity); }
rapidjson::Value toJSON(rapidjson::Document &doc) const;
private:
std::vector<CpuThread> m_data;
};
} /* namespace xmrig */
#endif /* XMRIG_CPUTHREADS_H */

View file

@ -4,6 +4,7 @@ set(HEADERS_BACKEND_CPU
src/backend/cpu/CpuConfig.h
src/backend/cpu/CpuLaunchData.cpp
src/backend/cpu/CpuThread.h
src/backend/cpu/CpuThreads.h
src/backend/cpu/CpuWorker.h
src/backend/cpu/interfaces/ICpuInfo.h
)
@ -14,6 +15,7 @@ set(SOURCES_BACKEND_CPU
src/backend/cpu/CpuConfig.cpp
src/backend/cpu/CpuLaunchData.h
src/backend/cpu/CpuThread.cpp
src/backend/cpu/CpuThreads.cpp
src/backend/cpu/CpuWorker.cpp
)

View file

@ -26,7 +26,7 @@
#define XMRIG_CPUINFO_H
#include "backend/cpu/CpuThread.h"
#include "backend/cpu/CpuThreads.h"
#include "crypto/common/Assembly.h"
#include "crypto/common/Algorithm.h"

View file

@ -182,23 +182,25 @@ const char *xmrig::BasicCpuInfo::backend() const
xmrig::CpuThreads xmrig::BasicCpuInfo::threads(const Algorithm &algorithm) const
{
if (threads() == 1) {
return CpuThreads(1);
const size_t count = std::thread::hardware_concurrency();
if (count == 1) {
return 1;
}
# ifdef XMRIG_ALGO_CN_GPU
if (algorithm == Algorithm::CN_GPU) {
return CpuThreads(threads());
return count;
}
# endif
if (algorithm.family() == Algorithm::CN_LITE || algorithm.family() == Algorithm::CN_PICO) {
return CpuThreads(threads());
return count;
}
if (algorithm.family() == Algorithm::CN_HEAVY) {
return CpuThreads(std::max<size_t>(threads() / 4, 1));
return std::max<size_t>(count / 4, 1);
}
return CpuThreads(std::max<size_t>(threads() / 2, 1));
return std::max<size_t>(count / 2, 1);
}

View file

@ -222,7 +222,7 @@ xmrig::CpuThreads xmrig::HwlocCpuInfo::threads(const Algorithm &algorithm) const
processTopLevelCache(cache, algorithm, threads);
}
if (threads.empty()) {
if (threads.isEmpty()) {
LOG_WARN("hwloc auto configuration for algorithm \"%s\" failed.", algorithm.shortName());
return BasicCpuInfo::threads(algorithm);
@ -286,7 +286,7 @@ void xmrig::HwlocCpuInfo::processTopLevelCache(hwloc_obj_t cache, const Algorith
for (hwloc_obj_t core : cores) {
const std::vector<hwloc_obj_t> units = findByType(core, HWLOC_OBJ_PU);
for (hwloc_obj_t pu : units) {
threads.push_back(CpuThread(1, pu->os_index));
threads.add(pu->os_index);
}
}
@ -307,7 +307,7 @@ void xmrig::HwlocCpuInfo::processTopLevelCache(hwloc_obj_t cache, const Algorith
PUs--;
allocated_pu = true;
threads.push_back(CpuThread(1, units[pu_id]->os_index));
threads.add(units[pu_id]->os_index);
if (cacheHashes == 0) {
break;