[v2] usertools/dpdk-telemetry: print name of app when connected

Message ID 20210216113921.28725-1-bruce.richardson@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] usertools/dpdk-telemetry: print name of app when connected |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/travis-robot fail travis build: failed
ci/iol-broadcom-Functional success Functional Testing PASS
ci/github-robot success github build: passed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS

Commit Message

Bruce Richardson Feb. 16, 2021, 11:39 a.m. UTC
  When the dpdk-telemetry client connects to a DPDK instance, we can use the
PID provided in the initial connection message to query from /proc the name
of the process we are connected to, and display that to the user. We use
the "cmdline" procfs entry for the query since that is available on both
Linux and FreeBSD (assuming procfs is mounted on the BSD instance).

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>

---
V2:
  Updated with stylistic feedback from Anatoly, and split name query into a
  separate function.
---
 usertools/dpdk-telemetry.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

--
2.27.0
  

Comments

Burakov, Anatoly Feb. 16, 2021, 12:19 p.m. UTC | #1
On 16-Feb-21 11:39 AM, Bruce Richardson wrote:
> When the dpdk-telemetry client connects to a DPDK instance, we can use the
> PID provided in the initial connection message to query from /proc the name
> of the process we are connected to, and display that to the user. We use
> the "cmdline" procfs entry for the query since that is available on both
> Linux and FreeBSD (assuming procfs is mounted on the BSD instance).
> 
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> 
> ---
> V2:
>    Updated with stylistic feedback from Anatoly, and split name query into a
>    separate function.
> ---
>   usertools/dpdk-telemetry.py | 18 ++++++++++++++++++
>   1 file changed, 18 insertions(+)
> 
> diff --git a/usertools/dpdk-telemetry.py b/usertools/dpdk-telemetry.py
> index 181859658..b2acd92ba 100755
> --- a/usertools/dpdk-telemetry.py
> +++ b/usertools/dpdk-telemetry.py
> @@ -11,6 +11,7 @@
>   import os
>   import glob
>   import json
> +import errno
>   import readline
> 
>   # global vars
> @@ -32,6 +33,20 @@ def read_socket(sock, buf_len, echo=True):
>       return ret
> 
> 
> +def get_app_name(pid):
> +    """ return the app name for a given pid, for printing """
> +    proc_cmdline = os.path.join('/proc', str(pid), 'cmdline')
> +    try:
> +        with open(proc_cmdline) as f:
> +            argv0 = f.read(1024).split('\0')[0]
> +            return os.path.basename(argv0)
> +    except IOError as e:
> +        # ignore file not found errors
> +        if e.errno != errno.ENOENT:
> +            raise
> +    return None
> +
> +
>   def handle_socket(path):
>       """ Connect to socket and handle user input """
>       sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
> @@ -45,6 +60,9 @@ def handle_socket(path):
>           return
>       json_reply = read_socket(sock, 1024)
>       output_buf_len = json_reply["max_output_len"]
> +    app_name = get_app_name(json_reply["pid"])
> +    if app_name:
> +        print('Connected to application: "%s"' % app_name)
> 
>       # get list of commands for readline completion
>       sock.send("/".encode())
> --
> 2.27.0
> 

Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
  
Kevin Laatz Feb. 17, 2021, 1:57 p.m. UTC | #2
On 16/02/2021 11:39, Bruce Richardson wrote:
> When the dpdk-telemetry client connects to a DPDK instance, we can use the
> PID provided in the initial connection message to query from /proc the name
> of the process we are connected to, and display that to the user. We use
> the "cmdline" procfs entry for the query since that is available on both
> Linux and FreeBSD (assuming procfs is mounted on the BSD instance).
>
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
>
Acked-by: Kevin Laatz <kevin.laatz@intel.com>
  
Thomas Monjalon March 25, 2021, 5 p.m. UTC | #3
17/02/2021 14:57, Kevin Laatz:
> On 16/02/2021 11:39, Bruce Richardson wrote:
> > When the dpdk-telemetry client connects to a DPDK instance, we can use the
> > PID provided in the initial connection message to query from /proc the name
> > of the process we are connected to, and display that to the user. We use
> > the "cmdline" procfs entry for the query since that is available on both
> > Linux and FreeBSD (assuming procfs is mounted on the BSD instance).
> >
> > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
> Acked-by: Kevin Laatz <kevin.laatz@intel.com>

Applied, thanks
  

Patch

diff --git a/usertools/dpdk-telemetry.py b/usertools/dpdk-telemetry.py
index 181859658..b2acd92ba 100755
--- a/usertools/dpdk-telemetry.py
+++ b/usertools/dpdk-telemetry.py
@@ -11,6 +11,7 @@ 
 import os
 import glob
 import json
+import errno
 import readline

 # global vars
@@ -32,6 +33,20 @@  def read_socket(sock, buf_len, echo=True):
     return ret


+def get_app_name(pid):
+    """ return the app name for a given pid, for printing """
+    proc_cmdline = os.path.join('/proc', str(pid), 'cmdline')
+    try:
+        with open(proc_cmdline) as f:
+            argv0 = f.read(1024).split('\0')[0]
+            return os.path.basename(argv0)
+    except IOError as e:
+        # ignore file not found errors
+        if e.errno != errno.ENOENT:
+            raise
+    return None
+
+
 def handle_socket(path):
     """ Connect to socket and handle user input """
     sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
@@ -45,6 +60,9 @@  def handle_socket(path):
         return
     json_reply = read_socket(sock, 1024)
     output_buf_len = json_reply["max_output_len"]
+    app_name = get_app_name(json_reply["pid"])
+    if app_name:
+        print('Connected to application: "%s"' % app_name)

     # get list of commands for readline completion
     sock.send("/".encode())