Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1297476

Re: [PATCH 8/8] perf script: Add stat-cpi.py script

Path csiph.com!news.mixmin.net!aioe.org!bofh.it!news.nic.it!robomod
From Namhyung Kim <namhyung@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH 8/8] perf script: Add stat-cpi.py script
Date Wed, 23 Dec 2015 14:50:01 +0100
Message-ID <qIRXH-1Sp-7@gated-at.bofh.it> (permalink)
References <qIxmi-5MK-3@gated-at.bofh.it> <qIxmi-5MK-15@gated-at.bofh.it>
X-Original-To Jiri Olsa <jolsa@kernel.org>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=0XbUH7ZaM3sir0EBEbs42FAKIwX70IX7Wj+PxnjGBoA=; b=Yj5X5yOQEFAr1rBvEdAo5exKDwl8LjkEtAhagyTgU5T946J9lBUmhCkmBN/9cI/ZCu sK2jbmfNmjne26Rkd7+IP4P2AQ3zEY3vc8sD2Aw3DMq1iq1EFpJa78ZSbk2mywfsPgal YG7FtMJkRy60PR/1w3U8rML1BEH19mE5B66z41dVaYyJlzLovwsq4yLgpejKpTsFshhs Pg0McTz67qojxIp1dh4P+9uLYPktX4n8/qPzdCpQGztoABix8q7vLpROdn+7xxHktUuJ hXmxL9JAUHYB8guA6tv+zPfnt7N5k8e7OfriTS077EHFt2xlIlk+fdVccUy9j3LtzsoU uX3A==
X-Received by 10.98.15.143 with SMTP id 15mr251964pfp.60.1450878306349; Wed, 23 Dec 2015 05:45:06 -0800 (PST)
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Disposition inline
User-Agent Mutt/1.5.24 (2015-08-30)
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 160
Organization linux.* mail to news gateway
X-Original-Cc Arnaldo Carvalho de Melo <acme@kernel.org>, lkml <linux-kernel@vger.kernel.org>, David Ahern <dsahern@gmail.com>, Ingo Molnar <mingo@kernel.org>, Peter Zijlstra <a.p.zijlstra@chello.nl>, "Liang, Kan" <kan.liang@intel.com>
X-Original-Date Wed, 23 Dec 2015 22:44:20 +0900
X-Original-Message-ID <20151223134420.GD23199@danjae.kornet>
X-Original-References <1450799014-31469-1-git-send-email-jolsa@kernel.org> <1450799014-31469-9-git-send-email-jolsa@kernel.org>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1297476

Show key headers only | View raw


On Tue, Dec 22, 2015 at 04:43:34PM +0100, Jiri Olsa wrote:
> Adding stat-cpi.py as an example of how to do stat scripting.
> It computes the CPI metrics from cycles and instructions
> events.
> 
> Following stat record/report/script combinations could be used:
> 
> - get CPI for given workload
> 
>     $ perf stat -e cycles,instructions record ls
> 
>     SNIP
> 
>      Performance counter stats for 'ls':
> 
>              2,904,431      cycles
>              3,346,878      instructions              #    1.15  insns per cycle
> 
>            0.001782686 seconds time elapsed
> 
>     $ perf script -s ./scripts/python/stat-cpi.py
>            0.001783: cpu -1, thread -1 -> cpi 0.867803 (2904431/3346878)
> 
>     $ perf stat -e cycles,instructions record ls | perf script -s ./scripts/python/stat-cpi.py
> 
>     SNIP
> 
>            0.001730: cpu -1, thread -1 -> cpi 0.869026 (2928292/3369627)
> 
> - get CPI systemwide:
> 
>     $ perf stat -e cycles,instructions -a -I 1000 record sleep 3
>     #           time             counts unit events
>          1.000158618        594,274,711      cycles                     (100.00%)
>          1.000158618        441,898,250      instructions
>          2.000350973        567,649,705      cycles                     (100.00%)
>          2.000350973        432,669,206      instructions
>          3.000559210        561,940,430      cycles                     (100.00%)
>          3.000559210        420,403,465      instructions
>          3.000670798            780,105      cycles                     (100.00%)
>          3.000670798            326,516      instructions
> 
>     $ perf script -s ./scripts/python/stat-cpi.py
>            1.000159: cpu -1, thread -1 -> cpi 1.344823 (594274711/441898250)
>            2.000351: cpu -1, thread -1 -> cpi 1.311972 (567649705/432669206)
>            3.000559: cpu -1, thread -1 -> cpi 1.336669 (561940430/420403465)
>            3.000671: cpu -1, thread -1 -> cpi 2.389178 (780105/326516)
> 
>     $ perf stat -e cycles,instructions -a -I 1000 record sleep 3 | perf script -s ./scripts/python/stat-cpi.py
>            1.000202: cpu -1, thread -1 -> cpi 1.035091 (940778881/908885530)
>            2.000392: cpu -1, thread -1 -> cpi 1.442600 (627493992/434974455)
>            3.000545: cpu -1, thread -1 -> cpi 1.353612 (741463930/547766890)
>            3.000622: cpu -1, thread -1 -> cpi 2.642110 (784083/296764)
> 
> Tested-by: Kan Liang <kan.liang@intel.com>
> Link: http://lkml.kernel.org/n/tip-15vwwb4yea15wzz6bqbxdpc0@git.kernel.org
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  tools/perf/scripts/python/stat-cpi.py | 77 +++++++++++++++++++++++++++++++++++
>  1 file changed, 77 insertions(+)
>  create mode 100644 tools/perf/scripts/python/stat-cpi.py
> 
> diff --git a/tools/perf/scripts/python/stat-cpi.py b/tools/perf/scripts/python/stat-cpi.py
> new file mode 100644
> index 000000000000..32689a83b6e6
> --- /dev/null
> +++ b/tools/perf/scripts/python/stat-cpi.py
> @@ -0,0 +1,77 @@
> +#!/bin/python

It an unusual place IMHO.  Wouldn't it be '#!/usr/bin/env python' ?

Thanks,
Namhyung


> +
> +data    = {}
> +times   = []
> +threads = []
> +cpus    = []
> +
> +def get_key(time, event, cpu, thread):
> +    return "%d-%s-%d-%d" % (time, event, cpu, thread)
> +
> +def store_key(time, cpu, thread):
> +    if (time not in times):
> +        times.append(time)
> +
> +    if (cpu not in cpus):
> +        cpus.append(cpu)
> +
> +    if (thread not in threads):
> +        threads.append(thread)
> +
> +def store(time, event, cpu, thread, val, ena, run):
> +    #print "event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" % \
> +    #      (event, cpu, thread, time, val, ena, run)
> +
> +    store_key(time, cpu, thread)
> +    key = get_key(time, event, cpu, thread)
> +    data[key] = [ val, ena, run]
> +
> +def get(time, event, cpu, thread):
> +    key = get_key(time, event, cpu, thread)
> +    return data[key][0]
> +
> +def stat__cycles_k(cpu, thread, time, val, ena, run):
> +    store(time, "cycles", cpu, thread, val, ena, run);
> +
> +def stat__instructions_k(cpu, thread, time, val, ena, run):
> +    store(time, "instructions", cpu, thread, val, ena, run);
> +
> +def stat__cycles_u(cpu, thread, time, val, ena, run):
> +    store(time, "cycles", cpu, thread, val, ena, run);
> +
> +def stat__instructions_u(cpu, thread, time, val, ena, run):
> +    store(time, "instructions", cpu, thread, val, ena, run);
> +
> +def stat__cycles(cpu, thread, time, val, ena, run):
> +    store(time, "cycles", cpu, thread, val, ena, run);
> +
> +def stat__instructions(cpu, thread, time, val, ena, run):
> +    store(time, "instructions", cpu, thread, val, ena, run);
> +
> +def stat__interval(time):
> +    for cpu in cpus:
> +        for thread in threads:
> +            cyc = get(time, "cycles", cpu, thread)
> +            ins = get(time, "instructions", cpu, thread)
> +            cpi = 0
> +
> +            if ins != 0:
> +                cpi = cyc/float(ins)
> +
> +            print "%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins)
> +
> +def trace_end():
> +    pass
> +# XXX trace_end callback could be used as an alternative place
> +#     to compute same values as in the script above:
> +#
> +#    for time in times:
> +#        for cpu in cpus:
> +#            for thread in threads:
> +#                cyc = get(time, "cycles", cpu, thread)
> +#                ins = get(time, "instructions", cpu, thread)
> +#
> +#                if ins != 0:
> +#                    cpi = cyc/float(ins)
> +#
> +#                print "time %.9f, cpu %d, thread %d -> cpi %f" % (time/(float(1000000000)), cpu, thread, cpi)
> -- 
> 2.4.3
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCHv7 00/25] perf stat: Add scripting support Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
  [PATCH 7/8] perf script: Add python support for stat events Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
    Re: [PATCH 7/8] perf script: Add python support for stat events Namhyung Kim <namhyung@kernel.org> - 2015-12-23 14:50 +0100
      Re: [PATCH 7/8] perf script: Add python support for stat events Jiri Olsa <jolsa@redhat.com> - 2015-12-23 17:10 +0100
  [PATCH 1/8] perf stat record: Keep sample_type 0 for pipe session Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
  [PATCH 2/8] perf script: Process cpu/threads maps Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
  [PATCH 8/8] perf script: Add stat-cpi.py script Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
    Re: [PATCH 8/8] perf script: Add stat-cpi.py script Namhyung Kim <namhyung@kernel.org> - 2015-12-23 14:50 +0100
      Re: [PATCH 8/8] perf script: Add stat-cpi.py script Jiri Olsa <jolsa@redhat.com> - 2015-12-23 17:10 +0100
  [PATCH 6/8] perf script: Display stat events by default Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
  [PATCH 4/8] perf script: Add process_stat/process_stat_interval scripting interface Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
  [PATCH 5/8] perf script: Add stat default handlers Jiri Olsa <jolsa@kernel.org> - 2015-12-22 16:50 +0100
    Re: [PATCH 5/8] perf script: Add stat default handlers Namhyung Kim <namhyung@kernel.org> - 2015-12-23 14:50 +0100
      Re: [PATCH 5/8] perf script: Add stat default handlers Jiri Olsa <jolsa@redhat.com> - 2015-12-23 17:10 +0100
  Re: [PATCHv7 00/25] perf stat: Add scripting support Jiri Olsa <jolsa@redhat.com> - 2015-12-22 17:00 +0100

csiph-web