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 os, logging, shutil
6from autotest_lib.client.bin import test, utils
7
8
9class hardware_RamFio(test.test):
10    """
11    Create ram disk and use FIO to test for ram throughput
12    """
13
14    version = 1
15
16    DEFAULT_SIZE = 1024 * 1024 * 1024
17
18    def run_once(self, size=DEFAULT_SIZE, requirements=None, dry_run=False):
19        """
20        Call hardware_StorageFio to test on ram drive
21
22        @param size: size to test in byte
23                     0 means all usable memory
24        @param requirements: requirement to pass to hardware_StorageFio
25        """
26        # assume 20% overhead with ramfs
27        usable_mem = int(utils.usable_memtotal() * 1024 * 0.8)
28
29        if size == 0:
30            size = usable_mem
31        elif usable_mem < size:
32            logging.info('Not enough memory. Want: %d, Usable: %d',
33                         size, usable_mem)
34            size = usable_mem
35
36        self.write_perf_keyval({'Size' : size})
37
38        if dry_run:
39            return
40
41        utils.run('mkdir -p /tmp/ramdisk')
42        utils.run('mount -t ramfs ramfs /tmp/ramdisk')
43
44        self.job.run_test('hardware_StorageFio',
45                          dev='/tmp/ramdisk/test_file',
46                          size=size,
47                          requirements=requirements)
48
49        utils.run('umount /tmp/ramdisk')
50
51        dst = os.path.join(self.resultsdir, 'perf_measurements')
52        src = dst.replace('hardware_RamFio', 'hardware_StorageFio')
53        shutil.copyfile(src, dst)
54