Skip to contentSkip to conversation

Accessing sample data (like period) for trace events from python scripts

DISCUSSION Milian Wolff 2 participants 8 messages Aug 16, 2016 – Aug 23, 2016

lore
Share & tools
lore
#0 lore raw

Hey all,

It doesn't seem to be possible to access the period, tid and various other sample fields from a python (and perl?) script using perf script. My use- case would be analyzing a perf data file created by perf inject --sched-stat.

I have hacked something together locally in python_process_tracepoint, but wonder what an upstreamable version should look like:

- I simply added the sample data to the dict, like is done in python_process_general_event

- Alternatively, one could think of more common_period(context) functions. If that is preferable, I could add the sample to the scripting_context and make that work.

Please advise the path forward, I'm willing to send a patch upstream to make it work.

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#1 lore raw

Hi Milian,

Quoted 6 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various other sample fields from a python (and perl?) script using perf script. My use- case would be analyzing a perf data file created by perf inject --sched-stat.

I think you can access param_dict["sample"]["period"].

Thanks,
Namhyung

Quoted 20 lines

I have hacked something together locally in python_process_tracepoint, but wonder what an upstreamable version should look like:

- I simply added the sample data to the dict, like is done in python_process_general_event

- Alternatively, one could think of more common_period(context) functions. If that is preferable, I could add the sample to the scripting_context and make that work.

Please advise the path forward, I'm willing to send a patch upstream to make it work.

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#2 lore raw
Quoted 5 lines · CEST Namhyung Kim

Hi Milian,

Quoted 2 lines · Milian Wolff

Hey all,

Quoted 7 lines
Quoted 5 lines

It doesn't seem to be possible to access the period, tid and various other sample fields from a python (and perl?) script using perf script. My use-
case would be analyzing a perf data file created by perf inject --sched-stat.

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

~~~~~~~~~~~~~~~~~~
/tmp$ cat perf-script.py

import os
import sys

sys.path.append(os.environ['PERF_EXEC_PATH'] + \
'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')

from perf_trace_context import *
from Core import *

def trace_unhandled(event_name, context, event_fields_dict):
print event_fields_dict['sample']['period']

/tmp$ perf script -s perf-script.py
Traceback (most recent call last):
File "perf-script.py", line 11, in trace_unhandled
print event_fields_dict['sample']['period']
KeyError: 'sample'
Fatal Python error: problem in Python trace event handler Aborted (core dumped)
~~~~~~~~~~~~~~~~~~

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#3 lore raw
Quoted 12 lines · Milian Wolff
Quoted 11 lines · CEST Namhyung Kim

Hi Milian,

Quoted 7 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various other sample fields from a python (and perl?) script using perf script. My use-
case would be analyzing a perf data file created by perf inject --sched-stat.

Quoted 3 lines
Quoted 1 line

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

Sorry for late reply. Oh, you wanted to access period of tracepoint but I guess it's always 1, no?

Thanks,
Namhyung

Quoted 31 lines

~~~~~~~~~~~~~~~~~~
/tmp$ cat perf-script.py

import os
import sys

sys.path.append(os.environ['PERF_EXEC_PATH'] + \
'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')

from perf_trace_context import *
from Core import *

def trace_unhandled(event_name, context, event_fields_dict):
print event_fields_dict['sample']['period']

/tmp$ perf script -s perf-script.py
Traceback (most recent call last):
File "perf-script.py", line 11, in trace_unhandled
print event_fields_dict['sample']['period']
KeyError: 'sample'
Fatal Python error: problem in Python trace event handler Aborted (core dumped)
~~~~~~~~~~~~~~~~~~

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#4 lore raw
Quoted 13 lines · Namhyung Kim
Quoted 12 lines · Milian Wolff
Quoted 11 lines · CEST Namhyung Kim

Hi Milian,

Quoted 7 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various other sample fields from a python (and perl?) script using perf script. My use-
case would be analyzing a perf data file created by perf inject --sched-stat.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

Sorry for late reply. Oh, you wanted to access period of tracepoint but I guess it's always 1, no?

OK, it seems sched_stat events have periods bigger than 1. But you can access it using 'runtime' or 'delay' field in the event too. I think sample data of tracepoint event is mostly meaningless and you should access to the event fields for useful information. That's why it doesn't provide the info for tracepoint events, I guess.

What do you specifically want to know from sample data?

Thanks,
Namhyung

Quoted 34 lines
Quoted 32 lines

~~~~~~~~~~~~~~~~~~
/tmp$ cat perf-script.py

import os
import sys

sys.path.append(os.environ['PERF_EXEC_PATH'] + \
'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')

from perf_trace_context import *
from Core import *

def trace_unhandled(event_name, context, event_fields_dict):
print event_fields_dict['sample']['period']

/tmp$ perf script -s perf-script.py
Traceback (most recent call last):
File "perf-script.py", line 11, in trace_unhandled
print event_fields_dict['sample']['period']
KeyError: 'sample'
Fatal Python error: problem in Python trace event handler Aborted (core dumped)
~~~~~~~~~~~~~~~~~~

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#5 lore raw
Quoted 23 lines · On Freitag, 19. August 2016 10:51:01 CEST Namhyung Kim wrote:
Quoted 15 lines · Namhyung Kim
Quoted 14 lines · Milian Wolff
Quoted 13 lines · CEST Namhyung Kim

Hi Milian,

Quoted 9 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various other
sample fields from a python (and perl?) script using perf script. My
use-
case would be analyzing a perf data file created by perf inject --sched-stat.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

Sorry for late reply. Oh, you wanted to access period of tracepoint but I guess it's always 1, no?

Quoted 7 lines

OK, it seems sched_stat events have periods bigger than 1. But you can access it using 'runtime' or 'delay' field in the event too. I think sample data of tracepoint event is mostly meaningless and you should access to the event fields for useful information. That's why it doesn't provide the info for tracepoint events, I guess.

What do you specifically want to know from sample data?

Using perf inject --sched-stat, I can get the sched_stat event period to the sched_switch event (which has the proper backtrace). That is the data I want to access. This is doable when I run:

perf script -F event,period

But it is not accessible from Perl or Python.

Note: the runtime/delay fields are not translated by perf inject, it simply sets the period which is then used by the other perf tools.

I really think it should be made possible to access that data from Python/ Perl.

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
#6 lore raw

Hello Milian,

Quoted 38 lines · Milian Wolff
Quoted 23 lines · On Freitag, 19. August 2016 10:51:01 CEST Namhyung Kim wrote:
Quoted 15 lines · Namhyung Kim
Quoted 14 lines · Milian Wolff
Quoted 13 lines · CEST Namhyung Kim

Hi Milian,

Quoted 9 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various other
sample fields from a python (and perl?) script using perf script. My
use-
case would be analyzing a perf data file created by perf inject --sched-stat.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

Sorry for late reply. Oh, you wanted to access period of tracepoint but I guess it's always 1, no?

Quoted 7 lines

OK, it seems sched_stat events have periods bigger than 1. But you can access it using 'runtime' or 'delay' field in the event too. I think sample data of tracepoint event is mostly meaningless and you should access to the event fields for useful information. That's why it doesn't provide the info for tracepoint events, I guess.

What do you specifically want to know from sample data?

Using perf inject --sched-stat, I can get the sched_stat event period to the sched_switch event (which has the proper backtrace). That is the data I want to access. This is doable when I run:

perf script -F event,period

Quoted 7 lines

But it is not accessible from Perl or Python.

Note: the runtime/delay fields are not translated by perf inject, it simply sets the period which is then used by the other perf tools.

I really think it should be made possible to access that data from Python/ Perl.

As I said, what you really want is 'runtime' or 'delay' and they are already accessible from scripts. The perf inject uses the period directly since it knows it's same as the value of the fields but I'm not sure it's the right way to do. Anyway, if you want to make the period accessible from scripts, you can add it in util/scripting-engines/trace-event-python.c:python_process_tracepoint() and the perl counterpart.

Thanks,
Namhyung

#7 lore raw
Quoted 1 line · On Dienstag, 23. August 2016 11:42:54 CEST Namhyung Kim wrote:

Hello Milian,

Hey Namhyung,

Quoted 47 lines · Milian Wolff
Quoted 46 lines · On Freitag, 19. August 2016 10:51:01 CEST Namhyung Kim wrote:
Quoted 25 lines · Namhyung Kim
Quoted 17 lines · Milian Wolff
Quoted 16 lines · CEST Namhyung Kim
Quoted 15 lines

Hi Milian,

Quoted 11 lines · Milian Wolff

Hey all,

It doesn't seem to be possible to access the period, tid and various
other
sample fields from a python (and perl?) script using perf script.
My
use-
case would be analyzing a perf data file created by perf inject --sched-stat.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I think you can access param_dict["sample"]["period"].

No, that is not possible, but would be a viable solution as well.

Sorry for late reply. Oh, you wanted to access period of tracepoint but I guess it's always 1, no?

Quoted 7 lines

OK, it seems sched_stat events have periods bigger than 1. But you can access it using 'runtime' or 'delay' field in the event too. I think sample data of tracepoint event is mostly meaningless and you should access to the event fields for useful information. That's why it doesn't provide the info for tracepoint events, I guess.

What do you specifically want to know from sample data?

Using perf inject --sched-stat, I can get the sched_stat event period to the sched_switch event (which has the proper backtrace). That is the data I want to access. This is doable when I run:

perf script -F event,period

But it is not accessible from Perl or Python.

Note: the runtime/delay fields are not translated by perf inject, it simply
sets the period which is then used by the other perf tools.

Quoted 7 lines
Quoted 2 lines

I really think it should be made possible to access that data from Python/ Perl.

As I said, what you really want is 'runtime' or 'delay' and they are already accessible from scripts. The perf inject uses the period directly since it knows it's same as the value of the fields but I'm not sure it's the right way to do.

If we don't store it in the event's period field, then we would not be able to use either of the following in perf report:

--show-total-period
-g period

I think this would make this feature less intuitive to use, no?

Quoted 1 line

Anyway, if you want to make the period accessible from scripts, you can add

it in util/scripting-engines/trace-event-python.c:python_process_tracepoint()

Quoted 1 line

and the perl counterpart.

OK, I will provide a patch for that. Would it be OK to simply make the whole sample accessible, just like it already is for non-tracepoint events?

Cheers

Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts