Skip to content

Commit

Permalink
Got the file size by halving if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
fidlej committed Feb 16, 2011
1 parent 2fc5974 commit 8b54cbc
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions seeker.py
Expand Up @@ -2,6 +2,9 @@
"""Usage: seeker.py /path/to/raw/device
Measures the possible number of seeks per seconds.
"""
# It should work on any Unix.
# It is based on the Linux specific seeker.c from:
# http://www.linuxinsight.com/how_fast_is_your_disk.html

import os
import sys
Expand All @@ -12,15 +15,38 @@

def _get_size(input):
input.seek(0, os.SEEK_END)
return input.tell()
size = input.tell()
if size != 0:
return size

# FreeBSD does not support SEEK_END on devices.
# We need to get the size by binary halving.
pos = 0
step = 2**40 # 1TB
while True:
pos += step
try:
input.seek(pos)
data = input.read(1)
except IOError, possible:
data = ""

if len(data) != 1:
if step == 1:
# Size is the possible position + 1.
return (pos - step) + 1

pos -= step
step = max(1, step // 2)


def _seek_randomly(dev, size, num_seeks):
num_blocks = size // BLOCKSIZE
for i in xrange(num_seeks):
block = random.randrange(num_blocks)
dev.seek(block * BLOCKSIZE)
dev.read(BLOCKSIZE)
data = dev.read(BLOCKSIZE)
assert len(data) == BLOCKSIZE


def main():
Expand Down

0 comments on commit 8b54cbc

Please sign in to comment.