• 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            'sched'
29            ]
30
31    def set_scaling_governor_mode(self, index, mode):
32        """Set mode of CPU scaling governor on one CPU.
33
34        @param index: CPU index starting from 0.
35
36        @param mode: Mode of scaling governor, accept 'interactive' or
37                     'performance'.
38
39        @returns: The original mode.
40
41        """
42        if mode not in self.SCALING_GOVERNOR_MODES:
43            raise SystemFacadeNativeError('mode %s is invalid' % mode)
44
45        governor_path = os.path.join(
46                '/sys/devices/system/cpu/cpu%d' % index,
47                'cpufreq/scaling_governor')
48        if not os.path.exists(governor_path):
49            raise SystemFacadeNativeError(
50                    'scaling governor of CPU %d is not available' % index)
51
52        original_mode = utils.read_one_line(governor_path)
53        utils.open_write_close(governor_path, mode)
54
55        return original_mode
56
57
58    def get_cpu_usage(self):
59        """Returns machine's CPU usage.
60
61        Returns:
62            A dictionary with 'user', 'nice', 'system' and 'idle' values.
63            Sample dictionary:
64            {
65                'user': 254544,
66                'nice': 9,
67                'system': 254768,
68                'idle': 2859878,
69            }
70        """
71        return utils.get_cpu_usage()
72
73
74    def compute_active_cpu_time(self, cpu_usage_start, cpu_usage_end):
75        """Computes the fraction of CPU time spent non-idling.
76
77        This function should be invoked using before/after values from calls to
78        get_cpu_usage().
79        """
80        return utils.compute_active_cpu_time(cpu_usage_start,
81                                                  cpu_usage_end)
82
83
84    def get_mem_total(self):
85        """Returns the total memory available in the system in MBytes."""
86        return utils.get_mem_total()
87
88
89    def get_mem_free(self):
90        """Returns the currently free memory in the system in MBytes."""
91        return utils.get_mem_free()
92
93    def get_mem_free_plus_buffers_and_cached(self):
94        """
95        Returns the free memory in MBytes, counting buffers and cached as free.
96
97        This is most often the most interesting number since buffers and cached
98        memory can be reclaimed on demand. Note however, that there are cases
99        where this as misleading as well, for example used tmpfs space
100        count as Cached but can not be reclaimed on demand.
101        See https://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt.
102        """
103        return utils.get_mem_free_plus_buffers_and_cached()
104
105    def get_ec_temperatures(self):
106        """Uses ectool to return a list of all sensor temperatures in Celsius.
107        """
108        return utils.get_ec_temperatures()
109
110    def get_current_temperature_max(self):
111        """
112        Returns the highest reported board temperature (all sensors) in Celsius.
113        """
114        return utils.get_current_temperature_max()
115
116    def get_current_board(self):
117        """Returns the current device board name."""
118        return utils.get_current_board()
119
120
121    def get_chromeos_release_version(self):
122        """Returns chromeos version in device under test as string. None on
123        fail.
124        """
125        return utils.get_chromeos_release_version()
126
127    def get_num_allocated_file_handles(self):
128        """
129        Returns the number of currently allocated file handles.
130        """
131        return utils.get_num_allocated_file_handles()
132
133    def get_storage_statistics(self, device=None):
134        """
135        Fetches statistics for a storage device.
136        """
137        return utils.get_storage_statistics(device)
138