1# Copyright (c) 2014 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
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros import service_stopper
11
12
13class hardware_RamFio(test.test):
14    """
15    Create ram disk and use FIO to test for ram throughput
16    """
17
18    version = 1
19
20    _MB = 1024 * 1024
21    _DEFAULT_SIZE = 1024 * _MB
22    _RESERVED_RAM_SIZE = 200 * _MB
23    _RAMDISK = '/tmp/ramdisk'
24    _RAMFS_OVERHEAD = 0.2
25
26    def initialize(self):
27        # This test grabs a lot of system memory. Lets move Chrome out of the
28        # picture to avoid interference with OOM killer.
29        self._services = service_stopper.ServiceStopper(['ui'])
30        self._services.stop_services()
31
32    def cleanup(self):
33        if self._services:
34            self._services.restore_services()
35
36    def run_once(self, size=_DEFAULT_SIZE, requirements=None, dry_run=False):
37        """Call hardware_StorageFio to test on ram drive
38
39        @param size: size to test in byte
40                     0 means all usable memory
41        @param requirements: requirement to pass to hardware_StorageFio
42        """
43        usable_mem = utils.usable_memtotal() * 1024
44        logging.info('Found %d bytes of usable memory.', usable_mem)
45        # Assume 20% overhead with ramfs.
46        usable_mem *= 1 - self._RAMFS_OVERHEAD
47
48        # crbug.com/762315 Reserved 200 MiB to prevent OOM error.
49        if usable_mem <= self._RESERVED_RAM_SIZE:
50            raise error.TestNAError(
51                'Usable memory (%d MiB) is less than reserved size (%d MiB).' %
52                (usable_mem / self._MB, self._RESERVED_RAM_SIZE / self._MB))
53        usable_mem -= self._RESERVED_RAM_SIZE
54
55        if size == 0:
56            size = usable_mem
57        elif usable_mem < size:
58            logging.info('Not enough memory. Want: %d, Usable: %d', size,
59                         usable_mem)
60            size = usable_mem
61        self.write_perf_keyval({'Size': size})
62
63        if dry_run:
64            return
65
66        utils.run('mkdir -p %s' % self._RAMDISK)
67        # Don't throw an exception on errors.
68        result = utils.run('mount -t ramfs -o context=u:object_r:tmpfs:s0 '
69                           'ramfs %s' % self._RAMDISK, ignore_status = True)
70        if result.exit_status:
71            logging.info('cannot mount ramfs with context=u:object_r:tmpfs:s0,'
72                         ' trying plain mount')
73            # Try again without selinux options.  This time fail on error.
74            utils.run('mount -t ramfs ramfs %s' % self._RAMDISK)
75
76        self.job.run_test('hardware_StorageFio',
77                          dev='%s/test_file' % self._RAMDISK,
78                          size=size,
79                          requirements=requirements)
80
81        utils.run('umount %s' % self._RAMDISK)
82