Skip to contentSkip to conversation

credit: Limit load balancing to once per millisecond

PATCH In review George Dunlap 5 participants 13 messages Sep 21, 2023 – Oct 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 credit: Limit load balancing to once per millisecond

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
 docs/misc/xen-command-line.pandoc |  8 ++++++
 xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
 2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
 it is available.  This option can be used to cause Xen to fall back to
 older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
 ### noirqbalance (x86)
 > `= <boolean>`
 
xen/common/sched/credit.c +38 -9

99 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 2/2 credit: Don't steal vcpus which have yielded

On large systems with many vcpus yielding due to spinlock priority inversion, it's not uncommon for a vcpu to yield its timeslice, only to be immediately stolen by another pcpu looking for higher-priority work.

To prevent this:

* Keep the YIELD flag until a vcpu is removed from a runqueue

* When looking for work to steal, skip vcpus which have yielded

NB that this does mean that sometimes a VM is inserted into an empty runqueue; handle that case.

---
Changes since v1:
- Moved a comment tweak to the right patch

CC Dario Faggioli <[email protected]>
---
 xen/common/sched/credit.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
xen/common/sched/credit.c +14 -11
index 5c06f596d2..38a6f6fa6d 100644
@@ -298,14 +298,10 @@ __runq_insert(struct csched_unit *svc)
      * runnable unit if we can.  The next runq_sort will bring it forward
      * within 30ms if the queue too long. */
     if ( test_bit(CSCHED_FLAG_UNIT_YIELD, &svc->flags)
-         && __runq_elem(iter)->pri > CSCHED_PRI_IDLE )
-    {
+         && __runq_elem(iter)->pri > CSCHED_PRI_IDLE
+         && iter->next != runq)
         iter=iter->next;
 
-        /* Some sanity checks */
-        BUG_ON(iter == runq);
-    }
-
     list_add_tail(&svc->runq_elem, iter);
 }
 
@@ -321,6 +317,11 @@ __runq_remove(struct csched_unit *svc)
 {
     BUG_ON( !__unit_on_runq(svc) );
     list_del_init(&svc->runq_elem);
+
+    /*
+     * Clear YIELD flag when scheduling back in
+     */
+    clear_bit(CSCHED_FLAG_UNIT_YIELD, &svc->flags);
 }
 
 static inline void
@@ -1637,6 +1638,13 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
         if ( speer->pri <= pri )
             break;
 
+        /*
+         * Don't steal a UNIT which has yielded; it's waiting for a
+         * reason
+         */
+        if (test_bit(CSCHED_FLAG_UNIT_YIELD, &speer->flags))
+            continue;
+
         /* Is this UNIT runnable on our PCPU? */
         unit = speer->unit;
         BUG_ON( is_idle_unit(unit) );
@@ -1954,11 +1962,6 @@ static void cf_check csched_schedule(
         dec_nr_runnable(sched_cpu);
     }
 
-    /*
-     * Clear YIELD flag before scheduling out
-     */
-    clear_bit(CSCHED_FLAG_UNIT_YIELD, &scurr->flags);
-
     do {
         snext = __runq_elem(runq->next);
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 2/2] credit: Don't steal vcpus which have yielded

Quoted 8 lines · George Dunlap

On large systems with many vcpus yielding due to spinlock priority inversion, it's not uncommon for a vcpu to yield its timeslice, only to be immediately stolen by another pcpu looking for higher-priority work.

To prevent this:

Quoted 8 lines

* Keep the YIELD flag until a vcpu is removed from a runqueue

* When looking for work to steal, skip vcpus which have yielded

NB that this does mean that sometimes a VM is inserted into an empty runqueue; handle that case.

Marcus,

Just wanted to verify my interpretation of the testing you did of this patch several months ago:

1. On the problematic workload, mean execution time for the task under heavy load was around 12 seconds
2. With only patch 2 of this series (0004 in your tests), mean execution time under heavy load was around 5 seconds
3. With only patch 1 of this series (0003 in your tests), mean execution time under heavy load was around 3 seconds
4. With both patch 1 and patch 2 of this series (0003+0004 in your tests), mean execution time under heavy load was also around 3 seconds

So both patches independently exhibit an improvement; but the combined effect is about the same as the first patch.

Assuming those results are accurate, I would argue that we should take both patches. Does anyone want to argue we should only take the first one?

-George

#3 lore raw

Re: [PATCH v2 2/2] credit: Don't steal vcpus which have yielded

Quoted 29 lines · George Dunlap
Quoted 6 lines · George Dunlap

On large systems with many vcpus yielding due to spinlock priority inversion, it's not uncommon for a vcpu to yield its timeslice, only to be immediately stolen by another pcpu looking for higher-priority work.

Quoted 6 lines

To prevent this:

* Keep the YIELD flag until a vcpu is removed from a runqueue

* When looking for work to steal, skip vcpus which have yielded

Marcus,

Just wanted to verify my interpretation of the testing you did of this patch several months ago:

1. On the problematic workload, mean execution time for the task under heavy load was around 12 seconds
2. With only patch 2 of this series (0004 in your tests), mean execution time under heavy load was around 5 seconds
3. With only patch 1 of this series (0003 in your tests), mean execution time under heavy load was around 3 seconds
4. With both patch 1 and patch 2 of this series (0003+0004 in your tests), mean execution time under heavy load was also around 3 seconds

Quoted 3 lines

So both patches independently exhibit an improvement; but the combined effect is about the same as the first patch.

Yes, thanks for the summary, that's pretty much it. Marcus

#4 lore raw

Re: [PATCH v2 2/2] credit: Don't steal vcpus which have yielded

In reply to George Dunlap

Hi George,

Quoted 9 lines · George Dunlap

On large systems with many vcpus yielding due to spinlock priority inversion, it's not uncommon for a vcpu to yield its timeslice, only to be immediately stolen by another pcpu looking for higher-priority work.

To prevent this:

Quoted 8 lines

* Keep the YIELD flag until a vcpu is removed from a runqueue

* When looking for work to steal, skip vcpus which have yielded

NB that this does mean that sometimes a VM is inserted into an empty runqueue; handle that case.

Kind regards,
Henry

Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] credit: Don't steal vcpus which have yielded

In reply to George Dunlap
Quoted 29 lines · George Dunlap

On large systems with many vcpus yielding due to spinlock priority inversion, it's not uncommon for a vcpu to yield its timeslice, only to be immediately stolen by another pcpu looking for higher-priority work.

To prevent this:

* Keep the YIELD flag until a vcpu is removed from a runqueue

* When looking for work to steal, skip vcpus which have yielded

NB that this does mean that sometimes a VM is inserted into an empty runqueue; handle that case.

---
Changes since v1:
- Moved a comment tweak to the right patch

CC Dario Faggioli <[email protected]>
---
  xen/common/sched/credit.c | 25 ++++++++++++++-----------
  1 file changed, 14 insertions(+), 11 deletions(-)
xen/common/sched/credit.c +0 -0
index 5c06f596d2..38a6f6fa6d 100644

xen/common/sched/credit.c __runq_insert(struct csched_unit *svc) · line 302

Quoted 7 lines
xen/common/sched/credit.c +2 -2
@@ -298,14 +298,10 @@ __runq_insert(struct csched_unit *svc)
       * runnable unit if we can.  The next runq_sort will bring it forward
       * within 30ms if the queue too long. */
      if ( test_bit(CSCHED_FLAG_UNIT_YIELD, &svc->flags)
-         && __runq_elem(iter)->pri > CSCHED_PRI_IDLE )
-    {
+         && __runq_elem(iter)->pri > CSCHED_PRI_IDLE
+         && iter->next != runq)

Style

Quoted 22 lines
xen/common/sched/credit.c +5 -4
          iter=iter->next;
 
-        /* Some sanity checks */
-        BUG_ON(iter == runq);
-    }
-
      list_add_tail(&svc->runq_elem, iter);
  }
 
@@ -321,6 +317,11 @@ __runq_remove(struct csched_unit *svc)
  {
      BUG_ON( !__unit_on_runq(svc) );
      list_del_init(&svc->runq_elem);
+
+    /*
+     * Clear YIELD flag when scheduling back in
+     */
+    clear_bit(CSCHED_FLAG_UNIT_YIELD, &svc->flags);
  }
 
  static inline void

xen/common/sched/credit.c csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step) · line 1645

Quoted 8 lines
xen/common/sched/credit.c +5 -0
@@ -1637,6 +1638,13 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
          if ( speer->pri <= pri )
              break;
 
+        /*
+         * Don't steal a UNIT which has yielded; it's waiting for a
+         * reason
+         */
+        if (test_bit(CSCHED_FLAG_UNIT_YIELD, &speer->flags))

Style

Quoted 7 lines
xen/common/sched/credit.c +2 -0
+            continue;
+
          /* Is this UNIT runnable on our PCPU? */
          unit = speer->unit;
          BUG_ON( is_idle_unit(unit) );
@@ -1954,11 +1962,6 @@ static void cf_check csched_schedule(
          dec_nr_runnable(sched_cpu);

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1967

Quoted 10 lines
xen/common/sched/credit.c +0 -5
      }
 
-    /*
-     * Clear YIELD flag before scheduling out
-     */
-    clear_bit(CSCHED_FLAG_UNIT_YIELD, &scurr->flags);
-
      do {
          snext = __runq_elem(runq->next);
 

With the style issues fixed:

Juergen

Reviewed-by on v2 1/2
#6 lore raw
In reply to George Dunlap
Quoted 25 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

Quoted 5 lines

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

Juergen

#7 lore raw
In reply to George Dunlap
Quoted 141 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
  docs/misc/xen-command-line.pandoc |  8 ++++++
  xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
  2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
  it is available.  This option can be used to cause Xen to fall back to
  older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
  ### noirqbalance (x86)
  > `= <boolean>`
 
xen/common/sched/credit.c +18 -6
index f2cd3d9da3..5c06f596d2 100644
@@ -50,6 +50,10 @@
  #define CSCHED_TICKS_PER_TSLICE     3
  /* Default timeslice: 30ms */
  #define CSCHED_DEFAULT_TSLICE_MS    30
+/* Default load balancing ratelimit: 1ms */
+#define CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US 1000
+/* Max load balancing ratelimit: 1s */
+#define CSCHED_MAX_LOAD_BALANCE_RATELIMIT_US     1000000
  #define CSCHED_CREDITS_PER_MSEC     10
  /* Never set a timer shorter than this value. */
  #define CSCHED_MIN_TIMER            XEN_SYSCTL_SCHED_RATELIMIT_MIN
@@ -153,6 +157,7 @@ struct csched_pcpu {
 
      unsigned int idle_bias;
      unsigned int nr_runnable;
+    s_time_t last_load_balance;
 
      unsigned int tick;
      struct timer ticker;
@@ -218,7 +223,7 @@ struct csched_private {
 
      /* Period of master and tick in milliseconds */
      unsigned int tick_period_us, ticks_per_tslice;
-    s_time_t ratelimit, tslice, unit_migr_delay;
+    s_time_t ratelimit, tslice, unit_migr_delay, load_balance_ratelimit;
 
      struct list_head active_sdom;
      uint32_t weight;
@@ -612,6 +617,8 @@ init_pdata(struct csched_private *prv, struct csched_pcpu *spc, int cpu)
      BUG_ON(!is_idle_unit(curr_on_cpu(cpu)));
      cpumask_set_cpu(cpu, prv->idlers);
      spc->nr_runnable = 0;
+
+    spc->last_load_balance = NOW();
  }
 
  static void cf_check
@@ -1676,9 +1683,17 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
      return NULL;
  }
 
+/*
+ * Minimum delay, in microseconds, between load balance operations.
+ * This prevents spending too much time doing load balancing, particularly
+ * when the system has a high number of YIELDs due to spinlock priority inversion.
+ */
+static unsigned int __ro_after_init load_balance_ratelimit_us = CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US;
+integer_param("load-balance-ratelimit", load_balance_ratelimit_us);
+
  static struct csched_unit *
  csched_load_balance(struct csched_private *prv, int cpu,
-    struct csched_unit *snext, bool *stolen)
+                    struct csched_unit *snext, bool *stolen)
  {
      const struct cpupool *c = get_sched_res(cpu)->cpupool;
      struct csched_unit *speer;
@@ -1958,15 +1973,19 @@ static void cf_check csched_schedule(
          /*
           * SMP Load balance:
           *
-         * If the next highest priority local runnable UNIT has already eaten
-         * through its credits, look on other PCPUs to see if we have more
-         * urgent work... If not, csched_load_balance() will return snext, but
-         * already removed from the runq.
+         * If the next highest priority local runnable UNIT has

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1983

Quoted 10 lines
xen/common/sched/credit.c +6 -3
+         * already eaten through its credits (and we're below the
+         * balancing ratelimit), look on other PCPUs to see if we have
+         * more urgent work... If we don't, csched_load_balance() will
+         * return snext, but already removed from the runq.
           */
-        if ( snext->pri > CSCHED_PRI_TS_OVER )
-            __runq_remove(snext);
-        else
+        if ( snext->pri <= CSCHED_PRI_TS_OVER
+             && now - spc->last_load_balance > prv->load_balance_ratelimit) {

^ Just found a style issue (after Andrew pointing out the ones in patch 2).

My R-b stands with that fixed.

Juergen

#8 lore raw
Quoted 147 lines · Juergen Gross
Quoted 145 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
  docs/misc/xen-command-line.pandoc |  8 ++++++
  xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
  2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
  it is available.  This option can be used to cause Xen to fall back to
  older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
  ### noirqbalance (x86)
  > `= <boolean>`
 
xen/common/sched/credit.c +22 -6
index f2cd3d9da3..5c06f596d2 100644
@@ -50,6 +50,10 @@
  #define CSCHED_TICKS_PER_TSLICE     3
  /* Default timeslice: 30ms */
  #define CSCHED_DEFAULT_TSLICE_MS    30
+/* Default load balancing ratelimit: 1ms */
+#define CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US 1000
+/* Max load balancing ratelimit: 1s */
+#define CSCHED_MAX_LOAD_BALANCE_RATELIMIT_US     1000000
  #define CSCHED_CREDITS_PER_MSEC     10
  /* Never set a timer shorter than this value. */
  #define CSCHED_MIN_TIMER            XEN_SYSCTL_SCHED_RATELIMIT_MIN
@@ -153,6 +157,7 @@ struct csched_pcpu {
 
      unsigned int idle_bias;
      unsigned int nr_runnable;
+    s_time_t last_load_balance;
 
      unsigned int tick;
      struct timer ticker;
@@ -218,7 +223,7 @@ struct csched_private {
 
      /* Period of master and tick in milliseconds */
      unsigned int tick_period_us, ticks_per_tslice;
-    s_time_t ratelimit, tslice, unit_migr_delay;
+    s_time_t ratelimit, tslice, unit_migr_delay, load_balance_ratelimit;
 
      struct list_head active_sdom;
      uint32_t weight;
@@ -612,6 +617,8 @@ init_pdata(struct csched_private *prv, struct csched_pcpu *spc, int cpu)
      BUG_ON(!is_idle_unit(curr_on_cpu(cpu)));
      cpumask_set_cpu(cpu, prv->idlers);
      spc->nr_runnable = 0;
+
+    spc->last_load_balance = NOW();
  }
 
  static void cf_check
@@ -1676,9 +1683,17 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
      return NULL;
  }
 
+/*
+ * Minimum delay, in microseconds, between load balance operations.
+ * This prevents spending too much time doing load balancing, particularly
+ * when the system has a high number of YIELDs due to spinlock priority inversion.
+ */
+static unsigned int __ro_after_init load_balance_ratelimit_us = CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US;
+integer_param("load-balance-ratelimit", load_balance_ratelimit_us);
+
  static struct csched_unit *
  csched_load_balance(struct csched_private *prv, int cpu,
-    struct csched_unit *snext, bool *stolen)
+                    struct csched_unit *snext, bool *stolen)
  {
      const struct cpupool *c = get_sched_res(cpu)->cpupool;
      struct csched_unit *speer;
@@ -1958,15 +1973,19 @@ static void cf_check csched_schedule(
          /*
           * SMP Load balance:
           *
-         * If the next highest priority local runnable UNIT has already eaten
-         * through its credits, look on other PCPUs to see if we have more
-         * urgent work... If not, csched_load_balance() will return snext, but
-         * already removed from the runq.
+         * If the next highest priority local runnable UNIT has
+         * already eaten through its credits (and we're below the
+         * balancing ratelimit), look on other PCPUs to see if we have
+         * more urgent work... If we don't, csched_load_balance() will
+         * return snext, but already removed from the runq.
Quoted 8 lines

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1983

Quoted 6 lines
xen/common/sched/credit.c +2 -3
           */
-        if ( snext->pri > CSCHED_PRI_TS_OVER )
-            __runq_remove(snext);
-        else
+        if ( snext->pri <= CSCHED_PRI_TS_OVER
+             && now - spc->last_load_balance > prv->load_balance_ratelimit) {

^ Just found a style issue (after Andrew pointing out the ones in patch 2).

Just checking, you mean the space before the closing )?

-George

#9 lore raw
Quoted 157 lines · George Dunlap
Quoted 147 lines · Juergen Gross
Quoted 145 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
   docs/misc/xen-command-line.pandoc |  8 ++++++
   xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
   2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
   it is available.  This option can be used to cause Xen to fall back to
   older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
   ### noirqbalance (x86)
   > `= <boolean>`
 
xen/common/sched/credit.c +22 -6
index f2cd3d9da3..5c06f596d2 100644
@@ -50,6 +50,10 @@
   #define CSCHED_TICKS_PER_TSLICE     3
   /* Default timeslice: 30ms */
   #define CSCHED_DEFAULT_TSLICE_MS    30
+/* Default load balancing ratelimit: 1ms */
+#define CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US 1000
+/* Max load balancing ratelimit: 1s */
+#define CSCHED_MAX_LOAD_BALANCE_RATELIMIT_US     1000000
   #define CSCHED_CREDITS_PER_MSEC     10
   /* Never set a timer shorter than this value. */
   #define CSCHED_MIN_TIMER            XEN_SYSCTL_SCHED_RATELIMIT_MIN
@@ -153,6 +157,7 @@ struct csched_pcpu {
 
       unsigned int idle_bias;
       unsigned int nr_runnable;
+    s_time_t last_load_balance;
 
       unsigned int tick;
       struct timer ticker;
@@ -218,7 +223,7 @@ struct csched_private {
 
       /* Period of master and tick in milliseconds */
       unsigned int tick_period_us, ticks_per_tslice;
-    s_time_t ratelimit, tslice, unit_migr_delay;
+    s_time_t ratelimit, tslice, unit_migr_delay, load_balance_ratelimit;
 
       struct list_head active_sdom;
       uint32_t weight;
@@ -612,6 +617,8 @@ init_pdata(struct csched_private *prv, struct csched_pcpu *spc, int cpu)
       BUG_ON(!is_idle_unit(curr_on_cpu(cpu)));
       cpumask_set_cpu(cpu, prv->idlers);
       spc->nr_runnable = 0;
+
+    spc->last_load_balance = NOW();
   }
 
   static void cf_check
@@ -1676,9 +1683,17 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
       return NULL;
   }
 
+/*
+ * Minimum delay, in microseconds, between load balance operations.
+ * This prevents spending too much time doing load balancing, particularly
+ * when the system has a high number of YIELDs due to spinlock priority inversion.
+ */
+static unsigned int __ro_after_init load_balance_ratelimit_us = CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US;
+integer_param("load-balance-ratelimit", load_balance_ratelimit_us);
+
   static struct csched_unit *
   csched_load_balance(struct csched_private *prv, int cpu,
-    struct csched_unit *snext, bool *stolen)
+                    struct csched_unit *snext, bool *stolen)
   {
       const struct cpupool *c = get_sched_res(cpu)->cpupool;
       struct csched_unit *speer;
@@ -1958,15 +1973,19 @@ static void cf_check csched_schedule(
           /*
            * SMP Load balance:
            *
-         * If the next highest priority local runnable UNIT has already eaten
-         * through its credits, look on other PCPUs to see if we have more
-         * urgent work... If not, csched_load_balance() will return snext, but
-         * already removed from the runq.
+         * If the next highest priority local runnable UNIT has
+         * already eaten through its credits (and we're below the
+         * balancing ratelimit), look on other PCPUs to see if we have
+         * more urgent work... If we don't, csched_load_balance() will
+         * return snext, but already removed from the runq.
Quoted 8 lines

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1983

Quoted 6 lines
xen/common/sched/credit.c +2 -3
            */
-        if ( snext->pri > CSCHED_PRI_TS_OVER )
-            __runq_remove(snext);
-        else
+        if ( snext->pri <= CSCHED_PRI_TS_OVER
+             && now - spc->last_load_balance > prv->load_balance_ratelimit) {

^ Just found a style issue (after Andrew pointing out the ones in patch 2).

Quoted 1 line

Just checking, you mean the space before the closing )?

Yes.

Juergen

#10 lore raw
In reply to George Dunlap
Quoted 157 lines · George Dunlap
Quoted 147 lines · Juergen Gross
Quoted 145 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
  docs/misc/xen-command-line.pandoc |  8 ++++++
  xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
  2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
  it is available.  This option can be used to cause Xen to fall back to
  older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
  ### noirqbalance (x86)
  > `= <boolean>`
 
xen/common/sched/credit.c +22 -6
index f2cd3d9da3..5c06f596d2 100644
@@ -50,6 +50,10 @@
  #define CSCHED_TICKS_PER_TSLICE     3
  /* Default timeslice: 30ms */
  #define CSCHED_DEFAULT_TSLICE_MS    30
+/* Default load balancing ratelimit: 1ms */
+#define CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US 1000
+/* Max load balancing ratelimit: 1s */
+#define CSCHED_MAX_LOAD_BALANCE_RATELIMIT_US     1000000
  #define CSCHED_CREDITS_PER_MSEC     10
  /* Never set a timer shorter than this value. */
  #define CSCHED_MIN_TIMER            XEN_SYSCTL_SCHED_RATELIMIT_MIN
@@ -153,6 +157,7 @@ struct csched_pcpu {
 
      unsigned int idle_bias;
      unsigned int nr_runnable;
+    s_time_t last_load_balance;
 
      unsigned int tick;
      struct timer ticker;
@@ -218,7 +223,7 @@ struct csched_private {
 
      /* Period of master and tick in milliseconds */
      unsigned int tick_period_us, ticks_per_tslice;
-    s_time_t ratelimit, tslice, unit_migr_delay;
+    s_time_t ratelimit, tslice, unit_migr_delay, load_balance_ratelimit;
 
      struct list_head active_sdom;
      uint32_t weight;
@@ -612,6 +617,8 @@ init_pdata(struct csched_private *prv, struct csched_pcpu *spc, int cpu)
      BUG_ON(!is_idle_unit(curr_on_cpu(cpu)));
      cpumask_set_cpu(cpu, prv->idlers);
      spc->nr_runnable = 0;
+
+    spc->last_load_balance = NOW();
  }
 
  static void cf_check
@@ -1676,9 +1683,17 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
      return NULL;
  }
 
+/*
+ * Minimum delay, in microseconds, between load balance operations.
+ * This prevents spending too much time doing load balancing, particularly
+ * when the system has a high number of YIELDs due to spinlock priority inversion.
+ */
+static unsigned int __ro_after_init load_balance_ratelimit_us = CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US;
+integer_param("load-balance-ratelimit", load_balance_ratelimit_us);
+
  static struct csched_unit *
  csched_load_balance(struct csched_private *prv, int cpu,
-    struct csched_unit *snext, bool *stolen)
+                    struct csched_unit *snext, bool *stolen)
  {
      const struct cpupool *c = get_sched_res(cpu)->cpupool;
      struct csched_unit *speer;
@@ -1958,15 +1973,19 @@ static void cf_check csched_schedule(
          /*
           * SMP Load balance:
           *
-         * If the next highest priority local runnable UNIT has already eaten
-         * through its credits, look on other PCPUs to see if we have more
-         * urgent work... If not, csched_load_balance() will return snext, but
-         * already removed from the runq.
+         * If the next highest priority local runnable UNIT has
+         * already eaten through its credits (and we're below the
+         * balancing ratelimit), look on other PCPUs to see if we have
+         * more urgent work... If we don't, csched_load_balance() will
+         * return snext, but already removed from the runq.
Quoted 8 lines

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1983

Quoted 6 lines
xen/common/sched/credit.c +2 -3
           */
-        if ( snext->pri > CSCHED_PRI_TS_OVER )
-            __runq_remove(snext);
-        else
+        if ( snext->pri <= CSCHED_PRI_TS_OVER
+             && now - spc->last_load_balance > prv->load_balance_ratelimit) {

^ Just found a style issue (after Andrew pointing out the ones in patch 2).

Quoted 1 line

Just checking, you mean the space before the closing )?

And the placement of the opening {.

Jan

#11 lore raw
Quoted 159 lines · Jan Beulich
Quoted 157 lines · George Dunlap
Quoted 155 lines · Juergen Gross
Quoted 141 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

---
Changes since v1:
- Fix editing mistake in commit message
- Improve documentation
- global var is __ro_after_init
- Remove sysctl, as it's not used. Define max value in credit.c.
- Fix some style issues
- Move comment tweak to the right patch
- In the event that the commandline-parameter value is too high, clip to the maximum value rather than setting to the default.

CC Dario Faggioli <[email protected]>
CC Andrew Cooper <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Wei Liu <[email protected]>
---
  docs/misc/xen-command-line.pandoc |  8 ++++++
  xen/common/sched/credit.c         | 47 +++++++++++++++++++++++++------
  2 files changed, 46 insertions(+), 9 deletions(-)
docs/misc/xen-command-line.pandoc +8 -0
index f88e6a70ae..9c3c72a7f9 100644
@@ -1884,6 +1884,14 @@ By default, Xen will use the INVPCID instruction for TLB management if
  it is available.  This option can be used to cause Xen to fall back to
  older mechanisms, which are generally slower.
 
+### load-balance-ratelimit
+> `= <integer>`
+
+The minimum interval between load balancing events on a given pcpu, in
+microseconds.  A value of '0' will disable rate limiting.  Maximum
+value 1 second. At the moment only credit honors this parameter.
+Default 1ms.
+
  ### noirqbalance (x86)
  > `= <boolean>`
 
xen/common/sched/credit.c +18 -6
index f2cd3d9da3..5c06f596d2 100644
@@ -50,6 +50,10 @@
  #define CSCHED_TICKS_PER_TSLICE     3
  /* Default timeslice: 30ms */
  #define CSCHED_DEFAULT_TSLICE_MS    30
+/* Default load balancing ratelimit: 1ms */
+#define CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US 1000
+/* Max load balancing ratelimit: 1s */
+#define CSCHED_MAX_LOAD_BALANCE_RATELIMIT_US     1000000
  #define CSCHED_CREDITS_PER_MSEC     10
  /* Never set a timer shorter than this value. */
  #define CSCHED_MIN_TIMER            XEN_SYSCTL_SCHED_RATELIMIT_MIN
@@ -153,6 +157,7 @@ struct csched_pcpu {
 
      unsigned int idle_bias;
      unsigned int nr_runnable;
+    s_time_t last_load_balance;
 
      unsigned int tick;
      struct timer ticker;
@@ -218,7 +223,7 @@ struct csched_private {
 
      /* Period of master and tick in milliseconds */
      unsigned int tick_period_us, ticks_per_tslice;
-    s_time_t ratelimit, tslice, unit_migr_delay;
+    s_time_t ratelimit, tslice, unit_migr_delay, load_balance_ratelimit;
 
      struct list_head active_sdom;
      uint32_t weight;
@@ -612,6 +617,8 @@ init_pdata(struct csched_private *prv, struct csched_pcpu *spc, int cpu)
      BUG_ON(!is_idle_unit(curr_on_cpu(cpu)));
      cpumask_set_cpu(cpu, prv->idlers);
      spc->nr_runnable = 0;
+
+    spc->last_load_balance = NOW();
  }
 
  static void cf_check
@@ -1676,9 +1683,17 @@ csched_runq_steal(int peer_cpu, int cpu, int pri, int balance_step)
      return NULL;
  }
 
+/*
+ * Minimum delay, in microseconds, between load balance operations.
+ * This prevents spending too much time doing load balancing, particularly
+ * when the system has a high number of YIELDs due to spinlock priority inversion.
+ */
+static unsigned int __ro_after_init load_balance_ratelimit_us = CSCHED_DEFAULT_LOAD_BALANCE_RATELIMIT_US;
+integer_param("load-balance-ratelimit", load_balance_ratelimit_us);
+
  static struct csched_unit *
  csched_load_balance(struct csched_private *prv, int cpu,
-    struct csched_unit *snext, bool *stolen)
+                    struct csched_unit *snext, bool *stolen)
  {
      const struct cpupool *c = get_sched_res(cpu)->cpupool;
      struct csched_unit *speer;
@@ -1958,15 +1973,19 @@ static void cf_check csched_schedule(
          /*
           * SMP Load balance:
           *
-         * If the next highest priority local runnable UNIT has already eaten
-         * through its credits, look on other PCPUs to see if we have more
-         * urgent work... If not, csched_load_balance() will return snext, but
-         * already removed from the runq.
+         * If the next highest priority local runnable UNIT has

xen/common/sched/credit.c static void cf_check csched_schedule( · line 1983

Quoted 10 lines
xen/common/sched/credit.c +6 -3
+         * already eaten through its credits (and we're below the
+         * balancing ratelimit), look on other PCPUs to see if we have
+         * more urgent work... If we don't, csched_load_balance() will
+         * return snext, but already removed from the runq.
           */
-        if ( snext->pri > CSCHED_PRI_TS_OVER )
-            __runq_remove(snext);
-        else
+        if ( snext->pri <= CSCHED_PRI_TS_OVER
+             && now - spc->last_load_balance > prv->load_balance_ratelimit) {

^ Just found a style issue (after Andrew pointing out the ones in patch 2).

Quoted 3 lines
Quoted 1 line

Just checking, you mean the space before the closing )?

And the placement of the opening {.

Gah, sorry I missed that. :-( (Golang won't compile if the { isn't on the same line.)

I guess I'll send a follow-up patch.

-George

#12 lore raw
In reply to George Dunlap

Hi George,

Quoted 27 lines · George Dunlap

The credit scheduler tries as hard as it can to ensure that it always runs scheduling units with positive credit (PRI_TS_UNDER) before running those with negative credit (PRI_TS_OVER). If the next runnable scheduling unit is of priority OVER, it will always run the load balancer, which will scour the system looking for another scheduling unit of the UNDER priority.

Unfortunately, as the number of cores on a system has grown, the cost of the work-stealing algorithm has dramatically increased; a recent trace on a system with 128 cores showed this taking over 50 microseconds.

Add a parameter, load_balance_ratelimit, to limit the frequency of load balance operations on a given pcpu. Default this to 1 millisecond.

Invert the load balancing conditional to make it more clear, and line up more closely with the comment above it.

Overall it might be cleaner to have the last_load_balance checking happen inside csched_load_balance(), but that would require either passing both now and spc into the function, or looking them up again; both of which seemed to be worse than simply checking and setting the values before calling it.

Quoted 5 lines

On a system with a vcpu:pcpu ratio of 2:1, running Windows guests (which will end up calling YIELD during spinlock contention), this patch increased performance significantly.

Kind regards,
Henry