Skip to contentSkip to conversation

credit: track residual from divisions done during accounting

PATCH Superseded Jan Beulich 6 participants 18 messages Feb 18, 2013 – Feb 26, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 credit: track residual from divisions done during accounting

This should help with under-accounting of vCPU-s running for extremly short periods of time, but becoming runnable again at a high frequency.

xen/common/sched_credit.c +7 -1
@@ -19,6 +19,7 @@
 #include <xen/sched-if.h>
 #include <xen/softirq.h>
 #include <asm/atomic.h>
+#include <asm/div64.h>
 #include <xen/errno.h>
 #include <xen/keyhandler.h>
 #include <xen/trace.h>
@@ -136,6 +137,7 @@ struct csched_vcpu {
     struct csched_dom *sdom;
     struct vcpu *vcpu;
     atomic_t credit;
+    unsigned int residual;
     s_time_t start_time;   /* When we were scheduled (used for credit) */
     uint16_t flags;
     int16_t pri;
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);
     atomic_sub(credits, &svc->credit);
     svc->start_time += (credits * MILLISECS(1)) / CSCHED_CREDITS_PER_MSEC;
 }
xen/common/sched_credit2.c +9 -5
@@ -21,7 +21,7 @@
 #include <xen/perfc.h>
 #include <xen/sched-if.h>
 #include <xen/softirq.h>
-#include <asm/atomic.h>
+#include <asm/div64.h>
 #include <xen/errno.h>
 #include <xen/trace.h>
 #include <xen/cpu.h>
@@ -205,7 +205,7 @@ struct csched_runqueue_data {
 
     struct list_head runq; /* Ordered list of runnable vms */
     struct list_head svc;  /* List of all vcpus assigned to this runqueue */
-    int max_weight;
+    unsigned int max_weight;
 
     cpumask_t idle,        /* Currently idle */
         tickled;           /* Another cpu in the queue is already targeted for this one */
@@ -244,7 +244,8 @@ struct csched_vcpu {
     struct csched_dom *sdom;
     struct vcpu *vcpu;
 
-    int weight;
+    unsigned int weight;
+    unsigned int residual;
 
     int credit;
     s_time_t start_time; /* When we were scheduled (used for credit) */
@@ -275,12 +276,15 @@ struct csched_dom {
  */
 static s_time_t t2c(struct csched_runqueue_data *rqd, s_time_t time, struct csched_vcpu *svc)
 {
-    return time * rqd->max_weight / svc->weight;
+    uint64_t val = time * rqd->max_weight + svc->residual;
+
+    svc->residual = do_div(val, svc->weight);
+    return val;
 }
 
 static s_time_t c2t(struct csched_runqueue_data *rqd, s_time_t credit, struct csched_vcpu *svc)
 {
-    return credit * svc->weight / rqd->max_weight;
+    return (credit * svc->weight - svc->residual) / rqd->max_weight;
 }
 
 /*

credit: track residual from divisions done during accounting

This should help with under-accounting of vCPU-s running for extremly short periods of time, but becoming runnable again at a high frequency.

xen/common/sched_credit.c +7 -1
@@ -19,6 +19,7 @@
 #include <xen/sched-if.h>
 #include <xen/softirq.h>
 #include <asm/atomic.h>
+#include <asm/div64.h>
 #include <xen/errno.h>
 #include <xen/keyhandler.h>
 #include <xen/trace.h>
@@ -136,6 +137,7 @@ struct csched_vcpu {
     struct csched_dom *sdom;
     struct vcpu *vcpu;
     atomic_t credit;
+    unsigned int residual;
     s_time_t start_time;   /* When we were scheduled (used for credit) */
     uint16_t flags;
     int16_t pri;
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);
     atomic_sub(credits, &svc->credit);
     svc->start_time += (credits * MILLISECS(1)) / CSCHED_CREDITS_PER_MSEC;
 }
xen/common/sched_credit2.c +9 -5
@@ -21,7 +21,7 @@
 #include <xen/perfc.h>
 #include <xen/sched-if.h>
 #include <xen/softirq.h>
-#include <asm/atomic.h>
+#include <asm/div64.h>
 #include <xen/errno.h>
 #include <xen/trace.h>
 #include <xen/cpu.h>
@@ -205,7 +205,7 @@ struct csched_runqueue_data {
 
     struct list_head runq; /* Ordered list of runnable vms */
     struct list_head svc;  /* List of all vcpus assigned to this runqueue */
-    int max_weight;
+    unsigned int max_weight;
 
     cpumask_t idle,        /* Currently idle */
         tickled;           /* Another cpu in the queue is already targeted for this one */
@@ -244,7 +244,8 @@ struct csched_vcpu {
     struct csched_dom *sdom;
     struct vcpu *vcpu;
 
-    int weight;
+    unsigned int weight;
+    unsigned int residual;
 
     int credit;
     s_time_t start_time; /* When we were scheduled (used for credit) */
@@ -275,12 +276,15 @@ struct csched_dom {
  */
 static s_time_t t2c(struct csched_runqueue_data *rqd, s_time_t time, struct csched_vcpu *svc)
 {
-    return time * rqd->max_weight / svc->weight;
+    uint64_t val = time * rqd->max_weight + svc->residual;
+
+    svc->residual = do_div(val, svc->weight);
+    return val;
 }
 
 static s_time_t c2t(struct csched_runqueue_data *rqd, s_time_t credit, struct csched_vcpu *svc)
 {
-    return credit * svc->weight / rqd->max_weight;
+    return (credit * svc->weight - svc->residual) / rqd->max_weight;
 }
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 12 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 8 lines
xen/common/sched_credit.c +4 -1
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

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]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 15 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
Quoted 8 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 5 lines
xen/common/sched_credit.c +4 -1
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

val being wider than credit, this checks that there was no truncation.

Jan

#3 lore raw
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 15 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
Quoted 8 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 5 lines
xen/common/sched_credit.c +4 -1
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

Quoted 2 lines

val being wider than credit, this checks that there was no truncation.

Ok. At least I was right in the "missing something" part. :-) TBH, I figured it had to be something related to that, but was failing to spot it... Sorry for the noise and thanks for the explanation!

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]
http://lists.xen.org/xen-devel

#4 lore raw
In reply to Jan Beulich
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 15 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
Quoted 8 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 5 lines
xen/common/sched_credit.c +4 -1
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

Quoted 1 line

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Would be clearer.

David

#5 lore raw
Quoted 26 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 10 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
 static void burn_credits(struct csched_vcpu *svc, s_time_t now)
 {
     s_time_t delta;
+    uint64_t val;
     unsigned int credits;
 
Quoted 6 lines
Quoted 6 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 253

Quoted 6 lines
xen/common/sched_credit.c +0 -1
     /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
     if ( (delta = now - svc->start_time) <= 0 )
         return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / 

MILLISECS(1);

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/common/sched_credit.c +4 -0
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I may be missing something, but how can the assert ever be false, given the assignment right before it?

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Would be clearer.

A matter of taste perhaps...

Jan

#6 lore raw
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 10 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
  static void burn_credits(struct csched_vcpu *svc, s_time_t now)
  {
      s_time_t delta;
+    uint64_t val;
      unsigned int credits;
 
Quoted 6 lines
Quoted 6 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 253

Quoted 6 lines
xen/common/sched_credit.c +0 -1
      /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
      if ( (delta = now - svc->start_time) <= 0 )
          return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) /

MILLISECS(1);

Quoted 8 lines
Quoted 7 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 4 lines
xen/common/sched_credit.c +4 -0
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Would be clearer.

A matter of taste perhaps...

I have a taste for coders having to keep as little state in their head as possible. :-) Comparing to UINT_MAX prompts the coder specifically to think about the size of the variables.

-George

#7 lore raw
Quoted 35 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 10 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
  static void burn_credits(struct csched_vcpu *svc, s_time_t now)
  {
      s_time_t delta;
+    uint64_t val;
      unsigned int credits;
 
Quoted 6 lines
Quoted 6 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 253

Quoted 6 lines
xen/common/sched_credit.c +0 -1
      /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
      if ( (delta = now - svc->start_time) <= 0 )
          return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) /

MILLISECS(1);

Quoted 9 lines
Quoted 7 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 4 lines
xen/common/sched_credit.c +4 -0
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Would be clearer.

A matter of taste perhaps...

I have a taste for coders having to keep as little state in their head as possible. :-) Comparing to UINT_MAX prompts the coder specifically to think about the size of the variables.

Okay, assuming this is the only thing you dislike, I'll change it then and re-submit.

But for the record - using UINT_MAX here will get things out of sync the moment the type of "credits" changes, whereas with the way I had coded it this would be taken care of implicitly.

Jan

#8 lore raw
Quoted 43 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 10 lines · Jan Beulich
xen/common/sched_credit.c +1 -0
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
  static void burn_credits(struct csched_vcpu *svc, s_time_t now)
  {
      s_time_t delta;
+    uint64_t val;
      unsigned int credits;
 
Quoted 6 lines
Quoted 6 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 253

Quoted 6 lines
xen/common/sched_credit.c +0 -1
      /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
      if ( (delta = now - svc->start_time) <= 0 )
          return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) /

MILLISECS(1);

Quoted 9 lines
Quoted 7 lines

xen/common/sched_credit.c static void burn_credits(struct csched_v · line 259

Quoted 4 lines
xen/common/sched_credit.c +4 -0
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Would be clearer.

A matter of taste perhaps...

I have a taste for coders having to keep as little state in their head as possible. :-) Comparing to UINT_MAX prompts the coder specifically to think about the size of the variables.

Quoted 6 lines

Okay, assuming this is the only thing you dislike, I'll change it then and re-submit.

But for the record - using UINT_MAX here will get things out of sync the moment the type of "credits" changes, whereas with the way I had coded it this would be taken care of implicitly.

How about ASSERT(((typeof credits) val) == val) before the assignment?

Tim.

#9 lore raw
Quoted 24 lines · Tim Deegan
Quoted 23 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Dario Faggioli
Quoted 1 line · Jan Beulich

+ ASSERT(credits == val);

I may be missing something, but how can the assert ever be false, given the assignment right before it?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

val being wider than credit, this checks that there was no truncation.

ASSERT(val <= UINT_MAX);

Would be clearer.

A matter of taste perhaps...

I have a taste for coders having to keep as little state in their head as possible. :-) Comparing to UINT_MAX prompts the coder specifically to think about the size of the variables.

Quoted 8 lines
Quoted 6 lines

Okay, assuming this is the only thing you dislike, I'll change it then and re-submit.

But for the record - using UINT_MAX here will get things out of sync the moment the type of "credits" changes, whereas with the way I had coded it this would be taken care of implicitly.

How about ASSERT(((typeof credits) val) == val) before the assignment?

FWIW, this works for me.

David

#10 lore raw
Quoted 2 lines · David Vrabel
Quoted 1 line · Tim Deegan

How about ASSERT(((typeof credits) val) == val) before the assignment?

Why not just

	ASSERT(credits == val); /* Ensure we haven't truncated val */
#11 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 2 lines · David Vrabel
Quoted 1 line · Tim Deegan

How about ASSERT(((typeof credits) val) == val) before the assignment?

Why not just

	ASSERT(credits == val); /* Ensure we haven't truncated val */

I.e. just want em to add a comment? That's certainly fine with me.

Jan

#12 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 2 lines · David Vrabel
Quoted 1 line · Tim Deegan

How about ASSERT(((typeof credits) val) == val) before the assignment?

Why not just

	ASSERT(credits == val); /* Ensure we haven't truncated val */

I.e. just want em to add a comment?

That's right s/val/credits/ depending on what the ASSERT is really doing (I didn't go back and check)

Ian.

#13 lore raw
In reply to Ian Campbell
Quoted 6 lines · Ian Campbell
Quoted 2 lines · David Vrabel
Quoted 1 line · Tim Deegan

How about ASSERT(((typeof credits) val) == val) before the assignment?

Why not just

	ASSERT(credits == val); /* Ensure we haven't truncated val */

I prefer this one to having "typeof credits" in the ASSERT. The main thing is just to minimize the amount of effort a programmer has to expend trying to figure out what's going on, so he can spend it on something else. :-)

-George

#14 lore raw
Quoted 5 lines · George Dunlap
Quoted 4 lines · Ian Campbell
Quoted 2 lines · David Vrabel
Quoted 1 line · Tim Deegan

How about ASSERT(((typeof credits) val) == val) before the assignment?

Quoted 7 lines
Quoted 2 lines

Why not just

	ASSERT(credits == val); /* Ensure we haven't truncated val */

I prefer this one to having "typeof credits" in the ASSERT. The main thing is just to minimize the amount of effort a programmer has to expend trying to figure out what's going on, so he can spend it on something else. :-)

Quite. It occurred to me afterwards that we could have a FITS_IN() macro for that purpose. Or, as mentioned, a comment. :)

Tim.

#15 lore raw
In reply to Jan Beulich
Quoted 4 lines · Jan Beulich

This should help with under-accounting of vCPU-s running for extremly short periods of time, but becoming runnable again at a high frequency.

The changes to credit1 look good, and I'm fine with a patch having those (and a commented ASSERT) go in.

Credit2 I'm not so happy with, because the names "t2c" and "c2t" imply (at least to me) that they are only converting, not changing anything; particularly in the way that t2c is called. At the moment everything will work fine, but it's just laying a trap for someone in the future. :-)

I've got a patch in my queue dealing with this section already -- why don't you apply just the sched_credit.c part of the patch, and I'll take the credit2 part of your patch and rework it so it satisfies me.

-George

Quoted 90 lines
xen/common/sched_credit.c +7 -1
@@ -19,6 +19,7 @@
  #include <xen/sched-if.h>
  #include <xen/softirq.h>
  #include <asm/atomic.h>
+#include <asm/div64.h>
  #include <xen/errno.h>
  #include <xen/keyhandler.h>
  #include <xen/trace.h>
@@ -136,6 +137,7 @@ struct csched_vcpu {
      struct csched_dom *sdom;
      struct vcpu *vcpu;
      atomic_t credit;
+    unsigned int residual;
      s_time_t start_time;   /* When we were scheduled (used for credit) */
      uint16_t flags;
      int16_t pri;
@@ -242,6 +244,7 @@ __runq_remove(struct csched_vcpu *svc)
  static void burn_credits(struct csched_vcpu *svc, s_time_t now)
  {
      s_time_t delta;
+    uint64_t val;
      unsigned int credits;
 
      /* Assert svc is current */
@@ -250,7 +253,10 @@ static void burn_credits(struct csched_v
      if ( (delta = now - svc->start_time) <= 0 )
          return;
 
-    credits = (delta*CSCHED_CREDITS_PER_MSEC + MILLISECS(1)/2) / MILLISECS(1);
+    val = delta * CSCHED_CREDITS_PER_MSEC + svc->residual;
+    svc->residual = do_div(val, MILLISECS(1));
+    credits = val;
+    ASSERT(credits == val);
      atomic_sub(credits, &svc->credit);
      svc->start_time += (credits * MILLISECS(1)) / CSCHED_CREDITS_PER_MSEC;
  }
xen/common/sched_credit2.c +9 -5
@@ -21,7 +21,7 @@
  #include <xen/perfc.h>
  #include <xen/sched-if.h>
  #include <xen/softirq.h>
-#include <asm/atomic.h>
+#include <asm/div64.h>
  #include <xen/errno.h>
  #include <xen/trace.h>
  #include <xen/cpu.h>
@@ -205,7 +205,7 @@ struct csched_runqueue_data {
 
      struct list_head runq; /* Ordered list of runnable vms */
      struct list_head svc;  /* List of all vcpus assigned to this runqueue */
-    int max_weight;
+    unsigned int max_weight;
 
      cpumask_t idle,        /* Currently idle */
          tickled;           /* Another cpu in the queue is already targeted for this one */
@@ -244,7 +244,8 @@ struct csched_vcpu {
      struct csched_dom *sdom;
      struct vcpu *vcpu;
 
-    int weight;
+    unsigned int weight;
+    unsigned int residual;
 
      int credit;
      s_time_t start_time; /* When we were scheduled (used for credit) */
@@ -275,12 +276,15 @@ struct csched_dom {
   */
  static s_time_t t2c(struct csched_runqueue_data *rqd, s_time_t time, struct csched_vcpu *svc)
  {
-    return time * rqd->max_weight / svc->weight;
+    uint64_t val = time * rqd->max_weight + svc->residual;
+
+    svc->residual = do_div(val, svc->weight);
+    return val;
  }
 
  static s_time_t c2t(struct csched_runqueue_data *rqd, s_time_t credit, struct csched_vcpu *svc)
  {
-    return credit * svc->weight / rqd->max_weight;
+    return (credit * svc->weight - svc->residual) / rqd->max_weight;
  }
 
  /*
#16 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 4 lines · Jan Beulich

This should help with under-accounting of vCPU-s running for extremly short periods of time, but becoming runnable again at a high frequency.

The changes to credit1 look good, and I'm fine with a patch having those (and a commented ASSERT) go in.

Quoted 8 lines

Credit2 I'm not so happy with, because the names "t2c" and "c2t" imply (at least to me) that they are only converting, not changing anything; particularly in the way that t2c is called. At the moment everything will work fine, but it's just laying a trap for someone in the future. :-)

I've got a patch in my queue dealing with this section already -- why don't you apply just the sched_credit.c part of the patch, and I'll take the credit2 part of your patch and rework it so it satisfies me.

That's fine with me of course.

May I take the above as a pre-ack to a patch modified accordingly?

Jan

#17 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 4 lines · Jan Beulich

This should help with under-accounting of vCPU-s running for extremly short periods of time, but becoming runnable again at a high frequency.

The changes to credit1 look good, and I'm fine with a patch having those (and a commented ASSERT) go in.

Credit2 I'm not so happy with, because the names "t2c" and "c2t" imply (at least to me) that they are only converting, not changing anything; particularly in the way that t2c is called. At the moment everything will work fine, but it's just laying a trap for someone in the future. :-)

Quoted 7 lines
Quoted 3 lines

I've got a patch in my queue dealing with this section already -- why don't you apply just the sched_credit.c part of the patch, and I'll take the credit2 part of your patch and rework it so it satisfies me.

That's fine with me of course.

May I take the above as a pre-ack to a patch modified accordingly?

Yes, I suppose that's fine. :-)

-George