#!/usr/bin/env python3

import json
import sys

traces = {}
for arg in sys.argv[1:]:
    events = json.load(open(arg))["traceEvents"]
    for event in events:
        if event["name"] == "" and event["ph"] == "I":
            time = float(event["args"]["time"])
            # print "%.6f" % time
            traces[time] = events
            break

times = sorted(traces)
min_time = min(times)

combined_events = []
for time in times:
    offset = (time - min_time) * 1000000.0
    events = traces[time]
    for event in events:
        event["ts"] = int(event["ts"] + offset)
        combined_events.append(event)

print(json.dumps({"traceEvents": combined_events}))
