[PATCH v3 5/6] scripts: python: Implement add sample function and return finish

Anup Sharma posted 6 patches 2 years, 5 months ago
There is a newer version of this series
[PATCH v3 5/6] scripts: python: Implement add sample function and return finish
Posted by Anup Sharma 2 years, 5 months ago
The addSample function appends a new entry to the 'samples' data structure.

The finish function generates a dictionary containing various profile
information such as 'tid', 'pid', 'name', 'markers', 'samples',
'frameTable', 'stackTable', 'stringTable', 'registerTime',
'unregisterTime', and 'processType'.

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

diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
index 39818a603265..6c934de1f608 100644
--- a/tools/perf/scripts/python/firefox-gecko-converter.py
+++ b/tools/perf/scripts/python/firefox-gecko-converter.py
@@ -106,11 +106,36 @@ def process_event(param_dict):
 		}
 		stringTable = []
 
+		def addSample(threadName, stackArray, time):
+			responsiveness = 0
+			samples['data'].append([stack, time, responsiveness])
+
+		def finish():
+			return {
+				"tid": tid,
+				"pid": pid,
+				"name": name,
+				"markers": markers,
+				"samples": samples,
+				"frameTable": frameTable,
+				"stackTable": stackTable,
+				"stringTable": stringTable,
+				"registerTime": 0,
+				"unregisterTime": None,
+				"processType": 'default'
+			}
+
+		return {
+			"addSample": addSample,
+			"finish": finish
+		}
+
 	def _addThreadSample(pid, tid, threadName, time_stamp, stack):
 		thread = thread_map.get(tid)
 		if not thread:
 			thread = _createThread(threadName, pid, tid)
 			thread_map[tid] = thread
+		thread['addSample'](threadName, stack, time_stamp)
 
 	# Extract relevant information from the event parameters. The event parameters
 	# are in a dictionary:
-- 
2.34.1
Re: [PATCH v3 5/6] scripts: python: Implement add sample function and return finish
Posted by Ian Rogers 2 years, 5 months ago
On Mon, Jul 10, 2023 at 4:14 PM Anup Sharma <anupnewsmail@gmail.com> wrote:
>
> The addSample function appends a new entry to the 'samples' data structure.
>
> The finish function generates a dictionary containing various profile
> information such as 'tid', 'pid', 'name', 'markers', 'samples',
> 'frameTable', 'stackTable', 'stringTable', 'registerTime',
> 'unregisterTime', and 'processType'.
>
> Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
> ---
>  .../scripts/python/firefox-gecko-converter.py | 25 +++++++++++++++++++
>  1 file changed, 25 insertions(+)
>
> diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
> index 39818a603265..6c934de1f608 100644
> --- a/tools/perf/scripts/python/firefox-gecko-converter.py
> +++ b/tools/perf/scripts/python/firefox-gecko-converter.py
> @@ -106,11 +106,36 @@ def process_event(param_dict):
>                 }
>                 stringTable = []
>
> +               def addSample(threadName, stackArray, time):

I think these aren't following general naming conventions:
https://peps.python.org/pep-0008/#function-and-variable-names
So use thread_name, stack_array.

> +                       responsiveness = 0
> +                       samples['data'].append([stack, time, responsiveness])
> +
> +               def finish():
> +                       return {
> +                               "tid": tid,
> +                               "pid": pid,
> +                               "name": name,
> +                               "markers": markers,
> +                               "samples": samples,
> +                               "frameTable": frameTable,
> +                               "stackTable": stackTable,
> +                               "stringTable": stringTable,
> +                               "registerTime": 0,
> +                               "unregisterTime": None,
> +                               "processType": 'default'
> +                       }
> +
> +               return {
> +                       "addSample": addSample,
> +                       "finish": finish
> +               }

I think the use of a dictionary here isn't idiomatic. Rather than use
a dictionary I think you can make a class Thread, then have functions
passed self called addSample and finish. So:

class Thread:
  def addSample(self, thread_name: str, stack_array: list[...], time: int):
     responsiveness = 0
     self.samples['data'] ...
...
thread.addSample(threadName, stack, time_stamp)

Should samples be its own class here?

Thanks,
Ian

> +
>         def _addThreadSample(pid, tid, threadName, time_stamp, stack):
>                 thread = thread_map.get(tid)
>                 if not thread:
>                         thread = _createThread(threadName, pid, tid)
>                         thread_map[tid] = thread
> +               thread['addSample'](threadName, stack, time_stamp)
>
>         # Extract relevant information from the event parameters. The event parameters
>         # are in a dictionary:
> --
> 2.34.1
>
Re: [PATCH v3 5/6] scripts: python: Implement add sample function and return finish
Posted by Anup Sharma 2 years, 4 months ago
On Wed, Jul 12, 2023 at 10:35:16AM -0700, Ian Rogers wrote:
> On Mon, Jul 10, 2023 at 4:14 PM Anup Sharma <anupnewsmail@gmail.com> wrote:
> >
> > The addSample function appends a new entry to the 'samples' data structure.
> >
> > The finish function generates a dictionary containing various profile
> > information such as 'tid', 'pid', 'name', 'markers', 'samples',
> > 'frameTable', 'stackTable', 'stringTable', 'registerTime',
> > 'unregisterTime', and 'processType'.
> >
> > Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
> > ---
> >  .../scripts/python/firefox-gecko-converter.py | 25 +++++++++++++++++++
> >  1 file changed, 25 insertions(+)
> >
> > diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
> > index 39818a603265..6c934de1f608 100644
> > --- a/tools/perf/scripts/python/firefox-gecko-converter.py
> > +++ b/tools/perf/scripts/python/firefox-gecko-converter.py
> > @@ -106,11 +106,36 @@ def process_event(param_dict):
> >                 }
> >                 stringTable = []
> >
> > +               def addSample(threadName, stackArray, time):
> 
> I think these aren't following general naming conventions:
> https://peps.python.org/pep-0008/#function-and-variable-names
> So use thread_name, stack_array.

Noted. Will fix in v4.

> > +                       responsiveness = 0
> > +                       samples['data'].append([stack, time, responsiveness])
> > +
> > +               def finish():
> > +                       return {
> > +                               "tid": tid,
> > +                               "pid": pid,
> > +                               "name": name,
> > +                               "markers": markers,
> > +                               "samples": samples,
> > +                               "frameTable": frameTable,
> > +                               "stackTable": stackTable,
> > +                               "stringTable": stringTable,
> > +                               "registerTime": 0,
> > +                               "unregisterTime": None,
> > +                               "processType": 'default'
> > +                       }
> > +
> > +               return {
> > +                       "addSample": addSample,
> > +                       "finish": finish
> > +               }
> 
> I think the use of a dictionary here isn't idiomatic. Rather than use
> a dictionary I think you can make a class Thread, then have functions
> passed self called addSample and finish. So:

agreed.

> class Thread:
>   def addSample(self, thread_name: str, stack_array: list[...], time: int):
>      responsiveness = 0
>      self.samples['data'] ...
> ...
> thread.addSample(threadName, stack, time_stamp)
>
> Should samples be its own class here?

I have changed the code to use object oriented approach by taking
reference from simpleperf. I will make class Thread and Sample.

> Thanks,
> Ian
> 
> > +
> >         def _addThreadSample(pid, tid, threadName, time_stamp, stack):
> >                 thread = thread_map.get(tid)
> >                 if not thread:
> >                         thread = _createThread(threadName, pid, tid)
> >                         thread_map[tid] = thread
> > +               thread['addSample'](threadName, stack, time_stamp)
> >
> >         # Extract relevant information from the event parameters. The event parameters
> >         # are in a dictionary:
> > --
> > 2.34.1
> >