[v3] usertools: telemetry pretty print in interactive mode

Message ID 20221017074102.55509-1-fengchengwen@huawei.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series [v3] usertools: telemetry pretty print in interactive mode |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

fengchengwen Oct. 17, 2022, 7:41 a.m. UTC
  Currently, the dpdk-telemetry.py show json in raw format under
interactive mode, which is not good for human reading.

E.g. The command '/ethdev/xstats,0' will output:
{"/ethdev/xstats": {"rx_good_packets": 0, "tx_good_packets": 0,
"rx_good_bytes": 0, "tx_good_bytes": 0, "rx_missed_errors": 0,
"rx_errors": 0, "tx_errors": 0, "rx_mbuf_allocation_errors": 0,
"rx_q0_packets": 0,...}}

This patch supports json pretty print by adding extra indent=2
parameter under interactive mode, so the same command will output:
{
  "/ethdev/xstats": {
    "rx_good_packets": 0,
    "tx_good_packets": 0,
    "rx_good_bytes": 0,
    "tx_good_bytes": 0,
    "rx_missed_errors": 0,
    "rx_errors": 0,
    "rx_mbuf_allocation_errors": 0,
    "rx_q0_packets": 0,
    ...
  }
}

Note: the non-interactive mode is made machine-readable and remains the
original way (it means don't use indent to pretty print).

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: David Marchand <david.marchand@redhat.com>
Acked-by: Ciara Power <ciara.power@intel.com>

---
v3: indent modify to 2 and make sure indent under interactive mode
according comments.
v2: fix typo of output.

---
 usertools/dpdk-telemetry.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
  

Comments

Bruce Richardson Oct. 17, 2022, 9:15 a.m. UTC | #1
On Mon, Oct 17, 2022 at 07:41:02AM +0000, Chengwen Feng wrote:
> Currently, the dpdk-telemetry.py show json in raw format under
> interactive mode, which is not good for human reading.
> 
> E.g. The command '/ethdev/xstats,0' will output:
> {"/ethdev/xstats": {"rx_good_packets": 0, "tx_good_packets": 0,
> "rx_good_bytes": 0, "tx_good_bytes": 0, "rx_missed_errors": 0,
> "rx_errors": 0, "tx_errors": 0, "rx_mbuf_allocation_errors": 0,
> "rx_q0_packets": 0,...}}
> 
> This patch supports json pretty print by adding extra indent=2
> parameter under interactive mode, so the same command will output:
> {
>   "/ethdev/xstats": {
>     "rx_good_packets": 0,
>     "tx_good_packets": 0,
>     "rx_good_bytes": 0,
>     "tx_good_bytes": 0,
>     "rx_missed_errors": 0,
>     "rx_errors": 0,
>     "rx_mbuf_allocation_errors": 0,
>     "rx_q0_packets": 0,
>     ...
>   }
> }
> 
> Note: the non-interactive mode is made machine-readable and remains the
> original way (it means don't use indent to pretty print).
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> Acked-by: David Marchand <david.marchand@redhat.com>
> Acked-by: Ciara Power <ciara.power@intel.com>
> 
Tested-by: Bruce Richardson <bruce.richardson@intel.com>
  
Thomas Monjalon Oct. 31, 2022, 3:16 p.m. UTC | #2
17/10/2022 11:15, Bruce Richardson:
> On Mon, Oct 17, 2022 at 07:41:02AM +0000, Chengwen Feng wrote:
> > Currently, the dpdk-telemetry.py show json in raw format under
> > interactive mode, which is not good for human reading.
> > 
> > E.g. The command '/ethdev/xstats,0' will output:
> > {"/ethdev/xstats": {"rx_good_packets": 0, "tx_good_packets": 0,
> > "rx_good_bytes": 0, "tx_good_bytes": 0, "rx_missed_errors": 0,
> > "rx_errors": 0, "tx_errors": 0, "rx_mbuf_allocation_errors": 0,
> > "rx_q0_packets": 0,...}}
> > 
> > This patch supports json pretty print by adding extra indent=2
> > parameter under interactive mode, so the same command will output:
> > {
> >   "/ethdev/xstats": {
> >     "rx_good_packets": 0,
> >     "tx_good_packets": 0,
> >     "rx_good_bytes": 0,
> >     "tx_good_bytes": 0,
> >     "rx_missed_errors": 0,
> >     "rx_errors": 0,
> >     "rx_mbuf_allocation_errors": 0,
> >     "rx_q0_packets": 0,
> >     ...
> >   }
> > }
> > 
> > Note: the non-interactive mode is made machine-readable and remains the
> > original way (it means don't use indent to pretty print).
> > 
> > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > Acked-by: David Marchand <david.marchand@redhat.com>
> > Acked-by: Ciara Power <ciara.power@intel.com>
> > 
> Tested-by: Bruce Richardson <bruce.richardson@intel.com>

Applied, thanks.
  

Patch

diff --git a/usertools/dpdk-telemetry.py b/usertools/dpdk-telemetry.py
index a81868a547..568f222a03 100755
--- a/usertools/dpdk-telemetry.py
+++ b/usertools/dpdk-telemetry.py
@@ -23,7 +23,7 @@ 
 CMDS = []
 
 
-def read_socket(sock, buf_len, echo=True):
+def read_socket(sock, buf_len, echo=True, pretty=False):
     """ Read data from socket and return it in JSON format """
     reply = sock.recv(buf_len).decode()
     try:
@@ -33,7 +33,8 @@  def read_socket(sock, buf_len, echo=True):
         sock.close()
         raise
     if echo:
-        print(json.dumps(ret))
+        indent = 2 if pretty else None
+        print(json.dumps(ret, indent=indent))
     return ret
 
 
@@ -127,7 +128,7 @@  def handle_socket(args, path):
         else:
             list_fp()
         return
-    json_reply = read_socket(sock, 1024, prompt)
+    json_reply = read_socket(sock, 1024, prompt, prompt)
     output_buf_len = json_reply["max_output_len"]
     app_name = get_app_name(json_reply["pid"])
     if app_name and prompt:
@@ -143,7 +144,7 @@  def handle_socket(args, path):
         while text != "quit":
             if text.startswith('/'):
                 sock.send(text.encode())
-                read_socket(sock, output_buf_len)
+                read_socket(sock, output_buf_len, pretty=prompt)
             text = input(prompt).strip()
     except EOFError:
         pass