1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import re
7import tempfile
8
9from telemetry.internal.platform import profiler
10
11
12class V8Profiler(profiler.Profiler):
13
14  _V8_ARG = '--js-flags=--logfile=%s --prof --log-timer-events'
15
16  @classmethod
17  def name(cls):
18    return 'v8'
19
20  @classmethod
21  def is_supported(cls, browser_type):
22    return not browser_type.startswith('cros')
23
24  @classmethod
25  def CustomizeBrowserOptions(cls, browser_type, options):
26    if browser_type.startswith('android'):
27      dump_file = '/data/local/tmp/v8-profile.log'
28    else:
29      dump_file = tempfile.mkstemp()[1]
30    options.AppendExtraBrowserArgs([cls._V8_ARG % dump_file, '--no-sandbox'])
31
32  def CollectProfile(self):
33    # Find output filename from browser argument.
34    for i in self._browser_backend.browser_options.extra_browser_args:
35      match = re.match(self._V8_ARG % r'(\S+)', i)
36      if match:
37        output_file = match.groups(0)[0]
38    assert output_file
39    # On Android pull the output file to the host.
40    if self._platform_backend.GetOSName() == 'android':
41      host_output_file = '%s.log' % self._output_path
42      try:
43        self._browser_backend.device.PullFile(
44            output_file, host_output_file)
45      except:
46        logging.exception('New exception caused by DeviceUtils conversion')
47        raise
48      # Clean the device
49      self._browser_backend.device.RunShellCommand('rm %s' % output_file)
50      output_file = host_output_file
51    print 'V8 profile saved as %s' % output_file
52    print 'To view, open in ' \
53          'http://v8.googlecode.com/svn/trunk/tools/tick-processor.html'
54    return [output_file]
55