More CPU usage stats

This commit is contained in:
Marcus Holland-Moritz 2021-04-04 17:34:40 +02:00
parent 6fb4d341c8
commit 68cb857916
4 changed files with 46 additions and 1 deletions

View File

@ -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 <typename T>
bool add_job(std::packaged_task<T()>&& 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:

View File

@ -613,6 +613,8 @@ void scanner_<LoggerPolicy>::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_<LoggerPolicy>::scan(filesystem_writer& fsw,
blockify.wait();
LOG_INFO << "segmenting/blockifying CPU time: "
<< time_with_unit(blockify.get_cpu_time());
bm.finish_blocks();
wg_.wait();

View File

@ -22,26 +22,44 @@
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <type_traits>
#include <vector>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <folly/Conv.h>
#include <folly/system/ThreadName.h>
#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<pthread_t, std::thread::native_handle_type>);
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 <typename Policy>
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<worker_group::job_t>;

View File

@ -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()) {