xen: credit2: rb-tree for runqueues
v2 cover xen: credit2: rb-tree for runqueues
Hi All,
This is the continued work with respect to rb-tree usage in Credit2, as mentioned in previous conversations
https://lists.xen.org/archives/html/xen-devel/2017-06/msg01968.html
https://lists.xen.org/archives/html/xen-devel/2018-04/msg00119.html
The patch optimized the Credit2 runqueue from sorted queue to rb-tree. This will help in performance and scalability, when we have huge number of vCPUs.
Please provide your comments over the changes done.
Changes since v1:
* Renamed the rb_runq_insert
* Corrected the next probable runq element from rb_first to rb_last
* Introduced new cached attribute to have next runqueue entry cached
root (2):
xen: credit2: rb-tree for runqueues
xen: credit2: cached attribute for next runqueue entry
xen/common/sched_credit2.c | 95 +++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 34 deletions(-)
Signature · 7 lines
2.13.7 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 1/2 xen: credit2: rb-tree for runqueues
The patch optimized the sorted credit2 runq from simple linked list to rb-tree implementation. This way we will gain performance and scalability when the number of vCPUs are huge.
---
Changes since v1:
* Renamed the rb_runq_insert
* Corrected the next probable runq element from rb_first to rb_last
--- xen/common/sched_credit2.c | 88 +++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 33 deletions(-)
xen/common/sched_credit2.c +55 -33
@@ -25,6 +25,7 @@#include <xen/trace.h> #include <xen/cpu.h> #include <xen/keyhandler.h> +#include <xen/rbtree.h> /* Meant only for helping developers during debugging. */ /* #define d2printk printk */
@@ -471,7 +472,7 @@ custom_param("credit2_runqueue", parse_credit2_runqueue);struct csched2_runqueue_data {spinlock_t lock;/* Lock for this runqueue */ -struct list_head runq;/* Ordered list of runnable vms */ +struct rb_root runq;/* Runqueue is an rbtree */ int id;/* ID of this runqueue (-1 if invalid) */ int load;/* Instantaneous load (num of non-idle vcpus) */
@@ -535,7 +536,7 @@ struct csched2_vcpu {s_time_t load_last_update;/* Last time average was updated */ s_time_t avgload;/* Decaying queue load */ -struct list_head runq_elem;/* On the runqueue (rqd->runq) */ +struct rb_node runq_elem;/* On the runqueue (rqd->runq) */ struct list_head parked_elem;/* On the parked_vcpus list */ struct list_head rqd_elem;/* On csched2_runqueue_data's svc list */ struct csched2_runqueue_data *migrate_rqd;/* Pre-determined migr. target */
@@ -604,6 +605,28 @@ static inline bool has_cap(const struct csched2_vcpu *svc)return svc->budget !=STIME_MAX ; } +static void rb_runq_insert (struct rb_root *root, +struct csched2_vcpu *svc, +int *pos) +{ +struct csched2_vcpu *entry =NULL ; +struct rb_node **node = &root->rb_node; +struct rb_node *parent =NULL ; + +while (*node) { + parent = *node; + entry = rb_entry(parent, struct csched2_vcpu, runq_elem); +if ( svc->credit < entry->credit ) + node = &parent->rb_left; +else + node = &parent->rb_right; + + (*pos)++; + } +rb_link_node (&svc->runq_elem, parent, node); +rb_insert_color (&svc->runq_elem, root); +} +/* * Hyperthreading (SMT) support. *
@@ -797,12 +820,12 @@ static s_time_t c2t(struct csched2_runqueue_data *rqd, s_time_t credit, struct cstatic inline int vcpu_on_runq (struct csched2_vcpu *svc) { -return !list_empty (&svc->runq_elem); +return !RB_EMPTY_NODE (&svc->runq_elem); } -static inline struct csched2_vcpu *runq_elem (struct list_head *elem) +static inline struct csched2_vcpu *runq_elem (struct rb_node *elem) { - return list_entry(elem, struct csched2_vcpu, runq_elem); + return rb_entry(elem, struct csched2_vcpu, runq_elem); }static void activate_runqueue (struct csched2_private *prv,int rqi)
@@ -816,7 +839,7 @@ static void activate_runqueue(struct csched2_private *prv, int rqi) rqd->max_weight =1 ; rqd->id = rqi;INIT_LIST_HEAD (&rqd->svc); -INIT_LIST_HEAD (&rqd->runq); + rqd->runq =RB_ROOT ;spin_lock_init (&rqd->lock);__cpumask_set_cpu (rqi, &prv->active_queues);
@@ -1276,9 +1299,8 @@ update_load(const struct scheduler *ops,static void runq_insert (const struct scheduler *ops,struct csched2_vcpu *svc) { -struct list_head *iter;unsigned int cpu = svc->vcpu->processor; -struct list_head * runq = &c2rqd (ops, cpu)->runq; +struct rb_root *runq = &c2rqd (ops, cpu)->runq;int pos =0 ;ASSERT (spin_is_locked (per_cpu (schedule_data, cpu).schedule_lock));
@@ -1291,16 +1313,7 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc)ASSERT (!svc->vcpu->is_running);ASSERT (!(svc->flags & CSFLAG_scheduled)); -list_for_each ( iter, runq ) - { -struct csched2_vcpu * iter_svc =runq_elem (iter); - -if ( svc->credit > iter_svc->credit ) -break ; - - pos++; - } -list_add_tail (&svc->runq_elem, iter); +rb_runq_insert (runq, svc, &pos);if (unlikely (tb_init_done) ) {
@@ -1320,7 +1333,8 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc)static inline void runq_remove (struct csched2_vcpu *svc) {ASSERT (vcpu_on_runq (svc)); -list_del_init (&svc->runq_elem); +rb_erase (&svc->runq_elem, &svc->rqd->runq); +RB_CLEAR_NODE (&svc->runq_elem); }void burn_credits (struct csched2_runqueue_data *rqd,struct csched2_vcpu *,s_time_t );
@@ -1789,6 +1803,7 @@ static void park_vcpu(struct csched2_vcpu *svc)* In both cases, we also add it to the list of parked vCPUs of the domain. */ __set_bit (_VPF_parked, &v->pause_flags); +if (vcpu_on_runq (svc) ) {runq_remove (svc);
@@ -2041,7 +2056,7 @@ csched2_alloc_vdata(const struct scheduler *ops, struct vcpu *vc, void *dd)return NULL ;INIT_LIST_HEAD (&svc->rqd_elem); -INIT_LIST_HEAD (&svc->runq_elem); +RB_CLEAR_NODE (&svc->runq_elem); svc->sdom = dd; svc->vcpu = vc;
@@ -2087,6 +2102,8 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc) }else if (vcpu_on_runq (svc) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__); +ASSERT (svc->rqd ==c2rqd (ops, vc->processor));update_load (ops, svc->rqd, svc,-1 ,NOW ());runq_remove (svc);
@@ -2114,6 +2131,7 @@ csched2_vcpu_wake(const struct scheduler *ops, struct vcpu *vc)if (unlikely (vcpu_on_runq (svc)) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__);SCHED_STAT_CRANK (vcpu_wake_onrunq); goto out; }
@@ -2505,6 +2523,7 @@ static void migrate(const struct scheduler *ops,/* It's not running; just move it */ if (vcpu_on_runq (svc) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__);runq_remove (svc);update_load (ops, svc->rqd,NULL ,-1 , now); on_runq =1 ;
@@ -2764,8 +2783,10 @@ csched2_vcpu_migrate(if (unlikely (!cpumask_test_cpu (new_cpu,cpupool_domain_cpumask (d))) ) {ASSERT (system_state == SYS_STATE_suspend); +if (vcpu_on_runq (svc) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__);runq_remove (svc);update_load (ops, svc->rqd,NULL ,-1 , now); }
@@ -3108,7 +3129,7 @@ csched2_vcpu_insert(const struct scheduler *ops, struct vcpu *vc)spinlock_t *lock;ASSERT (!is_idle_vcpu (vc)); -ASSERT (list_empty (&svc->runq_elem)); +ASSERT (RB_EMPTY_NODE (&svc->runq_elem));/* csched2_cpu_pick() expects the pcpu lock to be held */ lock =vcpu_schedule_lock_irq (vc);
@@ -3146,7 +3167,7 @@ csched2_vcpu_remove(const struct scheduler *ops, struct vcpu *vc)spinlock_t *lock;ASSERT (!is_idle_vcpu (vc)); -ASSERT (list_empty (&svc->runq_elem)); +ASSERT (RB_EMPTY_NODE (&svc->runq_elem));SCHED_STAT_CRANK (vcpu_remove);
@@ -3168,7 +3189,7 @@ csched2_runtime(const struct scheduler *ops, int cpu,s_time_t time, min_time;int rt_credit;/* Proposed runtime measured in credits */ struct csched2_runqueue_data *rqd =c2rqd (ops, cpu); -struct list_head *runq = &rqd->runq; +struct rb_root *runq = &rqd->runq;struct csched2_private *prv =csched2_priv (ops);/*
@@ -3206,17 +3227,18 @@ csched2_runtime(const struct scheduler *ops, int cpu,* 2) If there's someone waiting whose credit is positive, * run until your credit ~= his. */ -if ( !list_empty (runq) ) +if ( !RB_EMPTY_ROOT (runq) ) { -struct csched2_vcpu *swait =runq_elem (runq->next); +// Find the left most element, which is the most probable candidate +struct rb_node *node =rb_last (runq); +struct csched2_vcpu *swait =runq_elem (node);if ( !is_idle_vcpu (swait->vcpu) && swait->credit >0 ) { rt_credit = snext->credit - swait->credit; } } -/* * The next guy on the runqueue may actually have a higher credit, * if we've tried to avoid migrating him from a different cpu.
@@ -3265,7 +3287,7 @@ runq_candidate(struct csched2_runqueue_data *rqd, int cpu, s_time_t now, unsigned int *skipped) { - struct list_head *iter, *temp; + struct rb_node *iter = NULL;struct csched2_vcpu *snext =NULL ;struct csched2_private *prv =csched2_priv (per_cpu (scheduler, cpu));bool yield = false, soft_aff_preempt = false;
@@ -3350,9 +3372,8 @@ runq_candidate(struct csched2_runqueue_data *rqd, snext =csched2_vcpu (idle_vcpu[cpu]); check_runq: -list_for_each_safe ( iter, temp, &rqd->runq ) - { - struct csched2_vcpu * svc = list_entry(iter, struct csched2_vcpu, runq_elem); +for (iter =rb_last (&rqd->runq); iter !=NULL ; iter =rb_prev (iter)) { + struct csched2_vcpu * svc = rb_entry(iter, struct csched2_vcpu, runq_elem);if (unlikely (tb_init_done) ) {
@@ -3750,7 +3771,8 @@ csched2_dump(const struct scheduler *ops)for_each_cpu (i, &prv->active_queues) {struct csched2_runqueue_data *rqd = prv->rqd + i; -struct list_head *iter, *runq = &rqd->runq; +struct rb_root *runq = &rqd->runq; +struct rb_node *iter;int loop =0 ;/* We need the lock to scan the runqueue. */
@@ -3762,8 +3784,8 @@ csched2_dump(const struct scheduler *ops)dump_pcpu (ops, j);printk ("RUNQ:\n" ); -list_for_each ( iter, runq ) - { + +for (iter =rb_last (runq); iter !=NULL ; iter =rb_prev (iter)) {struct csched2_vcpu *svc =runq_elem (iter);if ( svc )
Signature · 7 lines
2.13.7 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines · Praveen Kumar
xen/common/sched_credit2.c +0
-0@@ -471,7 +472,7 @@ custom_param("credit2_runqueue", parse_credit2_runqueue);struct csched2_runqueue_data {spinlock_t lock;/* Lock for this runqueue */
Quoted 5 lines
- struct
list_headrunq; /* Ordered list of runnable
vms */
+ structrb_rootrunq; /* Runqueue is an
rbtree */
I wouldn't change the comment. It's useful to know that the idea is to use this field to keep a list of runnable vcpus, and that we want it to be ordered, which is what the comment currently says.
It's pointless to state that we're using an rb-tree, because once can tell that, by just looking at the data type.
Actually, the comment says "Ordered list of runnable vms", while I think it would be better if it said "Ordered list of runnable vcpus".
Changing "vms" to "vcpus" does not belong in this patch, strictly speaking, but if you want to do that, I could live with that. If you don't want to do it, then don't, and just leave the comment alone.
xen/common/sched_credit2.c static inline bool has_cap(const struct · line 614
Quoted 11 lines
xen/common/sched_credit2.c +5
-0@@ -604,6 +605,28 @@ static inline bool has_cap(const struct csched2_vcpu *svc)return svc->budget !=STIME_MAX ; } +static void rb_runq_insert (struct rb_root *root, +struct csched2_vcpu *svc, +int *pos) +{ +struct csched2_vcpu *entry =NULL ;
I'd call this (something like) 'parent_svc'.
It makes it easier to understand what's actually happening in the loop below.
Quoted 17 lines
xen/common/sched_credit2.c +13
-0+struct rb_node **node = &root->rb_node; +struct rb_node *parent =NULL ; + +while (*node) { + parent = *node; + entry = rb_entry(parent, struct csched2_vcpu, runq_elem); +if ( svc->credit < entry->credit ) + node = &parent->rb_left; +else + node = &parent->rb_right; + + (*pos)++; + }@@ -1789,6 +1803,7 @@ static void park_vcpu(struct csched2_vcpu *svc)* In both cases, we also add it to the list of parked vCPUs of the domain.
Quoted 7 lines
*/
__set_bit(_VPF_parked, &v->pause_flags);
+
if ( vcpu_on_runq(svc) )
{
runq_remove(svc);Unrelated change. Don't do it. :-)
xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler *ops, · line 2109
Quoted 9 lines
xen/common/sched_credit2.c +2
-0@@ -2087,6 +2102,8 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc) }else if (vcpu_on_runq (svc) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__); +
So, this, and all the various other lines similar to this are/have been useful for debugging, I guess?
That is ok, but I don't think they should land upstream. If you think they (or maybe some of them) should, then make it/them proper debugging output.
And, probably, do that in its own patch, as it would not be related to what the data structure used for the runqueues is.
But again, IMO, you can get rid of all of them.
xen/common/sched_credit2.c csched2_vcpu_migrate( · line 2790
Quoted 13 lines
ASSERT(svc->rqd == c2rqd(ops, vc->processor)); update_load(ops, svc->rqd, svc, -1, NOW()); runq_remove(svc);xen/common/sched_credit2.c +1
-0@@ -2764,8 +2783,10 @@ csched2_vcpu_migrate(if (unlikely (!cpumask_test_cpu (new_cpu,cpupool_domain_cpumask (d))) ) {ASSERT (system_state == SYS_STATE_suspend); +if (vcpu_on_runq (svc) ) {
Stray new-line again.
Quoted 10 lines
xen/common/sched_credit2.c +2
-2@@ -3206,17 +3227,18 @@ csched2_runtime(const struct scheduler *ops,int cpu, * 2) If there's someone waiting whose credit is positive, * run until your credit ~= his. */ -if ( !list_empty (runq) ) +if ( !RB_EMPTY_ROOT (runq) ) { -struct csched2_vcpu *swait =runq_elem (runq->next); +// Find the left most element, which is the most probable
Quoted 3 lines
candidate
+ structrb_node*node =rb_last(runq);
Comment style. And I think I'd say "Check the rightmost element in the tree, which is the one with the highest credits"
Quoted 2 lines
+ struct
csched2_vcpu*swait =runq_elem(node);
I think we can do:
struct csched2_vcpu *swait = runq_elem(rb_last(runq));
Yeah, matter of taste, mostly. Still...
Anyway, if you keep the code like this, no blanks in-between definitions. And you instead want one between the last definition and the if below.
Quoted 11 lines
if ( ! is_idle_vcpu(swait->vcpu) && swait->credit > 0 ) { rt_credit = snext->credit - swait->credit; } }-
/*
* The next guy on the runqueue may actually have a higher
credit,
Spurious change.
Quoted 14 lines
* if we've tried to avoid migrating him from a different cpu.
xen/common/sched_credit2.c +2
-3@@ -3350,9 +3372,8 @@ runq_candidate(struct csched2_runqueue_data *rqd, snext =csched2_vcpu (idle_vcpu[cpu]); check_runq: -list_for_each_safe ( iter, temp, &rqd->runq ) - { - struct csched2_vcpu * svc = list_entry(iter, struct csched2_vcpu, runq_elem); +for (iter =rb_last (&rqd->runq); iter !=NULL ; iter =rb_prev (iter)) { + struct csched2_vcpu * svc = rb_entry(iter, struct
Quoted 2 lines
csched2_vcpu,runq_elem);
I was about to comment about the fact that doing a for() is not the same as doing the list_for_each_safe() (because of the *_safe() part).
However, looking better, I don't think I see why list_for_each_safe() (I mean, as opposed to just list_for_each()) was used in the first place.
So, yes, I think this is actually ok. Anyone, if there's something I'm missing, please, point it out. :-D
Quoted 11 lines
xen/common/sched_credit2.c +2
-2@@ -3762,8 +3784,8 @@ csched2_dump(const struct scheduler *ops)dump_pcpu (ops, j);printk ("RUNQ:\n" ); -list_for_each ( iter, runq ) - { + +for (iter =rb_last (runq); iter !=NULL ; iter =rb_prev (iter)) {struct csched2_vcpu *svc =runq_elem (iter);
Quoted 2 lines
if ( svc )
Ok, this makes sense. Have you verified that the runqueue is printed in credits order in the dump?
Regards,
Dario
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli Software Engineer @ SUSE https://www.suse.com/
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi Dario,
Thanks for your comments.
Quoted 187 lines · Dario Faggioli
Quoted 10 lines · Praveen Kumar
xen/common/sched_credit2.c +0
-0@@ -471,7 +472,7 @@ custom_param("credit2_runqueue", parse_credit2_runqueue);struct csched2_runqueue_data {spinlock_t lock;/* Lock for this runqueue */
Quoted 5 lines
- struct
list_headrunq; /* Ordered list of runnable
vms */
+ structrb_rootrunq; /* Runqueue is an
rbtree */I wouldn't change the comment. It's useful to know that the idea is to use this field to keep a list of runnable vcpus, and that we want it to be ordered, which is what the comment currently says.
It's pointless to state that we're using an rb-tree, because once can tell that, by just looking at the data type.
Actually, the comment says "Ordered list of runnable vms", while I think it would be better if it said "Ordered list of runnable vcpus".
Changing "vms" to "vcpus" does not belong in this patch, strictly speaking, but if you want to do that, I could live with that. If you don't want to do it, then don't, and just leave the comment alone.
xen/common/sched_credit2.c static inline bool has_cap(const struct · line 614
Quoted 11 lines
xen/common/sched_credit2.c +5
-0@@ -604,6 +605,28 @@ static inline bool has_cap(const struct csched2_vcpu *svc)return svc->budget !=STIME_MAX ; } +static void rb_runq_insert (struct rb_root *root, +struct csched2_vcpu *svc, +int *pos) +{ +struct csched2_vcpu *entry =NULL ;I'd call this (something like) '
parent_svc'.It makes it easier to understand what's actually happening in the loop below.
Quoted 17 lines
xen/common/sched_credit2.c +13
-0+struct rb_node **node = &root->rb_node; +struct rb_node *parent =NULL ; + +while (*node) { + parent = *node; + entry = rb_entry(parent, struct csched2_vcpu, runq_elem); +if ( svc->credit < entry->credit ) + node = &parent->rb_left; +else + node = &parent->rb_right; + + (*pos)++; + }@@ -1789,6 +1803,7 @@ static void park_vcpu(struct csched2_vcpu *svc)* In both cases, we also add it to the list of parked vCPUs of the domain.
Quoted 7 lines
*/ __set_bit(_VPF_parked, &v->pause_flags); + if ( vcpu_on_runq(svc) ) { runq_remove(svc);Unrelated change. Don't do it. :-)
xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler *ops, · line 2109
Quoted 9 lines
xen/common/sched_credit2.c +2
-0@@ -2087,6 +2102,8 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc) }else if (vcpu_on_runq (svc) ) { +printk (XENLOG_WARNING "%s : %d : vcpu on runq rb !\n" , __func__, __LINE__); +So, this, and all the various other lines similar to this are/have been useful for debugging, I guess?
That is ok, but I don't think they should land upstream. If you think they (or maybe some of them) should, then make it/them proper debugging output.
And, probably, do that in its own patch, as it would not be related to what the data structure used for the runqueues is.
But again, IMO, you can get rid of all of them.
xen/common/sched_credit2.c csched2_vcpu_migrate( · line 2790
Quoted 13 lines
ASSERT(svc->rqd == c2rqd(ops, vc->processor)); update_load(ops, svc->rqd, svc, -1, NOW()); runq_remove(svc);xen/common/sched_credit2.c +1
-0@@ -2764,8 +2783,10 @@ csched2_vcpu_migrate(if (unlikely (!cpumask_test_cpu (new_cpu,cpupool_domain_cpumask (d))) ) {ASSERT (system_state == SYS_STATE_suspend); +if (vcpu_on_runq (svc) ) {Stray new-line again.
Quoted 10 lines
xen/common/sched_credit2.c +2
-2@@ -3206,17 +3227,18 @@ csched2_runtime(const struct scheduler *ops,int cpu, * 2) If there's someone waiting whose credit is positive, * run until your credit ~= his. */ -if ( !list_empty (runq) ) +if ( !RB_EMPTY_ROOT (runq) ) { -struct csched2_vcpu *swait =runq_elem (runq->next); +// Find the left most element, which is the most probable Quoted 3 lines
candidate
+ structrb_node*node =rb_last(runq);Comment style. And I think I'd say "Check the rightmost element in the tree, which is the one with the highest credits"
Quoted 2 lines
+ struct
csched2_vcpu*swait =runq_elem(node);I think we can do:
struct csched2_vcpu *swait = runq_elem(rb_last(runq));Yeah, matter of taste, mostly. Still...
Anyway, if you keep the code like this, no blanks in-between definitions. And you instead want one between the last definition and the if below.
Quoted 11 lines
if ( ! is_idle_vcpu(swait->vcpu) && swait->credit > 0 ) { rt_credit = snext->credit - swait->credit; } }-
/*
* The next guy on the runqueue may actually have a higher
credit,Spurious change.
Quoted 14 lines
* if we've tried to avoid migrating him from a different cpu.
xen/common/sched_credit2.c +2
-3@@ -3350,9 +3372,8 @@ runq_candidate(struct csched2_runqueue_data *rqd, snext =csched2_vcpu (idle_vcpu[cpu]); check_runq: -list_for_each_safe ( iter, temp, &rqd->runq ) - { - struct csched2_vcpu * svc = list_entry(iter, struct csched2_vcpu, runq_elem); +for (iter =rb_last (&rqd->runq); iter !=NULL ; iter =rb_prev (iter)) { + struct csched2_vcpu * svc = rb_entry(iter, structQuoted 2 lines
csched2_vcpu,runq_elem);I was about to comment about the fact that doing a
for()is not the same as doing thelist_for_each_safe()(because of the*_safe()part).However, looking better, I don't think I see why
list_for_each_safe()(I mean, as opposed to justlist_for_each()) was used in the first place.So, yes, I think this is actually ok. Anyone, if there's something I'm missing, please, point it out. :-D
Quoted 11 lines
xen/common/sched_credit2.c +2
-2@@ -3762,8 +3784,8 @@ csched2_dump(const struct scheduler *ops)dump_pcpu (ops, j);printk ("RUNQ:\n" ); -list_for_each ( iter, runq ) - { + +for (iter =rb_last (runq); iter !=NULL ; iter =rb_prev (iter)) {struct csched2_vcpu *svc =runq_elem (iter);
Quoted 5 lines
Quoted 2 lines
if ( svc )
Ok, this makes sense. Have you verified that the runqueue is printed in credits order in the dump?
Yes, I have dumped this using 'xl debug-keys r'
dmesg output :
...
(XEN) sched_smt_power_savings: disabled (XEN) NOW=549728084734
(XEN) Online Cpus: 0-3
(XEN) Cpupool 0:
(XEN) Cpus: 0-3
(XEN) Scheduler: SMP Credit Scheduler rev2 (credit2) (XEN) Active queues: 1
(XEN) default-weight = 256
(XEN) Runqueue 0:
(XEN) ncpus = 4 (XEN) cpus = 0-3 (XEN) max_weight = 256 (XEN) pick_bias = 1 (XEN) instload = 1 (XEN) aveload = 10002 (~3%)
(XEN) idlers: 7
(XEN) tickled: 0
(XEN) fully idle cores: 7
(XEN) Domain info:
(XEN) Domain: 0 w 256 c 0 v 4
(XEN) 1: [0.0] flags=2 cpu=3 credit=9803083 [w=256] load=517 (~0%) (XEN) 2: [0.1] flags=0 cpu=1 credit=10404026 [w=256] load=239 (~0%) (XEN) 3: [0.2] flags=0 cpu=0 credit=10369899 [w=256] load=2193 (~0%) (XEN) 4: [0.3] flags=0 cpu=2 credit=10500000 [w=256] load=1354 (~0%)
(XEN) Runqueue 0:
(XEN) CPU[00] runq=0, sibling=1, core=f
(XEN) CPU[01] runq=0, sibling=2, core=f
(XEN) CPU[02] runq=0, sibling=4, core=f
(XEN) CPU[03] runq=0, sibling=8, core=f
(XEN) run: [0.0] flags=2 cpu=3 credit=9803083 [w=256] load=517 (~0%) (XEN) RUNQ:
kern-xen-dev:~ #
Will send the updated patch soon. Thanks.
Regards,
~Praveen.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 2 lines · Praveen Kumar
Hi Dario,
Hi,
Quoted 2 lines
Thanks for your comments.
So, two things. Can you please not top post? :-)
Also, trim the quotes, which means, when quoting while replying, which is the good thing to do, remove the stuff that are not necessary for understanding what you are replying to.
This avoids people having to scroll down for pages and pages, before finding what you wrote. Even worse, lack of trimming, especially if combined with top posting, like in this email, may make people think that there's no other content in your mail, besides the top-posted one! :-O
For instance, the first time I opened this message of yours, I thought like that and did not scrolled down. It was only on second thoughts that I came back and went double checking. :-)
Trying to make all the above clear, what I mean is do like this:
Quoted 51 lines
Quoted 15 lines · Dario Faggioli
Quoted 14 lines · Praveen Kumar
xen/common/sched_credit2.c +2
-2@@ -3762,8 +3784,8 @@ csched2_dump(const struct scheduler *ops)dump_pcpu (ops, j);printk ("RUNQ:\n" ); -list_for_each ( iter, runq ) - { + +for (iter =rb_last (runq); iter !=NULL ; iter =rb_prev (iter)) {struct csched2_vcpu *svc =runq_elem (iter);Quoted 6 lines
Quoted 2 lines
if ( svc )
Ok, this makes sense. Have you verified that the runqueue is printed in
credits order in the dump?Yes, I have dumped this using 'xl debug-keys r'
dmesg output :
...
(XEN)sched_smt_power_savings: disabled (XEN) NOW=549728084734
(XEN) Online Cpus: 0-3
(XEN) Cpupool 0:
(XEN) Cpus: 0-3
(XEN) Scheduler: SMP Credit Scheduler rev2 (credit2) (XEN) Active queues: 1
(XEN) default-weight = 256
(XEN) Runqueue 0:(XEN) ncpus = 4 (XEN) cpus = 0-3 (XEN) max_weight = 256 (XEN) pick_bias = 1 (XEN) instload = 1 (XEN) aveload = 10002 (~3%)(XEN) idlers: 7
(XEN) tickled: 0
(XEN) fully idle cores: 7
(XEN) Domain info:
(XEN) Domain: 0 w 256 c 0 v 4(XEN) 1: [0.0] flags=2 cpu=3 credit=9803083 [w=256] load=517 (~0%) (XEN) 2: [0.1] flags=0 cpu=1 credit=10404026 [w=256] load=239 (~0%) (XEN) 3: [0.2] flags=0 cpu=0 credit=10369899 [w=256] load=2193
Quoted 11 lines
(~0%)
(XEN) 4: [0.3] flags=0 cpu=2 credit=10500000 [w=256] load=1354
(~0%)
(XEN) Runqueue 0:
(XEN) CPU[00] runq=0, sibling=1, core=f
(XEN) CPU[01] runq=0, sibling=2, core=f
(XEN) CPU[02] runq=0, sibling=4, core=f
(XEN) CPU[03] runq=0, sibling=8, core=f
(XEN) run: [0.0] flags=2 cpu=3 credit=9803083 [w=256] load=517 (~0%) (XEN) RUNQ:
Well, ok, but in this case there's no vcpu sitting in the runqueue. So, we can't really see whether the runqueue is actually kept in the proper order.
Not that a test like this is too valuable and, now that I think more about this, it's probably worth putting some ASSERT()-s in the code to properly make sure of that... but still, it's something.
In order to actually have and see vcpus within the scheduler runqueues, you can start a lot of domains --at least enough for having more vcpus than pcpus-- and run some synthetic workload (like `yes') inside them, so their vcpu will be busy.
Playing with pinning and cpupools can also help.
In the end, what you want is to have something printed after that:
(XEN) RUNQ:
marker, in the dump.
Quoted 2 lines
Will send the updated patch soon. Thanks.
Cool, thanks. :-)
Dario
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli Software Engineer @ SUSE https://www.suse.com/
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 2/2 xen: credit2: cached attribute for next runqueue entry
In reply to Praveen KumarThe patch introduces a cached attribute (next_elem) in csched2_runqueue_data which will keep track of next runq candidate which has the maximum credit score within the runqueue. This will save unnecessary tree traversal during the time of scheduling. This element will be populated during the removal and addition of the vcpus in the runqueue.
---
Changed since v1:
* Newly introduced patch
--- xen/common/sched_credit2.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
xen/common/sched_credit2.c +9 -4
@@ -473,6 +473,7 @@ struct csched2_runqueue_data {spinlock_t lock;/* Lock for this runqueue */ struct rb_root runq;/* Runqueue is an rbtree */ +struct rb_node *next_elem;/* Cached entry to run next */ int id;/* ID of this runqueue (-1 if invalid) */ int load;/* Instantaneous load (num of non-idle vcpus) */
@@ -1301,6 +1302,7 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc) {unsigned int cpu = svc->vcpu->processor;struct rb_root *runq = &c2rqd (ops, cpu)->runq; +struct csched2_runqueue_data *rqd = svc->rqd;int pos =0 ;ASSERT (spin_is_locked (per_cpu (schedule_data, cpu).schedule_lock));
@@ -1314,6 +1316,7 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc)ASSERT (!(svc->flags & CSFLAG_scheduled));rb_runq_insert (runq, svc, &pos); + rqd->next_elem =rb_last (runq);if (unlikely (tb_init_done) ) {
@@ -1332,9 +1335,13 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc)static inline void runq_remove (struct csched2_vcpu *svc) { +struct csched2_runqueue_data *rqd = svc->rqd; +ASSERT (vcpu_on_runq (svc));rb_erase (&svc->runq_elem, &svc->rqd->runq);RB_CLEAR_NODE (&svc->runq_elem); + + rqd->next_elem =rb_last (&rqd->runq); }void burn_credits (struct csched2_runqueue_data *rqd,struct csched2_vcpu *,s_time_t );
@@ -3230,9 +3237,7 @@ csched2_runtime(const struct scheduler *ops, int cpu,if ( !RB_EMPTY_ROOT (runq) ) {// Find the left most element, which is the most probable candidate -struct rb_node *node =rb_last (runq); - -struct csched2_vcpu *swait =runq_elem (node); +struct csched2_vcpu *swait =runq_elem (rqd->next_elem);if ( !is_idle_vcpu (swait->vcpu) && swait->credit >0 ) {
@@ -3372,7 +3377,7 @@ runq_candidate(struct csched2_runqueue_data *rqd, snext =csched2_vcpu (idle_vcpu[cpu]); check_runq: -for (iter =rb_last (&rqd->runq); iter !=NULL ; iter =rb_prev (iter)) { +for (iter = rqd->next_elem; iter !=NULL ; iter =rb_prev (iter)) { struct csched2_vcpu * svc = rb_entry(iter, struct csched2_vcpu, runq_elem);if (unlikely (tb_init_done) )
Signature · 7 lines
2.13.7 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [RFC PATCH 2/2] xen: credit2: cached attribute for next runqueue entry
Quoted 11 lines · Praveen Kumar
xen/common/sched_credit2.c +0
-0@@ -473,6 +473,7 @@ struct csched2_runqueue_data {spinlock_t lock;/* Lock for this runqueue */
struct
rb_rootrunq; /* Runqueue is an
rbtree */
+ structrb_node*next_elem; /* Cached entry to run
next */
I don't like the name much. IIRC, in Linux, the field was called 'rb_leftmost'. So we can call it rb_rightmost.
Or maybe something like rq_rightmost, or rq_first (or runq_first).
And in the comment, I'd say something like, "vcpu with highest credits in the runq"
Quoted 11 lines
xen/common/sched_credit2.c +1
-0@@ -1301,6 +1302,7 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc) {unsigned int cpu = svc->vcpu->processor;struct rb_root *runq = &c2rqd (ops, cpu)->runq; +struct csched2_runqueue_data *rqd = svc->rqd;int pos =0 ;ASSERT(spin_is_locked(per_cpu(schedule_data,
cpu).schedule_lock));
xen/common/sched_credit2.c runq_insert(const struct scheduler *ops, struct · line 1321
Quoted 6 lines
xen/common/sched_credit2.c +1
-0@@ -1314,6 +1316,7 @@ runq_insert(const struct scheduler *ops, struct csched2_vcpu *svc) ASSERT(!(svc->flags & CSFLAG_scheduled));rb_runq_insert (runq, svc, &pos); + rqd->next_elem =rb_last (runq);
Ah, ok, yes, using rb_last() here (and in runq_remove()) is indeed better than doing it int runq_candidate().
Still not what I had in mind, though.
In fact, the whole point of this "let's cache the rightmost element" would be to avoid having to call rb_last() pretty much at all.
How? Well, basically, inside the while() that you're adding here in runq_insert(), you make a note about whether the new vcpu has been inserted in the righmost spot of the tree. And if it was, you update the cache.
Similarly, when removing a vcpu, you check whether you are removing the rightmost element of the tree. And if you are, you update the cache.
For instance, see how they've been doing this in CFS (before the rb- tree cached helpers were introduced), when inserting a task in the runqueue:
https://elixir.bootlin.com/linux/v3.15/source/kernel/sched/fair.c#L490
and when removing it:
https://elixir.bootlin.com/linux/v3.15/source/kernel/sched/fair.c#L526
Regards,
Dario
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli Software Engineer @ SUSE https://www.suse.com/
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi All,
Quoted 24 lines · Praveen Kumar
Hi All,
This is the continued work with respect to rb-tree usage in Credit2, as mentioned in previous conversations
https://lists.xen.org/archives/html/xen-devel/2017-06/msg01968.html
https://lists.xen.org/archives/html/xen-devel/2018-04/msg00119.htmlThe patch optimized the Credit2 runqueue from sorted queue to rb-tree. This will help in performance and scalability, when we have huge number of vCPUs.
Please provide your comments over the changes done.
Changes since v1:
* Renamed therb_runq_insert
* Corrected the next probable runq element fromrb_firsttorb_last
* Introduced new cached attribute to have next runqueue entry cachedroot (2):
xen: credit2: rb-tree for runqueues
xen: credit2: cached attribute for next runqueue entry
Quoted 6 lines
xen/common/sched_credit2.c | 95 +++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 34 deletions(-)--
2.13.7
Please provide your comments on the changes.
Thanks in advance.
Regards,
~Praveen.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 2 lines · Praveen Kumar
Hi All,
Hi Praveen,
Quoted 17 lines
On Fri, Jan 4, 2019 at 3:21 PM Praveen Kumar <[email protected]
Quoted 16 lines
wrote:
Hi All,This is the continued work with respect to rb-tree usage in Credit2, as
mentioned in previous conversationshttps://lists.xen.org/archives/html/xen-devel/2017-06/msg01968.html
https://lists.xen.org/archives/html/xen-devel/2018-04/msg00119.htmlThe patch optimized the Credit2 runqueue from sorted queue to rb- tree.
This will help in performance and scalability, when we have huge number of
vCPUs.
Quoted 6 lines
Quoted 2 lines
Please provide your comments over the changes done.
Please provide your comments on the changes.
Thanks in advance.
Yes, I am actually looking at this series right now.
Sorry it's taking a bit, but you hit the combination of Winter vacation and freeze for the next Xen release. :-P
I'll let you have my comments latter today.
Regards,
Dario
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli Software Engineer @ SUSE https://www.suse.com/
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel