1# Copyright (c) 2013 The Chromium OS 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, os
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10
11CGROUP_DIR = '/sys/fs/cgroup/cpu/chrome_renderers'
12FG_CGROUP_DIR = os.path.join(CGROUP_DIR, 'foreground')
13BG_CGROUP_DIR = os.path.join(CGROUP_DIR, 'background')
14
15class platform_ChromeCgroups(test.test):
16    version = 1
17
18    def _get_cgroup_tasks(self, cgroup_dir):
19        """
20        Returns the set of tasks in a cgroup.
21
22        @param cgroup_dir Directory containing the cgroup.
23        """
24        task_path = os.path.join(cgroup_dir, 'tasks')
25        task_file = open(task_path)
26        if not task_file:
27            raise error.TestError('failed to open %s' % task_path)
28        tasks = set(line.rstrip() for line in task_file.readlines())
29        task_file.close()
30        logging.info('tasks in cgroup %s: %s', cgroup_dir, ','.join(tasks))
31        return tasks
32
33    def run_once(self):
34        """
35        Check that the chrome_renderers cgroups are created and that tasks
36        are placed in them.
37        """
38        with chrome.Chrome() as cr:
39            # Make sure the cgroup directories actually exist.
40            if not os.path.isdir(CGROUP_DIR):
41                raise error.TestFail('chrome_renderers cgroup does not exist')
42            if not os.path.isdir(FG_CGROUP_DIR):
43                raise error.TestFail('foreground cgroup does not exist')
44            if not os.path.isdir(BG_CGROUP_DIR):
45                raise error.TestFail('background cgroup does not exist')
46
47            # Open up two tabs in the same window. One should be in the foreground
48            # while the other is in the background.
49            tab1 = cr.browser.tabs[0]
50            tab1.Navigate('about:blank')
51            tab1.WaitForDocumentReadyStateToBeComplete()
52            tab2 = cr.browser.tabs.New()
53            tab2.Navigate('chrome:system')
54            tab2.WaitForDocumentReadyStateToBeComplete()
55
56            # Make sure the foreground and background cgroups are non-empty.
57            if not self._get_cgroup_tasks(FG_CGROUP_DIR):
58                raise error.TestFail('no tasks in foreground cgroup')
59            if not self._get_cgroup_tasks(BG_CGROUP_DIR):
60                raise error.TestFail('no tasks in background cgroup')
61