refactor: remove dependency on folly::Expected

This commit is contained in:
Marcus Holland-Moritz 2024-07-28 19:33:47 +02:00
parent 29e09d94c7
commit bfa17289b6
5 changed files with 33 additions and 20 deletions

View File

@ -28,9 +28,9 @@
#include <future> #include <future>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <optional>
#include <utility> #include <utility>
#include <folly/Expected.h>
#include <folly/Function.h> #include <folly/Function.h>
namespace dwarfs { namespace dwarfs {
@ -87,10 +87,15 @@ class worker_group {
size_t size() const { return impl_->size(); } size_t size() const { return impl_->size(); }
size_t queue_size() const { return impl_->queue_size(); } size_t queue_size() const { return impl_->queue_size(); }
folly::Expected<std::chrono::nanoseconds, std::error_code>
get_cpu_time() const { std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const {
return impl_->get_cpu_time(); return impl_->get_cpu_time(ec);
} }
std::optional<std::chrono::nanoseconds> try_get_cpu_time() const {
return impl_->try_get_cpu_time();
}
bool set_affinity(std::vector<int> const& cpus) { bool set_affinity(std::vector<int> const& cpus) {
return impl_->set_affinity(cpus); return impl_->set_affinity(cpus);
} }
@ -111,8 +116,10 @@ class worker_group {
virtual bool add_moveonly_job(moveonly_job_t&& job) = 0; virtual bool add_moveonly_job(moveonly_job_t&& job) = 0;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual size_t queue_size() const = 0; virtual size_t queue_size() const = 0;
virtual folly::Expected<std::chrono::nanoseconds, std::error_code> virtual std::chrono::nanoseconds
get_cpu_time() const = 0; get_cpu_time(std::error_code& ec) const = 0;
virtual std::optional<std::chrono::nanoseconds>
try_get_cpu_time() const = 0;
virtual bool set_affinity(std::vector<int> const& cpus) = 0; virtual bool set_affinity(std::vector<int> const& cpus) = 0;
}; };

View File

@ -25,6 +25,7 @@
#include <functional> #include <functional>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <optional>
#include <system_error> #include <system_error>
namespace dwarfs { namespace dwarfs {
@ -66,6 +67,7 @@ class thread_pool {
void stop(); void stop();
void wait(); void wait();
bool running() const; bool running() const;
std::optional<std::chrono::nanoseconds> try_get_cpu_time() const;
std::chrono::nanoseconds get_cpu_time() const; std::chrono::nanoseconds get_cpu_time() const;
std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const; std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const;

View File

@ -168,22 +168,28 @@ class basic_worker_group final : public worker_group::impl, private Policy {
return jobs_.size(); return jobs_.size();
} }
folly::Expected<std::chrono::nanoseconds, std::error_code> std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const override {
get_cpu_time() const override { ec.clear();
std::lock_guard lock(mx_); std::lock_guard lock(mx_);
std::chrono::nanoseconds t{}; std::chrono::nanoseconds t{};
for (auto const& w : workers_) { for (auto const& w : workers_) {
std::error_code ec;
t += os_.thread_get_cpu_time(w.get_id(), ec); t += os_.thread_get_cpu_time(w.get_id(), ec);
if (ec) { if (ec) {
return folly::makeUnexpected(ec); return {};
} }
} }
return t; return t;
} }
std::optional<std::chrono::nanoseconds> try_get_cpu_time() const override {
std::error_code ec;
auto t = get_cpu_time(ec);
return ec ? std::nullopt : std::make_optional(t);
}
bool set_affinity(std::vector<int> const& cpus) override { bool set_affinity(std::vector<int> const& cpus) override {
if (cpus.empty()) { if (cpus.empty()) {
return false; return false;

View File

@ -647,7 +647,7 @@ void scanner_<LoggerPolicy>::scan(
wg_.wait(); wg_.wait();
LOG_INFO << "scanning CPU time: " LOG_INFO << "scanning CPU time: "
<< time_with_unit(wg_.get_cpu_time().value_or(0ns)); << time_with_unit(wg_.try_get_cpu_time().value_or(0ns));
dump_state(kEnvVarDumpFilesRaw, "raw files", fa, dump_state(kEnvVarDumpFilesRaw, "raw files", fa,
[&fs](auto& os) { fs.dump(os); }); [&fs](auto& os) { fs.dump(os); });
@ -827,10 +827,10 @@ void scanner_<LoggerPolicy>::scan(
wg_ordering.wait(); wg_ordering.wait();
LOG_INFO << "total ordering CPU time: " LOG_INFO << "total ordering CPU time: "
<< time_with_unit(wg_ordering.get_cpu_time().value_or(0ns)); << time_with_unit(wg_ordering.try_get_cpu_time().value_or(0ns));
LOG_INFO << "total segmenting CPU time: " LOG_INFO << "total segmenting CPU time: "
<< time_with_unit(wg_blockify.get_cpu_time().value_or(0ns)); << time_with_unit(wg_blockify.try_get_cpu_time().value_or(0ns));
} }
// seg.finish(); // seg.finish();

View File

@ -43,13 +43,7 @@ void thread_pool::wait() { wg_->wait(); }
bool thread_pool::running() const { return wg_->running(); } bool thread_pool::running() const { return wg_->running(); }
std::chrono::nanoseconds thread_pool::get_cpu_time(std::error_code& ec) const { std::chrono::nanoseconds thread_pool::get_cpu_time(std::error_code& ec) const {
auto rv = wg_->get_cpu_time(); return wg_->get_cpu_time(ec);
if (rv) {
ec.clear();
return rv.value();
}
ec = rv.error();
return {};
} }
std::chrono::nanoseconds thread_pool::get_cpu_time() const { std::chrono::nanoseconds thread_pool::get_cpu_time() const {
@ -61,4 +55,8 @@ std::chrono::nanoseconds thread_pool::get_cpu_time() const {
return rv; return rv;
} }
std::optional<std::chrono::nanoseconds> thread_pool::try_get_cpu_time() const {
return wg_->try_get_cpu_time();
}
} // namespace dwarfs } // namespace dwarfs