Skip to contentSkip to conversation

cobalt/sched-quota: Account for already consumed time on limit update

PATCH New Jan Kiszka 2 participants 4 messages Nov 21, 2018 – Nov 26, 2018

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 cobalt/sched-quota: Account for already consumed time on limit update

If we change the quota value of thread group, we should take its already consumed time during the current cycle into account. So far, we refilled the budget blindly, which could have given the group an uncontrolled "peak".

This commit first update the run_budget_ns of an active group and kills its limit timer. Then it refills run_budget_ns only with the remaining budget of the current cycle, taking the new quota as limit.

Accumulated credits are still dropped - not worth to deal with their complexity in this scenario.

---

Philippe, review would be appreciated.

I'm still struggling with writing a good test case for that.

 kernel/cobalt/sched-quota.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
kernel/cobalt/sched-quota.c +26 -1
index 817699457e..7e58f1b374 100644
@@ -572,6 +572,8 @@ void xnsched_quota_set_limit(struct xnsched_quota_group *tg,
 			     int *quota_sum_r)
 {
 	struct xnsched_quota *qs = &tg->sched->quota;
+	xnticks_t old_quota_ns = tg->quota_ns;
+	xnticks_t now, elapsed, consumed;
 
 	atomic_only();
 
@@ -592,7 +594,30 @@ void xnsched_quota_set_limit(struct xnsched_quota_group *tg,
 
 	tg->quota_percent = quota_percent;
 	tg->quota_peak_percent = quota_peak_percent;
-	tg->run_budget_ns = tg->quota_ns;
+
+	if (group_is_active(tg)) {
+		now = xnclock_read_monotonic(&nkclock);
+
+		elapsed = now - tg->run_start_ns;
+		if (elapsed < tg->run_budget_ns)
+			tg->run_budget_ns -= elapsed;
+		else
+			tg->run_budget_ns = 0;
+
+		tg->run_start_ns = now;
+
+		xntimer_stop(&qs->limit_timer);
+	}
+
+	if (tg->run_budget_ns <= old_quota_ns)
+		consumed = old_quota_ns - tg->run_budget_ns;
+	else
+		consumed = 0;
+	if (tg->quota_ns >= consumed)
+		tg->run_budget_ns = tg->quota_ns - consumed;
+	else
+		tg->run_budget_ns = 0;
+
 	tg->run_credit_ns = 0;	/* Drop accumulated credit. */
 
 	*quota_sum_r = quota_sum_all(qs);
Signature · 1 line
2.16.4
#1 lore raw

PATCH 1/1 cobalt/sched-quota: Account for already consumed time on limit update

Quoted 9 lines · Jan Kiszka

If we change the quota value of thread group, we should take its already consumed time during the current cycle into account. So far, we refilled the budget blindly, which could have given the group an uncontrolled "peak".

This commit first update the run_budget_ns of an active group and kills its limit timer. Then it refills run_budget_ns only with the remaining budget of the current cycle, taking the new quota as limit.

Quoted 7 lines

Accumulated credits are still dropped - not worth to deal with their complexity in this scenario.

Quoted 1 line

---

Philippe, review would be appreciated.

Looks good.

Quoted 2 lines

I'm still struggling with writing a good test case for that.

Absolutely untested, but what about doing this: calibrate some number crunching loop to have it running for a given time you would use as the initial quota value (e.g. 1s). Then,

. set the initial quota value
. run a test loop for 2/3 of the iterations spent in the calibration loop, . decrease the initial quota value by half
. re-run the test loop, it should not have run more than half of the calibrated iterations (+/- some error margin and fluctuations). A broken quota implementation would have allowed 2/3 if IIUC.

Checking for the actual number of iterations performed during the second run could be done from a low priority thread which resumes when the test runner is dequeued as the quota expires.

Both the test runner and the checker should be pinned on the same CPU. With QUOTA_PERIOD larger and not too close to the initial quota value, this should leave enough time for the low priority thread to check the result.

Signature · 1 line
Philippe.
author
#2 lore raw
Quoted 12 lines · Philippe Gerum
Quoted 11 lines · Jan Kiszka

If we change the quota value of thread group, we should take its already consumed time during the current cycle into account. So far, we refilled the budget blindly, which could have given the group an uncontrolled "peak".

This commit first update the run_budget_ns of an active group and kills its limit timer. Then it refills run_budget_ns only with the remaining budget of the current cycle, taking the new quota as limit.

Accumulated credits are still dropped - not worth to deal with their complexity in this scenario.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

---

Philippe, review would be appreciated.

Looks good.

Thanks. Applied to next now.

Quoted 8 lines
Quoted 3 lines

I'm still struggling with writing a good test case for that.

Absolutely untested, but what about doing this: calibrate some number crunching loop to have it running for a given time you would use as the initial quota value (e.g. 1s). Then,

Quoted 6 lines

. set the initial quota value
. run a test loop for 2/3 of the iterations spent in the calibration loop, . decrease the initial quota value by half
. re-run the test loop, it should not have run more than half of the calibrated iterations (+/- some error margin and fluctuations). A broken quota implementation would have allowed 2/3 if IIUC.

I think the difference was only visible in the cycle of the adjustment. It should have worked fine again on the next, clean round.

Quoted 10 lines

Checking for the actual number of iterations performed during the second run could be done from a low priority thread which resumes when the test runner is dequeued as the quota expires.

Both the test runner and the checker should be pinned on the same CPU. With QUOTA_PERIOD larger and not too close to the initial quota value, this should leave enough time for the low priority thread to check the result.

I will think about that. It's annoying when tests turn out to be as complex - if not more - as the actual code they test.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
author
#3 lore raw
Quoted 14 lines · Jan Kiszka
Quoted 13 lines · Philippe Gerum
Quoted 12 lines · Jan Kiszka

If we change the quota value of thread group, we should take its already consumed time during the current cycle into account. So far, we refilled the budget blindly, which could have given the group an uncontrolled "peak".

This commit first update the run_budget_ns of an active group and kills its limit timer. Then it refills run_budget_ns only with the remaining budget of the current cycle, taking the new quota as limit.

Accumulated credits are still dropped - not worth to deal with their complexity in this scenario.

Quoted 1 line
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

---

Philippe, review would be appreciated.

Looks good.

Thanks. Applied to next now.

...and it will go into stable as well.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux