From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 5150AA0550;
	Tue, 16 Feb 2021 11:40:43 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id CFDD31606DA;
	Tue, 16 Feb 2021 11:40:42 +0100 (CET)
Received: from mga09.intel.com (mga09.intel.com [134.134.136.24])
 by mails.dpdk.org (Postfix) with ESMTP id 394A440690
 for <dev@dpdk.org>; Tue, 16 Feb 2021 11:40:40 +0100 (CET)
IronPort-SDR: EIrh9YBudBXJ1Z0+QCleJ24Ue8cw+GQttD7GVIFprC0ciGWEvec+zoTRLSW7tasU/6Su94tQOD
 D8eLbNsLYOkA==
X-IronPort-AV: E=McAfee;i="6000,8403,9896"; a="182989300"
X-IronPort-AV: E=Sophos;i="5.81,183,1610438400"; d="scan'208";a="182989300"
Received: from orsmga008.jf.intel.com ([10.7.209.65])
 by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 16 Feb 2021 02:40:39 -0800
IronPort-SDR: qVF6zRvF6mCQBGA34t2DboG21XSxutI/zYsy4uYgXgk6uMnyIWaCeQlbPfiYWTkrenHOkHfI7r
 zK8n+feBYQAg==
X-IronPort-AV: E=Sophos;i="5.81,183,1610438400"; d="scan'208";a="399457886"
Received: from wardmich-mobl.ger.corp.intel.com (HELO [10.213.232.191])
 ([10.213.232.191])
 by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 16 Feb 2021 02:40:38 -0800
To: Bruce Richardson <bruce.richardson@intel.com>, dev@dpdk.org
Cc: Kevin Laatz <kevin.laatz@intel.com>
References: <20210216094415.28000-1-bruce.richardson@intel.com>
From: "Burakov, Anatoly" <anatoly.burakov@intel.com>
Message-ID: <e5ff396d-befa-2119-da19-32e1cfa5fbaf@intel.com>
Date: Tue, 16 Feb 2021 10:40:36 +0000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
 Thunderbird/68.12.0
MIME-Version: 1.0
In-Reply-To: <20210216094415.28000-1-bruce.richardson@intel.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Subject: Re: [dpdk-dev] [PATCH] usertools/dpdk-telemetry: print name of app
 when connected
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

On 16-Feb-21 9:44 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>
> ---
>   usertools/dpdk-telemetry.py | 5 +++++
>   1 file changed, 5 insertions(+)
> 
> diff --git a/usertools/dpdk-telemetry.py b/usertools/dpdk-telemetry.py
> index 181859658f..82b91f346f 100755
> --- a/usertools/dpdk-telemetry.py
> +++ b/usertools/dpdk-telemetry.py
> @@ -45,6 +45,11 @@ def handle_socket(path):
>           return
>       json_reply = read_socket(sock, 1024)
>       output_buf_len = json_reply["max_output_len"]
> +    pid = json_reply["pid"]
> +    if os.path.exists('/proc/' + str(pid) + '/cmdline'):
> +        with open('/proc/' + str(pid) + '/cmdline') as f:

First of all, this is better done using os.path.join:

path = os.path.join('/proc', str(pid), 'cmdline')
if os.path.exists(path):
     with open(path) as f:
         ...

More importantly this isn't terribly Pythonic as it's not over-using 
exceptions :) IMO a better way would be:

try:
     with open(path) as f:
         ...
except IOError as e:
     # ignore if doesn't exist
     if e.errno != errno.ENOENT:
         raise

> +            argv0 = f.read(1024).split('\0')[0]
> +            print("Connected to application: '" + os.path.basename(argv0) + "'")

Also, formatting is better than concatenation, e.g. at least:

bname = os.path.basename(argv0)
print("Connected to application: '{}'".format(bname))

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


-- 
Thanks,
Anatoly