2022-10-06 13:33:45 +02:00
# SPDX-FileCopyrightText: Red Hat, Inc.
# SPDX-License-Identifier: GPL-2.0-or-later
2017-12-17 14:44:32 +02:00
"""
2018-11-18 16:58:53 +02:00
Test script for file . BlockIO ( ) and file . FileIO ( ) , simulating fallocate
2018-07-07 18:51:09 +03:00
and blkdiscard - - zeroout .
2017-12-17 14:44:32 +02:00
"""
import argparse
import time
2020-08-27 15:43:51 +02:00
import urllib . parse
2019-10-08 20:09:58 +03:00
2020-05-10 09:40:03 +03:00
from ovirt_imageio . _internal . backends import file
2017-12-17 14:44:32 +02:00
def humansize ( s ) :
if s . isdigit ( ) :
return int ( s )
value , unit = s [ : - 1 ] , s [ - 1 ]
value = int ( value )
unit = unit . lower ( )
if unit == " k " :
return value * 1024
elif unit == " m " :
return value * 1024 * * 2
elif unit == " g " :
return value * 1024 * * 3
else :
raise ValueError ( " Unsupported unit: %r " % unit )
parser = argparse . ArgumentParser ( )
parser . add_argument (
" -l " , " --length " ,
dest = " length " ,
type = humansize ,
help = ( " The number of bytes to zero (counting from the starting point) "
2018-07-07 18:51:09 +03:00
" (default entire device or file) " ) )
2017-12-17 14:44:32 +02:00
parser . add_argument (
" -o " , " --offset " ,
dest = " offset " ,
type = humansize ,
default = 0 ,
help = " Byte offset into the device from which to start zeroing (default 0) " )
parser . add_argument (
" -p " , " --step " ,
dest = " step " ,
type = humansize ,
2018-07-07 18:51:09 +03:00
help = ( " The number of bytes to zero within one iteration. The default "
2017-12-17 14:44:32 +02:00
" is to discard all by one ioctl call " ) )
2018-07-08 17:52:31 +03:00
parser . add_argument (
" -s " , " --sparse " ,
dest = " sparse " ,
action = " store_true " ,
help = " Deallocate zeroed space (punch holes) " )
2017-12-17 14:44:32 +02:00
parser . add_argument (
2018-07-07 18:51:09 +03:00
" filename " ,
help = " file or block device to fill with zeros " )
2017-12-17 14:44:32 +02:00
args = parser . parse_args ( )
2020-08-27 15:43:51 +02:00
url = urllib . parse . urlparse ( " file:// " + args . filename )
2019-10-08 20:09:58 +03:00
2020-04-30 00:41:02 +03:00
start_time = time . monotonic ( )
2017-12-17 14:44:32 +02:00
2019-10-08 20:09:58 +03:00
with file . open ( url , " r+ " , sparse = args . sparse ) as f :
2017-12-17 14:44:32 +02:00
if args . length is None :
2019-10-08 20:09:58 +03:00
args . length = f . size ( ) - args . offset
2017-12-17 14:44:32 +02:00
if args . step is None :
args . step = args . length
2018-07-07 16:07:11 +03:00
f . seek ( args . offset )
2017-12-17 14:44:32 +02:00
2018-07-07 16:07:11 +03:00
count = args . length
while count :
step = min ( args . step , count )
2019-10-08 20:09:58 +03:00
f . zero ( step )
2018-07-07 16:07:11 +03:00
count - = step
2017-12-17 14:44:32 +02:00
2018-07-07 16:07:11 +03:00
f . flush ( )
2017-12-17 14:44:32 +02:00
2020-04-30 00:41:02 +03:00
elapsed_time = time . monotonic ( ) - start_time
2017-12-17 14:44:32 +02:00
2019-10-08 20:09:58 +03:00
print ( " Zero %.2f GiB in %.3f seconds ( %.2f GiB/s) " % (
2017-12-17 14:44:32 +02:00
float ( args . length ) / 1024 * * 3 ,
elapsed_time ,
2019-10-08 20:09:58 +03:00
float ( args . length ) / 1024 * * 3 / elapsed_time ) )