xen/tools: Credit2: implement caps.
v2 cover xen/tools: Credit2: implement caps.
This is v2 of the 'caps for Credit2' series.
Posting of v1 is here:
https://lists.xen.org/archives/html/xen-devel/2017-06/msg00700.html
No change wrt that, apart from taking care of the review comments. The patch that required more rework is patch 1, as I changed how a corner case (budget overrun, due to potential timer or accounting issues) is dealt with, complying with what George suggested and thought it was best.
Note, however, that this series is *NOT* based on top of staging. In fact, it is based on top of staging + "Soft affinity for Credit2, v2":
https://lists.xen.org/archives/html/xen-devel/2017-07/msg02802.html
Reason I did things like this is that the two series do clash, and since the soft affinity one is pretty much all acked and ready to go in (with the only exception of patch 2, as George still needs to look at it), I just assumed that one will go in first, and based on top of it.
In fact, as I'm leaving for 2 weeks, having done things like this allows one to commit both the series, even with me away, in case both collect all the needed acks, of course (hey, one can dream, can't him? :-D :-D).
As usual, I aslo prepared a git branch:
git://xenbits.xen.org/people/dariof/xen.git rel/sched/credit2-caps-v2
https://travis-ci.org/fdario/xen/builds/266018957
Thanks and Regards,
Dario
---
Dario Faggioli (4):
xen: credit2: implement utilization cap
xen: credit2: allow to set and get utilization cap
xen: credit2: improve distribution of budget (for domains with caps) libxl/xl: allow to get and set cap on Credit2.
tools/libxl/libxl_sched.c | 21 + tools/xentrace/formats | 2 tools/xentrace/xenalyze.c | 10 - tools/xl/xl_cmdtable.c | 1 tools/xl/xl_sched.c | 25 +- xen/common/sched_credit2.c | 676 ++++++++++++++++++++++++++++++++++++++++--- xen/include/public/domctl.h | 1 xen/include/xen/sched.h | 3 8 files changed, 682 insertions(+), 57 deletions(-)
--
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 1/4 xen: credit2: implement utilization cap
This commit implements the Xen part of the cap mechanism for Credit2.
A cap is how much, in terms of % of physical CPU time, a domain can execute at most.
For instance, a domain that must not use more than 1/4 of one physical CPU, must have a cap of 25%; one that must not use more than 1+1/2 of physical CPU time, must be given a cap of 150%.
Caps are per domain, so it is all a domain's vCPUs, cumulatively, that will be forced to execute no more than the decided amount.
This is implemented by giving each domain a 'budget', and using a (per-domain again) periodic timer. Values of budget and 'period' are chosen so that budget/period is equal to the cap itself.
Budget is burned by the domain's vCPUs, in a similar way to how credits are.
When a domain runs out of budget, its vCPUs can't run any longer. They can gain, when the budget is replenishment by the timer, which event happens once every period.
Blocking the vCPUs because of lack of budget happens by means of a new (_VPF_parked) pause flag, so that, e.g., vcpu_runnable() still works. This is similar to what is done in sched_rtds.c, as opposed to what happens in sched_credit.c, where vcpu_pause() and vcpu_unpause() (which means, among other things, more overhead).
Note that, while adding new fields to csched2_vcpu and csched2_dom, currently existing members are being moved around, to achieve best placement inside cache lines.
Note also that xenalyze and tools/xentrace/format are being updated too.
---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Anshul Makkar <[email protected]>
---
Changed from v1:
* used has_cap() instead of open coding it in burn_credits();
* removed some of the unlikely() around has_cap(), as, although cap is not on by default, it's up to the user to decide how many domains will have caps, and we can't assume much about what users will actually do;
* tried to clarify the comment about (the non deterministic nature of the) CPU capacity distribution between the vCPUs of a multi-vCPUs guest;
* clarified the comment about budget being replenished to nothing more than top capacity, i.e., about the fact that budget is *not* being accumulated across different period;
* fixed many style and typo issues in comments;
* added a comment about the budget distribution logic (to the vCPUs) being subject to be refined in subsequent commits;
* renaming:
vcpu_try_to_get_budget() --> vcpu_grab_budget() vcpu_give_back_budget() --> vcpu_return_budget() repl_sdom_budget() --> replanish_domain_budget()
* change how replenishment logic deals with cases of overrun. In v1, we were always doing multiple replenishment at once, until the domain's budget was back into the black. Now, in cases of substantial overrun, we just do one replenishment, and rely on future ones to bring back the budget into being a positive number. This was agreed upon with George during v1's review.
--- tools/xentrace/formats | 2 tools/xentrace/xenalyze.c | 10 + xen/common/sched_credit2.c | 521 +++++++++++++++++++++++++++++++++++++++++--- xen/include/xen/sched.h | 3 4 files changed, 492 insertions(+), 44 deletions(-)
tools/xentrace/formats +1 -1
@@ -51,7 +51,7 @@ 0x00022201 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:tick 0x00022202 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:runq_pos [ dom:vcpu = 0x%(1)08x, pos = %(2)d] -0x00022203 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:credit burn [ dom:vcpu = 0x%(1)08x, credit = %(2)d, delta = %(3)d ] +0x00022203 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:credit burn [ dom:vcpu = 0x%(1)08x, credit = %(2)d, budget = %(3)d, delta = %(4)d ] 0x00022204 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:credit_add 0x00022205 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:tickle_check [ dom:vcpu = 0x%(1)08x, credit = %(2)d, score = %(3)d ] 0x00022206 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) csched2:tickle [ cpu = %(1)d ]
tools/xentrace/xenalyze.c +6 -4
@@ -7680,12 +7680,14 @@ void sched_process(struct pcpu_info *p)if (opt.dump_all) {struct {unsigned int vcpuid:16 , domid:16 ; -int credit, delta; +int credit, budget, delta; } *r = (typeof (r ))ri->d; -printf (" %s csched2:burn_credits d%uv%u, credit = %d, delta = %d\n" , - ri->dump_header, r->domid, r->vcpuid, - r->credit, r->delta); +printf (" %s csched2:burn_credits d%uv%u, credit = %d, " , + ri->dump_header, r->domid, r->vcpuid, r->credit); +if ( r->budget !=INT_MIN ) +printf ("budget = %d, " , r->budget); +printf ("delta = %d\n" , r->delta); }break ;case TRC_SCHED_CLASS_EVT (CSCHED2 ,5 ):/* TICKLE_CHECK */
xen/common/sched_credit2.c +482 -39
747 lines not on this page · raw message
xen/include/xen/sched.h +3 -0
@@ -792,6 +792,9 @@ static inline struct domain *next_domain_in_cpupool(/* VCPU is being reset. */ #define _VPF_in_reset 7#define VPF_in_reset (1UL<<_VPF_in_reset) +/* VCPU is parked. */ +#define _VPF_parked 8 +#define VPF_parked (1UL<<_VPF_parked)static inline int vcpu_runnable (struct vcpu *v) {
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/4] xen: credit2: implement utilization cap
Quoted 17 lines · Dario Faggioli
xen/common/sched_credit2.c +6
-0@@ -474,6 +586,12 @@ static inline struct csched2_runqueue_data *c2rqd(const struct scheduler *ops,return &csched2_priv (ops)->rqd[c2r (cpu)]; } +/* Does the domain of this vCPU have a cap? */ +static inline bool has_cap (const struct csched2_vcpu *svc) +{ +return svc->budget !=STIME_MAX ; +} +/* * Hyperthreading (SMT) support. * @@ -1515,7 +1633,16 @@ static void reset_credit(const struct scheduler *ops, int cpu, s_time_t now,* that the credit it has spent so far get accounted. */
xen/common/sched_credit2.c static void reset_credit(const struct scheduler *ops, int cpu, s_time_t now, · line 1644
Quoted 10 lines
xen/common/sched_credit2.c +8
-0if ( svc->vcpu ==curr_on_cpu (svc_cpu) ) + {burn_credits (rqd, svc, now); +/* +* And, similarly, in case it has run out of budget, as a +* consequence of this round of accounting, we also must inform +* its pCPU that it's time to park it, and pick up someone else. +*/ +if (unlikely (svc->budget <=0 ) ) +tickle_cpu (svc_cpu, rqd);
This is for accounting of credit. Why it willl impact the budget. Do you intend to refer that budget of current vcpu expired while doing calculation for credit ??
Quoted 46 lines
xen/common/sched_credit2.c +30
-7+ } start_credit = svc->credit;@@ -1571,27 +1698,35 @@ void burn_credits(struct csched2_runqueue_data *rqd, delta = now - svc->start_time; -if (likely (delta >0 ) ) - { -SCHED_STAT_CRANK (burn_credits_t2c); -t2c_update (rqd, delta, svc); - svc->start_time = now; - } -else if ( delta <0 ) +if (unlikely (delta <=0 ) ) { +static void replenish_domain_budget (void * data) +{ +struct csched2_dom *sdom = data; +unsigned long flags; +s_time_t now; +LIST_HEAD (parked); + +spin_lock_irqsave (&sdom->budget_lock, flags); + + now =NOW (); + +/* +* Let's do the replenishment. Note, though, that a domain may overrun, +* which means the budget would have gone below 0 (reasons may be system +* overbooking, accounting issues, etc.). It also may happen that we are +* handling the replenishment (much) later than we should (reasons may +* again be overbooking, or issues with timers). +* +* Even in cases of overrun or delay, however, we expect that in 99% of +* cases, doing just one replenishment will be good enough for being able +* to unpark the vCPUs that are waiting for some budget. +*/ +do_replenish (sdom); + +/* +* And now, the special cases: +* 1) if we are late enough to have skipped (at least) one full period, +* what we must do is doing more replenishments. Note that, however,
xen/common/sched_credit2.c void burn_credits(struct csched2_runqueue_data *rqd,
Quoted 10 lines
xen/common/sched_credit2.c +10
-0+* every time we add tot_budget to the budget, we also move next_repl +* away by CSCHED2_BDGT_REPL_PERIOD, to make sure the cap is always +* respected. +*/ +if (unlikely (sdom->next_repl <= now) ) + { +do +do_replenish (sdom); +while ( sdom->next_repl <= now ); + }
Just a bit confused. Have you seen this kind of scenario. Please can you explain it. Is this condition necessary.
xen/common/sched_credit2.c void burn_credits(struct csched2_runqueue_data *rqd, · line 1959
Quoted 10 lines
xen/common/sched_credit2.c +10
-0+/* +* 2) if we overrun by more than tot_budget, then budget+tot_budget is +* still < 0, which means that we can't unpark the vCPUs. Let's bail, +* and wait for future replenishments. +*/ +if (unlikely (sdom->budget <=0 ) ) + { +spin_unlock_irqrestore (&sdom->budget_lock, flags); + goto out; + }
"if we overran by more than tot_budget in previous run", make is more clear..
Quoted 23 lines
xen/common/sched_credit2.c +19
-0+ +/* Since we do more replenishments, make sure we didn't overshot. */ + sdom->budget =min (sdom->budget, sdom->tot_budget); + +/* +* As above, let's prepare the temporary list, out of the domain's +* parked_vcpus list, now that we hold the budget_lock. Then, drop such +* lock, and pass the list to the unparking function. +*/ +list_splice_init (&sdom->parked_vcpus, &parked); + +spin_unlock_irqrestore (&sdom->budget_lock, flags); + +unpark_parked_vcpus (sdom->dom->cpupool->sched, &parked); + + out: +set_timer (sdom->repl_timer, sdom->next_repl); +} +#ifndef NDEBUG static inline void csched2_vcpu_check (struct vcpu *vc)
xen/common/sched_credit2.c csched2_alloc_vdata(const struct scheduler *ops, struct vcpu *vc, void *dd) · line 2038
Quoted 4 lines
xen/common/sched_credit2.c +1
-0@@ -1658,6 +2035,9 @@ csched2_alloc_vdata(const struct scheduler *ops, struct vcpu *vc, void *dd) } svc->tickled_cpu =-1 ; +
Rest, looks good to me.
Thanks
Anshul
Quoted 1 line
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/4] xen: credit2: implement utilization cap
Quoted 16 lines · Anshul Makkar
Quoted 15 lines · Dario Faggioli
xen/common/sched_credit2.c +3
-0@@ -1515,7 +1633,16 @@ static void reset_credit(const structscheduler *ops, int cpu, s_time_t now, * that the credit it has spent so far get accounted. */ if ( svc->vcpu == curr_on_cpu(svc_cpu) ) + { burn_credits(rqd, svc, now); + /* +* And, similarly, in case it has run out of budget, as a
+ * consequence of this round of accounting, we also must inform
+ * its pCPU that it's time to park it, and pick up someone else.
Quoted 9 lines
Quoted 3 lines
+ */
+ if (unlikely(svc->budget <= 0))
+ tickle_cpu(svc_cpu, rqd);This is for accounting of credit. Why it willl impact the budget. Do you
intend to refer that
budget of current vcpu expired while doing calculation for credit ??
burn_credits() burns does budget acounting too now. So, it's entirely possible that the vCPU has actually run out of budget, and we figure it out now (and we should take appropriate actions!).
Quoted 60 lines
Quoted 60 lines
(quoted diff) +0
-0@@ -1571,27 +1698,35 @@ void burn_credits(struct csched2_runqueue_data *rqd,
delta = now -svc->start_time;
- if ( likely(delta > 0) )
- {
- SCHED_STAT_CRANK(burn_credits_t2c);
- t2c_update(rqd, delta, svc);
- svc->start_time = now;
- }
- else if ( delta < 0 )
+ if ( unlikely(delta <= 0) )
{xen/common/sched_credit2.c +13
-0+static void replenish_domain_budget (void * data) +{ + struct csched2_dom *sdom = data; + unsigned long flags; + s_time_t now; + LIST_HEAD(parked); + + spin_lock_irqsave(&sdom->budget_lock, flags); + + now = NOW(); + + /* +* Let's do the replenishment. Note, though, that a domain may overrun,
+ * which means the budget would have gone below 0 (reasons may be system
+ * overbooking, accounting issues, etc.). It also may happen that we are
+ * handling the replenishment (much) later than we should (reasons may
+ * again be overbooking, or issues with timers).
+ *
+ * Even in cases of overrun or delay, however, we expect that in 99% of
+ * cases, doing just one replenishment will be good enough for being able
+ * to unpark the vCPUs that are waiting for some budget. + */
+ do_replenish(sdom);
+
+ /*
+ * And now, the special cases:
+ * 1) if we are late enough to have skipped (at least) one full period,
+ * what we must do is doing more replenishments. Note that, however,
+ * every time we addtot_budgetto the budget, we also movenext_repl
+ * away byCSCHED2_BDGT_REPL_PERIOD, to make sure the cap is always
+ * respected.
+ */
+ if (unlikely(sdom->next_repl <= now))
+ {
+ do
Quoted 9 lines
Quoted 3 lines
+ do_replenish(sdom);
+ while (sdom->next_repl<= now );
+ }Just a bit confused. Have you seen this kind of scenario. Please can you
explain it.
Is this condition necessary.
This was discussed (with George) during v1 review. It's a corner case, which should never happen, and I in fact have never seen it happening in my tests.
But we can't rule out that it won't occur, so it makes sense to deal with it (instead of just ignoring it, causing the cap mechanism to [temporarily] malfunction / become inaccurate).
Quoted 13 lines
Quoted 4 lines
+ /*
+ * 2) if we overrun by more thantot_budget, then budget+tot_budget is
+ * still < 0, which means that we can't unpark the vCPUs.Quoted 8 lines
Let's bail,
+ * and wait for future replenishments.
+ */
+ if (unlikely(sdom->budget <= 0))
+ {
+ spin_unlock_irqrestore(&sdom->budget_lock, flags); + goto out;
+ }
Quoted 4 lines
"if we overran by more than
tot_budgetin previous run", make is more
clear..
Mmm... perhaps, but not so much, IMO. It's quite clear to which time window we are referring to already, and I don't feel like re-sending for this.
Let's see if there are other comments/requests.
Quoted 2 lines
Rest, looks good to me.
Thanks and Regards,
Dario
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/4] xen: credit2: implement utilization cap
In reply to Dario FaggioliQuoted 33 lines · Dario Faggioli
This commit implements the Xen part of the cap mechanism for Credit2.
A cap is how much, in terms of % of physical CPU time, a domain can execute at most.
For instance, a domain that must not use more than 1/4 of one physical CPU, must have a cap of 25%; one that must not use more than 1+1/2 of physical CPU time, must be given a cap of 150%.
Caps are per domain, so it is all a domain's vCPUs, cumulatively, that will be forced to execute no more than the decided amount.
This is implemented by giving each domain a 'budget', and using a (per-domain again) periodic timer. Values of budget and 'period' are chosen so that budget/period is equal to the cap itself.
Budget is burned by the domain's vCPUs, in a similar way to how credits are.
When a domain runs out of budget, its vCPUs can't run any longer. They can gain, when the budget is replenishment by the timer, which event happens once every period.
Blocking the vCPUs because of lack of budget happens by means of a new (
_VPF_parked) pause flag, so that, e.g.,vcpu_runnable()still works. This is similar to what is done insched_rtds.c, as opposed to what happens insched_credit.c, wherevcpu_pause()andvcpu_unpause()(which means, among other things, more overhead).
Quoted 8 lines
Note that, while adding new fields to
csched2_vcpuandcsched2_dom, currently existing members are being moved around, to achieve best placement inside cache lines.Note also that xenalyze and
tools/xentrace/formatare being updated too.Signed-off-by Dario Faggioli <[email protected]>
Looks good, with one minor nit...
xen/common/sched_credit2.c void burn_credits(struct csched2_runqueue_data *rqd, · line 1801
Quoted 8 lines
xen/common/sched_credit2.c +8
-0+/* +* NB: we give the whole remaining budget a domain has, to the first +* vCPU that comes here and asks for it. This means that, in a domain +* with a cap, only 1 vCPU is able to run, at any given time. +* /THIS IS GOING TO CHANGE/ in subsequent patches, toward something +* that allows much better fairness and parallelism. Proceeding in +* two steps, is for making things easy to understand, when looking +* at the signle commits.
*single
But I can fix that up on check-in.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/4] xen: credit2: implement utilization cap
Quoted 55 lines · George Dunlap
Quoted 33 lines · Dario Faggioli
This commit implements the Xen part of the cap mechanism for Credit2.
A cap is how much, in terms of % of physical CPU time, a domain can execute at most.
For instance, a domain that must not use more than 1/4 of one physical CPU, must have a cap of 25%; one that must not use more than 1+1/2 of physical CPU time, must be given a cap of 150%.
Caps are per domain, so it is all a domain's vCPUs, cumulatively, that will be forced to execute no more than the decided amount.
This is implemented by giving each domain a 'budget', and using a (per-domain again) periodic timer. Values of budget and 'period' are chosen so that budget/period is equal to the cap itself.
Budget is burned by the domain's vCPUs, in a similar way to how credits are.
When a domain runs out of budget, its vCPUs can't run any longer. They can gain, when the budget is replenishment by the timer, which event happens once every period.
Blocking the vCPUs because of lack of budget happens by means of a new (
_VPF_parked) pause flag, so that, e.g.,vcpu_runnable()still works. This is similar to what is done insched_rtds.c, as opposed to what happens insched_credit.c, wherevcpu_pause()andvcpu_unpause()(which means, among other things, more overhead).Quoted 8 lines
Note that, while adding new fields to
csched2_vcpuandcsched2_dom, currently existing members are being moved around, to achieve best placement inside cache lines.Note also that xenalyze and
tools/xentrace/formatare being updated too.Signed-off-by Dario Faggioli <[email protected]>Looks good, with one minor nit...
xen/common/sched_credit2.c void burn_credits(struct csched2_runqueue_data *rqd, · line 1801
Quoted 8 lines
xen/common/sched_credit2.c +8
-0+/* +* NB: we give the whole remaining budget a domain has, to the first +* vCPU that comes here and asks for it. This means that, in a domain +* with a cap, only 1 vCPU is able to run, at any given time. +* /THIS IS GOING TO CHANGE/ in subsequent patches, toward something +* that allows much better fairness and parallelism. Proceeding in +* two steps, is for making things easy to understand, when looking +* at the signle commits.
Quoted 3 lines
*single
But I can fix that up on check-in.
Well turns out it gets clobbered in the 3rd patch anyway. But at least we a well-spelled commit history. :-)
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 2/4 xen: credit2: allow to set and get utilization cap
In reply to Dario FaggioliAs cap is already present in Credit1, as a parameter, all the wiring is there already for it to be percolate down to csched2_dom_cntl() too.
In this commit, we actually deal with it, and implement setting, changing or disabling the cap of a domain.
---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Anshul Makkar <[email protected]>
---
Changes from v1:
- check that cap is below 100*nr_vcpus;
- do multiplication first when computing the domain's budget, given the cap;
- when disabling cap, take the budget lock for manipulating the list of parked vCPUs. Things would have been safe without it, but it's just more linear, more robust and more future-proof, to "do thing properly".
--- xen/common/sched_credit2.c | 129 +++++++++++++++++++++++++++++++++++++++++-- xen/include/public/domctl.h | 1 2 files changed, 125 insertions(+), 5 deletions(-)
xen/common/sched_credit2.c +124 -5
@@ -2772,30 +2772,35 @@ csched2_dom_cntl(struct csched2_dom *const sdom =csched2_dom (d);struct csched2_private *prv =csched2_priv (ops);unsigned long flags; +struct vcpu *v;int rc =0 ;/* * Locking: * - we must take the private lock for accessing the weights of the -* vcpus of d, +* vcpus of d, and/or the cap; * - in the putinfo case, we also need the runqueue lock(s), for * updating the max waight of the runqueue(s). +* If changing the cap, we also need the budget_lock, for updating +* the value of the domain budget pool (and the runqueue lock, +* for adjusting the parameters and rescheduling any vCPU that is +* running at the time of the change). */ switch ( op->cmd ) {case XEN_DOMCTL_SCHEDOP_getinfo:read_lock_irqsave (&prv->lock, flags); op->u.credit2.weight = sdom->weight; + op->u.credit2.cap = sdom->cap;read_unlock_irqrestore (&prv->lock, flags);break ;case XEN_DOMCTL_SCHEDOP_putinfo: +write_lock_irqsave (&prv->lock, flags); +/* Weight */ if ( op->u.credit2.weight !=0 ) { -struct vcpu *v;int old_weight; -write_lock_irqsave (&prv->lock, flags); - old_weight = sdom->weight; sdom->weight = op->u.credit2.weight;
@@ -2813,9 +2818,123 @@ csched2_dom_cntl(vcpu_schedule_unlock (lock, svc->vcpu); } + } +/* Cap */ +if ( op->u.credit2.cap !=0 ) + { +/* Cap is only valid if it's below 100 * nr_of_vCPUS */ +if ( op->u.credit2.cap >100 * sdom->nr_vcpus ) + { + rc = -EINVAL ; +break ; + } + +spin_lock (&sdom->budget_lock); + sdom->tot_budget = (CSCHED2_BDGT_REPL_PERIOD * op->u.credit2.cap); + sdom->tot_budget /=100 ; +spin_unlock (&sdom->budget_lock); + +if ( sdom->cap ==0 ) + { +/* +* We give to the domain the budget to which it is entitled, +* and queue its first replenishment event. +* +* Since cap is currently disabled for this domain, we +* know no vCPU is messing with the domain's budget, and +* the replenishment timer is still off. +* For these reasons, it is safe to do the following without +* taking the budget_lock. +*/ + sdom->budget = sdom->tot_budget; + sdom->next_repl =NOW () +CSCHED2_BDGT_REPL_PERIOD ; +set_timer (sdom->repl_timer, sdom->next_repl); + +/* +* Now, let's enable budget accounting for all the vCPUs. +* For making sure that they will start to honour the domain's +* cap, we set their budget to 0. +* This way, as soon as they will try to run, they will have +* to get some budget. +* +* For the vCPUs that are already running, we trigger the +* scheduler on their pCPU. When, as a consequence of this, +* csched2_schedule() will run, it will figure out there is +* no budget, and the vCPU will try to get some (and be parked, +* if there's none, and we'll switch to someone else). +*/ + for_each_vcpu ( d, v ) + { +struct csched2_vcpu *svc =csched2_vcpu (v); +spinlock_t *lock =vcpu_schedule_lock (svc->vcpu); + +if ( v->is_running ) + { +unsigned int cpu = v->processor; +struct csched2_runqueue_data *rqd =c2rqd (ops, cpu); + +ASSERT (curr_on_cpu (cpu) == v); + +/* +* We are triggering a reschedule on the vCPU's +* pCPU. That will run burn_credits() and, since +* the vCPU is capped now, it would charge all the +* execution time of this last round as budget as +* well. That will make the vCPU budget go negative, +* potentially by a large amount, and it's unfair. +* +* To avoid that, call burn_credit() here, to do the +* accounting of this current running instance now, +* with budgetting still disabled. This does not +* prevent some small amount of budget being charged +* to the vCPU (i.e., the amount of time it runs from +* now, to when scheduling happens). The budget will +* also go below 0, but a lot less than how it would +* if we don't do this. +*/ +burn_credits (rqd, svc,NOW ()); +__cpumask_set_cpu (cpu, &rqd->tickled); +ASSERT (!cpumask_test_cpu (cpu, &rqd->smt_idle)); +cpu_raise_softirq (cpu,SCHEDULE_SOFTIRQ ); + } + svc->budget =0 ; +vcpu_schedule_unlock (lock, svc->vcpu); + } + } + + sdom->cap = op->u.credit2.cap; + } +else if ( sdom->cap !=0 ) + { +LIST_HEAD (parked); + +stop_timer (sdom->repl_timer); + +/* Disable budget accounting for all the vCPUs. */ + for_each_vcpu ( d, v ) + { +struct csched2_vcpu *svc =csched2_vcpu (v); +spinlock_t *lock =vcpu_schedule_lock (svc->vcpu); + + svc->budget =STIME_MAX ; + +vcpu_schedule_unlock (lock, svc->vcpu); + } + sdom->cap =0 ; +/* +* We are disabling the cap for this domain, which may have +* vCPUs waiting for a replenishment, so we unpark them all. +* Note that, since we have already disabled budget accounting +* for all the vCPUs of the domain, no currently running vCPU +* will be added to the parked vCPUs list any longer. +*/ +spin_lock (&sdom->budget_lock); +list_splice_init (&sdom->parked_vcpus, &parked); +spin_unlock (&sdom->budget_lock); -write_unlock_irqrestore (&prv->lock, flags); +unpark_parked_vcpus (ops, &parked); } +write_unlock_irqrestore (&prv->lock, flags);break ;default : rc = -EINVAL ;
xen/include/public/domctl.h +1 -0
@@ -355,6 +355,7 @@ typedef struct xen_domctl_sched_credit {typedef struct xen_domctl_sched_credit2 {uint16_t weight; +uint16_t cap; }xen_domctl_sched_credit2_t ; typedef struct xen_domctl_sched_rtds {
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/4] xen: credit2: allow to set and get utilization cap
Quoted 8 lines · Dario Faggioli
As cap is already present in Credit1, as a parameter, all the wiring is there already for it to be percolate down to
csched2_dom_cntl()too.In this commit, we actually deal with it, and implement setting, changing or disabling the cap of a domain.
Signed-off-by Dario Faggioli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 3/4 xen: credit2: improve distribution of budget (for domains with caps)
In reply to Dario FaggioliInstead of letting the vCPU that for first tries to get some budget take it all (although temporarily), allow each vCPU to only get a specific quota of the total budget.
This improves fairness, allows for more parallelism, and prevents vCPUs from not being able to get any budget (e.g., because some other vCPU always comes before and gets it all) for one or more period, and hence starve (and cause troubles in guest kernels, such as livelocks, triggering of whatchdogs, etc.).
---
Cc: Anshul Makkar <[email protected]>
---
Changes from v1:
- typos;
- spurious hunk moved to previous patch.
--- xen/common/sched_credit2.c | 56 ++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-)
xen/common/sched_credit2.c +41 -15
@@ -522,6 +522,8 @@ struct csched2_vcpu {unsigned flags;/* Status flags (16 bits would be ok, */ s_time_t budget;/* Current budget (if domains has cap) */ /* but clear_bit() does not like that) */ +s_time_t budget_quota;/* Budget to which vCPU is entitled */ +s_time_t start_time;/* Time we were scheduled (for credit) */ /* Individual contribution to load */
@@ -1791,17 +1793,16 @@ static bool vcpu_grab_budget(struct csched2_vcpu *svc)if ( sdom->budget >0 ) { -/* -* NB: we give the whole remaining budget a domain has, to the first -* vCPU that comes here and asks for it. This means that, in a domain -* with a cap, only 1 vCPU is able to run, at any given time. -* /THIS IS GOING TO CHANGE/ in subsequent patches, toward something -* that allows much better fairness and parallelism. Proceeding in -* two steps, is for making things easy to understand, when looking -* at the signle commits. -*/ - svc->budget = sdom->budget; - sdom->budget =0 ; +s_time_t budget; + +/* Get our quota, if there's at least as much budget */ +if (likely (sdom->budget >= svc->budget_quota) ) + budget = svc->budget_quota; +else + budget = sdom->budget; + + svc->budget = budget; + sdom->budget -= budget; }else {
@@ -2036,6 +2037,7 @@ csched2_alloc_vdata(const struct scheduler *ops, struct vcpu *vc, void *dd) svc->tickled_cpu =-1 ; svc->budget =STIME_MAX ; + svc->budget_quota =0 ;INIT_LIST_HEAD (&svc->parked_elem);SCHED_STAT_CRANK (vcpu_alloc);
@@ -2822,6 +2824,9 @@ csched2_dom_cntl(/* Cap */ if ( op->u.credit2.cap !=0 ) { +struct csched2_vcpu *svc; +spinlock_t *lock; +/* Cap is only valid if it's below 100 * nr_of_vCPUS */ if ( op->u.credit2.cap >100 * sdom->nr_vcpus ) {
@@ -2834,6 +2839,26 @@ csched2_dom_cntl( sdom->tot_budget /=100 ;spin_unlock (&sdom->budget_lock); +/* +* When trying to get some budget and run, each vCPU will grab +* from the pool 1/N (with N = nr of vCPUs of the domain) of +* the total budget. Roughly speaking, this means each vCPU will +* have at least one chance to run during every period. +*/ + for_each_vcpu ( d, v ) + { + svc =csched2_vcpu (v); + lock =vcpu_schedule_lock (svc->vcpu); +/* +* Too small quotas would in theory cause a lot of overhead, +* which then won't happen because, in csched2_runtime(), +* CSCHED2_MIN_TIMER is what would be used anyway. +*/ + svc->budget_quota =max (sdom->tot_budget / sdom->nr_vcpus, +CSCHED2_MIN_TIMER ); +vcpu_schedule_unlock (lock, svc->vcpu); + } +if ( sdom->cap ==0 ) {/*
@@ -2865,9 +2890,8 @@ csched2_dom_cntl(*/ for_each_vcpu ( d, v ) { -struct csched2_vcpu *svc =csched2_vcpu (v); -spinlock_t *lock =vcpu_schedule_lock (svc->vcpu); - + svc =csched2_vcpu (v); + lock =vcpu_schedule_lock (svc->vcpu);if ( v->is_running ) {unsigned int cpu = v->processor;
@@ -2917,6 +2941,7 @@ csched2_dom_cntl(spinlock_t *lock =vcpu_schedule_lock (svc->vcpu); svc->budget =STIME_MAX ; + svc->budget_quota =0 ;vcpu_schedule_unlock (lock, svc->vcpu); }
@@ -3601,7 +3626,8 @@ csched2_dump_vcpu(struct csched2_private *prv, struct csched2_vcpu *svc)printk (" credit=%" PRIi32" [w=%u]" , svc->credit, svc->weight);if (has_cap (svc) ) -printk (" budget=%" PRI_stime, svc->budget); +printk (" budget=%" PRI_stime"(%" PRI_stime")" , + svc->budget, svc->budget_quota);printk (" load=%" PRI_stime" (~%" PRI_stime"%%)" , svc->avgload, (svc->avgload *100 ) >> prv->load_precision_shift);
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 4/4 libxl/xl: allow to get and set cap on Credit2.
In reply to Dario FaggioliNote that a cap is considered valid only if
it is within the [1, nr_vcpus]% interval.
---
Cc: Ian Jackson <[email protected]>
--- tools/libxl/libxl_sched.c | 21 +++++++++++++++++++++ tools/xl/xl_cmdtable.c | 1 + tools/xl/xl_sched.c | 25 +++++++++++++++++-------- 3 files changed, 39 insertions(+), 8 deletions(-)
tools/libxl/libxl_sched.c +21 -0
@@ -405,6 +405,7 @@ static int sched_credit2_domain_get(libxl__gc *gc, uint32_t domid,libxl_domain_sched_params_init (scinfo); scinfo->sched =LIBXL_SCHEDULER_CREDIT2 ; scinfo->weight = sdom.weight; + scinfo->cap = sdom.cap;return 0 ; }
@@ -413,8 +414,17 @@ static int sched_credit2_domain_set(libxl__gc *gc, uint32_t domid, const libxl_domain_sched_params *scinfo) {struct xen_domctl_sched_credit2 sdom; +xc_domaininfo_t info;int rc; + rc =xc_domain_getinfolist (CTX ->xch, domid,1 , &info); +if (rc <0 ) { +LOGED (ERROR , domid,"Getting domain info" ); +return ERROR_FAIL ; + } +if (rc !=1 || info.domain != domid) +return ERROR_INVAL ; + rc =xc_sched_credit2_domain_get (CTX ->xch, domid, &sdom);if (rc !=0 ) {LOGED (ERROR , domid,"Getting domain sched credit2" );
@@ -430,6 +440,17 @@ static int sched_credit2_domain_set(libxl__gc *gc, uint32_t domid, sdom.weight = scinfo->weight; } +if (scinfo->cap !=LIBXL_DOMAIN_SCHED_PARAM_CAP_DEFAULT ) { +if (scinfo->cap <0 + || scinfo->cap > (info.max_vcpu_id +1 ) *100 ) { +LOGD (ERROR , domid,"Cpu cap out of range, " +"valid range is from 0 to %d for specified number of vcpus" , + ((info.max_vcpu_id +1 ) *100 )); +return ERROR_INVAL ; + } + sdom.cap = scinfo->cap; + } + rc =xc_sched_credit2_domain_set (CTX ->xch, domid, &sdom);if ( rc <0 ) {LOGED (ERROR , domid,"Setting domain sched credit2" );
tools/xl/xl_cmdtable.c +1 -0
@@ -265,6 +265,7 @@ struct cmd_spec cmd_table[] = {"[-d <Domain> [-w[=WEIGHT]]] [-p CPUPOOL]" ,"-d DOMAIN, --domain=DOMAIN Domain to modify\n" "-w WEIGHT, --weight=WEIGHT Weight (int)\n" +"-c CAP, --cap=CAP Cap (int)\n" "-s --schedparam Query / modify scheduler parameters\n" "-r RLIMIT, --ratelimit_us=RLIMIT Set the scheduling rate limit, in microseconds\n" "-p CPUPOOL, --cpupool=CPUPOOL Restrict output to CPUPOOL"
tools/xl/xl_sched.c +17 -8
@@ -209,7 +209,7 @@ static int sched_credit2_domain_output(int domid)libxl_domain_sched_params scinfo;if (domid <0 ) { -printf ("%-33s %4s %6s\n" ,"Name" ,"ID" ,"Weight" ); +printf ("%-33s %4s %6s %4s\n" ,"Name" ,"ID" ,"Weight" ,"Cap" );return 0 ; }
@@ -219,10 +219,11 @@ static int sched_credit2_domain_output(int domid)return 1 ; } domname =libxl_domid_to_name (ctx, domid); -printf ("%-33s %4d %6d\n" , +printf ("%-33s %4d %6d %4d\n" , domname, domid, - scinfo.weight); + scinfo.weight, + scinfo.cap);free (domname);libxl_domain_sched_params_dispose (&scinfo);return 0 ;
@@ -589,21 +590,23 @@ int main_sched_credit2(int argc, char **argv)const char *dom =NULL ;const char *cpupool =NULL ;int ratelimit =0 ; -int weight =256 ; +int weight =256 , cap =0 ;bool opt_s = false;bool opt_r = false;bool opt_w = false; +bool opt_c = false;int opt, rc;static struct option opts[] = { {"domain" ,1 ,0 ,'d' }, {"weight" ,1 ,0 ,'w' }, + {"cap" ,1 ,0 ,'c' }, {"schedparam" ,0 ,0 ,'s' }, {"ratelimit_us" ,1 ,0 ,'r' }, {"cpupool" ,1 ,0 ,'p' },COMMON_LONG_OPTS }; - SWITCH_FOREACH_OPT(opt, "d:w:p:r:s", opts, "sched-credit2", 0) { + SWITCH_FOREACH_OPT(opt, "d:w:c:p:r:s", opts, "sched-credit2", 0) {case 'd' : dom = optarg;break ;
@@ -611,6 +614,10 @@ int main_sched_credit2(int argc, char **argv) weight =strtol (optarg,NULL ,10 ); opt_w = true;break ; +case 'c' : + cap =strtol (optarg,NULL ,10 ); + opt_c = true; +break ;case 's' : opt_s = true;break ;
@@ -623,12 +630,12 @@ int main_sched_credit2(int argc, char **argv)break ; } -if (cpupool && (dom || opt_w)) { +if (cpupool && (dom || opt_w || opt_c)) {fprintf (stderr,"Specifying a cpupool is not allowed with other " "options.\n" );return EXIT_FAILURE ; } -if (!dom && opt_w) { +if (!dom && (opt_w || opt_c)) {fprintf (stderr,"Must specify a domain.\n" );return EXIT_FAILURE ; }
@@ -663,7 +670,7 @@ int main_sched_credit2(int argc, char **argv) }else {uint32_t domid =find_domain (dom); -if (!opt_w) {/* output credit2 scheduler info */ +if (!opt_w && !opt_c) {/* output credit2 scheduler info */ sched_credit2_domain_output (-1 );if (sched_credit2_domain_output (domid))return EXIT_FAILURE ;
@@ -673,6 +680,8 @@ int main_sched_credit2(int argc, char **argv) scinfo.sched =LIBXL_SCHEDULER_CREDIT2 ;if (opt_w) scinfo.weight = weight; +if (opt_c) + scinfo.cap = cap; rc =sched_domain_set (domid, &scinfo);libxl_domain_sched_params_dispose (&scinfo);if (rc)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel