metaforce/hecl/lib/ClientProcess.cpp

237 lines
7.4 KiB
C++
Raw Permalink Normal View History

#include "hecl/ClientProcess.hpp"
#include <algorithm>
2017-12-29 07:56:31 +00:00
#include "hecl/Blender/Connection.hpp"
#include "hecl/Database.hpp"
2018-03-23 21:40:12 +00:00
#include "hecl/MultiProgressPrinter.hpp"
#include <athena/FileReader.hpp>
#include <boo/IApplication.hpp>
#include <logvisor/logvisor.hpp>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <sys/wait.h>
#endif
2017-10-22 06:10:59 +00:00
#define HECL_MULTIPROCESSOR 1
2018-12-08 05:18:42 +00:00
namespace hecl {
2017-12-29 07:56:31 +00:00
static logvisor::Module CP_Log("hecl::ClientProcess");
2017-10-22 06:10:59 +00:00
ThreadLocalPtr<ClientProcess::Worker> ClientProcess::ThreadWorker;
2019-02-04 00:01:12 +00:00
int CpuCountOverride = 0;
void SetCpuCountOverride(int argc, char** argv) {
2019-02-04 00:01:12 +00:00
bool threadArg = false;
for (int i = 1; i < argc; ++i) {
if (threadArg) {
if (int count = int(hecl::StrToUl(argv[i], nullptr, 0))) {
CpuCountOverride = count;
return;
}
}
if (!hecl::StrNCmp(argv[i], "-j", 2)) {
2019-02-04 00:01:12 +00:00
if (int count = int(hecl::StrToUl(argv[i] + 2, nullptr, 0))) {
CpuCountOverride = count;
return;
}
threadArg = true;
}
}
}
2018-12-08 05:18:42 +00:00
static int GetCPUCount() {
2020-04-13 05:10:03 +00:00
if (CpuCountOverride > 0) {
return CpuCountOverride;
}
2019-02-04 00:01:12 +00:00
int ret;
#if _WIN32
2018-12-08 05:18:42 +00:00
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
2019-02-04 00:01:12 +00:00
ret = sysinfo.dwNumberOfProcessors;
#else
2019-02-04 00:01:12 +00:00
ret = sysconf(_SC_NPROCESSORS_ONLN);
#endif
2019-02-04 00:01:12 +00:00
return ret;
}
2018-12-08 05:18:42 +00:00
void ClientProcess::BufferTransaction::run(blender::Token& btok) {
athena::io::FileReader r(m_path.getAbsolutePath(), 32 * 1024, false);
if (r.hasError()) {
CP_Log.report(logvisor::Fatal, FMT_STRING("unable to background-buffer '{}'"), m_path.getAbsolutePath());
2018-12-08 05:18:42 +00:00
return;
}
if (m_offset)
r.seek(m_offset, athena::SeekOrigin::Begin);
2018-12-08 05:18:42 +00:00
r.readBytesToBuf(m_targetBuf, m_maxLen);
m_complete = true;
}
2018-12-08 05:18:42 +00:00
void ClientProcess::CookTransaction::run(blender::Token& btok) {
m_dataSpec->setThreadProject();
m_returnResult = m_parent.syncCook(m_path, m_dataSpec, btok, m_force, m_fast);
std::unique_lock lk{m_parent.m_mutex};
2018-12-08 05:18:42 +00:00
++m_parent.m_completedCooks;
m_parent.m_progPrinter->setMainFactor(m_parent.m_completedCooks / float(m_parent.m_addedCooks));
m_complete = true;
}
2018-12-08 05:18:42 +00:00
void ClientProcess::LambdaTransaction::run(blender::Token& btok) {
m_func(btok);
m_complete = true;
}
2018-12-08 05:18:42 +00:00
ClientProcess::Worker::Worker(ClientProcess& proc, int idx) : m_proc(proc), m_idx(idx) {
m_thr = std::thread(std::bind(&Worker::proc, this));
}
2018-12-08 05:18:42 +00:00
void ClientProcess::Worker::proc() {
ClientProcess::ThreadWorker.reset(this);
2020-04-11 22:48:11 +00:00
std::string thrName = fmt::format(FMT_STRING("HECL Worker {}"), m_idx);
2019-07-28 01:19:48 +00:00
logvisor::RegisterThreadName(thrName.c_str());
2018-12-08 05:18:42 +00:00
std::unique_lock lk{m_proc.m_mutex};
2018-12-08 05:18:42 +00:00
while (m_proc.m_running) {
if (!m_didInit) {
m_proc.m_initCv.notify_one();
m_didInit = true;
}
while (m_proc.m_running && m_proc.m_pendingQueue.size()) {
std::shared_ptr<Transaction> trans = std::move(m_proc.m_pendingQueue.front());
++m_proc.m_inProgress;
m_proc.m_pendingQueue.pop_front();
lk.unlock();
trans->run(m_blendTok);
lk.lock();
m_proc.m_completedQueue.push_back(std::move(trans));
--m_proc.m_inProgress;
}
2018-12-08 05:18:42 +00:00
m_proc.m_waitCv.notify_one();
if (!m_proc.m_running)
break;
m_proc.m_cv.wait(lk);
}
lk.unlock();
m_blendTok.shutdown();
}
2018-12-08 05:18:42 +00:00
ClientProcess::ClientProcess(const MultiProgressPrinter* progPrinter) : m_progPrinter(progPrinter) {
2018-02-24 06:33:01 +00:00
#if HECL_MULTIPROCESSOR
2018-12-08 05:18:42 +00:00
const int cpuCount = GetCPUCount();
2016-04-07 03:38:37 +00:00
#else
2018-12-08 05:18:42 +00:00
constexpr int cpuCount = 1;
2016-04-07 03:38:37 +00:00
#endif
2018-12-08 05:18:42 +00:00
m_workers.reserve(cpuCount);
for (int i = 0; i < cpuCount; ++i) {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
m_workers.emplace_back(*this, m_workers.size());
m_initCv.wait(lk);
}
}
2018-12-08 05:18:42 +00:00
std::shared_ptr<const ClientProcess::BufferTransaction> ClientProcess::addBufferTransaction(const ProjectPath& path,
void* target, size_t maxLen,
size_t offset) {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
auto ret = std::make_shared<BufferTransaction>(*this, path, target, maxLen, offset);
m_pendingQueue.emplace_back(ret);
m_cv.notify_one();
return ret;
}
2018-12-08 05:18:42 +00:00
std::shared_ptr<const ClientProcess::CookTransaction> ClientProcess::addCookTransaction(const hecl::ProjectPath& path,
bool force, bool fast,
Database::IDataSpec* spec) {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
auto ret = std::make_shared<CookTransaction>(*this, path, force, fast, spec);
m_pendingQueue.emplace_back(ret);
m_cv.notify_one();
++m_addedCooks;
m_progPrinter->setMainFactor(m_completedCooks / float(m_addedCooks));
return ret;
}
std::shared_ptr<const ClientProcess::LambdaTransaction>
2018-12-08 05:18:42 +00:00
ClientProcess::addLambdaTransaction(std::function<void(blender::Token&)>&& func) {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
auto ret = std::make_shared<LambdaTransaction>(*this, std::move(func));
m_pendingQueue.emplace_back(ret);
m_cv.notify_one();
return ret;
2016-03-28 04:36:32 +00:00
}
2018-12-08 05:18:42 +00:00
bool ClientProcess::syncCook(const hecl::ProjectPath& path, Database::IDataSpec* spec, blender::Token& btok, bool force,
bool fast) {
if (spec->canCook(path, btok)) {
2019-05-08 03:47:34 +00:00
const Database::DataSpecEntry* specEnt = spec->overrideDataSpec(path, spec->getDataSpecEntry());
2018-12-08 05:18:42 +00:00
if (specEnt) {
hecl::ProjectPath cooked = path.getCookedPath(*specEnt);
if (fast)
cooked = cooked.getWithExtension(".fast");
2018-12-08 05:18:42 +00:00
cooked.makeDirChain(false);
if (force || cooked.getPathType() == ProjectPath::Type::None || path.getModtime() > cooked.getModtime()) {
if (m_progPrinter) {
std::string str;
2018-12-08 05:18:42 +00:00
if (path.getAuxInfo().empty())
str = fmt::format(FMT_STRING("Cooking {}"), path.getRelativePath());
2018-12-08 05:18:42 +00:00
else
str = fmt::format(FMT_STRING("Cooking {}|{}"), path.getRelativePath(), path.getAuxInfo());
m_progPrinter->print(str, std::nullopt, -1.f, hecl::ClientProcess::GetThreadWorkerIdx());
2018-12-08 05:18:42 +00:00
m_progPrinter->flush();
} else {
if (path.getAuxInfo().empty())
LogModule.report(logvisor::Info, FMT_STRING("Cooking {}"), path.getRelativePath());
2018-12-08 05:18:42 +00:00
else
LogModule.report(logvisor::Info, FMT_STRING("Cooking {}|{}"), path.getRelativePath(), path.getAuxInfo());
2018-12-08 05:18:42 +00:00
}
spec->doCook(path, cooked, false, btok, [](const char*) {});
2018-12-08 05:18:42 +00:00
if (m_progPrinter) {
std::string str;
2018-12-08 05:18:42 +00:00
if (path.getAuxInfo().empty())
str = fmt::format(FMT_STRING("Cooked {}"), path.getRelativePath());
2018-12-08 05:18:42 +00:00
else
str = fmt::format(FMT_STRING("Cooked {}|{}"), path.getRelativePath(), path.getAuxInfo());
m_progPrinter->print(str, std::nullopt, -1.f, hecl::ClientProcess::GetThreadWorkerIdx());
2018-12-08 05:18:42 +00:00
m_progPrinter->flush();
2016-03-28 22:39:18 +00:00
}
2018-12-08 05:18:42 +00:00
}
return true;
2016-03-28 21:38:31 +00:00
}
2018-12-08 05:18:42 +00:00
}
return false;
}
2018-12-08 05:18:42 +00:00
void ClientProcess::swapCompletedQueue(std::list<std::shared_ptr<Transaction>>& queue) {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
queue.swap(m_completedQueue);
}
2018-12-08 05:18:42 +00:00
void ClientProcess::waitUntilComplete() {
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
while (isBusy())
m_waitCv.wait(lk);
2016-04-09 23:18:12 +00:00
}
2018-12-08 05:18:42 +00:00
void ClientProcess::shutdown() {
if (!m_running)
return;
std::unique_lock lk{m_mutex};
2018-12-08 05:18:42 +00:00
m_pendingQueue.clear();
m_running = false;
m_cv.notify_all();
lk.unlock();
for (Worker& worker : m_workers)
if (worker.m_thr.joinable())
worker.m_thr.join();
}
2018-12-08 05:18:42 +00:00
} // namespace hecl