timeline_trace.py 1.4 KB
Newer Older
M
MRXLT 已提交
1 2 3 4 5 6 7
#coding=utf-8
import json
import sys

profile_file = sys.argv[1]


M
MRXLT 已提交
8 9 10
def prase(pid_str, time_str, counter):
    pid = pid_str.split(":")[1]
    event_list = time_str.split(" ")
M
MRXLT 已提交
11 12 13 14 15 16 17 18
    trace_list = []
    for event in event_list:
        name, ts = event.split(":")
        name_list = name.split("_")
        ph = "B" if (name_list[-1] == "0") else "E"
        if len(name_list) == 2:
            name = name_list[0]
        else:
W
wangjiawei04 已提交
19 20 21 22 23 24 25
            name = "_".join(name_list[:-1])
        name_list = name.split("#")
        if len(name_list) > 1:
            tid = name_list[-1]
            name = "#".join(name_list[:-1])
        else:
            tid = 0
M
MRXLT 已提交
26 27
        event_dict = {}
        event_dict["name"] = name
W
wangjiawei04 已提交
28
        event_dict["tid"] = tid
M
MRXLT 已提交
29
        event_dict["pid"] = pid
M
MRXLT 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        event_dict["ts"] = ts
        event_dict["ph"] = ph

        trace_list.append(event_dict)
    return trace_list


if __name__ == "__main__":
    profile_file = sys.argv[1]
    trace_file = sys.argv[2]
    all_list = []
    counter = 0
    with open(profile_file) as f:
        for line in f.readlines():
            line = line.strip().split("\t")
            if line[0] == "PROFILE":
M
MRXLT 已提交
46
                trace_list = prase(line[1], line[2], counter)
M
MRXLT 已提交
47 48 49 50 51 52 53
                counter += 1
                for trace in trace_list:
                    all_list.append(trace)

    trace = json.dumps(all_list, indent=2, separators=(',', ':'))
    with open(trace_file, "w") as f:
        f.write(trace)