Skip to contentSkip to conversation

xnthread_set_periodic: Improve period adjusting support

RFC PATCH In review Richard Weinberger 2 participants 3 messages Dec 20, 2023 – Jan 2, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xnthread_set_periodic: Improve period adjusting support

Currently, when a periodic task invokes xnthread_set_periodic() multiple times, the timer is reset each time.
Consequently, the period drifts because the elapsed time is not taken into consideration.
To improve the situation, account the elapsed time of the already running time to the new start time if the time is smaller than
the new period when idate is XN_INFINITE.

Alternatively, callers of xnthread_set_periodic() could access task->ptimer directly and pass an adjusted start time to xnthread_set_periodic().

---
 kernel/cobalt/thread.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
kernel/cobalt/thread.c +12 -3
index ff12f288a..a5304cc8a 100644
@@ -1335,9 +1335,18 @@ int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate,
 
 	xntimer_set_affinity(&thread->ptimer, thread->sched);
 
-	if (idate == XN_INFINITE)
-		xntimer_start(&thread->ptimer, period, period, XN_RELATIVE);
-	else {
+	if (idate == XN_INFINITE) {
+		xnticks_t start = period;
+
+		if (xntimer_running_p(&thread->ptimer)) {
+			xnticks_t elapsed = xntimer_interval(&thread->ptimer) -
+						__xntimer_get_timeout(&thread->ptimer);
+			if (elapsed < period)
+				start = period - elapsed;
+		}
+
+		xntimer_start(&thread->ptimer, start, period, XN_RELATIVE);
+	} else {
 		if (timeout_mode == XN_REALTIME)
 			idate -= xnclock_get_offset(xntimer_clock(&thread->ptimer));
 		else if (timeout_mode != XN_ABSOLUTE) {
Signature · 1 line
2.35.3
#1 lore raw
Quoted 8 lines · Richard Weinberger

Currently, when a periodic task invokes xnthread_set_periodic() multiple times, the timer is reset each time.
Consequently, the period drifts because the elapsed time is not taken into consideration.
To improve the situation, account the elapsed time of the already running time to the new start time if the time is smaller than
the new period when idate is XN_INFINITE.

If I scan our code base correctly, this only affects RTDM tasks, at least in-tree. Is that your use case as well?

Quoted 24 lines

Alternatively, callers of xnthread_set_periodic() could access task->ptimer directly and pass an adjusted start time to xnthread_set_periodic().

---
 kernel/cobalt/thread.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
kernel/cobalt/thread.c +5 -3
index ff12f288a..a5304cc8a 100644
@@ -1335,9 +1335,18 @@ int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate,
 
 	xntimer_set_affinity(&thread->ptimer, thread->sched);
 
-	if (idate == XN_INFINITE)
-		xntimer_start(&thread->ptimer, period, period, XN_RELATIVE);
-	else {
+	if (idate == XN_INFINITE) {
+		xnticks_t start = period;
+
+		if (xntimer_running_p(&thread->ptimer)) {
+			xnticks_t elapsed = xntimer_interval(&thread->ptimer) -

kernel/cobalt/thread.c int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate, · line 1352

Quoted 10 lines
kernel/cobalt/thread.c +7 -0
+						__xntimer_get_timeout(&thread->ptimer);
+			if (elapsed < period)
+				start = period - elapsed;
+		}
+
+		xntimer_start(&thread->ptimer, start, period, XN_RELATIVE);
+	} else {
 		if (timeout_mode == XN_REALTIME)
 			idate -= xnclock_get_offset(xntimer_clock(&thread->ptimer));
 		else if (timeout_mode != XN_ABSOLUTE) {

If we changed the behavior of xnthread_set_period, we would also need to update its documentation - which so far states that "now" is the start time when calling this service with idate == XN_INFINITE.

But why not using idate for this use case instead? Is the pattern too common to push it to the user?

And if changing the current logic, I would wonder why not saying that idate == XN_INFINITE means that the change will only take affect for the next period, not the current one. Would avoid losing one shot like in your proposal above.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#2 lore raw

----- Ursprüngliche Mail -----

Quoted 3 lines

Von: "Jan Kiszka" <[email protected]>
If I scan our code base correctly, this only affects RTDM tasks, at least in-tree. Is that your use case as well?

Exactly. I didn't implement for userspace tasks so far because my use case is purely RTDM.

Quoted 32 lines
Quoted 32 lines

Alternatively, callers of xnthread_set_periodic() could access task->ptimer directly and pass an adjusted start time to xnthread_set_periodic().

---
 kernel/cobalt/thread.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
kernel/cobalt/thread.c +11 -3
index ff12f288a..a5304cc8a 100644
@@ -1335,9 +1335,18 @@ int xnthread_set_periodic(struct xnthread *thread,
 xnticks_t idate,
 
 	xntimer_set_affinity(&thread->ptimer, thread->sched);
 
-	if (idate == XN_INFINITE)
-		xntimer_start(&thread->ptimer, period, period, XN_RELATIVE);
-	else {
+	if (idate == XN_INFINITE) {
+		xnticks_t start = period;
+
+		if (xntimer_running_p(&thread->ptimer)) {
+			xnticks_t elapsed = xntimer_interval(&thread->ptimer) -
+						__xntimer_get_timeout(&thread->ptimer);
+			if (elapsed < period)
+				start = period - elapsed;
+		}
+
+		xntimer_start(&thread->ptimer, start, period, XN_RELATIVE);
Quoted 8 lines
Quoted 4 lines
kernel/cobalt/thread.c +1 -0
+	} else {
 		if (timeout_mode == XN_REALTIME)
 			idate -= xnclock_get_offset(xntimer_clock(&thread->ptimer));

else if (timeout_mode != XN_ABSOLUTE) {

If we changed the behavior of xnthread_set_period, we would also need to update its documentation - which so far states that "now" is the start time when calling this service with idate == XN_INFINITE.

Sure. This patch is just an RFC.

Quoted 2 lines

But why not using idate for this use case instead? Is the pattern too common to push it to the user?

While this is doable, I found it a bit hacky to access task->ptimer directly. IMHO ->ptimer is private and should not directly accessed.
At least my other prototype uses xntimer_interval() and __xntimer_get_timeout() to get all information need from the periodic timer to change the period while it runs.

Quoted 4 lines

And if changing the current logic, I would wonder why not saying that idate == XN_INFINITE means that the change will only take affect for the next period, not the current one. Would avoid losing one shot like in your proposal above.

I need to think about this but as far I understand your idea this requires an extra variable in the timer or task struct.

Thanks,
//richard