Received: by 10.192.165.148 with SMTP id m20csp4787714imm; Tue, 8 May 2018 14:31:28 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoNxd+NpJYhAFkL7U/gQZ8d0TaqCGs91j1ljOcT+e6GOerMX09dcDbEqgTuJpA8mkWWy/UQ X-Received: by 2002:a17:902:7e05:: with SMTP id b5-v6mr42091173plm.230.1525815088662; Tue, 08 May 2018 14:31:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525815088; cv=none; d=google.com; s=arc-20160816; b=brOpU6zk0fPSE6/F54UZo/Ix96SS0jXMZxprwPF1pR53ymB2dAoS4pc8h7l2aQuchh PK7xoynwwyVbjZDBYm1hkiuvGDo+BI74+q3HRCoiektc0TIu8MQ6qlbm9BD7c3ywgIrn /JKRJT8LkBzXoQUqqyn1L+NshFAvnWsUszn/78FV6SPy/otQv982rZL4+KpRcOn291MB vWaQoC7NPdFNTAq+mdMzMXGwV3Sv0tRUXu8gQ7FINHicfh3cPbaF46Ddgy0a55mc3YNs sxwFJzHqGwC7O9T0IIyiAUz9a0RsjbU89W8Z9coThZeoeZ3J2WTl1vrHtU1KMa+EGhrB s5Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:feedback-id:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature :arc-authentication-results; bh=eQ9qZ/NXUdAkA5t3PqcCK6hXrLp7aKPieBtUGWGh3QY=; b=Aa8xH8RB0oG3vAULmMYnsfn1lM10zc0FEsCiBnV3PJMj3zTAOxruetq/unWw9PDUgz Rf9lXZoca/L6LnaEN9mij3RXFcmT+BSqd1Pa3/T8eIpYA2qejcNu3dAHPzYn0ml9Pi8P sdltAL8OwDkrEhzi7vUCyu3x7y6lq2I9lpuClLcAvpSA7AxjdJ42FfHqrJrp9AKvf1k/ 07mCKRyDfCH/kSTPZe5OYMWYf17ZLeERYLrRHt8KHxo1m1a/1UU2DgSIiW0rrdyzZ4kp NLltctTak5s3n6ObXnNyAz2RBfKm3wAUBHWlT81Jue9Xu0F+lyXyEOHrrNNXH+J0jIcz rFFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@jcline.org header.s=rdybrs3533vx7mghocfwl3vdwgpl2v5u header.b=VY9ny1Hu; dkim=pass header.i=@amazonses.com header.s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug header.b=e6J/Wn6g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f88si25472060pfk.107.2018.05.08.14.31.14; Tue, 08 May 2018 14:31:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@jcline.org header.s=rdybrs3533vx7mghocfwl3vdwgpl2v5u header.b=VY9ny1Hu; dkim=pass header.i=@amazonses.com header.s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug header.b=e6J/Wn6g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756088AbeEHVaN (ORCPT + 99 others); Tue, 8 May 2018 17:30:13 -0400 Received: from a8-53.smtp-out.amazonses.com ([54.240.8.53]:41244 "EHLO a8-53.smtp-out.amazonses.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756020AbeEHV1p (ORCPT ); Tue, 8 May 2018 17:27:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=rdybrs3533vx7mghocfwl3vdwgpl2v5u; d=jcline.org; t=1525814864; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; bh=z0Ki8BQtz+0aIgh9mRkPU83sM7EO6tztgwGdpgXI5qQ=; b=VY9ny1HulwB3ArOgxqU2hJc6HBDXtBG1Sg6+dUfgJFkC7ik19KGx0YU/X78yKWvc ztV7aOjjNr0bId4iE84azT0XT2rMlbFymnp0J4aTEIRGK5RqD1H3Ebo4cN/YF+a15D/ afeOnMSeU44gB3hxXiQGviIINc/NPYExn3iFFJVw= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1525814864; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:Feedback-ID; bh=z0Ki8BQtz+0aIgh9mRkPU83sM7EO6tztgwGdpgXI5qQ=; b=e6J/Wn6gyjJV02fmZvh47CEPdWMI5C+0aN3cUYVTtBhiNMdUOkbdMRNgdyKMyhb+ 7j7p0hUe97pu3C/H5NOToHeI1k45heyHVt3CffYnpGBFc9uJLwdcSNOR4bgAZTsDhVa FBM4ZJqwtQkVpMqyPSjFR93tfxJwz1AtMJ17LpQM= X-Virus-Scanned: amavisd-new at jcline.org From: Jeremy Cline To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Alexander Shishkin , Jiri Olsa , Namhyung Kim , "Herton R . Krzesinski" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Jeremy Cline Subject: [PATCH 1/8] perf tools: Generate a Python script compatible with Python 2 and 3 Date: Tue, 8 May 2018 21:27:43 +0000 Message-ID: <0100016341a7278a-d178c724-2b0f-49ca-be93-80a7d51aaa0d-000000@email.amazonses.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: X-SES-Outgoing: 2018.05.08-54.240.8.53 Feedback-ID: 1.us-east-1.z18Isoc/FaoPOvCyJyi1mnTt8STwoRuibXVNoUcvG6g=:AmazonSES Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When generating a Python script with "perf script -g python", produce one that is compatible with Python 2 and 3. The difference between the two generated scripts is: --- python2-perf-script.py 2018-05-08 15:35:00.865889705 -0400 +++ python3-perf-script.py 2018-05-08 15:34:49.019789564 -0400 @@ -7,6 +7,8 @@ # be retrieved using Python functions of the form common_*(context). # See the perf-script-python Documentation for the list of available functions. +from __future__ import print_function + import os import sys @@ -18,10 +20,10 @@ def trace_begin(): - print "in trace_begin" + print("in trace_begin") def trace_end(): - print "in trace_end" + print("in trace_end") def raw_syscalls__sys_enter(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, @@ -29,26 +31,26 @@ print_header(event_name, common_cpu, common_secs, common_nsecs, common_pid, common_comm) - print "id=%d, args=%s" % \ - (id, args) + print("id=%d, args=%s" % \ + (id, args)) - print 'Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}' + print('Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}') for node in common_callchain: if 'sym' in node: - print "\t[%x] %s" % (node['ip'], node['sym']['name']) + print("\t[%x] %s" % (node['ip'], node['sym']['name'])) else: - print " [%x]" % (node['ip']) + print(" [%x]" % (node['ip'])) - print "\n" + print() def trace_unhandled(event_name, context, event_fields_dict, perf_sample_dict): - print get_dict_as_string(event_fields_dict) - print 'Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}' + print(get_dict_as_string(event_fields_dict)) + print('Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}') def print_header(event_name, cpu, secs, nsecs, pid, comm): - print "%-20s %5u %05u.%09u %8u %-20s " % \ - (event_name, cpu, secs, nsecs, pid, comm), + print("%-20s %5u %05u.%09u %8u %-20s " % \ + (event_name, cpu, secs, nsecs, pid, comm), end="") def get_dict_as_string(a_dict, delimiter=' '): return delimiter.join(['%s=%s'%(k,str(v))for k,v in sorted(a_dict.items())]) Signed-off-by: Jeremy Cline --- .../scripting-engines/trace-event-python.c | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index 10dd5fce082b..01d5f255ef62 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -1393,6 +1393,7 @@ static int python_generate_script(struct pevent *pevent, const char *outfile) fprintf(ofp, "# See the perf-script-python Documentation for the list " "of available functions.\n\n"); + fprintf(ofp, "from __future__ import print_function\n\n"); fprintf(ofp, "import os\n"); fprintf(ofp, "import sys\n\n"); @@ -1402,10 +1403,10 @@ static int python_generate_script(struct pevent *pevent, const char *outfile) fprintf(ofp, "from Core import *\n\n\n"); fprintf(ofp, "def trace_begin():\n"); - fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); + fprintf(ofp, "\tprint(\"in trace_begin\")\n\n"); fprintf(ofp, "def trace_end():\n"); - fprintf(ofp, "\tprint \"in trace_end\"\n\n"); + fprintf(ofp, "\tprint(\"in trace_end\")\n\n"); while ((event = trace_find_next_event(pevent, event))) { fprintf(ofp, "def %s__%s(", event->system, event->name); @@ -1441,7 +1442,7 @@ static int python_generate_script(struct pevent *pevent, const char *outfile) "common_secs, common_nsecs,\n\t\t\t" "common_pid, common_comm)\n\n"); - fprintf(ofp, "\t\tprint \""); + fprintf(ofp, "\t\tprint(\""); not_first = 0; count = 0; @@ -1502,31 +1503,31 @@ static int python_generate_script(struct pevent *pevent, const char *outfile) fprintf(ofp, "%s", f->name); } - fprintf(ofp, ")\n\n"); + fprintf(ofp, "))\n\n"); - fprintf(ofp, "\t\tprint 'Sample: {'+" - "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n"); + fprintf(ofp, "\t\tprint('Sample: {'+" + "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); fprintf(ofp, "\t\tfor node in common_callchain:"); fprintf(ofp, "\n\t\t\tif 'sym' in node:"); - fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])"); + fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))"); fprintf(ofp, "\n\t\t\telse:"); - fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n"); - fprintf(ofp, "\t\tprint \"\\n\"\n\n"); + fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n"); + fprintf(ofp, "\t\tprint()\n\n"); } fprintf(ofp, "def trace_unhandled(event_name, context, " "event_fields_dict, perf_sample_dict):\n"); - fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n"); - fprintf(ofp, "\t\tprint 'Sample: {'+" - "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n"); + fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n"); + fprintf(ofp, "\t\tprint('Sample: {'+" + "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n"); fprintf(ofp, "def print_header(" "event_name, cpu, secs, nsecs, pid, comm):\n" - "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" - "(event_name, cpu, secs, nsecs, pid, comm),\n\n"); + "\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" + "(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n"); fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n" "\treturn delimiter.join" -- 2.17.0