[PATCH 2/9] scripts: python: implement add sample function and return finish function

From: Anup Sharma
Date: Wed Jun 21 2023 - 15:40:13 EST


The main convertPerfScriptProfile function returns a dictionary with
references to the addSample and finish functions, allowing external
code to utilize them for profile conversion. This function has few
more functions which will be added.

The addSample function appends a new entry to the 'samples' data structure.
It takes the thread name, stack array, and time as input parameters.
If the thread name differs from the current name, it updates the name.
The function utilizes the get_or_create_stack and get_or_create_frame
methods to construct the stack structure. Finally, it adds the stack,
time, and responsiveness values to the 'data' list within 'samples'.

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@xxxxxxxxx>
---
.../scripts/python/firefox-gecko-converter.py | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)

diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
index 73a431d0c7d1..2817d4a96269 100644
--- a/tools/perf/scripts/python/firefox-gecko-converter.py
+++ b/tools/perf/scripts/python/firefox-gecko-converter.py
@@ -13,3 +13,36 @@ def isPerfScriptFormat(profile):

firstLine = profile[:profile.index('\n')]
return bool(re.match(r'^\S.*?\s+(?:\d+/)?\d+\s+(?:\d+\d+\s+)?[\d.]+:', firstLine))
+
+def convertPerfScriptProfile(profile):
+
+ def addSample(threadName, stackArray, time):
+ nonlocal name
+ if name != threadName:
+ name = threadName
+ # TODO:
+ # get_or_create_stack will create a new stack if it doesn't exist, or return the existing stack if it does.
+ # get_or_create_frame will create a new frame if it doesn't exist, or return the existing frame if it does.
+ stack = reduce(lambda prefix, stackFrame: get_or_create_stack(get_or_create_frame(stackFrame), prefix), stackArray, None)
+ 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
+ }
--
2.34.1