hardware_RamFio: refactor test.

Main change is to stop chrome before running the test to avoid its memory
usage as a dependency in activating OOM killer.

BUG=chromium:623116
TEST=Ran on cyan, lars (pass), veyron_minnie (unrelated fail).

Change-Id: I71281b08b48e037dcb196a5f32d902a2ad454c18
Reviewed-on: https://chromium-review.googlesource.com/356143
Reviewed-by: Haixia Shi <hshi@chromium.org>
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Reviewed-by: Puthikorn Voravootivat <puthik@chromium.org>
Commit-Queue: Ilja H. Friedel <ihf@chromium.org>
Tested-by: Ilja H. Friedel <ihf@chromium.org>
diff --git a/client/site_tests/hardware_RamFio/hardware_RamFio.py b/client/site_tests/hardware_RamFio/hardware_RamFio.py
index 9b2f404..c4a812c 100644
--- a/client/site_tests/hardware_RamFio/hardware_RamFio.py
+++ b/client/site_tests/hardware_RamFio/hardware_RamFio.py
@@ -2,8 +2,13 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import os, logging, shutil
-from autotest_lib.client.bin import test, utils
+import logging
+import os
+import shutil
+
+from autotest_lib.client.bin import test
+from autotest_lib.client.bin import utils
+from autotest_lib.client.cros import service_stopper
 
 
 class hardware_RamFio(test.test):
@@ -13,40 +18,51 @@
 
     version = 1
 
-    DEFAULT_SIZE = 1024 * 1024 * 1024
+    _DEFAULT_SIZE = 1024 * 1024 * 1024
+    _RAMDISK = '/tmp/ramdisk'
 
-    def run_once(self, size=DEFAULT_SIZE, requirements=None, dry_run=False):
-        """
-        Call hardware_StorageFio to test on ram drive
+    def initialize(self):
+        # This test grabs a lot of system memory. Lets move Chrome out of the
+        # picture to avoid interference with OOM killer.
+        self._services = service_stopper.ServiceStopper(['ui'])
+        self._services.stop_services()
+
+    def cleanup(self):
+        if self._services:
+            self._services.restore_services()
+
+    def run_once(self, size=_DEFAULT_SIZE, requirements=None, dry_run=False):
+        """Call hardware_StorageFio to test on ram drive
 
         @param size: size to test in byte
                      0 means all usable memory
         @param requirements: requirement to pass to hardware_StorageFio
         """
-        # assume 20% overhead with ramfs
-        usable_mem = int(utils.usable_memtotal() * 1024 * 0.8)
-
+        usable_mem = utils.usable_memtotal() * 1024
+        logging.info('Found %d bytes of usable memory.', usable_mem)
+        # Assume 20% overhead with ramfs.
+        usable_mem = 0.8 * usable_mem
         if size == 0:
             size = usable_mem
         elif usable_mem < size:
-            logging.info('Not enough memory. Want: %d, Usable: %d',
-                         size, usable_mem)
+            logging.info('Not enough memory. Want: %d, Usable: %d', size,
+                         usable_mem)
             size = usable_mem
-
-        self.write_perf_keyval({'Size' : size})
+        self.write_perf_keyval({'Size': size})
 
         if dry_run:
             return
 
-        utils.run('mkdir -p /tmp/ramdisk')
-        utils.run('mount -t ramfs -o context=u:object_r:tmpfs:s0 ramfs /tmp/ramdisk')
+        utils.run('mkdir -p %s' % self._RAMDISK)
+        utils.run('mount -t ramfs -o context=u:object_r:tmpfs:s0 ramfs %s' %
+                  self._RAMDISK)
 
         self.job.run_test('hardware_StorageFio',
-                          dev='/tmp/ramdisk/test_file',
+                          dev='%s/test_file' % self._RAMDISK,
                           size=size,
                           requirements=requirements)
 
-        utils.run('umount /tmp/ramdisk')
+        utils.run('umount %s' % self._RAMDISK)
 
         dst = os.path.join(self.resultsdir, 'results-chart.json')
         src = dst.replace('hardware_RamFio', 'hardware_StorageFio')