History

perf scripts python: Add trace end processing and PRODUCT and CATEGORIES information

The final output will now be presented in JSON format following the Gecko profile structure. Additionally, the inclusion of PRODUCT allows easy retrieval of header information for UI. Furthermore, CATEGORIES have been introduced to enable customization of kernel and user colors using input arguments. To facilitate this functionality, an argparse-based parser has been implemented. Note: The implementation of threads will be addressed in subsequent commits for now I have commented it out.

Anup Sharma committed 3 years ago

Commit 833daec1 parent 5aacd7f1 file · +64 −1Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/gecko.py+64−1
@@ -11,6 +11,8 @@ · 10 unmodified lines
1111
1212 import os
1313 import sys
14+import json
15+import argparse
1416 from dataclasses import dataclass, field
1517 from typing import List, Dict, Optional, NamedTuple, Set, Tuple, Any
1618
@@ -30,6 +32,13 @@ Milliseconds = float · 13 unmodified lines
3032 # start_time is intialiazed only once for the all event traces.
3133 start_time = None
3234
35+# https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/profile.js#L425
36+# Follow Brendan Gregg's Flamegraph convention: orange for kernel and yellow for user space by default.
37+CATEGORIES = None
38+
39+# The product name is used by the profiler UI to show the Operating system and Processor.
40+PRODUCT = os.popen('uname -op').read().strip()
41+
3342 # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156
3443 class Frame(NamedTuple):
3544 string_id: StringID
@@ -171,4 +180,58 @@ def process_event(param_dict: Dict) -> None: · 135 unmodified lines
171180 # Trace_end runs at the end and will be used to aggregate
172181 # the data into the final json object and print it out to stdout.
173182 def trace_end() -> None:
174− pass
183+ # Schema: https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L305
184+ gecko_profile_with_meta = {
185+ "meta": {
186+ "interval": 1,
187+ "processType": 0,
188+ "product": PRODUCT,
189+ "stackwalk": 1,
190+ "debug": 0,
191+ "gcpoison": 0,
192+ "asyncstack": 1,
193+ "startTime": start_time,
194+ "shutdownTime": None,
195+ "version": 24,
196+ "presymbolicated": True,
197+ "categories": CATEGORIES,
198+ "markerSchema": [],
199+ },
200+ "libs": [],
201+ # threads will be implemented in later commits.
202+ # "threads": threads,
203+ "processes": [],
204+ "pausedRanges": [],
205+ }
206+ json.dump(gecko_profile_with_meta, sys.stdout, indent=2)
207+
208+def main() -> None:
209+ global CATEGORIES
210+ parser = argparse.ArgumentParser(description="Convert perf.data to Firefox\'s Gecko Profile format")
211+
212+ # Add the command-line options
213+ # Colors must be defined according to this:
214+ # https://github.com/firefox-devtools/profiler/blob/50124adbfa488adba6e2674a8f2618cf34b59cd2/res/css/categories.css
215+ parser.add_argument('--user-color', default='yellow', help='Color for the User category')
216+ parser.add_argument('--kernel-color', default='orange', help='Color for the Kernel category')
217+ # Parse the command-line arguments
218+ args = parser.parse_args()
219+ # Access the values provided by the user
220+ user_color = args.user_color
221+ kernel_color = args.kernel_color
222+
223+ CATEGORIES = [
224+ {
225+ "name": 'User',
226+ "color": user_color,
227+ "subcategories": ['Other']
228+ },
229+ {
230+ "name": 'Kernel',
231+ "color": kernel_color,
232+ "subcategories": ['Other']
233+ },
234+ ]
235+
236+if __name__ == '__main__':
237+ main()