
import re
import sys

output = sys.argv[1]
fp = open(output, 'r')

cache_miss=0
cache_hit=0

total_fetch=0
buffered_bytes = 0

buffered_re = re.compile('Using existing buffer to read (.*) bytes')
buffered_and_fetch = re.compile('Taking the rest (.*) and Fetching new buffer')
#iocmd = re.compile('Sending IOCMD_SEEK_READ. \((.*)\)')
iocmd = re.compile('Expected position: .+ @ .+ bytes readed. Returning (.*)')
too_small_buffer = re.compile('Buffer .GE. than read-ahead buffer')

for line in fp.readlines():
    if not line.startswith('[8]'):
        continue
    if line.find('Initially fetching new buffer') >= 0:
        cache_miss += 1
        continue
    m = buffered_and_fetch.search(line)
    if m:
        cache_miss += 1
        cache_hit += 1
        buffered_bytes += int(m.groups()[0])
        continue
    m = buffered_re.search(line)
    if m:
        cache_hit += 1
        buffered_bytes += int(m.groups()[0])
    m = buffered_re.search(line)
    if m:
        cache_miss += 1
        continue
    m = iocmd.search(line)
    if m:
        total_fetch += int(m.groups()[0])

print "There were %i cache hits and %i cache misses (miss rate: %.2f%%)" % (cache_hit, cache_miss, 100*cache_miss/float(cache_hit+cache_miss))
print "There were %i bytes read and %i buffered bytes used; efficiency: %.2f%%" % (total_fetch, buffered_bytes, 100*buffered_bytes/float(total_fetch))
