diff --git a/include/dwarfs/worker_group.h b/include/dwarfs/worker_group.h index ec13e261..4dd35fae 100644 --- a/include/dwarfs/worker_group.h +++ b/include/dwarfs/worker_group.h @@ -72,6 +72,7 @@ class worker_group { bool add_job(job_t&& job) { return impl_->add_job(std::move(job)); } size_t size() const { return impl_->size(); } size_t queue_size() const { return impl_->queue_size(); } + double get_cpu_time() const { return impl_->get_cpu_time(); } template bool add_job(std::packaged_task&& task) { @@ -88,6 +89,7 @@ class worker_group { virtual bool add_job(job_t&& job) = 0; virtual size_t size() const = 0; virtual size_t queue_size() const = 0; + virtual double get_cpu_time() const = 0; }; private: diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index ca2762f7..aebbc157 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -613,6 +613,8 @@ void scanner_::scan(filesystem_writer& fsw, LOG_INFO << "waiting for background scanners..."; wg_.wait(); + LOG_INFO << "scanning CPU time: " << time_with_unit(wg_.get_cpu_time()); + LOG_INFO << "finalizing file inodes..."; uint32_t first_device_inode = first_file_inode; fs.finalize(first_device_inode); @@ -682,6 +684,9 @@ void scanner_::scan(filesystem_writer& fsw, blockify.wait(); + LOG_INFO << "segmenting/blockifying CPU time: " + << time_with_unit(blockify.get_cpu_time()); + bm.finish_blocks(); wg_.wait(); diff --git a/src/dwarfs/worker_group.cpp b/src/dwarfs/worker_group.cpp index bb8f8990..03b7d4de 100644 --- a/src/dwarfs/worker_group.cpp +++ b/src/dwarfs/worker_group.cpp @@ -22,26 +22,44 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include -#include #include +#include + #include #include #include "dwarfs/error.h" #include "dwarfs/semaphore.h" +#include "dwarfs/util.h" #include "dwarfs/worker_group.h" namespace dwarfs { +namespace { + +pthread_t std_to_pthread_id(std::thread::id tid) { + static_assert(std::is_same_v); + static_assert(sizeof(std::thread::id) == + sizeof(std::thread::native_handle_type)); + pthread_t id{0}; + std::memcpy(&id, &tid, sizeof(id)); + return id; +} + +} // namespace + template class basic_worker_group final : public worker_group::impl, private Policy { public: @@ -55,6 +73,7 @@ class basic_worker_group final : public worker_group::impl, private Policy { if (num_workers < 1) { DWARFS_THROW(runtime_error, "invalid number of worker threads"); } + if (!group_name) { group_name = "worker"; } @@ -153,6 +172,22 @@ class basic_worker_group final : public worker_group::impl, private Policy { return jobs_.size(); } + double get_cpu_time() const override { + std::lock_guard lock(mx_); + double t = 0.0; + + for (auto const& w : workers_) { + ::clockid_t cid; + struct ::timespec ts; + if (::pthread_getcpuclockid(std_to_pthread_id(w.get_id()), &cid) == 0 && + ::clock_gettime(cid, &ts) == 0) { + t += ts.tv_sec + 1e-9 * ts.tv_nsec; + } + } + + return t; + } + private: using jobs_t = std::queue; diff --git a/src/mkdwarfs.cpp b/src/mkdwarfs.cpp index 1909cd03..308edca6 100644 --- a/src/mkdwarfs.cpp +++ b/src/mkdwarfs.cpp @@ -865,6 +865,9 @@ int mkdwarfs(int argc, char** argv) { return 1; } + LOG_INFO << "compression CPU time: " + << time_with_unit(wg_compress.get_cpu_time()); + ofs.close(); if (ofs.bad()) {