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.
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -11,6 +11,8 @@ · 10 unmodified lines | ||
| 11 | 11 | |
| 12 | 12 | import os |
| 13 | 13 | import sys |
| 14 | +import json | |
| 15 | +import argparse | |
| 14 | 16 | from dataclasses import dataclass, field |
| 15 | 17 | from typing import List, Dict, Optional, NamedTuple, Set, Tuple, Any |
| 16 | 18 | |
| @@ -30,6 +32,13 @@ Milliseconds = float · 13 unmodified lines | ||
| 30 | 32 | # start_time is intialiazed only once for the all event traces. |
| 31 | 33 | start_time = None |
| 32 | 34 | |
| 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 | + | |
| 33 | 42 | # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156 |
| 34 | 43 | class Frame(NamedTuple): |
| 35 | 44 | string_id: StringID |
| @@ -171,4 +180,58 @@ def process_event(param_dict: Dict) -> None: · 135 unmodified lines | ||
| 171 | 180 | # Trace_end runs at the end and will be used to aggregate |
| 172 | 181 | # the data into the final json object and print it out to stdout. |
| 173 | 182 | 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() | |