#!/usr/bin/env python

# Manually examine output from TestExcessGCLockerCollections.java.
# Looking for atypically small times between the end of one collection
# and the start of the next.
# Parses output from -Xlog:gc,gc+start, so may require modification of
# the test.

import re
import sys

timestamp_re = "\\[(\\d+\\.\\d+)s\\s*\\]"

start_re = timestamp_re + ".*\\[gc,start\\s*\\]"
end_re = timestamp_re + ".*\\[gc\\s*\\]"

def find_line(f, regexp):
    for line in f:
        match = re.match(regexp, line)
        if match:
            return line, match
    return None, None

def timestamp(line, match):
    return float(line[match.start(1): match.end(1)])

f = sys.stdin
data = list()

# Skip some early [gc] log entries that aren't collection ends.
# Things like logging which GC is being used and such.
if not find_line(f, start_re):
    sys.exit(0)
    
# Search for collection end and following collection starts, and
# record the time between them and the associated lines.
while True:
    end_line, end_match = find_line(f, end_re)
    if not end_line:
        break
    start_line, start_match = find_line(f, start_re)
    if not start_line:
        break
    end_timestamp = timestamp(end_line, end_match)
    start_timestamp = timestamp(start_line, start_match)
    data.append([start_timestamp - end_timestamp, end_line, start_line])

# Sort on the time deltas.
data.sort(key = lambda x: x[0])

#for td in data:
#    print td[0]
#    print td[1][0:-1]
#    print td[2][0:-1]
#    print ""

print "number of entries", len(data)
print "minimum time", data[0][0]
print data[0][1][0:-1]
print data[0][2][0:-1]
# We can get one improperly large delta if the jtreg log output
# suppression occurs between after a gc end and before the next start.
# So ignore the last entry and print the penultimate entry.
print "maximum time", data[-2][0]
print data[-2][1][0:-1]
print data[-2][2][0:-1]
