Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf scripts python gecko: Launch the profiler UI on the default browser with the appropriate URL

All required libraries have been imported and make sure that none of
them are external dependencies. To achieve this, created a virt env and
verified.

Modified usage information and added combined command.

Modified the main() function to read the --save-only command-line option
and set the output_file variable accordingly.

Modified the trace_end() function to check for the output_file variable.
If it is set, the profiler data is saved to a local file in Gecko
Profile format, or the profiler.firefox.com is opened on the default
browser.

Included trace_begin() to initialize the Firefox Profiler and launch the
default browser to display the profiler.firefox.com.

Added a new function launchFirefox() to start a local server and launch
the profiler UI on the default browser with the appropriate URL.

Created the "CORSRequestHandler" class to enable Cross-Origin Resource
Sharing.

Summary:

This integration now includes a exiting feature to conveniently host the
Gecko Profile data on a local server and open it directly in the default
web browser.

This means that users can now effortlessly visualize and analyze the
profiler results with just a single click.

The addition of the --save-only command-line option allows users to save
the profiler output to a local file in Gecko Profile format, but the
real highlight lies in the capability to seamlessly launch a local
server, making the data accessible to Firefox Profiler via a web
browser.

In addition, it's important to highlight that all data are hosted
locally, eliminating any concerns about data privacy rules and
regulations.

Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
Tested-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/ZNOS0vo58DnVLpD8@yoga
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Anup Sharma and committed by
Arnaldo Carvalho de Melo
f208b2c6 43803cb1

+63 -7
+63 -7
tools/perf/scripts/python/gecko.py
··· 1 - # firefox-gecko-converter.py - Convert perf record output to Firefox's gecko profile format 1 + # gecko.py - Convert perf record output to Firefox's gecko profile format 2 2 # SPDX-License-Identifier: GPL-2.0 3 3 # 4 4 # The script converts perf.data to Gecko Profile Format, ··· 7 7 # Usage: 8 8 # 9 9 # perf record -a -g -F 99 sleep 60 10 - # perf script report gecko > output.json 10 + # perf script report gecko 11 + # 12 + # Combined: 13 + # 14 + # perf script gecko -F 99 -a sleep 60 11 15 12 16 import os 13 17 import sys 18 + import time 14 19 import json 20 + import string 21 + import random 15 22 import argparse 23 + import threading 24 + import webbrowser 25 + import urllib.parse 26 + from os import system 16 27 from functools import reduce 17 28 from dataclasses import dataclass, field 29 + from http.server import HTTPServer, SimpleHTTPRequestHandler, test 18 30 from typing import List, Dict, Optional, NamedTuple, Set, Tuple, Any 19 31 20 32 # Add the Perf-Trace-Util library to the Python path ··· 52 40 # The product name is used by the profiler UI to show the Operating system and Processor. 53 41 PRODUCT = os.popen('uname -op').read().strip() 54 42 43 + # store the output file 44 + output_file = None 45 + 55 46 # Here key = tid, value = Thread 56 47 tid_to_thread = dict() 48 + 49 + # The HTTP server is used to serve the profile to the profiler UI. 50 + http_server_thread = None 57 51 58 52 # The category index is used by the profiler UI to show the color of the flame graph. 59 53 USER_CATEGORY_INDEX = 0 ··· 296 278 tid_to_thread[tid] = thread 297 279 thread._add_sample(comm=comm, stack=stack, time_ms=time_stamp) 298 280 281 + def trace_begin() -> None: 282 + global output_file 283 + if (output_file is None): 284 + print("Staring Firefox Profiler on your default browser...") 285 + global http_server_thread 286 + http_server_thread = threading.Thread(target=test, args=(CORSRequestHandler, HTTPServer,)) 287 + http_server_thread.daemon = True 288 + http_server_thread.start() 289 + 299 290 # Trace_end runs at the end and will be used to aggregate 300 291 # the data into the final json object and print it out to stdout. 301 292 def trace_end() -> None: 293 + global output_file 302 294 threads = [thread._to_json_dict() for thread in tid_to_thread.values()] 303 295 304 296 # Schema: https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L305 ··· 333 305 "processes": [], 334 306 "pausedRanges": [], 335 307 } 336 - json.dump(gecko_profile_with_meta, sys.stdout, indent=2) 308 + # launch the profiler on local host if not specified --save-only args, otherwise print to file 309 + if (output_file is None): 310 + output_file = 'gecko_profile.json' 311 + with open(output_file, 'w') as f: 312 + json.dump(gecko_profile_with_meta, f, indent=2) 313 + launchFirefox(output_file) 314 + time.sleep(1) 315 + print(f'[ perf gecko: Captured and wrote into {output_file} ]') 316 + else: 317 + print(f'[ perf gecko: Captured and wrote into {output_file} ]') 318 + with open(output_file, 'w') as f: 319 + json.dump(gecko_profile_with_meta, f, indent=2) 320 + 321 + # Used to enable Cross-Origin Resource Sharing (CORS) for requests coming from 'https://profiler.firefox.com', allowing it to access resources from this server. 322 + class CORSRequestHandler(SimpleHTTPRequestHandler): 323 + def end_headers (self): 324 + self.send_header('Access-Control-Allow-Origin', 'https://profiler.firefox.com') 325 + SimpleHTTPRequestHandler.end_headers(self) 326 + 327 + # start a local server to serve the gecko_profile.json file to the profiler.firefox.com 328 + def launchFirefox(file): 329 + safe_string = urllib.parse.quote_plus(f'http://localhost:8000/{file}') 330 + url = 'https://profiler.firefox.com/from-url/' + safe_string 331 + webbrowser.open(f'{url}') 337 332 338 333 def main() -> None: 334 + global output_file 339 335 global CATEGORIES 340 - parser = argparse.ArgumentParser(description="Convert perf.data to Firefox\'s Gecko Profile format") 336 + parser = argparse.ArgumentParser(description="Convert perf.data to Firefox\'s Gecko Profile format which can be uploaded to profiler.firefox.com for visualization") 341 337 342 338 # Add the command-line options 343 339 # Colors must be defined according to this: 344 340 # https://github.com/firefox-devtools/profiler/blob/50124adbfa488adba6e2674a8f2618cf34b59cd2/res/css/categories.css 345 - parser.add_argument('--user-color', default='yellow', help='Color for the User category') 346 - parser.add_argument('--kernel-color', default='orange', help='Color for the Kernel category') 341 + parser.add_argument('--user-color', default='yellow', help='Color for the User category', choices=['yellow', 'blue', 'purple', 'green', 'orange', 'red', 'grey', 'magenta']) 342 + parser.add_argument('--kernel-color', default='orange', help='Color for the Kernel category', choices=['yellow', 'blue', 'purple', 'green', 'orange', 'red', 'grey', 'magenta']) 343 + # If --save-only is specified, the output will be saved to a file instead of opening Firefox's profiler directly. 344 + parser.add_argument('--save-only', help='Save the output to a file instead of opening Firefox\'s profiler') 345 + 347 346 # Parse the command-line arguments 348 347 args = parser.parse_args() 349 348 # Access the values provided by the user 350 349 user_color = args.user_color 351 350 kernel_color = args.kernel_color 351 + output_file = args.save_only 352 352 353 353 CATEGORIES = [ 354 354 { ··· 392 336 ] 393 337 394 338 if __name__ == '__main__': 395 - main() 339 + main()