Skip to contentSkip to conversation

console: usb: kbd: Limit poll frequency to improve performance

PATCH In review Thomas Watson 3 participants 8 messages Feb 10, 2022 – Feb 11, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 console: usb: kbd: Limit poll frequency to improve performance

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

 common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
common/usb_kbd.c +26 -5
index afad260d3d..352d86fb2e 100644
@@ -17,6 +17,9 @@
 #include <stdio_dev.h>
 #include <watchdog.h>
 #include <asm/byteorder.h>
+#ifdef CONFIG_SANDBOX
+#include <asm/state.h>
+#endif
 
 #include <usb.h>
 
@@ -118,7 +121,7 @@ struct usb_kbd_pdata {
 extern int __maybe_unused net_busy_flag;
 
 /* The period of time between two calls of usb_kbd_testc(). */
-static unsigned long __maybe_unused kbd_testc_tms;
+static unsigned long kbd_testc_tms;
 
 /* Puts character in the queue and sets up the in and out pointer. */
 static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
@@ -394,21 +397,39 @@ static int usb_kbd_testc(struct stdio_dev *sdev)
 	struct usb_device *usb_kbd_dev;
 	struct usb_kbd_pdata *data;
 
+	/*
+	 * Polling the keyboard for an event can take dozens of milliseconds.
+	 * Add a delay between polls to avoid blocking activity which polls
+	 * rapidly, like the UEFI console timer.
+	 */
+	unsigned long poll_delay = CONFIG_SYS_HZ / 50;
+
 #ifdef CONFIG_CMD_NET
 	/*
 	 * If net_busy_flag is 1, NET transfer is running,
 	 * then we check key-pressed every second (first check may be
 	 * less than 1 second) to improve TFTP booting performance.
 	 */
-	if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
-		return 0;
-	kbd_testc_tms = get_timer(0);
+	if (net_busy_flag)
+		poll_delay = CONFIG_SYS_HZ;
+#endif
+
+#ifdef CONFIG_SANDBOX
+	/*
+	 * Skip delaying polls if a test requests it.
+	 */
+	if (state_get_skip_delays())
+		poll_delay = 0;
 #endif
+
 	dev = stdio_get_by_name(sdev->name);
 	usb_kbd_dev = (struct usb_device *)dev->priv;
 	data = usb_kbd_dev->privptr;
 
-	usb_kbd_poll_for_event(usb_kbd_dev);
+	if (get_timer(kbd_testc_tms) >= poll_delay) {
+		usb_kbd_poll_for_event(usb_kbd_dev);
+		kbd_testc_tms = get_timer(0);
+	}
 
 	return !(data->usb_in_pointer == data->usb_out_pointer);
 }
Signature · 1 line
2.31.1
#1 lore raw
Quoted 69 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

  common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)
common/usb_kbd.c +12 -4
index afad260d3d..352d86fb2e 100644
@@ -17,6 +17,9 @@
  #include <stdio_dev.h>
  #include <watchdog.h>
  #include <asm/byteorder.h>
+#ifdef CONFIG_SANDBOX
+#include <asm/state.h>
+#endif
 
  #include <usb.h>
 
@@ -118,7 +121,7 @@ struct usb_kbd_pdata {
  extern int __maybe_unused net_busy_flag;
 
  /* The period of time between two calls of usb_kbd_testc(). */
-static unsigned long __maybe_unused kbd_testc_tms;
+static unsigned long kbd_testc_tms;
 
  /* Puts character in the queue and sets up the in and out pointer. */
  static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
@@ -394,21 +397,39 @@ static int usb_kbd_testc(struct stdio_dev *sdev)
  	struct usb_device *usb_kbd_dev;
  	struct usb_kbd_pdata *data;
 
+	/*
+	 * Polling the keyboard for an event can take dozens of milliseconds.
+	 * Add a delay between polls to avoid blocking activity which polls
+	 * rapidly, like the UEFI console timer.
+	 */
+	unsigned long poll_delay = CONFIG_SYS_HZ / 50;
+
  #ifdef CONFIG_CMD_NET
  	/*
  	 * If net_busy_flag is 1, NET transfer is running,
  	 * then we check key-pressed every second (first check may be
  	 * less than 1 second) to improve TFTP booting performance.
  	 */
-	if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
-		return 0;
-	kbd_testc_tms = get_timer(0);
+	if (net_busy_flag)

common/usb_kbd.c static int usb_kbd_testc(struct stdio_dev *sdev) · line 423

Quoted 10 lines
common/usb_kbd.c +9 -0
+		poll_delay = CONFIG_SYS_HZ;
+#endif
+
+#ifdef CONFIG_SANDBOX
+	/*
+	 * Skip delaying polls if a test requests it.
+	 */
+	if (state_get_skip_delays())
+		poll_delay = 0;
  #endif

Is this architecture-specific ifdef really needed in common code?

#2 lore raw
Quoted 67 lines · Marek Vasut
Quoted 63 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.
The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.
To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

 common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
common/usb_kbd.c +3 -0
index afad260d3d..352d86fb2e 100644
@@ -17,6 +17,9 @@
 #include <stdio_dev.h>
 #include <watchdog.h>
 #include <asm/byteorder.h>
+#ifdef CONFIG_SANDBOX
+#include <asm/state.h>
+#endif
   #include <usb.h>
 @@ -118,7 +121,7 @@ struct usb_kbd_pdata {
 extern int __maybe_unused net_busy_flag;
   /* The period of time between two calls of usb_kbd_testc(). */
common/usb_kbd.c +12 -4
-static unsigned long __maybe_unused kbd_testc_tms;
+static unsigned long kbd_testc_tms;
   /* Puts character in the queue and sets up the in and out pointer. */
 static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
@@ -394,21 +397,39 @@ static int usb_kbd_testc(struct stdio_dev *sdev)
 	struct usb_device *usb_kbd_dev;
 	struct usb_kbd_pdata *data;
 +	/*
+	 * Polling the keyboard for an event can take dozens of milliseconds.
+	 * Add a delay between polls to avoid blocking activity which polls
+	 * rapidly, like the UEFI console timer.
+	 */
+	unsigned long poll_delay = CONFIG_SYS_HZ / 50;
+
 #ifdef CONFIG_CMD_NET
 	/*
 	 * If net_busy_flag is 1, NET transfer is running,
 	 * then we check key-pressed every second (first check may be
 	 * less than 1 second) to improve TFTP booting performance.
 	 */
-	if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
-		return 0;
-	kbd_testc_tms = get_timer(0);
+	if (net_busy_flag)
+		poll_delay = CONFIG_SYS_HZ;
+#endif
+
+#ifdef CONFIG_SANDBOX
Quoted 8 lines

common/usb_kbd.c static int usb_kbd_testc(struct stdio_dev *sdev) · line 423

Quoted 6 lines
common/usb_kbd.c +5 -0
+	/*
+	 * Skip delaying polls if a test requests it.
+	 */
+	if (state_get_skip_delays())
+		poll_delay = 0;
 #endif

Is this architecture-specific ifdef really needed in common code?

I modeled this check off a similar one around line 184 of common/usb_hub.c which effectively sets some get_timer() based delay values to 0 like I propose here. Checking this pre-existing flag here seems like a less invasive option to me than modifying the core timer code to also check the flag.

Modifying the test to add a delay would also theoretically be possible, but I am not sure how to accomplish that and it would make the test take longer.

#3 lore raw
Quoted 76 lines · Thomas Watson
Quoted 67 lines · Marek Vasut
Quoted 63 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.
The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.
To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

  common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)
common/usb_kbd.c +3 -0
index afad260d3d..352d86fb2e 100644
@@ -17,6 +17,9 @@
  #include <stdio_dev.h>
  #include <watchdog.h>
  #include <asm/byteorder.h>
+#ifdef CONFIG_SANDBOX
+#include <asm/state.h>
+#endif
    #include <usb.h>
  @@ -118,7 +121,7 @@ struct usb_kbd_pdata {
  extern int __maybe_unused net_busy_flag;
    /* The period of time between two calls of usb_kbd_testc(). */
common/usb_kbd.c +12 -4
-static unsigned long __maybe_unused kbd_testc_tms;
+static unsigned long kbd_testc_tms;
    /* Puts character in the queue and sets up the in and out pointer. */
  static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
@@ -394,21 +397,39 @@ static int usb_kbd_testc(struct stdio_dev *sdev)
  	struct usb_device *usb_kbd_dev;
  	struct usb_kbd_pdata *data;
  +	/*
+	 * Polling the keyboard for an event can take dozens of milliseconds.
+	 * Add a delay between polls to avoid blocking activity which polls
+	 * rapidly, like the UEFI console timer.
+	 */
+	unsigned long poll_delay = CONFIG_SYS_HZ / 50;
+
  #ifdef CONFIG_CMD_NET
  	/*
  	 * If net_busy_flag is 1, NET transfer is running,
  	 * then we check key-pressed every second (first check may be
  	 * less than 1 second) to improve TFTP booting performance.
  	 */
-	if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
-		return 0;
-	kbd_testc_tms = get_timer(0);
+	if (net_busy_flag)
+		poll_delay = CONFIG_SYS_HZ;
+#endif
+
+#ifdef CONFIG_SANDBOX
Quoted 8 lines

common/usb_kbd.c static int usb_kbd_testc(struct stdio_dev *sdev) · line 423

Quoted 6 lines
common/usb_kbd.c +5 -0
+	/*
+	 * Skip delaying polls if a test requests it.
+	 */
+	if (state_get_skip_delays())
+		poll_delay = 0;
  #endif

Is this architecture-specific ifdef really needed in common code?

Quoted 7 lines

I modeled this check off a similar one around line 184 of common/usb_hub.c which effectively sets some get_timer() based delay values to 0 like I propose here. Checking this pre-existing flag here seems like a less invasive option to me than modifying the core timer code to also check the flag.

Modifying the test to add a delay would also theoretically be possible, but I am not sure how to accomplish that and it would make the test take longer.

I CCed Simon, maybe he can help out.

#4 lore raw
In reply to Marek Vasut

Hi Marek,

Quoted 19 lines · Marek Vasut
Quoted 17 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

Quoted 7 lines
Quoted 7 lines

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

  common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)

We don't want to delay the CI tests which take enough time as it is. So time needs to be faked.

It could be an if() though, rather than #if, perhaps?

Regards,
Simon

#5 lore raw
Quoted 27 lines · Simon Glass

Hi Marek,

Quoted 24 lines · Marek Vasut
Quoted 22 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
   common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
   1 file changed, 26 insertions(+), 5 deletions(-)

We don't want to delay the CI tests which take enough time as it is. So time needs to be faked.

It could be an if() though, rather than #if, perhaps?

Since there are multiple instances of the ifdef already , subsequent patch to clean them up would be fine.

#6 lore raw
Quoted 30 lines · Marek Vasut
Quoted 26 lines · Simon Glass

Hi Marek,

Quoted 24 lines · Marek Vasut
Quoted 22 lines · Thomas Watson

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
  common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)

We don't want to delay the CI tests which take enough time as it is. So time needs to be faked.
It could be an if() though, rather than #if, perhaps?

Since there are multiple instances of the ifdef already , subsequent patch to clean them up would be fine.

That function state_get_skip_delays() and that state.h header file in general is only defined in the sandbox architecture, so I don't think it's possible to remove the #ifdef unless that is changed.

#7 lore raw

()Hi Thomas,

Quoted 42 lines · Thomas Watson

On Feb 10, 2022, at 5:04 PM, Marek Vasut <[email protected]> wrote:

On 2/11/22 00:02, Simon Glass wrote:

Hi Marek,
On Thu, 10 Feb 2022 at 14:42, Marek Vasut <[email protected]> wrote:

On 2/10/22 22:13, Thomas Watson wrote:

Using the XHCI driver, the function usb_kbd_poll_for_event takes 30-40ms to run. The exact time is dependent on the polling interval the keyboard requests in its descriptor, and likely cannot be significantly reduced without major rework to the XHCI driver.

The U-Boot EFI console service sets a timer to poll the keyboard every 5 microseconds, and this timer is checked every time a block is read off disk. The net effect is that, on my system, loading a ~40MiB kernel and initrd takes about 62 seconds with a slower keyboard and 53 seconds with a faster one, with the vast majority of the time spent polling the keyboard.

To solve this problem, this patch adds a 20ms delay between consecutive calls to usb_kbd_poll_for_event. This is sufficient to reduce the total loading time to under half a second for both keyboards, and does not impact the perceived keystroke latency.

---
This revision fixes a sandbox test failure by honoring the test's request to skip delays.

  common/usb_kbd.c | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)

We don't want to delay the CI tests which take enough time as it is. So time needs to be faked.
It could be an if() though, rather than #if, perhaps?

Quoted 6 lines

Since there are multiple instances of the ifdef already , subsequent patch to clean them up would be fine.

That function state_get_skip_delays() and that state.h header file in general is only defined in the sandbox architecture, so I don't think it's possible to remove the #ifdef unless that is changed.

We can add it to a generic header, e.g. timer_skip_delays(), with a null implementation for other boards and a call to state_get_skip_delays() for sandbox. We have done that in a few other cases. I agree we should avoid arch-specific code.

Regards,
Simon