[PATCH v2 2/7] scripts: python: Introduce thread sample processing to create thread

Anup Sharma posted 7 patches 2 years, 3 months ago
There is a newer version of this series
[PATCH v2 2/7] scripts: python: Introduce thread sample processing to create thread
Posted by Anup Sharma 2 years, 3 months ago
The _addThreadSample function is responsible for adding a sample to a specific
thread. It first checks if the thread exists in the thread_map dictionary.
If not, it creates a new thread using the _createtread function and assigns
it to the thread_map. Finally, it calls the 'addSample' method of the thread,
passing the thread name, stack, and timestamp.

Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
---
 .../perf/scripts/python/firefox-gecko-converter.py  | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
index ce663840d212..95b061a97cbc 100644
--- a/tools/perf/scripts/python/firefox-gecko-converter.py
+++ b/tools/perf/scripts/python/firefox-gecko-converter.py
@@ -18,7 +18,20 @@ sys.path.append(os.environ['PERF_EXEC_PATH'] + \
 from perf_trace_context import *
 from Core import *
 
+thread_map = {}
+start_time = None
+
 def process_event(param_dict):
+	global start_time
+	global thread_map
+
+	def _addThreadSample(pid, tid, threadName, time_stamp, stack):
+		thread = thread_map.get(tid)
+		if not thread:
+			thread = _createtread(threadName, pid, tid)
+			thread_map[tid] = thread
+		thread['addSample'](threadName, stack, time_stamp)
+
 	time_stamp = (param_dict['sample']['time'] // 1000) / 1000
 	pid = param_dict['sample']['pid']
 	tid = param_dict['sample']['tid']
-- 
2.34.1
Re: [PATCH v2 2/7] scripts: python: Introduce thread sample processing to create thread
Posted by Namhyung Kim 2 years, 3 months ago
On Wed, Jul 5, 2023 at 12:44 PM Anup Sharma <anupnewsmail@gmail.com> wrote:
>
> The _addThreadSample function is responsible for adding a sample to a specific
> thread. It first checks if the thread exists in the thread_map dictionary.
> If not, it creates a new thread using the _createtread function and assigns
> it to the thread_map. Finally, it calls the 'addSample' method of the thread,
> passing the thread name, stack, and timestamp.
>
> Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
> ---
>  .../perf/scripts/python/firefox-gecko-converter.py  | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
> index ce663840d212..95b061a97cbc 100644
> --- a/tools/perf/scripts/python/firefox-gecko-converter.py
> +++ b/tools/perf/scripts/python/firefox-gecko-converter.py
> @@ -18,7 +18,20 @@ sys.path.append(os.environ['PERF_EXEC_PATH'] + \
>  from perf_trace_context import *
>  from Core import *
>
> +thread_map = {}
> +start_time = None
> +
>  def process_event(param_dict):
> +       global start_time
> +       global thread_map
> +
> +       def _addThreadSample(pid, tid, threadName, time_stamp, stack):
> +               thread = thread_map.get(tid)
> +               if not thread:
> +                       thread = _createtread(threadName, pid, tid)

Shouldn't it be '_createThread'?

> +                       thread_map[tid] = thread
> +               thread['addSample'](threadName, stack, time_stamp)

Why is it like this?  What do you intend with the thread['addSample']
method?  Can it be simpler like a direct function call?

And more importantly, you'd better make each patch work properly.
AFAICS it won't do the job because both _createtread() and
thread['addSample'] are not implemented yet.

You can either move those definitions to this commit or have the
commit implementing them before this one.

Thanks,
Namhyung


> +
>         time_stamp = (param_dict['sample']['time'] // 1000) / 1000
>         pid = param_dict['sample']['pid']
>         tid = param_dict['sample']['tid']
> --
> 2.34.1
>
Re: [PATCH v2 2/7] scripts: python: Introduce thread sample processing to create thread
Posted by Anup Sharma 2 years, 3 months ago
On Wed, Jul 05, 2023 at 10:42:47PM -0700, Namhyung Kim wrote:
> On Wed, Jul 5, 2023 at 12:44 PM Anup Sharma <anupnewsmail@gmail.com> wrote:
> >
> > The _addThreadSample function is responsible for adding a sample to a specific
> > thread. It first checks if the thread exists in the thread_map dictionary.
> > If not, it creates a new thread using the _createtread function and assigns
> > it to the thread_map. Finally, it calls the 'addSample' method of the thread,
> > passing the thread name, stack, and timestamp.
> >
> > Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
> > ---
> >  .../perf/scripts/python/firefox-gecko-converter.py  | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> >
> > diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
> > index ce663840d212..95b061a97cbc 100644
> > --- a/tools/perf/scripts/python/firefox-gecko-converter.py
> > +++ b/tools/perf/scripts/python/firefox-gecko-converter.py
> > @@ -18,7 +18,20 @@ sys.path.append(os.environ['PERF_EXEC_PATH'] + \
> >  from perf_trace_context import *
> >  from Core import *
> >
> > +thread_map = {}
> > +start_time = None
> > +
> >  def process_event(param_dict):
> > +       global start_time
> > +       global thread_map
> > +
> > +       def _addThreadSample(pid, tid, threadName, time_stamp, stack):
> > +               thread = thread_map.get(tid)
> > +               if not thread:
> > +                       thread = _createtread(threadName, pid, tid)
> 
> Shouldn't it be '_createThread'?

Yes, it should be '_createThread'. I will fix it in the next version.

> > +                       thread_map[tid] = thread
> > +               thread['addSample'](threadName, stack, time_stamp)
> 
> Why is it like this?  What do you intend with the thread['addSample']
> method?  Can it be simpler like a direct function call?

The purpose of the addSample function is to append stack frames to the
samples['data'] collection. While it could be implemented as a standalone
function, doing so would increase complexity due to shared properties
among threads such as pid, tid, and threadName. Although a decorator
could potentially address this, it would likely result in code that
is functionally and structurally similar. Alternatively, if addSample
were implemented as a separate function, the shared elements would need
to be repeatedly passed to the function.

> And more importantly, you'd better make each patch work properly.
> AFAICS it won't do the job because both _createtread() and
> thread['addSample'] are not implemented yet.
>
> You can either move those definitions to this commit or have the
> commit implementing them before this one.

Thanks, Preparing commit in series is new to me. I will try to fix
it in the next version.

> Thanks,
> Namhyung
> 
> 
> > +
> >         time_stamp = (param_dict['sample']['time'] // 1000) / 1000
> >         pid = param_dict['sample']['pid']
> >         tid = param_dict['sample']['tid']
> > --
> > 2.34.1
> >