mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems
@ 2024-05-02 15:14 Ahmad Fatoum
  2024-05-02 15:14 ` [PATCH 2/2] poller: report pollers taking more than 20 milliseconds Ahmad Fatoum
  2024-05-03  6:53 ` [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-05-02 15:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

ktime_devns is an inline function for constant divisors. For
non-constant divisors, it's only inline for 64-bit systems and calls to
an out-of-line __ktime_divns function otherwise.

Implement the out-of-line function, so it's use of the function in
common code is portable.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 lib/math/div64.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/lib/math/div64.c b/lib/math/div64.c
index 386497592b0b..3d646c487b3d 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -21,6 +21,7 @@
 #include <linux/bitops.h>
 #include <module.h>
 #include <linux/kernel.h>
+#include <linux/ktime.h>
 #include <linux/math64.h>
 #include <linux/log2.h>
 
@@ -233,3 +234,32 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
 	return res + div64_u64(a * b, c);
 }
 #endif
+
+/*
+ * Functions for the union type storage format of ktime_t which are
+ * too large for inlining:
+ */
+#if BITS_PER_LONG < 64
+/*
+ * Divide a ktime value by a nanosecond value
+ */
+s64 __ktime_divns(const ktime_t kt, s64 div)
+{
+	int sft = 0;
+	s64 dclc;
+	u64 tmp;
+
+	dclc = ktime_to_ns(kt);
+	tmp = dclc < 0 ? -dclc : dclc;
+
+	/* Make sure the divisor is less than 2^32: */
+	while (div >> 32) {
+		sft++;
+		div >>= 1;
+	}
+	tmp >>= sft;
+	do_div(tmp, (u32) div);
+	return dclc < 0 ? -tmp : tmp;
+}
+EXPORT_SYMBOL_GPL(__ktime_divns);
+#endif /* BITS_PER_LONG >= 64 */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] poller: report pollers taking more than 20 milliseconds
  2024-05-02 15:14 [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Ahmad Fatoum
@ 2024-05-02 15:14 ` Ahmad Fatoum
  2024-05-03  6:53 ` [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-05-02 15:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Pollers are meant to be running for a short duration. A poller that runs
longer than 20 milliseconds probably deserves a closer look.

Let's print a one time warning in this case and have the poller command
output report how many times the condition occurred.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/poller.c  | 35 ++++++++++++++++++++++++++++++++---
 include/poller.h |  3 ++-
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/common/poller.c b/common/poller.c
index 0409d3cf8111..77d93ae8ccdf 100644
--- a/common/poller.c
+++ b/common/poller.c
@@ -3,6 +3,8 @@
  * Copyright (C) 2010 Marc Kleine-Budde <mkl@pengutronix.de>
  */
 
+#define pr_fmt(fmt) "poller: " fmt
+
 #include <common.h>
 #include <driver.h>
 #include <malloc.h>
@@ -10,6 +12,13 @@
 #include <param.h>
 #include <poller.h>
 #include <clock.h>
+#include <linux/ktime.h>
+
+/*
+ * Pollers are meant to poll and quickly execute actions.
+ * Exceeding the maximum runtime below triggers a one-time warning.
+ */
+#define POLLER_MAX_RUNTIME_MS	20
 
 static LIST_HEAD(poller_list);
 static int __poller_active;
@@ -116,9 +125,23 @@ void poller_call(void)
 
 	__poller_active = 1;
 
-	list_for_each_entry_safe(poller, tmp, &poller_list, list)
+	list_for_each_entry_safe(poller, tmp, &poller_list, list) {
+		ktime_t start = ktime_get();
+		s64 duration_ms;
+
 		poller->func(poller);
 
+		duration_ms = ktime_ms_delta(ktime_get(), start);
+		if (duration_ms > POLLER_MAX_RUNTIME_MS) {
+			if (!poller->overtime)
+				pr_warn("'%s' took unexpectedly long: %llums\n",
+					poller->name, duration_ms);
+
+			if (poller->overtime < U16_MAX)
+				poller->overtime++;
+		}
+	}
+
 	__poller_active = 0;
 }
 
@@ -155,8 +178,14 @@ static void poller_info(void)
 		return;
 	}
 
-	list_for_each_entry(poller, &poller_list, list)
-		printf("%s\n", poller->name);
+	list_for_each_entry(poller, &poller_list, list) {
+		printf("%s", poller->name);
+		if (poller->overtime)
+			printf(": overtime %s%u",
+			       poller->overtime == U16_MAX ? ">= " : "",
+			       poller->overtime);
+		printf("\n");
+	}
 }
 
 BAREBOX_CMD_HELP_START(poller)
diff --git a/include/poller.h b/include/poller.h
index 6e51a0613356..31db907ba5b8 100644
--- a/include/poller.h
+++ b/include/poller.h
@@ -11,7 +11,8 @@
 
 struct poller_struct {
 	void (*func)(struct poller_struct *poller);
-	int registered;
+	u16 registered:1;
+	u16 overtime;
 	struct list_head list;
 	char *name;
 };
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems
  2024-05-02 15:14 [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Ahmad Fatoum
  2024-05-02 15:14 ` [PATCH 2/2] poller: report pollers taking more than 20 milliseconds Ahmad Fatoum
@ 2024-05-03  6:53 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-05-03  6:53 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Thu, 02 May 2024 17:14:29 +0200, Ahmad Fatoum wrote:
> ktime_devns is an inline function for constant divisors. For
> non-constant divisors, it's only inline for 64-bit systems and calls to
> an out-of-line __ktime_divns function otherwise.
> 
> Implement the out-of-line function, so it's use of the function in
> common code is portable.
> 
> [...]

Applied, thanks!

[1/2] include: ktime: implement __ktime_divns for 32-bit systems
      https://git.pengutronix.de/cgit/barebox/commit/?id=d3f1bbc9ae16 (link may not be stable)
[2/2] poller: report pollers taking more than 20 milliseconds
      https://git.pengutronix.de/cgit/barebox/commit/?id=c5d2a98c51b2 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-03  6:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-02 15:14 [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Ahmad Fatoum
2024-05-02 15:14 ` [PATCH 2/2] poller: report pollers taking more than 20 milliseconds Ahmad Fatoum
2024-05-03  6:53 ` [PATCH 1/2] include: ktime: implement __ktime_divns for 32-bit systems Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox