Skip to contentSkip to conversation

cyclic: update and optimization

PATCH Superseded Patrice Chotard 2 participants 5 messages Jul 3 – Jul 6 Generic include/asm header files

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover cyclic: update and optimization

First patch is replacing uint64_t by u64 as suggested by b4 Second patch optimizes cyclic_run() to parse cyclic list only if a cyclic function's timestamp is elapsed.

To: [email protected]
Cc: Stefan Roese <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Marek Vasut <[email protected]>

---
Changes in v2:
- move cyclic with nearest deadline on list's head.
- Link to v1: https://patch.msgid.link/[email protected]

To: [email protected]
Cc: Stefan Roese <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Patrice Chotard <[email protected]>

---
Patrice Chotard (2):
cyclic: replace uint64_t by u64 suggested by b4
cyclic: parse cyclic list only if next cyclic timestamp is reached

 common/cyclic.c                   | 20 ++++++++++++++++++--
 include/asm-generic/global_data.h |  4 ++++
 2 files changed, 22 insertions(+), 2 deletions(-)

---
base-commit: 1296a428c67cf103eca482d4a63349661c1b799f
change-id: 20260702-optimize_cyclic_run-ecdc46f12829

Best regards,

Signature · 1 line
Patrice Chotard <[email protected]>
#1 lore raw

PATCH v2 1/2 cyclic: replace uint64_t by u64 suggested by b4

For new patch, b4 is suggested to replace type 'uint64_t' by 'u64' :
CHECK: Prefer kernel type 'u64' over 'uint64_t'

Update cyclic.c accordingly in order to be coherent with following commit.

Cc Marek Vasut <[email protected]>
---
 common/cyclic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
common/cyclic.c +2 -2
index ec952a01ee1..b37cd6d8ff0 100644
@@ -41,7 +41,7 @@ static bool cyclic_is_registered(const struct cyclic_info *cyclic)
 }
 
 void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
-		     uint64_t delay_us, const char *name)
+		     u64 delay_us, const char *name)
 {
 	cyclic_unregister(cyclic);
 
@@ -67,7 +67,7 @@ static void cyclic_run(void)
 {
 	struct cyclic_info *cyclic;
 	struct hlist_node *tmp;
-	uint64_t now, cpu_time;
+	u64 now, cpu_time;
 
 	/* Prevent recursion */
 	if (gd->flags & GD_FLG_CYCLIC_RUNNING)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 cyclic: parse cyclic list only if next cyclic timestamp is reached

In reply to Patrice Chotard

On STM32MP157C-DK2, when using the "ums" command, in sleep_thread(), ctrlc() is called every ~640ms which doesn't allows high reactivity when user press CTRL+C in U-Boot console.

In sleep_thread() loop, ctrlc() is called every 200000 iterations. But schedule is called on each loop iteration.

Optimize cyclic_run() in order to parse the cyclic list only is the next cyclic to run is due.
During cyclic list parsing, save the nearest cyclic timestamp, which allows to exit from next cyclic_run() call if no cyclic will be executed.

This allow to save computation time :
_ before : ctrlc() is called every ~640ms
_ after : ctrlc() is called every ~230ms

---
 common/cyclic.c                   | 16 ++++++++++++++++
 include/asm-generic/global_data.h |  4 ++++
 2 files changed, 20 insertions(+)
common/cyclic.c +16 -0
index b37cd6d8ff0..10ea0711ba1 100644
@@ -68,11 +68,17 @@ static void cyclic_run(void)
 	struct cyclic_info *cyclic;
 	struct hlist_node *tmp;
 	u64 now, cpu_time;
+	u64 next_call = UINT64_MAX;
 
 	/* Prevent recursion */
 	if (gd->flags & GD_FLG_CYCLIC_RUNNING)
 		return;
 
+	/* check if the next cyclic function's timestamp is reached */
+	now = get_timer_us(0);
+	if (time_after_eq64(gd->next_cyclic_call, now))
+		return;
+
 	gd->flags |= GD_FLG_CYCLIC_RUNNING;
 	hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
 		/*
@@ -102,7 +108,17 @@ static void cyclic_run(void)
 				cyclic->already_warned = true;
 			}
 		}
+		if (next_call > cyclic->next_call) {
+			next_call = cyclic->next_call;
+			/* move this cyclic to list's head */
+			hlist_del(&cyclic->list);
+			hlist_add_head(&cyclic->list, cyclic_get_list());
+		}
 	}
+
+	if (next_call != UINT64_MAX)
+		gd->next_cyclic_call = next_call;
+
 	gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
 }
 
include/asm-generic/global_data.h +4 -0
index 745d2c3a966..25e488d7020 100644
@@ -441,6 +441,10 @@ struct global_data {
 	 * @cyclic_list: list of registered cyclic functions
 	 */
 	struct hlist_head cyclic_list;
+	/**
+	 * @next_cyclic_call: timestamp of next cyclic call
+	 */
+	u64 next_cyclic_call;
 #endif
 #if CONFIG_IS_ENABLED(UPL)
 	/**
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2 2/2] cyclic: parse cyclic list only if next cyclic timestamp is reached

Quoted 49 lines · Patrice Chotard

On STM32MP157C-DK2, when using the "ums" command, in sleep_thread(), ctrlc() is called every ~640ms which doesn't allows high reactivity when user press CTRL+C in U-Boot console.

In sleep_thread() loop, ctrlc() is called every 200000 iterations. But schedule is called on each loop iteration.

Optimize cyclic_run() in order to parse the cyclic list only is the next cyclic to run is due.
During cyclic list parsing, save the nearest cyclic timestamp, which allows to exit from next cyclic_run() call if no cyclic will be executed.

This allow to save computation time :
_ before : ctrlc() is called every ~640ms
_ after : ctrlc() is called every ~230ms

---
 common/cyclic.c                   | 16 ++++++++++++++++
 include/asm-generic/global_data.h |  4 ++++
 2 files changed, 20 insertions(+)
common/cyclic.c +6 -0
index b37cd6d8ff0..10ea0711ba1 100644
@@ -68,11 +68,17 @@ static void cyclic_run(void)
 	struct cyclic_info *cyclic;
 	struct hlist_node *tmp;
 	u64 now, cpu_time;
+	u64 next_call = UINT64_MAX;
 
 	/* Prevent recursion */
 	if (gd->flags & GD_FLG_CYCLIC_RUNNING)
 		return;
 
+	/* check if the next cyclic function's timestamp is reached */
+	now = get_timer_us(0);
+	if (time_after_eq64(gd->next_cyclic_call, now))
+		return;
+
 	gd->flags |= GD_FLG_CYCLIC_RUNNING;
 	hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
 		/*
@@ -102,7 +108,17 @@ static void cyclic_run(void)
 				cyclic->already_warned = true;
 			}
 		}

common/cyclic.c static void cyclic_run(void) · line 120

Quoted 10 lines
common/cyclic.c +9 -0
+		if (next_call > cyclic->next_call) {
+			next_call = cyclic->next_call;
+			/* move this cyclic to list's head */
+			hlist_del(&cyclic->list);
+			hlist_add_head(&cyclic->list, cyclic_get_list());
+		}
 	}
+
+	if (next_call != UINT64_MAX)
+		gd->next_cyclic_call = next_call;

Are you sure this is correct? There are at least two things I can think of that might be wrong:

(1) Doing > or < comparison (as done in the next_call > cyclic_next_call line), that should never be done, because the logic around timestamps is that we do allow wrapping, assuming the two timestamps being compared never represent absolute times that differ by more than what can be represented in the signed version of the timestamp type - that's why the time_after*/time_before* family do a subtraction and a signed compare to 0.

(2) What happens if we have a single cyclic consumer registered, with a rather long period (say 1s), hence a long time till its next_call, then another cyclic consumer is registered which should be called every 50ms. Since I don't see anything added to cyclic_register(), it seems that the new consumer would be ignored for almost a second?

I'm also not really sure what the "move the first expiring timer to the head" logic is supposed to achieve. Either way, if any timer has expired, we'll run through the whole list, so while the first expiring timer would then be handled first on the next cyclic_run(), that doesn't mean we'll save any runtime. And this exercise seems to be about doing as little as possible in cyclic_run(), so can you explain what the pointer juggling does?

I assume that what makes cyclic_run() somewhat expensive isn't actually running through the cyclic_list(), but the get_timer_us() calls done for each entry. Could you try to see what something like this (entirely untested, but you get the idea) would do:

common/cyclic.c +5 -3
index ec952a01ee1..2616c4d5bc6 100644
@@ -67,26 +67,28 @@ static void cyclic_run(void)
 {
        struct cyclic_info *cyclic;
        struct hlist_node *tmp;
-       uint64_t now, cpu_time;
+       uint64_t now, after, cpu_time;
 
        /* Prevent recursion */
        if (gd->flags & GD_FLG_CYCLIC_RUNNING)
                return;
 
        gd->flags |= GD_FLG_CYCLIC_RUNNING;
+       now = get_timer_us(0);
        hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
                /*
                 * Check if this cyclic function needs to get called, e.g.
                 * do not call the cyclic func too often
                 */
-               now = get_timer_us(0);
                if (time_after_eq64(now, cyclic->next_call)) {
                        /* Call cyclic function and account it's cpu-time */
                        cyclic->next_call = now + cyclic->delay_us;
                        cyclic->func(cyclic);
+                       after = get_timer_us(0);
                        cyclic->run_cnt++;
-                       cpu_time = get_timer_us(0) - now;
+                       cpu_time = after - now;
                        cyclic->cpu_time_us += cpu_time;
+                       now = after;
 
                        /* Check if cpu-time exceeds max allowed time */
                        if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&

Because the time_after_eq64() test really doesn't take anywhere near a micro-second, nor does fetching the next element from the linked list, so using the same value for "now" until we actually do hit an expired timer should be ok.

[I also want to move the computation of the new cyclic->next_call to after the callback, so that the callback can update its ->delay_us value if it needs to be called at another frequency, but that's a separate topic.]

Rasmus

#4 lore raw

Re: [PATCH v2 2/2] cyclic: parse cyclic list only if next cyclic timestamp is reached

Quoted 65 lines · Rasmus Villemoes
Quoted 49 lines · Patrice Chotard

On STM32MP157C-DK2, when using the "ums" command, in sleep_thread(), ctrlc() is called every ~640ms which doesn't allows high reactivity when user press CTRL+C in U-Boot console.

In sleep_thread() loop, ctrlc() is called every 200000 iterations. But schedule is called on each loop iteration.

Optimize cyclic_run() in order to parse the cyclic list only is the next cyclic to run is due.
During cyclic list parsing, save the nearest cyclic timestamp, which allows to exit from next cyclic_run() call if no cyclic will be executed.

This allow to save computation time :
_ before : ctrlc() is called every ~640ms
_ after : ctrlc() is called every ~230ms

---
 common/cyclic.c                   | 16 ++++++++++++++++
 include/asm-generic/global_data.h |  4 ++++
 2 files changed, 20 insertions(+)
common/cyclic.c +6 -0
index b37cd6d8ff0..10ea0711ba1 100644
@@ -68,11 +68,17 @@ static void cyclic_run(void)
 	struct cyclic_info *cyclic;
 	struct hlist_node *tmp;
 	u64 now, cpu_time;
+	u64 next_call = UINT64_MAX;
 
 	/* Prevent recursion */
 	if (gd->flags & GD_FLG_CYCLIC_RUNNING)
 		return;
 
+	/* check if the next cyclic function's timestamp is reached */
+	now = get_timer_us(0);
+	if (time_after_eq64(gd->next_cyclic_call, now))
+		return;
+
 	gd->flags |= GD_FLG_CYCLIC_RUNNING;
 	hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
 		/*
@@ -102,7 +108,17 @@ static void cyclic_run(void)
 				cyclic->already_warned = true;
 			}
 		}

common/cyclic.c static void cyclic_run(void) · line 120

Quoted 10 lines
common/cyclic.c +9 -0
+		if (next_call > cyclic->next_call) {
+			next_call = cyclic->next_call;
+			/* move this cyclic to list's head */
+			hlist_del(&cyclic->list);
+			hlist_add_head(&cyclic->list, cyclic_get_list());
+		}
 	}
+
+	if (next_call != UINT64_MAX)
+		gd->next_cyclic_call = next_call;

Are you sure this is correct? There are at least two things I can think of that might be wrong:

Quoted 7 lines

(1) Doing > or < comparison (as done in the next_call > cyclic_next_call line), that should never be done, because the logic around timestamps is that we do allow wrapping, assuming the two timestamps being compared never represent absolute times that differ by more than what can be represented in the signed version of the timestamp type - that's why the time_after*/time_before* family do a subtraction and a signed compare to 0.

Ah yes, agree

Quoted 6 lines

(2) What happens if we have a single cyclic consumer registered, with a rather long period (say 1s), hence a long time till its next_call, then another cyclic consumer is registered which should be called every 50ms. Since I don't see anything added to cyclic_register(), it seems that the new consumer would be ignored for almost a second?

You are right, i forgot this case

Quoted 8 lines

I'm also not really sure what the "move the first expiring timer to the head" logic is supposed to achieve. Either way, if any timer has expired, we'll run through the whole list, so while the first expiring timer would then be handled first on the next cyclic_run(), that doesn't mean we'll save any runtime. And this exercise seems to be about doing as little as possible in cyclic_run(), so can you explain what the pointer juggling does?

As suggested by Marek in v1, by putting the cyclic's callback with the nearest timestamp in head of list, we don't need to parse all the cyclic_list entries to find the next cyclic to run.

Quoted 43 lines

I assume that what makes cyclic_run() somewhat expensive isn't actually running through the cyclic_list(), but the get_timer_us() calls done for each entry. Could you try to see what something like this (entirely untested, but you get the idea) would do:

common/cyclic.c +5 -3
index ec952a01ee1..2616c4d5bc6 100644
@@ -67,26 +67,28 @@ static void cyclic_run(void)
 {
        struct cyclic_info *cyclic;
        struct hlist_node *tmp;
-       uint64_t now, cpu_time;
+       uint64_t now, after, cpu_time;
 
        /* Prevent recursion */
        if (gd->flags & GD_FLG_CYCLIC_RUNNING)
                return;
 
        gd->flags |= GD_FLG_CYCLIC_RUNNING;
+       now = get_timer_us(0);
        hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
                /*
                 * Check if this cyclic function needs to get called, e.g.
                 * do not call the cyclic func too often
                 */
-               now = get_timer_us(0);
                if (time_after_eq64(now, cyclic->next_call)) {
                        /* Call cyclic function and account it's cpu-time */
                        cyclic->next_call = now + cyclic->delay_us;
                        cyclic->func(cyclic);
+                       after = get_timer_us(0);
                        cyclic->run_cnt++;
-                       cpu_time = get_timer_us(0) - now;
+                       cpu_time = after - now;
                        cyclic->cpu_time_us += cpu_time;
+                       now = after;
 
                        /* Check if cpu-time exceeds max allowed time */
                        if ((cpu_time > CONFIG_CYCLIC_MAX_CPU_TIME_US) &&
Quoted 4 lines

Because the time_after_eq64() test really doesn't take anywhere near a micro-second, nor does fetching the next element from the linked list, so using the same value for "now" until we actually do hit an expired timer should be ok.

I tested your patch proposal, and yes, what cost the most is get_timer_us() calls. I obtain the same gain using your patch proposal than with my buggy implementation ;-) which doesn't, moreover, cover all cases.

I will submit a v3 with you proposal.

Thanks
Patrice>

Quoted 6 lines

[I also want to move the computation of the new cyclic->next_call to after the callback, so that the callback can update its ->delay_us value if it needs to be called at another frequency, but that's a separate topic.]

Rasmus