• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright (c) 2015 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import contextlib
8import logging
9import os
10import unittest
11
12from systrace import decorators
13from systrace import run_systrace
14from systrace import util
15from systrace.tracing_agents import atrace_agent
16
17from devil.android import device_utils
18from devil.android.sdk import intent
19from py_utils import tempfile_ext
20
21
22DEVICE_SERIAL = 'AG8404EC0444AGC'
23ATRACE_ARGS = ['atrace', '-z', '-t', '10', '-b', '4096']
24CATEGORIES = ['sched', 'gfx', 'view', 'wm']
25ADB_SHELL = ['adb', '-s', DEVICE_SERIAL, 'shell']
26
27SYSTRACE_CMD = ['./run_systrace.py', '--time', '10', '-o', 'out.html', '-e',
28                DEVICE_SERIAL] + CATEGORIES
29TRACE_ARGS = (ATRACE_ARGS + CATEGORIES)
30
31TEST_DIR = os.path.join(os.path.dirname(__file__), os.pardir, 'test_data')
32ATRACE_DATA = os.path.join(TEST_DIR, 'atrace_data')
33ATRACE_DATA_RAW = os.path.join(TEST_DIR, 'atrace_data_raw')
34ATRACE_DATA_STRIPPED = os.path.join(TEST_DIR, 'atrace_data_stripped')
35ATRACE_PROCFS_DUMP = os.path.join(TEST_DIR, 'atrace_procfs_dump')
36ATRACE_EXTRACTED_TGIDS = os.path.join(TEST_DIR, 'atrace_extracted_tgids')
37ATRACE_MISSING_TGIDS = os.path.join(TEST_DIR, 'atrace_missing_tgids')
38ATRACE_FIXED_TGIDS = os.path.join(TEST_DIR, 'atrace_fixed_tgids')
39
40
41class AtraceAgentTest(unittest.TestCase):
42
43  # TODO(washingtonp): These end-to-end tests do not work on the Trybot server
44  # because adb cannot be found on the Trybot servers. Figure out what the
45  # issue is and update this test.
46  @decorators.Disabled
47  def test_tracing(self):
48    TRACE_BUFFER_SIZE = '16384'
49    TRACE_TIME = '5'
50
51    devices = device_utils.DeviceUtils.HealthyDevices()
52    package_info = util.get_supported_browsers()['stable']
53    device = devices[0]
54    with tempfile_ext.TemporaryFileName() as output_file_name:
55      # Launch the browser before tracing.
56      device.StartActivity(
57          intent.Intent(activity=package_info.activity,
58                        package=package_info.package,
59                        data='about:blank',
60                        extras={'create_new_tab': True}),
61          blocking=True, force_stop=True)
62
63      # Run atrace agent.
64      run_systrace.main_impl(['./run_systrace.py',
65                              '-b',
66                              TRACE_BUFFER_SIZE,
67                              '-t',
68                              TRACE_TIME,
69                              '-o',
70                              output_file_name,
71                              '-e',
72                              str(device),
73                              '--atrace-categories=gfx,input,view'])
74
75      # Verify results.
76      with open(output_file_name, 'r') as f:
77        full_trace = f.read()
78      self.assertTrue('CPU#' in full_trace)
79
80  @decorators.HostOnlyTest
81  def test_construct_atrace_args(self):
82    options, categories = run_systrace.parse_options(SYSTRACE_CMD)
83    options.atrace_categories = categories
84    tracer_args = atrace_agent._construct_atrace_args(options, categories)
85    self.assertEqual(' '.join(TRACE_ARGS), ' '.join(tracer_args))
86
87  @decorators.HostOnlyTest
88  def test_strip_and_decompress_trace(self):
89    with contextlib.nested(open(ATRACE_DATA_RAW, 'r'),
90                           open(ATRACE_DATA_STRIPPED, 'r')) as (f1, f2):
91      atrace_data_raw = f1.read()
92      atrace_data_stripped = f2.read()
93
94      trace_data = atrace_agent.strip_and_decompress_trace(atrace_data_raw)
95      self.assertEqual(atrace_data_stripped, trace_data)
96
97  @decorators.HostOnlyTest
98  def test_extract_tgids(self):
99    with contextlib.nested(open(ATRACE_PROCFS_DUMP, 'r'),
100                           open(ATRACE_EXTRACTED_TGIDS, 'r')) as (f1, f2):
101
102      atrace_procfs_dump = f1.read()
103      atrace_procfs_extracted = f2.read()
104
105      tgids = eval(atrace_procfs_extracted)
106      result = atrace_agent.extract_tgids(atrace_procfs_dump.splitlines())
107
108      self.assertEqual(result, tgids)
109
110  @decorators.HostOnlyTest
111  def test_fix_missing_tgids(self):
112    with contextlib.nested(open(ATRACE_EXTRACTED_TGIDS, 'r'),
113                           open(ATRACE_MISSING_TGIDS, 'r'),
114                           open(ATRACE_FIXED_TGIDS, 'r')) as (f1, f2, f3):
115
116      atrace_data = f2.read()
117      tgid_map = eval(f1.read())
118      fixed = f3.read()
119
120      res = atrace_agent.fix_missing_tgids(atrace_data, tgid_map)
121      self.assertEqual(res, fixed)
122
123
124if __name__ == "__main__":
125  logging.getLogger().setLevel(logging.DEBUG)
126  unittest.main(verbosity=2)
127