#!/usr/bin/env python
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Merges Snapdragon profiler data into a Chrome trace.

Snapdragon is a series of SoCs for mobile devices designed by Qualcomm.
Snapdragon Profiler is a software written by Qualcomm that knows how to extract
a wide range of information from Snapdragon chips, e.g. the GPU. This tool helps
to align Snapdragon Profiler information with internal Chrome traces to get a
better insight on how changes to Chrome affect the system.

To obtain a trace with Snapdragon profiler data,

1- Run the Snapdragon profiler, connect to a device, and start capturing
real-time data.

2- While Snapdragon profiler is capturing real-time data, capture a chrome trace
(or many traces), either from chrome://inspect/?tracing or using Telemetry.

3- Stop real-time capturing in Snapdragon profiler and save the result in a CSV
file.

4- Run this script. For example:
   ./snapdragon2trace sd.csv amazon_mobile.html merged_trace.html
"""

import argparse
import codecs
import sys
import os

tracing_path = os.path.abspath(os.path.join(
  os.path.dirname(os.path.realpath(__file__)), '..'))
sys.path.append(tracing_path)
from tracing_build import snapdragon2trace


def main():
  parser = argparse.ArgumentParser(description='add Snapdragon profiler data '
                                   'to a trace.', add_help=False)
  parser.add_argument('snapdragon_csv', metavar='SNAPDRAGON_CSV',
                      help='Snapdragon CSV file path (input).')
  parser.add_argument('chrome_trace', metavar='CHROME_TRACE',
                      help='Chrome trace file path (input). Supported '
                           'extensions are .gz, .html, and .json.')
  parser.add_argument('output', metavar='OUTPUT',
                      help='Output file path. Supported extensions are .gz, '
                           '.html, and .json.')
  parser.add_argument('-h', '--help', action='help',
                      help='Show this help message and exit.')
  args = parser.parse_args()

  traces = snapdragon2trace.LoadTraces(args.chrome_trace)
  csv = snapdragon2trace.LoadCSV(args.snapdragon_csv)
  snapdragon2trace.AddSnapdragonProfilerData(traces, csv)
  snapdragon2trace.WriteTraces(args.output, traces)


if __name__ == '__main__':
  sys.exit(main())
