1# Copyright 2017 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
6import re
7
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import error
10
11_RESULT_STRING = 'Average page access speed: (\d+)'
12
13class hardware_MemoryZRAMThroughput(test.test):
14    """
15    This test uses a large buffer to measure the page access throughput with
16    and without ZRAM.
17    """
18    version = 1
19
20    def initialize(self):
21        utils.system('stop ui', ignore_status=True)
22        self._PATTERN = re.compile(_RESULT_STRING)
23
24    def _log_results(self, key, out):
25        logging.info("test output: %s", out)
26        pages_per_second = 0
27        data_points = 0
28        for line in out.splitlines():
29            result = self._PATTERN.match(line)
30            if result:
31                pages_per_second += float(result.group(1))
32                data_points += 1
33                continue
34
35        if data_points == 0:
36            raise error.TestError('Test results not found')
37
38        # Take the average of all data points. Currently when --fork is
39        # passed to memory-eater, there will be two data points from two
40        # processes.
41        average_pages_per_second = pages_per_second / data_points
42        self.output_perf_value(description=key,
43                               value=average_pages_per_second,
44                               higher_is_better=True,
45                               units='pages_per_sec')
46
47        return average_pages_per_second
48
49    def run_once(self):
50        mem_size = utils.memtotal()
51        swap_size = utils.swaptotal()
52        logging.info("MemTotal: %.0f KB", mem_size)
53        logging.info("SwapTotal: %.0f KB", swap_size)
54
55        # First run memory-eater wth 60% of total memory size to measure the
56        # page access throughput
57        cmd = ("memory-eater --size %d --speed --repeat 4 --chunk 500 "
58               "--wait 0" % long(mem_size * 0.60 / 1024))
59        logging.debug('cmd: %s', cmd)
60        out = utils.system_output(cmd)
61        self._log_results("60_Percent_RAM", out)
62
63        # Then run memory-eater wth total memory + 30% swap size to measure the
64        # page access throughput. On 32-bit system with 4GB of RAM, the memory
65        # footprint needed to generate enough memory pressure is larger than
66        # a single user-space process can own. So we divide the memory usage
67        # by half and the test itself will fork a child process to double the
68        # memory usage. Each process will take turns to access 500 pages
69        # (via --chunk) until all pages are accessed 4 times (via --repeat).
70        half_mem_pressure_size = long((mem_size+swap_size * 0.3) / 1024) / 2;
71        cmd = ("memory-eater --size %d --speed --fork --repeat 4 --chunk 500"
72               "--wait 0" % half_mem_pressure_size)
73        logging.debug('cmd: %s', cmd)
74        out = utils.system_output(cmd)
75        self._log_results("30_Percent_SWAP", out)
76
77    def cleanup(self):
78        utils.system('start ui')
79
80