[PATCH v2 7/7] scripts: python: Add trace end processing and JSON output

From: Anup Sharma
Date: Wed Jul 05 2023 - 15:50:08 EST


Inside the trace end function the final output will be dumped
to standard output in JSON gecko format. Additionally, constants
such as USER_CATEGORY_INDEX, KERNEL_CATEGORY_INDEX, CATEGORIES, and
PRODUCT are defined to provide contextual information.
Also added _addThreadSample call which was missing.

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

diff --git a/tools/perf/scripts/python/firefox-gecko-converter.py b/tools/perf/scripts/python/firefox-gecko-converter.py
index 910e598c743f..6a2a4d816799 100644
--- a/tools/perf/scripts/python/firefox-gecko-converter.py
+++ b/tools/perf/scripts/python/firefox-gecko-converter.py
@@ -18,9 +18,47 @@ sys.path.append(os.environ['PERF_EXEC_PATH'] + \
from perf_trace_context import *
from Core import *

+USER_CATEGORY_INDEX = 0
+KERNEL_CATEGORY_INDEX = 1
thread_map = {}
start_time = None

+CATEGORIES = [
+ {'name': 'User', 'color': 'yellow', 'subcategories': ['Other']},
+ {'name': 'Kernel', 'color': 'orange', 'subcategories': ['Other']}
+]
+
+PRODUCT = os.popen('uname -op').read().strip()
+
+def trace_end():
+ thread_array = list(map(lambda thread: thread['finish'](), thread_map.values()))
+ for thread in thread_array:
+ key = thread['samples']['schema']['time']
+ thread['samples']['data'].sort(key=lambda data : float(data[key]))
+
+ result = {
+ 'meta': {
+ 'interval': 1,
+ 'processType': 0,
+ 'product': PRODUCT,
+ 'stackwalk': 1,
+ 'debug': 0,
+ 'gcpoison': 0,
+ 'asyncstack': 1,
+ 'startTime': start_time,
+ 'shutdownTime': None,
+ 'version': 24,
+ 'presymbolicated': True,
+ 'categories': CATEGORIES,
+ 'markerSchema': []
+ },
+ 'libs': [],
+ 'threads': thread_array,
+ 'processes': [],
+ 'pausedRanges': []
+ }
+ json.dump(result, sys.stdout, indent=2)
+
def process_event(param_dict):
global start_time
global thread_map
@@ -159,6 +197,8 @@ def process_event(param_dict):
stack.append(call['sym']['name'] + f' (in {call["dso"]})')
if len(stack) != 0:
stack = stack[::-1]
+ _addThreadSample(pid, tid, thread_name, time_stamp, stack)
else:
mod = param_dict['symbol'] if 'symbol' in param_dict else '[unknown]'
dso = param_dict['dso'] if 'dso' in param_dict else '[unknown]'
+ _addThreadSample(pid, tid, thread_name, time_stamp, [mod + f' (in {dso})'])
--
2.34.1