[v1] dts: add testpmd port information caching

Message ID 20240823074137.13989-1-juraj.linkes@pantheon.tech (mailing list archive)
State Accepted, archived
Delegated to: Juraj Linkeš
Headers
Series [v1] dts: add testpmd port information caching |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

Juraj Linkeš Aug. 23, 2024, 7:41 a.m. UTC
When using port information multiple times in a testpmd shell instance
lifespan, it's desirable to not get the information each time, so
caching is added. In case the information changes, there's a way to
force the update with either TestPmdShell.show_port_info() or
TestPmdShell.show_port_info_all().

Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/remote_session/testpmd_shell.py | 30 +++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
  

Comments

Luca Vizzarro Sept. 2, 2024, 1:45 p.m. UTC | #1
Good one.

Reviewed-by: Luca Vizzarro <luca.vizzarro@arm.com>
  
Jeremy Spewock Sept. 5, 2024, 4:09 p.m. UTC | #2
Seems like a good change to me!

Reviewed-by: Jeremy Spewock <jspewock@iol.unh.edu>
  
Juraj Linkeš Sept. 9, 2024, 3:51 p.m. UTC | #3
Applied to next-dts, thanks.

On 23. 8. 2024 9:41, Juraj Linkeš wrote:
> When using port information multiple times in a testpmd shell instance
> lifespan, it's desirable to not get the information each time, so
> caching is added. In case the information changes, there's a way to
> force the update with either TestPmdShell.show_port_info() or
> TestPmdShell.show_port_info_all().
> 
> Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
> ---
>   dts/framework/remote_session/testpmd_shell.py | 30 +++++++++++++++++--
>   1 file changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py
> index 43e9f56517..0a5cb385c9 100644
> --- a/dts/framework/remote_session/testpmd_shell.py
> +++ b/dts/framework/remote_session/testpmd_shell.py
> @@ -586,6 +586,7 @@ class TestPmdShell(DPDKShell):
>       """
>   
>       _app_params: TestPmdParams
> +    _ports: list[TestPmdPort] | None
>   
>       #: The path to the testpmd executable.
>       path: ClassVar[PurePath] = PurePath("app", "dpdk-testpmd")
> @@ -618,6 +619,21 @@ def __init__(
>               TestPmdParams(**app_params),
>               name,
>           )
> +        self._ports = None
> +
> +    @property
> +    def ports(self) -> list[TestPmdPort]:
> +        """The ports of the instance.
> +
> +        This caches the ports returned by :meth:`show_port_info_all`.
> +        To force an update of port information, execute :meth:`show_port_info_all` or
> +        :meth:`show_port_info`.
> +
> +        Returns: The list of known testpmd ports.
> +        """
> +        if self._ports is None:
> +            return self.show_port_info_all()
> +        return self._ports
>   
>       def start(self, verify: bool = True) -> None:
>           """Start packet forwarding with the current configuration.
> @@ -747,7 +763,8 @@ def show_port_info_all(self) -> list[TestPmdPort]:
>           # executed on a pseudo-terminal created by paramiko on the remote node, lines end with CRLF.
>           # Therefore we also need to take the carriage return into account.
>           iter = re.finditer(r"\*{21}.*?[\r\n]{4}", output + "\r\n", re.S)
> -        return [TestPmdPort.parse(block.group(0)) for block in iter]
> +        self._ports = [TestPmdPort.parse(block.group(0)) for block in iter]
> +        return self._ports
>   
>       def show_port_info(self, port_id: int) -> TestPmdPort:
>           """Returns the given port information.
> @@ -765,7 +782,16 @@ def show_port_info(self, port_id: int) -> TestPmdPort:
>           if output.startswith("Invalid port"):
>               raise InteractiveCommandExecutionError("invalid port given")
>   
> -        return TestPmdPort.parse(output)
> +        port = TestPmdPort.parse(output)
> +        self._update_port(port)
> +        return port
> +
> +    def _update_port(self, port: TestPmdPort) -> None:
> +        if self._ports:
> +            self._ports = [
> +                existing_port if port.id != existing_port.id else port
> +                for existing_port in self._ports
> +            ]
>   
>       def show_port_stats_all(self) -> list[TestPmdPortStats]:
>           """Returns the statistics of all the ports.
  

Patch

diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py
index 43e9f56517..0a5cb385c9 100644
--- a/dts/framework/remote_session/testpmd_shell.py
+++ b/dts/framework/remote_session/testpmd_shell.py
@@ -586,6 +586,7 @@  class TestPmdShell(DPDKShell):
     """
 
     _app_params: TestPmdParams
+    _ports: list[TestPmdPort] | None
 
     #: The path to the testpmd executable.
     path: ClassVar[PurePath] = PurePath("app", "dpdk-testpmd")
@@ -618,6 +619,21 @@  def __init__(
             TestPmdParams(**app_params),
             name,
         )
+        self._ports = None
+
+    @property
+    def ports(self) -> list[TestPmdPort]:
+        """The ports of the instance.
+
+        This caches the ports returned by :meth:`show_port_info_all`.
+        To force an update of port information, execute :meth:`show_port_info_all` or
+        :meth:`show_port_info`.
+
+        Returns: The list of known testpmd ports.
+        """
+        if self._ports is None:
+            return self.show_port_info_all()
+        return self._ports
 
     def start(self, verify: bool = True) -> None:
         """Start packet forwarding with the current configuration.
@@ -747,7 +763,8 @@  def show_port_info_all(self) -> list[TestPmdPort]:
         # executed on a pseudo-terminal created by paramiko on the remote node, lines end with CRLF.
         # Therefore we also need to take the carriage return into account.
         iter = re.finditer(r"\*{21}.*?[\r\n]{4}", output + "\r\n", re.S)
-        return [TestPmdPort.parse(block.group(0)) for block in iter]
+        self._ports = [TestPmdPort.parse(block.group(0)) for block in iter]
+        return self._ports
 
     def show_port_info(self, port_id: int) -> TestPmdPort:
         """Returns the given port information.
@@ -765,7 +782,16 @@  def show_port_info(self, port_id: int) -> TestPmdPort:
         if output.startswith("Invalid port"):
             raise InteractiveCommandExecutionError("invalid port given")
 
-        return TestPmdPort.parse(output)
+        port = TestPmdPort.parse(output)
+        self._update_port(port)
+        return port
+
+    def _update_port(self, port: TestPmdPort) -> None:
+        if self._ports:
+            self._ports = [
+                existing_port if port.id != existing_port.id else port
+                for existing_port in self._ports
+            ]
 
     def show_port_stats_all(self) -> list[TestPmdPortStats]:
         """Returns the statistics of all the ports.