diff --git a/include/dwarfs/internal/worker_group.h b/include/dwarfs/internal/worker_group.h index 48299e20..b540ddb5 100644 --- a/include/dwarfs/internal/worker_group.h +++ b/include/dwarfs/internal/worker_group.h @@ -28,9 +28,9 @@ #include #include #include +#include #include -#include #include namespace dwarfs { @@ -87,10 +87,15 @@ class worker_group { size_t size() const { return impl_->size(); } size_t queue_size() const { return impl_->queue_size(); } - folly::Expected - get_cpu_time() const { - return impl_->get_cpu_time(); + + std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const { + return impl_->get_cpu_time(ec); } + + std::optional try_get_cpu_time() const { + return impl_->try_get_cpu_time(); + } + bool set_affinity(std::vector const& cpus) { return impl_->set_affinity(cpus); } @@ -111,8 +116,10 @@ class worker_group { virtual bool add_moveonly_job(moveonly_job_t&& job) = 0; virtual size_t size() const = 0; virtual size_t queue_size() const = 0; - virtual folly::Expected - get_cpu_time() const = 0; + virtual std::chrono::nanoseconds + get_cpu_time(std::error_code& ec) const = 0; + virtual std::optional + try_get_cpu_time() const = 0; virtual bool set_affinity(std::vector const& cpus) = 0; }; diff --git a/include/dwarfs/thread_pool.h b/include/dwarfs/thread_pool.h index dacd7b95..baccd423 100644 --- a/include/dwarfs/thread_pool.h +++ b/include/dwarfs/thread_pool.h @@ -25,6 +25,7 @@ #include #include #include +#include #include namespace dwarfs { @@ -66,6 +67,7 @@ class thread_pool { void stop(); void wait(); bool running() const; + std::optional try_get_cpu_time() const; std::chrono::nanoseconds get_cpu_time() const; std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const; diff --git a/src/dwarfs/internal/worker_group.cpp b/src/dwarfs/internal/worker_group.cpp index 669ef154..2cc7f83b 100644 --- a/src/dwarfs/internal/worker_group.cpp +++ b/src/dwarfs/internal/worker_group.cpp @@ -168,22 +168,28 @@ class basic_worker_group final : public worker_group::impl, private Policy { return jobs_.size(); } - folly::Expected - get_cpu_time() const override { + std::chrono::nanoseconds get_cpu_time(std::error_code& ec) const override { + ec.clear(); + std::lock_guard lock(mx_); std::chrono::nanoseconds t{}; for (auto const& w : workers_) { - std::error_code ec; t += os_.thread_get_cpu_time(w.get_id(), ec); if (ec) { - return folly::makeUnexpected(ec); + return {}; } } return t; } + std::optional 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 const& cpus) override { if (cpus.empty()) { return false; diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index cd6167ae..3639efcf 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -647,7 +647,7 @@ void scanner_::scan( wg_.wait(); 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, [&fs](auto& os) { fs.dump(os); }); @@ -827,10 +827,10 @@ void scanner_::scan( wg_ordering.wait(); 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: " - << 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(); diff --git a/src/dwarfs/thread_pool.cpp b/src/dwarfs/thread_pool.cpp index 55aa81b7..a80694ab 100644 --- a/src/dwarfs/thread_pool.cpp +++ b/src/dwarfs/thread_pool.cpp @@ -43,13 +43,7 @@ void thread_pool::wait() { wg_->wait(); } bool thread_pool::running() const { return wg_->running(); } std::chrono::nanoseconds thread_pool::get_cpu_time(std::error_code& ec) const { - auto rv = wg_->get_cpu_time(); - if (rv) { - ec.clear(); - return rv.value(); - } - ec = rv.error(); - return {}; + return wg_->get_cpu_time(ec); } std::chrono::nanoseconds thread_pool::get_cpu_time() const { @@ -61,4 +55,8 @@ std::chrono::nanoseconds thread_pool::get_cpu_time() const { return rv; } +std::optional thread_pool::try_get_cpu_time() const { + return wg_->try_get_cpu_time(); +} + } // namespace dwarfs