All of lore.kernel.org
 help / color / mirror / Atom feed
* User defined metrics for perf stat?
@ 2016-01-31 16:53 Andreas Hollmann
  2016-02-01 17:15 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 18+ messages in thread
From: Andreas Hollmann @ 2016-01-31 16:53 UTC (permalink / raw)
  To: linux-perf-use.

Hi,

is there any way to create user defined metrics in perf stat?

Many hardware performance counters make only sense if you relate them
to other counters, so it would be nice to have it in perf.

I thought that I have seen something like that, but I coundn't find it again.

Best regards,
Andreas

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-01-31 16:53 User defined metrics for perf stat? Andreas Hollmann
@ 2016-02-01 17:15 ` Arnaldo Carvalho de Melo
  2016-02-01 17:27   ` Jiri Olsa
  0 siblings, 1 reply; 18+ messages in thread
From: Arnaldo Carvalho de Melo @ 2016-02-01 17:15 UTC (permalink / raw)
  To: Andreas Hollmann; +Cc: Jiri Olsa, linux-perf-use.

Em Sun, Jan 31, 2016 at 05:53:49PM +0100, Andreas Hollmann escreveu:
> Hi,
> 
> is there any way to create user defined metrics in perf stat?
> 
> Many hardware performance counters make only sense if you relate them
> to other counters, so it would be nice to have it in perf.

I think what you want is the 'perf stat recording' feature that was
recently merged, contributed by Jiri Olsa, that I am CCing this reply.
Jiri, is there something still to be merged in that series?

- Arnaldo
 
> I thought that I have seen something like that, but I coundn't find it again.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-01 17:15 ` Arnaldo Carvalho de Melo
@ 2016-02-01 17:27   ` Jiri Olsa
  2016-02-01 23:33     ` Andreas Hollmann
  0 siblings, 1 reply; 18+ messages in thread
From: Jiri Olsa @ 2016-02-01 17:27 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo; +Cc: Andreas Hollmann, Jiri Olsa, linux-perf-use.

On Mon, Feb 01, 2016 at 02:15:40PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Sun, Jan 31, 2016 at 05:53:49PM +0100, Andreas Hollmann escreveu:
> > Hi,
> > 
> > is there any way to create user defined metrics in perf stat?
> > 
> > Many hardware performance counters make only sense if you relate them
> > to other counters, so it would be nice to have it in perf.
> 
> I think what you want is the 'perf stat recording' feature that was
> recently merged, contributed by Jiri Olsa, that I am CCing this reply.
> Jiri, is there something still to be merged in that series?

heya,
nope.. everything is in ;-)

Andreas,
please check on the series changelog:
  http://marc.info/?l=linux-kernel&m=145207742329050&w=2

it describes the scripting usage together with
stat recording/reporting

let me know if you have other questions,

thanks,
jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-01 17:27   ` Jiri Olsa
@ 2016-02-01 23:33     ` Andreas Hollmann
  2016-02-02  8:58       ` Jiri Olsa
  0 siblings, 1 reply; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-01 23:33 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

Thanks, this is what I was looking for!

Why was the formula language abandoned? To restricted?
It would be still useful for interval prints.

perf stat record und perf stat report work,
but I get with perf script the following output/error. (Kernel 4.4,
perf 4.5-rc2)

$ perf script -s scripts/python/stat-cpi.py
Display of symbols requested but neither sample IP nor sample address
is selected. Hence, no addresses to convert to symbols.

2016-02-01 18:27 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Mon, Feb 01, 2016 at 02:15:40PM -0300, Arnaldo Carvalho de Melo wrote:
>> Em Sun, Jan 31, 2016 at 05:53:49PM +0100, Andreas Hollmann escreveu:
>> > Hi,
>> >
>> > is there any way to create user defined metrics in perf stat?
>> >
>> > Many hardware performance counters make only sense if you relate them
>> > to other counters, so it would be nice to have it in perf.
>>
>> I think what you want is the 'perf stat recording' feature that was
>> recently merged, contributed by Jiri Olsa, that I am CCing this reply.
>> Jiri, is there something still to be merged in that series?
>
> heya,
> nope.. everything is in ;-)
>
> Andreas,
> please check on the series changelog:
>   http://marc.info/?l=linux-kernel&m=145207742329050&w=2
>
> it describes the scripting usage together with
> stat recording/reporting
>
> let me know if you have other questions,
>
> thanks,
> jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-01 23:33     ` Andreas Hollmann
@ 2016-02-02  8:58       ` Jiri Olsa
  2016-02-02  9:42         ` Andreas Hollmann
                           ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Jiri Olsa @ 2016-02-02  8:58 UTC (permalink / raw)
  To: Andreas Hollmann; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
> Thanks, this is what I was looking for!
> 
> Why was the formula language abandoned? To restricted?
> It would be still useful for interval prints.

yea the python script interface is way more powerful
then whatever we would come with

> 
> perf stat record und perf stat report work,
> but I get with perf script the following output/error. (Kernel 4.4,
> perf 4.5-rc2)
> 
> $ perf script -s scripts/python/stat-cpi.py
> Display of symbols requested but neither sample IP nor sample address
> is selected. Hence, no addresses to convert to symbols.

hum, what was the record command? so I could recreate..

thanks,
jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-02  8:58       ` Jiri Olsa
@ 2016-02-02  9:42         ` Andreas Hollmann
  2016-02-02 16:24         ` Andreas Hollmann
  2016-02-03 12:39         ` Milian Wolff
  2 siblings, 0 replies; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-02  9:42 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

It works! It was my fault I called the system wide version of perf
(4.4) with perf script and not (4.5-rc2).

Thanks!

2016-02-02 9:58 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
>> Thanks, this is what I was looking for!
>>
>> Why was the formula language abandoned? To restricted?
>> It would be still useful for interval prints.
>
> yea the python script interface is way more powerful
> then whatever we would come with
>
>>
>> perf stat record und perf stat report work,
>> but I get with perf script the following output/error. (Kernel 4.4,
>> perf 4.5-rc2)
>>
>> $ perf script -s scripts/python/stat-cpi.py
>> Display of symbols requested but neither sample IP nor sample address
>> is selected. Hence, no addresses to convert to symbols.
>
> hum, what was the record command? so I could recreate..
>
> thanks,
> jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-02  8:58       ` Jiri Olsa
  2016-02-02  9:42         ` Andreas Hollmann
@ 2016-02-02 16:24         ` Andreas Hollmann
  2016-02-03  7:48           ` Jiri Olsa
  2016-02-03 12:39         ` Milian Wolff
  2 siblings, 1 reply; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-02 16:24 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

Jiri, how do you handle raw counters with this python stat__*
callback? What is the name of the callback?

Is there any documentation on that? Thanks!

./perf stat -e cycles,"cpu/config=0x6530160/" record kill
kill: not enough arguments

 Performance counter stats for 'kill':

         1,016,510      cycles
             3,445      cpu/config=0x6530160/

       0.001278590 seconds time elapsed

./perf script
CPU   THREAD             VAL             ENA             RUN
 TIME EVENT
 -1    18827         1016510          747877          747877
1278590 cycles
 -1    18827            3445          747877          747877
1278590 cpu/config=0x6530160/

2016-02-02 9:58 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
>> Thanks, this is what I was looking for!
>>
>> Why was the formula language abandoned? To restricted?
>> It would be still useful for interval prints.
>
> yea the python script interface is way more powerful
> then whatever we would come with
>
>>
>> perf stat record und perf stat report work,
>> but I get with perf script the following output/error. (Kernel 4.4,
>> perf 4.5-rc2)
>>
>> $ perf script -s scripts/python/stat-cpi.py
>> Display of symbols requested but neither sample IP nor sample address
>> is selected. Hence, no addresses to convert to symbols.
>
> hum, what was the record command? so I could recreate..
>
> thanks,
> jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-02 16:24         ` Andreas Hollmann
@ 2016-02-03  7:48           ` Jiri Olsa
  2016-02-03 11:43             ` Andreas Hollmann
  2016-02-05 14:12             ` Andreas Hollmann
  0 siblings, 2 replies; 18+ messages in thread
From: Jiri Olsa @ 2016-02-03  7:48 UTC (permalink / raw)
  To: Andreas Hollmann; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

On Tue, Feb 02, 2016 at 05:24:16PM +0100, Andreas Hollmann wrote:
> Jiri, how do you handle raw counters with this python stat__*
> callback? What is the name of the callback?

you can use 'name' term like:
  perf stat -e cycles,"cpu/config=0x6530160,name=krava/"

and use following callback in your script:
  def stat__krava(cpu, thread, time, val, ena, run):

> 
> Is there any documentation on that? Thanks!

no.. ;-)

I'll try to come up with something.. meanwhile please just ask,
I'd be happy to help you

thanks,
jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03  7:48           ` Jiri Olsa
@ 2016-02-03 11:43             ` Andreas Hollmann
  2016-02-03 14:09               ` Jiri Olsa
  2016-02-05 14:12             ` Andreas Hollmann
  1 sibling, 1 reply; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-03 11:43 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

Thanks! Now it works.

Are there any other callbacks specific to the new functionality?

I saw this:

def stat__interval(time):
   ...

The name suggests that it is called once for each interval event entry?

Anything else?

Would it make sense to extend "perf script -g" to generate the callbacks?

2016-02-03 8:48 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Tue, Feb 02, 2016 at 05:24:16PM +0100, Andreas Hollmann wrote:
>> Jiri, how do you handle raw counters with this python stat__*
>> callback? What is the name of the callback?
>
> you can use 'name' term like:
>   perf stat -e cycles,"cpu/config=0x6530160,name=krava/"
>
> and use following callback in your script:
>   def stat__krava(cpu, thread, time, val, ena, run):
>
>>
>> Is there any documentation on that? Thanks!
>
> no.. ;-)
>
> I'll try to come up with something.. meanwhile please just ask,
> I'd be happy to help you
>
> thanks,
> jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-02  8:58       ` Jiri Olsa
  2016-02-02  9:42         ` Andreas Hollmann
  2016-02-02 16:24         ` Andreas Hollmann
@ 2016-02-03 12:39         ` Milian Wolff
  2016-02-03 14:11           ` Jiri Olsa
  2 siblings, 1 reply; 18+ messages in thread
From: Milian Wolff @ 2016-02-03 12:39 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Andreas Hollmann, Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

[-- Attachment #1: Type: text/plain, Size: 681 bytes --]

On Tuesday, February 2, 2016 9:58:02 AM CET Jiri Olsa wrote:
> On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
> > Thanks, this is what I was looking for!
> > 
> > Why was the formula language abandoned? To restricted?
> > It would be still useful for interval prints.
> 
> yea the python script interface is way more powerful
> then whatever we would come with

How does this compare to the ongoing eBPF effort? Will we be able to do eBPF 
based in-kernel aggregation for perf stat in the future?

Thanks

-- 
Milian Wolff | milian.wolff@kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5903 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03 11:43             ` Andreas Hollmann
@ 2016-02-03 14:09               ` Jiri Olsa
  2016-02-03 15:25                 ` Andreas Hollmann
  0 siblings, 1 reply; 18+ messages in thread
From: Jiri Olsa @ 2016-02-03 14:09 UTC (permalink / raw)
  To: Andreas Hollmann; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

On Wed, Feb 03, 2016 at 12:43:48PM +0100, Andreas Hollmann wrote:
> Thanks! Now it works.
> 
> Are there any other callbacks specific to the new functionality?
> 
> I saw this:
> 
> def stat__interval(time):
>    ...
> 
> The name suggests that it is called once for each interval event entry?
> 
> Anything else?

there was explanation in one of the patch changelogs, attached
I'll make it appear in some doc soon

> 
> Would it make sense to extend "perf script -g" to generate the callbacks?

yep, I thought about that.. will make it happen ;-)

thanks,
jirka


---
Add support to get stat events data in perf python scripts.

The python script shall implement following
new interface to process stat data:

  def stat__<event_name>_[<modifier>](cpu, thread, time, val, ena, run):

    - is called for every stat event for given counter,
      if user monitors 'cycles,instructions:u" following
      callbacks should be defined:

      def stat__cycles(cpu, thread, time, val, ena, run):
      def stat__instructions_u(cpu, thread, time, val, ena, run):

  def stat__interval(time):

    - is called for every interval with its time,
      in non interval mode it's called after last
      stat event with total measured time in ns

The rest of the current interface stays untouched..

Please check example CPI metrics script in following patch
with command line examples in changelogs.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03 12:39         ` Milian Wolff
@ 2016-02-03 14:11           ` Jiri Olsa
  2016-02-03 14:18             ` eBPF counters for 'perf stat' e.g.: " Arnaldo Carvalho de Melo
  2016-02-03 14:43             ` Milian Wolff
  0 siblings, 2 replies; 18+ messages in thread
From: Jiri Olsa @ 2016-02-03 14:11 UTC (permalink / raw)
  To: Milian Wolff
  Cc: Andreas Hollmann, Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

On Wed, Feb 03, 2016 at 01:39:20PM +0100, Milian Wolff wrote:
> On Tuesday, February 2, 2016 9:58:02 AM CET Jiri Olsa wrote:
> > On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
> > > Thanks, this is what I was looking for!
> > > 
> > > Why was the formula language abandoned? To restricted?
> > > It would be still useful for interval prints.
> > 
> > yea the python script interface is way more powerful
> > then whatever we would come with
> 
> How does this compare to the ongoing eBPF effort? Will we be able to do eBPF 
> based in-kernel aggregation for perf stat in the future?

hum, not sure what you mean by that, however this is
all user level scripting support to allow user defined
metrics/ratios from perf counters, more info ine here:
  http://marc.info/?l=linux-kernel&m=145207742329050&w=2

jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* eBPF counters for 'perf stat' e.g.: Re: User defined metrics for perf stat?
  2016-02-03 14:11           ` Jiri Olsa
@ 2016-02-03 14:18             ` Arnaldo Carvalho de Melo
  2016-02-04  9:46               ` Wangnan (F)
  2016-02-03 14:43             ` Milian Wolff
  1 sibling, 1 reply; 18+ messages in thread
From: Arnaldo Carvalho de Melo @ 2016-02-03 14:18 UTC (permalink / raw)
  To: Wang Nan
  Cc: Milian Wolff, Jiri Olsa, Andreas Hollmann, Jiri Olsa, linux-perf-users

Em Wed, Feb 03, 2016 at 03:11:57PM +0100, Jiri Olsa escreveu:
> On Wed, Feb 03, 2016 at 01:39:20PM +0100, Milian Wolff wrote:
> > How does this compare to the ongoing eBPF effort? Will we be able to do eBPF 
> > based in-kernel aggregation for perf stat in the future?
 
> hum, not sure what you mean by that, however this is
> all user level scripting support to allow user defined
> metrics/ratios from perf counters, more info ine here:
>   http://marc.info/?l=linux-kernel&m=145207742329050&w=2

I would have to check, Wang may help here, but perf has been getting
more and more integrated with the eBPF facilities in place in the
kernel.

To the point that yeah, I think that loading a C program that would hook
somewhere in the kernel and would then create a counter that could be
named and used with perf stat is something I think should even already
be possible if you use what is in Wang's tree, being reviewed to get
upstream, Wang?

- Arnaldo

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03 14:11           ` Jiri Olsa
  2016-02-03 14:18             ` eBPF counters for 'perf stat' e.g.: " Arnaldo Carvalho de Melo
@ 2016-02-03 14:43             ` Milian Wolff
  1 sibling, 0 replies; 18+ messages in thread
From: Milian Wolff @ 2016-02-03 14:43 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Andreas Hollmann, Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

[-- Attachment #1: Type: text/plain, Size: 1833 bytes --]

On Wednesday, February 3, 2016 3:11:57 PM CET Jiri Olsa wrote:
> On Wed, Feb 03, 2016 at 01:39:20PM +0100, Milian Wolff wrote:
> > On Tuesday, February 2, 2016 9:58:02 AM CET Jiri Olsa wrote:
> > > On Tue, Feb 02, 2016 at 12:33:51AM +0100, Andreas Hollmann wrote:
> > > > Thanks, this is what I was looking for!
> > > > 
> > > > Why was the formula language abandoned? To restricted?
> > > > It would be still useful for interval prints.
> > > 
> > > yea the python script interface is way more powerful
> > > then whatever we would come with
> > 
> > How does this compare to the ongoing eBPF effort? Will we be able to do
> > eBPF based in-kernel aggregation for perf stat in the future?
> 
> hum, not sure what you mean by that, however this is
> all user level scripting support to allow user defined
> metrics/ratios from perf counters, more info ine here:
>   http://marc.info/?l=linux-kernel&m=145207742329050&w=2

I have not used eBPF at all yet, only read about it. Brendan's recent blog 
series was quite educative in that regard. That said, maybe I've so far 
completly misunderstood it. My current assumptions are:

- eBPF can be programmed in a C dialect
- it can be used to aggregate data on the fly, without introducing the 
userspace overhead
- the latter is especially important when your input source is extremely 
large, but you are only interested in an aggregate

Due to that, I thought this applies to perf stat. A eBPF program could then 
aggregate e.g. cycles and instruction counts on the fly to a CPI value. I 
imagine that would be extremely cheap to do there, and only the final CPI 
value would need to be passed to userspace for evaluation.

Bye

-- 
Milian Wolff | milian.wolff@kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5903 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03 14:09               ` Jiri Olsa
@ 2016-02-03 15:25                 ` Andreas Hollmann
  0 siblings, 0 replies; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-03 15:25 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

Thanks again, Jiri!

I'm wondering why stat_interva(time) and the
time parameter has a second meaning for non
interval code?

What intention was behind your decision?

Isn't it redundant and maybe ambiguous
since we have trace_end()?

The issue I see is that you have to distinguish
between time for interval and non interval mode
in your python script and then you could use
trace_end() anyway?

Maybe I'm missing something.

  def stat__interval(time):

    - is called for every interval with its time,
      in non interval mode it's called after last
      stat event with total measured time in ns

2016-02-03 15:09 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Wed, Feb 03, 2016 at 12:43:48PM +0100, Andreas Hollmann wrote:
>> Thanks! Now it works.
>>
>> Are there any other callbacks specific to the new functionality?
>>
>> I saw this:
>>
>> def stat__interval(time):
>>    ...
>>
>> The name suggests that it is called once for each interval event entry?
>>
>> Anything else?
>
> there was explanation in one of the patch changelogs, attached
> I'll make it appear in some doc soon
>
>>
>> Would it make sense to extend "perf script -g" to generate the callbacks?
>
> yep, I thought about that.. will make it happen ;-)
>
> thanks,
> jirka
>
>
> ---
> Add support to get stat events data in perf python scripts.
>
> The python script shall implement following
> new interface to process stat data:
>
>   def stat__<event_name>_[<modifier>](cpu, thread, time, val, ena, run):
>
>     - is called for every stat event for given counter,
>       if user monitors 'cycles,instructions:u" following
>       callbacks should be defined:
>
>       def stat__cycles(cpu, thread, time, val, ena, run):
>       def stat__instructions_u(cpu, thread, time, val, ena, run):
>
>   def stat__interval(time):
>
>     - is called for every interval with its time,
>       in non interval mode it's called after last
>       stat event with total measured time in ns
>
> The rest of the current interface stays untouched..
>
> Please check example CPI metrics script in following patch
> with command line examples in changelogs.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: eBPF counters for 'perf stat' e.g.: Re: User defined metrics for perf stat?
  2016-02-03 14:18             ` eBPF counters for 'perf stat' e.g.: " Arnaldo Carvalho de Melo
@ 2016-02-04  9:46               ` Wangnan (F)
  0 siblings, 0 replies; 18+ messages in thread
From: Wangnan (F) @ 2016-02-04  9:46 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Milian Wolff, Jiri Olsa, Andreas Hollmann, Jiri Olsa, linux-perf-users



On 2016/2/3 22:18, Arnaldo Carvalho de Melo wrote:
> Em Wed, Feb 03, 2016 at 03:11:57PM +0100, Jiri Olsa escreveu:
>> On Wed, Feb 03, 2016 at 01:39:20PM +0100, Milian Wolff wrote:
>>> How does this compare to the ongoing eBPF effort? Will we be able to do eBPF
>>> based in-kernel aggregation for perf stat in the future?
>   
>> hum, not sure what you mean by that, however this is
>> all user level scripting support to allow user defined
>> metrics/ratios from perf counters, more info ine here:
>>    http://marc.info/?l=linux-kernel&m=145207742329050&w=2
> I would have to check, Wang may help here, but perf has been getting
> more and more integrated with the eBPF facilities in place in the
> kernel.
>
> To the point that yeah, I think that loading a C program that would hook
> somewhere in the kernel and would then create a counter that could be
> named and used with perf stat is something I think should even already
> be possible if you use what is in Wang's tree, being reviewed to get
> upstream, Wang?

Still not support 'perf stat'. I focus on 'perf record' now.

Here I provide an example shows how to compute IPC for a particular
kernel function. Usage:

# perf record -e output=bpf-output/no-inherit/ \
               -e cyc=cycles/no-inherit,period=0x7fffffffffffffff/ \
               -e insn=instructions/no-inherit,period=0x7fffffffffffffff/ \
               -e 
./test.c/maps:cycles_pmu.event=cyc,maps:insn_pmu.event=insn,maps:output_channel.event=output/ 
\
               cat /etc/passwd
...
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data (1 samples) ]

# perf data convert --to-ctf ./out.ctf
[ perf data convert: Converted 'perf.data' into CTF data './out.ctf' ]
[ perf data convert: Converted and wrote 0.000 MB (1 samples) ]
# babeltrace ./out.ctf/
[20:30:39.714787237] (+?.?????????) output=bpf-output/no-inherit/: { 
cpu_id = 0 }, { perf_ip = 0xFFFFFFFF8107CB81, perf_tid = 30718, perf_pid 
= 30718, perf_id = 3171, raw_len = 5, raw_data = [ [0] = 0x4B5C, [1] = 
0x0, [2] = 0x2CDE, [3] = 0x0, [4] = 0x0 ] }

So we know 'cat /etc/passwd' consumes 0x4b5c cycles and 0x2cde instructions
in sys_read(), so IPC for this kernel function is 0.595.

The example shows data aggregation BPF scripts can do. In the script
we can sum up cycles and instructions of each execution of the function
but filter other part of the program out.

I'm unable to compute IPC in kernel directly because LLVM doesn't support
compiling '/' operation into BPF code. We can bring raw data out through
bpf-output event and calculate them using other helpers.
The program is long and not very easy to read because we don't have enough
scaffold for building BPF script, so have to write them by hand. However,
at present the basic perf event and bpf output support have not been merged
into mainline (availible on my tree only), so I'm still working on this 
part.

-------------- test.c ---------------------

#include <uapi/linux/bpf.h>

#define SEC(NAME) __attribute__((section(NAME), used))

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = 
(void *)BPF_FUNC_trace_printk;
static void *(*map_lookup_elem)(void *map, void *key) = (void 
*)BPF_FUNC_map_lookup_elem;
static int (*bpf_get_current_comm)(char *map, int size_of_buf) = (void 
*)BPF_FUNC_get_current_comm;
static u64 (*bpf_perf_event_read)(void *map, int index) = (void 
*)BPF_FUNC_perf_event_read;
static u32 (*get_smp_processor_id)(void)= (void 
*)BPF_FUNC_get_smp_processor_id;
static void (*bpf_perf_event_output)(void *ctx, void *map, int index, 
void *data, int size) =
                         (void *)BPF_FUNC_perf_event_output;

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;

struct bpf_map_def {
         unsigned int type;
         unsigned int key_size;
         unsigned int value_size;
         unsigned int max_entries;
};

struct bpf_map_def SEC("maps") cycles_pmu = {
         .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
         .key_size = sizeof(int),
         .max_entries = __NR_CPUS__,
};

struct bpf_map_def SEC("maps") insn_pmu = {
         .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
         .key_size = sizeof(int),
         .value_size = sizeof(u32),
         .max_entries = __NR_CPUS__,
};

struct bpf_map_def SEC("maps") output_channel = {
         .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
         .key_size = sizeof(int),
         .value_size = sizeof(u32),
         .max_entries = __NR_CPUS__,
};

struct bpf_map_def SEC("maps") vals = {
         .type = BPF_MAP_TYPE_ARRAY,
         .key_size = sizeof(int),
         .value_size = sizeof(u64),
         .max_entries = __NR_CPUS__ * 2 + 2,
};

static inline bool basic_filter(void)
{
         char comm[24] = "";
         char name[4] = "cat";

         if (bpf_get_current_comm(comm, 24))
                 return false;
         if (__builtin_memcmp(comm, name, sizeof(name)))
                 return false;
         return true;
}

static inline bool read_pmu(void *map, u64 *val, u64 **p_val, int cpu, 
int key)
{
         *val = bpf_perf_event_read(map, cpu);
         if (*val >= 0xffffffff00000000)
                 return false;
         *p_val = map_lookup_elem(&vals, &key);
         if (!*p_val)
                 return false;
         return true;
}

static inline bool read_pmus(u64 *cycles, u64 **p_cycles,
                                u64 *insns, u64 **p_insns)
{
         int cpu = get_smp_processor_id();
         int key = cpu;

         if (!basic_filter())
                 return false;

         if (!read_pmu(&cycles_pmu, cycles, p_cycles, cpu, key))
                 return false;

         key += __NR_CPUS__;
         if (!read_pmu(&insn_pmu, insns, p_insns, cpu, key))
                 return false;
         return true;
}

SEC("bpf__sys_read=sys_read")
int bpf__sys_read(void *ctx)
{
         u64 cycles, *p_cycles, insns, *p_insns;

         if (read_pmus(&cycles, &p_cycles, &insns, &p_insns)) {
                 *p_cycles = cycles;
                 *p_insns = insns;
         }
         return 0;
}

SEC("bpf__sys_read_ret=sys_read%return")
int bpf__sys_read_ret(void *ctx)
{
         u64 cycles, *p_cycles, insns, *p_insns;
         u64 *total;
         int key;

         if (read_pmus(&cycles, &p_cycles, &insns, &p_insns)) {
                 cycles = cycles - *p_cycles;
                 insns = insns - *p_insns;

                 key = __NR_CPUS__ * 2;
                 total = map_lookup_elem(&vals, &key);
                 if (!total)
                         return 0;
                 __sync_fetch_and_add(total, cycles);

                 key = __NR_CPUS__ * 2 + 1;
                 total = map_lookup_elem(&vals, &key);
                 if (!total)
                         return 0;
                 __sync_fetch_and_add(total, insns);
         }
         return 0;
}

SEC("bpf__do_exit=do_exit")
int bpf__do_exit(void *ctx)
{
         int cpu = get_smp_processor_id();
         int key = __NR_CPUS__ * 2;
         u64 *p_total, total;

         struct {
                 u64 cycles;
                 u64 insns;
         } output;

         if (!basic_filter())
                 return 0;

         p_total = map_lookup_elem(&vals, &key);
         if (!p_total)
                 return 0;
         output.cycles = *p_total;

         key = __NR_CPUS__ * 2 + 1;
         p_total = map_lookup_elem(&vals, &key);
         if (!p_total)
                 return 0;
         output.insns = *p_total;

         bpf_perf_event_output(ctx, &output_channel, cpu, &output, 
sizeof(output));
         return 0;
}

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-03  7:48           ` Jiri Olsa
  2016-02-03 11:43             ` Andreas Hollmann
@ 2016-02-05 14:12             ` Andreas Hollmann
  2016-02-06 21:54               ` Jiri Olsa
  1 sibling, 1 reply; 18+ messages in thread
From: Andreas Hollmann @ 2016-02-05 14:12 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

Jiri, this method does not work when you pipe data to perf script.

Works with perf.data file:

./perf stat -a -I 1000 -e cycles,"cpu/config=0x6530160,name=krava/" record

/perf script -s test.py
event krava cpu -1, thread -1, time 1029898211, val 10959, ena
81192094638, run 81192094638
event krava cpu -1, thread -1, time 2043671594, val 10753, ena
81094362809, run 81094362809

does not work when using pipe

./perf stat -a -I 1000 -e cycles,"cpu/config=0x6530160,name=krava/"
record | ./perf script -s test.py

--- no output

Here the test.py

cat test.py
#!/usr/bin/env python

def stat__krava(cpu, thread, time, val, ena, run):
    print "event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" % \
          ("krava", cpu, thread, time, val, ena, run)


2016-02-03 8:48 GMT+01:00 Jiri Olsa <jolsa@redhat.com>:
> On Tue, Feb 02, 2016 at 05:24:16PM +0100, Andreas Hollmann wrote:
>> Jiri, how do you handle raw counters with this python stat__*
>> callback? What is the name of the callback?
>
> you can use 'name' term like:
>   perf stat -e cycles,"cpu/config=0x6530160,name=krava/"
>
> and use following callback in your script:
>   def stat__krava(cpu, thread, time, val, ena, run):
>
>>
>> Is there any documentation on that? Thanks!
>
> no.. ;-)
>
> I'll try to come up with something.. meanwhile please just ask,
> I'd be happy to help you
>
> thanks,
> jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: User defined metrics for perf stat?
  2016-02-05 14:12             ` Andreas Hollmann
@ 2016-02-06 21:54               ` Jiri Olsa
  0 siblings, 0 replies; 18+ messages in thread
From: Jiri Olsa @ 2016-02-06 21:54 UTC (permalink / raw)
  To: Andreas Hollmann; +Cc: Arnaldo Carvalho de Melo, Jiri Olsa, linux-perf-use.

On Fri, Feb 05, 2016 at 03:12:04PM +0100, Andreas Hollmann wrote:
> Jiri, this method does not work when you pipe data to perf script.
> 
> Works with perf.data file:
> 
> ./perf stat -a -I 1000 -e cycles,"cpu/config=0x6530160,name=krava/" record
> 
> /perf script -s test.py
> event krava cpu -1, thread -1, time 1029898211, val 10959, ena
> 81192094638, run 81192094638
> event krava cpu -1, thread -1, time 2043671594, val 10753, ena
> 81094362809, run 81094362809
> 
> does not work when using pipe
> 
> ./perf stat -a -I 1000 -e cycles,"cpu/config=0x6530160,name=krava/"
> record | ./perf script -s test.py
> 
> --- no output
> 
> Here the test.py
> 
> cat test.py
> #!/usr/bin/env python
> 
> def stat__krava(cpu, thread, time, val, ena, run):
>     print "event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" % \
>           ("krava", cpu, thread, time, val, ena, run)

right, we probably don't transfer the name term through the pipe,
I'll check on that..

jirka

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2016-02-06 21:54 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-31 16:53 User defined metrics for perf stat? Andreas Hollmann
2016-02-01 17:15 ` Arnaldo Carvalho de Melo
2016-02-01 17:27   ` Jiri Olsa
2016-02-01 23:33     ` Andreas Hollmann
2016-02-02  8:58       ` Jiri Olsa
2016-02-02  9:42         ` Andreas Hollmann
2016-02-02 16:24         ` Andreas Hollmann
2016-02-03  7:48           ` Jiri Olsa
2016-02-03 11:43             ` Andreas Hollmann
2016-02-03 14:09               ` Jiri Olsa
2016-02-03 15:25                 ` Andreas Hollmann
2016-02-05 14:12             ` Andreas Hollmann
2016-02-06 21:54               ` Jiri Olsa
2016-02-03 12:39         ` Milian Wolff
2016-02-03 14:11           ` Jiri Olsa
2016-02-03 14:18             ` eBPF counters for 'perf stat' e.g.: " Arnaldo Carvalho de Melo
2016-02-04  9:46               ` Wangnan (F)
2016-02-03 14:43             ` Milian Wolff

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.