• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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
5"""Facade to access the system-related functionality."""
6
7import os
8
9from autotest_lib.client.bin import utils
10
11
12class SystemFacadeNativeError(Exception):
13    """Error in SystemFacadeNative."""
14    pass
15
16
17class SystemFacadeNative(object):
18    """Facede to access the system-related functionality.
19
20    The methods inside this class only accept Python native types.
21
22    """
23    SCALING_GOVERNOR_MODES = [
24            'interactive',
25            'performance',
26            'ondemand',
27            'powersave',
28            ]
29
30    def set_scaling_governor_mode(self, index, mode):
31        """Set mode of CPU scaling governor on one CPU.
32
33        @param index: CPU index starting from 0.
34
35        @param mode: Mode of scaling governor, accept 'interactive' or
36                     'performance'.
37
38        @returns: The original mode.
39
40        """
41        if mode not in self.SCALING_GOVERNOR_MODES:
42            raise SystemFacadeNativeError('mode %s is invalid' % mode)
43
44        governor_path = os.path.join(
45                '/sys/devices/system/cpu/cpu%d' % index,
46                'cpufreq/scaling_governor')
47        if not os.path.exists(governor_path):
48            raise SystemFacadeNativeError(
49                    'scaling governor of CPU %d is not available' % index)
50
51        original_mode = utils.read_one_line(governor_path)
52        utils.open_write_close(governor_path, mode)
53
54        return original_mode
55