18a12e800f05f6862fc39c6a45c9c57d9051b795eEric Liimport datetime, logging, os, time
28a12e800f05f6862fc39c6a45c9c57d9051b795eEric Lifrom autotest_lib.client.bin import test, utils
38a12e800f05f6862fc39c6a45c9c57d9051b795eEric Lifrom autotest_lib.client.common_lib import error
48a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
58a12e800f05f6862fc39c6a45c9c57d9051b795eEric Liclass wb_kupdate(test.test):
68a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    version = 1
78a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
88a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
98a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _check_parameters(self, mount_point, write_size, file_count,
108a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                          old_cleanup=False):
118a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
128a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Check all test parameters.
138a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
148a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param mount_point: the path to the desired mount_point.
158a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param write_size: the size of data in MB to write.
168a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param file_count: the number of files to write.
178a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param old_cleanup: removes previous mount_point if it exists and is
188a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                not mounted. Default is False.
198a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
208a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Check mount_point.
218a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        if not os.path.exists(mount_point):
228a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.info('%s does not exist. Creating directory.', mount_point)
238a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        elif not os.path.ismount(mount_point) and old_cleanup:
248a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.info('Removing previous mount_point directory')
258a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            os.rmdir(mount_point)
268a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.info('Creating new mount_point.')
278a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        else:
288a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            raise error.TestError('Mount point: %s already exists.' %
298a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                                  mount_point)
308a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
318a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        os.makedirs(mount_point)
328a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Check write_size > 0.
338a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        if not (write_size > 0):
348a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            raise error.TestError('Write size should be a positive integer.')
358a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
368a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Check file_count > 0.
378a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        if not (file_count > 0) :
388a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            raise error.TestError('File count shoulde be a positive integer.')
398a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
408a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
418a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _reset_device(self):
428a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
438a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Reset the test. Reinitialize sparse file.
448a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
458a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Umount device.
468a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.debug('Cleanup - unmounting loopback device.')
478a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.system('umount %s' % self.mount_point, ignore_status=True)
488a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
498a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Remove sparse_file.
508a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.debug('Cleanup - removing sparse file.')
518a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        os.remove(self.sparse_file)
528a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
538a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Remove mount_point directory.
548a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.debug('Cleanup - removing the mount_point.')
558a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        os.rmdir(self.mount_point)
568a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
578a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
588a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _create_partition(self):
598a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
608a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Create and initialize the sparse file.
618a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
628a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Recreate sparse_file.
638a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.system('dd if=/dev/zero of=%s bs=1M seek=1024 count=1' %
648a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                      self.sparse_file)
658a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
668a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Format sparse_file.
678a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.system('echo "y" |  mkfs -t %s %s' %
688a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                     (self.file_system, self.sparse_file))
698a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
708a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Mount sparse_file.
718a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.system('mount -o loop -t %s %s %s' %
728a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                     (self.file_system, self.sparse_file, self.mount_point))
738a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
748a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
758a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _needs_more_time(self, start_time, duration, _now=None):
768a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
778a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Checks to see if the test has run its course.
788a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
798a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param start_time: a datetime object specifying the start time of the
808a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                test.
818a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param duration: test duration in minutes.
828a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param _now: used mostly for testing - ensures that the function returns
838a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                pass/fail depnding on the value of _now.
848a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
858a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @return: True if the test still needs to run longer.
868a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                 False if the test has run for 'duration' minutes.
878a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
888a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        if not _now:
898a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            time_diff = datetime.datetime.now() - start_time
908a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        else:
918a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            time_diff = _now - start_time
928a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        return time_diff <= datetime.timedelta(seconds=duration*60)
938a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
948a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
958a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _write_data(self, destination, counter, write_size):
968a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
978a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Writes data to the cache/memory.
988a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
998a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param destination: the absolute path to where the data needs to be
1008a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        written.
1018a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param counter: the file counter.
1028a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param write_size: the size of data to be written.
1038a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1048a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @return: the time when the write completed as a datetime object.
1058a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1068a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Write data to disk.
1078a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        file_name = os.path.join(destination, 'test_file_%s' % counter)
1088a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        write_cmd = ('dd if=/dev/zero of=%s bs=1M count=%s' %
1098a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                     (file_name, write_size))
1108a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.system(write_cmd)
1118a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1128a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Time the write operation.
1138a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        write_completion_time = datetime.datetime.now()
1148a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1158a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Return write completion time.
1168a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        return write_completion_time
1178a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1188a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1198a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _get_disk_usage(self, file_name):
1208a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1218a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Returns the disk usage of given file.
1228a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1238a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param file_name: the name of the file.
1248a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1258a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @return: the disk usage as an integer.
1268a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1278a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Check du stats.
1288a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        cmd = '%s %s' % (self._DU_CMD, file_name)
1298a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1308a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Expected value for  output = '1028\tfoo'
1318a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        output = utils.system_output(cmd)
1328a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1338a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Desired ouput = (1028, foo)
1348a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        output = output.split('\t')
1358a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1368a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        return int(output[0])
1378a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1388a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1398a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def _wait_until_data_flushed(self, start_time, max_wait_time):
1408a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1418a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Check to see if the sparse file size increases.
1428a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1438a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param start_time: the time when data was actually written into the
1448a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                cache.
1458a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param max_wait_time: the max amount of time to wait.
1468a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1478a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @return: time waited as a datetime.timedelta object.
1488a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1498a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        current_size = self._get_disk_usage(self.sparse_file)
1508a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        flushed_size = current_size
1518a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1528a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.debug('current_size: %s' % current_size)
1538a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.debug('flushed_size: %s' % flushed_size)
1548a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1558a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Keep checking until du value changes.
1568a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        while current_size == flushed_size:
1578a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Get flushed_size.
1588a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            flushed_size = self._get_disk_usage(self.sparse_file)
1598a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.debug('flushed_size: %s' % flushed_size)
1608a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            time.sleep(1)
1618a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1628a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Check if data has been synced to disk.
1638a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            if not self._needs_more_time(start_time, max_wait_time):
1648a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                raise error.TestError('Data not flushed. Waited for %s minutes '
1658a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                                      'for data to flush out.' % max_wait_time)
1668a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1678a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Return time waited.
1688a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        return datetime.datetime.now() - start_time
1698a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1708a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1718a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def initialize(self):
1728a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1738a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Initialize all private and global member variables.
1748a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1758a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self._DU_CMD = 'du'
1768a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.partition = None
1778a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.mount_point = ''
1788a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.sparse_file = ''
1798a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.result_map = {}
1808a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.file_system = None
1818a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1828a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1838a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def run_once(self, mount_point, file_count, write_size,
1848a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                 max_flush_time=1, file_system=None, remove_previous=False,
1858a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                 sparse_file=os.path.join(os.getcwd(),'sparse_file'),
1868a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                 old_cleanup=False):
1878a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
1888a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Control execution of the test.
1898a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
1908a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param mount_point: the absolute path to the mount point.
1918a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param file_count: the number of files to write.
1928a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param write_size: the size of each file in MB.
1938a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param max_flush_time: the maximum time to wait for the writeback to
1948a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                flush dirty data to disk. Default = 1 minute.
1958a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param file_system: the new file system to be mounted, if any.
1968a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                Default = None.
1978a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param remove_previous: boolean that allows the removal of previous
1988a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                files before creating a new one. Default = False.
1998a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param sparse_file: the absolute path to the sparse file.
2008a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        @param old_cleanup: removes previous mount_point if it exists and is
2018a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                not mounted. Default is False.
2028a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
2038a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Check validity of parameters.
2048a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self._check_parameters(mount_point, write_size, file_count,
2058a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                               old_cleanup)
2068a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2078a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Initialize class variables.
2088a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.mount_point = mount_point
2098a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.sparse_file = sparse_file
2108a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.file_system = file_system
2118a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2128a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Initialize partition values.
2138a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self._create_partition()
2148a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2158a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Flush read and write cache.
2168a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        utils.drop_caches()
2178a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2188a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Start iterations.
2198a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.info('Starting test operations.')
2208a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        test_start_time = datetime.datetime.now()
2218a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        counter = 1
2228a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2238a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Run test until file_count files are successfully written to disk.
2248a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        while counter < file_count:
2258a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.info('Iteration %s.', counter)
2268a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2278a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Write data to disk.
2288a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            write_completion_time = self._write_data(self.mount_point, counter,
2298a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                                                     write_size)
2308a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.debug('Write time:%s',
2318a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                          write_completion_time.strftime("%H:%M:%S"))
2328a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2338a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Wait until data get synced to disk.
2348a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            time_taken = self._wait_until_data_flushed(write_completion_time,
2358a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                                                       max_flush_time)
2368a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2378a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Log time statistics.
2388a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.info('Time taken to flush data: %s seconds.',
2398a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                         time_taken.seconds)
2408a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2418a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Check if there is a need to remove the previously written file.
2428a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            if remove_previous:
2438a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                logging.debug('Removing previous file instance.')
2448adf78936c915df6fd1edb6c592f40a7ed8350a5Dale Curtis                os.remove(sparse_file)
2458a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            else:
2468a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li                logging.debug('Not removing previous file instance.')
2478a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2488a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Flush cache.
2498a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            logging.debug('Flush cache between iterations.')
2508a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            utils.drop_caches()
2518a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2528a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li           # Update the result map.
2538a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            self.result_map[counter] = time_taken.seconds
2548a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2558a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            # Increment the counter.
2568a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li            counter += 1
2578a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2588a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2598a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li    def postprocess(self):
2608a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
2618a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        Cleanup routine.
2628a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        """
2638a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Write out keyval map.
2648a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self.write_perf_keyval(self.result_map)
2658a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2668a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        # Cleanup device.
2678a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        self._reset_device()
2688a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li
2698a12e800f05f6862fc39c6a45c9c57d9051b795eEric Li        logging.info('Test operations completed.')
270