Skip to content

Commit

Permalink
add suspend count to worker stats
Browse files Browse the repository at this point in the history
  • Loading branch information
kuron99 committed Dec 28, 2023
1 parent 39e3b59 commit e61ec8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 8 additions & 2 deletions include/tateyama/task_scheduler/impl/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ struct cache_align worker_stat {
/**
* @brief the count of when worker gets up (from suspension) and executes at least one task
*/
std::size_t wakeup_{};
std::size_t wakeup_run_{};

/**
* @brief the count of worker suspends
*/
std::size_t suspend_{};
};

/**
Expand Down Expand Up @@ -179,6 +184,7 @@ class cache_align worker {
if(empty_work_count > cfg_->worker_try_count()) {
empty_work_count = 0;
ctx.busy_working(false);
++stat_->suspend_;
ctx.thread()->suspend(std::chrono::microseconds{cfg_->worker_suspend_timeout()});
}
}
Expand Down Expand Up @@ -238,7 +244,7 @@ class cache_align worker {

void execute_task(task& t, context& ctx) {
if(! ctx.busy_working()) {
++stat_->wakeup_;
++stat_->wakeup_run_;
}
ctx.busy_working(true);
try {
Expand Down
12 changes: 10 additions & 2 deletions include/tateyama/task_scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#pragma once

#include <chrono>
#include <sched.h>

#include <tateyama/logging.h>
Expand All @@ -37,6 +38,8 @@ namespace tateyama::task_scheduler {
template <class T, class S = tateyama::task_scheduler::basic_conditional_task>
class cache_align scheduler {
public:
// clock used for stats
using clock = std::chrono::steady_clock;

/**
* @brief task type scheduled by this
Expand Down Expand Up @@ -221,6 +224,7 @@ class cache_align scheduler {
if(watcher_thread_) {
watcher_thread_->activate();
}
started_at_ = clock::now();
started_ = true;
}

Expand Down Expand Up @@ -346,8 +350,10 @@ class cache_align scheduler {
* @brief print worker stats
*/
void print_worker_stats(std::ostream& os) {
auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - started_at_).count();
auto count = contexts_.size();
os << "{";
os << "\"duration_us\":" << duration_us << ",";
os << "\"worker_count\":" << count << ",";
os << "\"workers\":[";
for(std::size_t i=0; i < count; ++i) {
Expand All @@ -360,7 +366,8 @@ class cache_align scheduler {
os << "\"count\":" << stat.count_ << ",";
os << "\"sticky\":" << stat.sticky_ << ",";
os << "\"steal\":" << stat.steal_ << ",";
os << "\"wakeup\":" << stat.wakeup_;
os << "\"wakeup_run\":" << stat.wakeup_run_ << ",";
os << "\"suspend\":" << stat.suspend_;
os << "}";
}
os << "]";
Expand All @@ -384,6 +391,7 @@ class cache_align scheduler {
std::unique_ptr<impl::thread_control> watcher_thread_{};
impl::conditional_worker_context conditional_worker_context_{};
conditional_worker conditional_worker_{}; // stored for testing
clock::time_point started_at_{};

void prepare() {
auto sz = cfg_.thread_count();
Expand Down Expand Up @@ -420,7 +428,7 @@ class cache_align scheduler {
auto ret = index++;
return ret % mod;
}

/**
* @brief print queue diagnostics
*/
Expand Down

0 comments on commit e61ec8d

Please sign in to comment.