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 re
6import tempfile
7
8from telemetry.core.platform import profiler
9
10
11class V8Profiler(profiler.Profiler):
12
13  _V8_ARG = '--js-flags=--logfile=%s --prof --log-timer-events'
14
15  @classmethod
16  def name(cls):
17    return 'v8'
18
19  @classmethod
20  def is_supported(cls, browser_type):
21    return not browser_type.startswith('cros')
22
23  @classmethod
24  def CustomizeBrowserOptions(cls, browser_type, options):
25    if browser_type.startswith('android'):
26      dump_file = '/data/local/tmp/v8-profile.log'
27    else:
28      dump_file = tempfile.mkstemp()[1]
29    options.AppendExtraBrowserArgs([cls._V8_ARG % dump_file, '--no-sandbox'])
30
31  def CollectProfile(self):
32    # Find output filename from browser argument.
33    for i in self._browser_backend.browser_options.extra_browser_args:
34      match = re.match(self._V8_ARG % '(\S+)', i)
35      if match:
36        output_file = match.groups(0)[0]
37    assert output_file
38    # On Android pull the output file to the host.
39    if self._platform_backend.GetOSName() == 'android':
40      host_output_file = '%s.log' % self._output_path
41      self._browser_backend.adb.device().old_interface.Adb().Pull(
42          output_file, host_output_file)
43      # Clean the device
44      self._browser_backend.adb.device().RunShellCommand('rm %s' % output_file)
45      output_file = host_output_file
46    print 'V8 profile saved as %s' % output_file
47    print 'To view, open in ' \
48          'http://v8.googlecode.com/svn/trunk/tools/tick-processor.html'
49    return [output_file]
50