From 4fceb439b3e5a963d975cc7ae6caf49b7322b6eb Mon Sep 17 00:00:00 2001 From: violence-max <2016520436@qq.com> Date: Wed, 24 May 2023 21:26:53 +0800 Subject: [PATCH 1/3] update CfsAgent's code style --- sched/cfs/scx_cfs.cpp | 117 ++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/sched/cfs/scx_cfs.cpp b/sched/cfs/scx_cfs.cpp index 3388bc6..9d3a5be 100644 --- a/sched/cfs/scx_cfs.cpp +++ b/sched/cfs/scx_cfs.cpp @@ -113,31 +113,39 @@ class Duration { } ~Duration() = default; - Duration operator=(Duration t) const { + Duration operator=(Duration t) const + { return Duration(t.timestamp_); } - Duration operator+(const Duration &t) const { + Duration operator+(const Duration &t) const + { return Duration(timestamp_ + t.timestamp_); } - Duration operator-(const Duration &t) const { + Duration operator-(const Duration &t) const + { return Duration(timestamp_ - t.timestamp_); } - Duration operator+=(const Duration& t) const { + Duration operator+=(const Duration& t) const + { return Duration(timestamp_ + t.timestamp_); } - bool operator==(const Duration &t) const { + bool operator==(const Duration &t) const + { return timestamp_ == t.timestamp_; } - bool operator<(const Duration &t) const { + bool operator<(const Duration &t) const + { return timestamp_ < t.timestamp_; } - bool operator>(const Duration &t) const { + bool operator>(const Duration &t) const + { return timestamp_ > t.timestamp_; } private: double timestamp_; - double since(Duration &t1, Duration &t2) { + double since(Duration &t1, Duration &t2) + { // calc t1.since(t2) Duration res = t1 - t2; return res.timestamp_; @@ -160,67 +168,66 @@ struct enqueued_task { kDone, // Task is dead or departed kNumStates, }; - __u64 sum_exec_runtime; - enqueued_task() : sum_exec_runtime(0), vruntime(0) { + __u64 sum_exec_runtime_; + enqueued_task() : sum_exec_runtime_(0), vruntime_(0) { set_state(State::kRunnable); } // record the virtual time has been spent running, as the factor to make cfs scheduler - Duration vruntime; + Duration vruntime_; bool operator<(const struct enqueued_task &s) const { - if (vruntime == s.vruntime) { + if (vruntime_ == s.vruntime_) { return (uintptr_t)this < (uintptr_t)(&s); } - return vruntime < s.vruntime; + return vruntime_ < s.vruntime_; } void set_state(const State& state); - void set_nice(long nice) {this->nice = nice;} - long get_nice() {return nice;} - void set_weight(uint32_t weight) {this->weight = weight;} - void set_inverse_weight(uint32_t inverse_weight) {this->inverse_weight = weight;} - uint32_t get_inverse_weight() {return inverse_weight;} - bool is_runnable() {return task_state == State::kRunnable;} - bool is_running() {return task_state == State::kRunning;} + void set_nice(long nice) {nice_ = nice;} + long get_nice() {return nice_;} + void set_weight(uint32_t weight) {weight_ = weight;} + void set_inverse_weight(uint32_t inverse_weight) {inverse_weight_ = inverse_weight;} + uint32_t get_inverse_weight() {return inverse_weight_;} + bool is_runnable() {return task_state_ == State::kRunnable;} + bool is_running() {return task_state_ == State::kRunning;} private: - // Nice value and its corresponding weight/inverse-weight values for this // task. - long nice; - uint32_t weight; - uint32_t inverse_weight; + long nice_; + uint32_t weight_; + uint32_t inverse_weight_; - State task_state; + State task_state_; }; void enqueued_task::set_state(const enqueued_task::State &state) { switch (state) { case enqueued_task::State::kBlocked: - this->task_state = enqueued_task::State::kBlocked; + this->task_state_ = enqueued_task::State::kBlocked; break; case enqueued_task::State::kRunnable: - this->task_state = enqueued_task::State::kRunnable; + this->task_state_ = enqueued_task::State::kRunnable; break; case enqueued_task::State::kRunning: - this->task_state = enqueued_task::State::kRunning; + this->task_state_ = enqueued_task::State::kRunning; break; case enqueued_task::State::kDone: - this->task_state = enqueued_task::State::kDone; + this->task_state_ = enqueued_task::State::kDone; break; default: - this->task_state = enqueued_task::State::kNumStates; + this->task_state_ = enqueued_task::State::kNumStates; break; } } class CfsRq { public: - CfsRq() : cur(nullptr), min_vruntime_(0), rq_size_(0) {} + CfsRq() : cur_(nullptr), min_vruntime_(0), rq_size_(0) {} ~CfsRq() = default; /* the most important function of the CFS scheduler. @@ -246,11 +253,11 @@ class CfsRq { /*return the left most task in the cfs_rq_*/ struct enqueued_task* left_most_rq_task() {return is_empty() ? nullptr : *cfs_rq_.begin();} - struct enqueued_task *cur; + struct enqueued_task *cur_; private: std::set<struct enqueued_task *> cfs_rq_; - int kmin_nice = -20; - int kmax_nice = 19; + static const int kmin_nice = -20; + static const int kmax_nice = 19; Duration min_vruntime_; uint32_t rq_size_; }; @@ -265,13 +272,15 @@ void CfsRq::task_new(struct enqueued_task *task, long nice) task->set_state(enqueued_task::State::kRunnable); } -void CfsRq::enqueue_cfs_task(struct enqueued_task *task) { +void CfsRq::enqueue_cfs_task(struct enqueued_task *task) +{ cfs_rq_.insert(task); rq_size_++; - min_vruntime_ = (*cfs_rq_.begin())->vruntime; // keep pace + min_vruntime_ = (*cfs_rq_.begin())->vruntime_; // keep pace } -void CfsRq::update_min_vruntime() { +void CfsRq::update_min_vruntime() +{ // We want to make sure min_vruntime_ is set to the min of curr's vruntime and // the vruntime of our leftmost node. We do this so that: // - if curr is immediately placed back into the rq, we don't go back in time @@ -279,7 +288,7 @@ void CfsRq::update_min_vruntime() { // - if a new task is inserted into the rq, it doesn't get treated unfairly // wrt to curr struct enqueued_task *leftmost = left_most_rq_task(); - struct enqueued_task *curr = cur; + struct enqueued_task *curr = cur_; Duration vruntime = min_vruntime_; @@ -287,7 +296,7 @@ void CfsRq::update_min_vruntime() { // for the min vruntime. if (curr) { if (curr->is_runnable() || curr->is_running()) { - vruntime = curr->vruntime; + vruntime = curr->vruntime_; } else { curr = nullptr; } @@ -296,16 +305,17 @@ void CfsRq::update_min_vruntime() { // non-empty rq if (leftmost) { if (!curr) { - vruntime = leftmost->vruntime; + vruntime = leftmost->vruntime_; } else { - vruntime = std::min(vruntime, leftmost->vruntime); + vruntime = std::min(vruntime, leftmost->vruntime_); } } min_vruntime_ = std::max(min_vruntime_, vruntime); } -bool CfsRq::dequeue_cfs_task(struct enqueued_task *task) { +bool CfsRq::dequeue_cfs_task(struct enqueued_task *task) +{ if (cfs_rq_.erase(task)) { rq_size_--; return true; @@ -314,14 +324,17 @@ bool CfsRq::dequeue_cfs_task(struct enqueued_task *task) { } } -struct enqueued_task* CfsRq::pick_next_cfs_task() { +struct enqueued_task* CfsRq::pick_next_cfs_task() +{ if (is_empty()) { update_min_vruntime(); return nullptr; } struct enqueued_task* next = left_most_rq_task(); - dequeue_cfs_task(next); + if (!dequeue_cfs_task(next)) { + printf("error : delete cfs task\n"); + } next->set_state(enqueued_task::State::kRunning); update_min_vruntime(); return next; @@ -373,7 +386,7 @@ static struct enqueued_task *get_enqueued_task(__s32 pid) static void update_enqueued(struct enqueued_task *enqueued, const struct scx_userland_enqueued_task *bpf_task) { - __u64 runtime = bpf_task->sum_exec_runtime - enqueued->sum_exec_runtime; + __u64 runtime = bpf_task->sum_exec_runtime - enqueued->sum_exec_runtime_; // Update task's vruntime, which is the physical runtime multiplied by // the inverse of the weight for the task's nice value. We additionally // divide the product by 2^22 (right shift by 22 bits) to make a nice @@ -388,8 +401,8 @@ static void update_enqueued(struct enqueued_task *enqueued, // = wall_runtime * 2^10 / precomputed_weight // = wall_runtime * 2^10 / (2^32 / precomputed_inverse_weight) // = wall_runtime * precomputed_inverse_weight / 2^22 - enqueued->vruntime += Duration(runtime * enqueued->get_inverse_weight() >> 22); - enqueued->sum_exec_runtime = bpf_task->sum_exec_runtime; + enqueued->vruntime_ += Duration(runtime * enqueued->get_inverse_weight() >> 22); + enqueued->sum_exec_runtime_ = bpf_task->sum_exec_runtime; } static int vruntime_enqueue(const struct scx_userland_enqueued_task *bpf_task) @@ -397,9 +410,9 @@ static int vruntime_enqueue(const struct scx_userland_enqueued_task *bpf_task) struct enqueued_task *curr; curr = get_enqueued_task(bpf_task->pid); - if (!curr) + if (!curr) { return ENOENT; - else { + } else { cfs_rq.task_new(curr, bpf_task->nice); } @@ -484,9 +497,11 @@ static void dispatch_batch(void) __s32 pid; task = cfs_rq.pick_next_cfs_task(); - if (!task) + if (!task) { return; - else cfs_rq.cur = task; + } else { + cfs_rq.cur_ = task; + } pid = task_pid(task); -- GitLab From c9b718b317a4845cecb1976ce78b57f3cc664f55 Mon Sep 17 00:00:00 2001 From: violence-max <2016520436@qq.com> Date: Sat, 27 May 2023 14:32:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat=20:=20enqueued=5Ftask=20=E7=9A=84?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E6=96=B9=E6=B3=95=E6=94=B9=E4=B8=BA=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=AF=94=E8=BE=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sched/cfs/scx_cfs.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sched/cfs/scx_cfs.cpp b/sched/cfs/scx_cfs.cpp index 9d3a5be..4082cd2 100644 --- a/sched/cfs/scx_cfs.cpp +++ b/sched/cfs/scx_cfs.cpp @@ -204,6 +204,10 @@ struct enqueued_task { State task_state_; }; +static bool less(struct enqueued_task* t1, struct enqueued_task* t2) { + return (*t1) < (*t2); +} + void enqueued_task::set_state(const enqueued_task::State &state) { switch (state) { @@ -255,9 +259,9 @@ class CfsRq { struct enqueued_task *cur_; private: - std::set<struct enqueued_task *> cfs_rq_; - static const int kmin_nice = -20; - static const int kmax_nice = 19; + std::set<struct enqueued_task *, decltype(&less)> cfs_rq_; + static const int kmin_nice_ = -20; + static const int kmax_nice_ = 19; Duration min_vruntime_; uint32_t rq_size_; }; @@ -267,8 +271,8 @@ CfsRq cfs_rq = CfsRq(); void CfsRq::task_new(struct enqueued_task *task, long nice) { task->set_nice(nice); - task->set_weight(kNiceToWeight[task->get_nice() - kmin_nice]); - task->set_inverse_weight(kNiceToInverseWeight[task->get_nice() - kmin_nice]); + task->set_weight(kNiceToWeight[task->get_nice() - kmin_nice_]); + task->set_inverse_weight(kNiceToInverseWeight[task->get_nice() - kmin_nice_]); task->set_state(enqueued_task::State::kRunnable); } -- GitLab From 34a55c7d9b921cdd733e98d4529d69c644cc00fb Mon Sep 17 00:00:00 2001 From: violence-max <2016520436@qq.com> Date: Sat, 27 May 2023 16:24:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=20:=20cfs.enqueued=5Ftask=20=E7=9A=84?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E5=87=BD=E6=95=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sched/cfs/scx_cfs.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sched/cfs/scx_cfs.cpp b/sched/cfs/scx_cfs.cpp index 4082cd2..5946be9 100644 --- a/sched/cfs/scx_cfs.cpp +++ b/sched/cfs/scx_cfs.cpp @@ -204,9 +204,11 @@ struct enqueued_task { State task_state_; }; -static bool less(struct enqueued_task* t1, struct enqueued_task* t2) { - return (*t1) < (*t2); -} +struct Compare { + bool operator()(const enqueued_task* t1, const enqueued_task* t2) const { + return (*t1) < (*t2); + } +}; void enqueued_task::set_state(const enqueued_task::State &state) { @@ -259,7 +261,7 @@ class CfsRq { struct enqueued_task *cur_; private: - std::set<struct enqueued_task *, decltype(&less)> cfs_rq_; + std::set<struct enqueued_task *, Compare> cfs_rq_; static const int kmin_nice_ = -20; static const int kmax_nice_ = 19; Duration min_vruntime_; -- GitLab