149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#!/usr/bin/env python
249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#
349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# Copyright (C) 2015 The Android Open Source Project
449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#
549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# Licensed under the Apache License, Version 2.0 (the "License");
649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# you may not use this file except in compliance with the License.
749b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# You may obtain a copy of the License at
849b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#
949b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#      http://www.apache.org/licenses/LICENSE-2.0
1049b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#
1149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# Unless required by applicable law or agreed to in writing, software
1249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# distributed under the License is distributed on an "AS IS" BASIS,
1349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# See the License for the specific language governing permissions and
1549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen# limitations under the License.
1649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen#
1749b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
1849b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenenimport argparse
1949b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenenimport os
2049b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenenimport subprocess
2149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenenimport sys
2249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
2349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenendef main():
2449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    # Create argument parser
2549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    parser = argparse.ArgumentParser()
2649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    parser.add_argument('systrace_file', metavar='SYSTRACE_FILE', help='systrace file to analyze')
2749b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    parser.add_argument('-e', metavar='EVENT_LOG', help='android event log file')
2849b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    args = parser.parse_args()
2949b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
3049b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    this_script_path = os.path.dirname(os.path.realpath(__file__))
3149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
3249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    # Find chromium-trace directory and vinn binary as offset from this script
3349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    chromium_trace_path = os.path.normpath(this_script_path + '/../../../external/chromium-trace')
3449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    if not os.path.exists(chromium_trace_path):
3549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen        sys.exit('Can\'t find chromium-trace in your source tree')
3649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
3749b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    vinn_path = chromium_trace_path + '/catapult/third_party/vinn/'
3849b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    if not os.path.exists(vinn_path):
3949b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen        sys.exit('Can\'t find vinn in your source tree')
4049b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
4149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    sys.path.append(vinn_path)
4249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    import vinn
4349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
4449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    # Find source paths and construct vinn launch arguments
4549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    tracing_path = chromium_trace_path + '/catapult/tracing/'
4649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    gldist_path = chromium_trace_path + '/catapult/tracing/third_party/gl-matrix/dist/'
4749b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    source_paths_arg = [tracing_path, gldist_path]
4849b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    js_args_arg = [args.systrace_file]
4949b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    if args.e is not None:
5049b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen        js_args_arg += [args.e]
5149b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    res = vinn.RunFile(this_script_path + '/analysis.html', source_paths=source_paths_arg,
5249b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen            js_args=js_args_arg, stdout=sys.stdout, stdin=sys.stdin);
5349b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    return res.returncode
5449b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen
5549b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenenif __name__ == '__main__':
5649b1535fdf6cd9ab74047d54c9028525c04adda8Martijn Coenen    main()
57