From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 3CC71A0558; Tue, 16 Feb 2021 12:13:45 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 17791160721; Tue, 16 Feb 2021 12:13:45 +0100 (CET) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mails.dpdk.org (Postfix) with ESMTP id EBD7240690 for ; Tue, 16 Feb 2021 12:13:42 +0100 (CET) IronPort-SDR: l/0IGK3u39Xuf9s+L5gqrmMucnWpEEXJAjZCi6kO5PU/GV3FYOih5RuuDNrA+HD0dirogBlkxl hvGWgu3QhMqQ== X-IronPort-AV: E=McAfee;i="6000,8403,9896"; a="267706710" X-IronPort-AV: E=Sophos;i="5.81,183,1610438400"; d="scan'208";a="267706710" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Feb 2021 03:13:41 -0800 IronPort-SDR: LvLL/4ohtHb3QmUh/QeiCl5Hdth9c8r3HkTu7APzHrHdiEr2U32IfKEzrs1QdyfVTn42YBptYx cHe4e4/SjdzQ== X-IronPort-AV: E=Sophos;i="5.81,183,1610438400"; d="scan'208";a="399466280" Received: from aburakov-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 03:13:40 -0800 To: Bruce Richardson Cc: dev@dpdk.org, Kevin Laatz References: <20210216094415.28000-1-bruce.richardson@intel.com> <20210216110223.GC136@bricha3-MOBL.ger.corp.intel.com> From: "Burakov, Anatoly" Message-ID: Date: Tue, 16 Feb 2021 11:13:38 +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: <20210216110223.GC136@bricha3-MOBL.ger.corp.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On 16-Feb-21 11:02 AM, Bruce Richardson wrote: > On Tue, Feb 16, 2021 at 10:40:36AM +0000, Burakov, Anatoly wrote: >> 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 >>> --- >>> 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: >> ... > > Ok, I forgot that os.path.join can take > 2 parameters. > >> >> 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 >> > > Yes, I was thinking that I just wanted any exceptions to be raised, but you > right that I should just ignore the not-found one and skip the printout. 'raise' will raise the exception up the stack, so you'll still get your exceptions that way - you'll just skip the one that says the file doesn't exist. plus, it also has a benefit of avoiding TOCTOU race :) > >>> + 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)) >> > And f-strings are best of all, but we need python 3.6 for those. :-) I > consider use of format vs concat a matter of taste, but I'll update as you > suggest. > > Thanks for the review. > > /Bruce > > PS: Python 3.5 has had its final release late last year: > https://www.python.org/downloads/release/python-3510/ > We should soon consider updating our minimum required version to Python > 3.6, allowing us to use f-strings in our python code rather than > concatenation or explicit format calls. > I seem to have a lot of difficulty remembering syntax for f-strings :P -- Thanks, Anatoly