1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
#!/usr/bin/env python3
# A python script to parse text traces generated with the LTTng tracer
# on pocl. For usage run './babel_parse.py -h'
import re
import json
import argparse
# different regexes that parse the arguments of a lttng trace
pocl_msg_regex = re.compile(
"^{ msg_id = (?P<msg_id>0x[0-9A-Fa-f]+), event_id = (?P<event_id>0x[0-9A-Fa-f]+), local_did = (?P<local_did>0x[0-9A-Fa-f]+), remote_did = (?P<remote_did>0x[0-9A-Fa-f]+), type = (?P<type>0x[0-9A-Fa-f]+), status = (?P<status>0x[0-9A-Fa-f]+) }$")
pocl_free_object_regex = re.compile(
"^{ context_id = (?P<context_id>0x[0-9A-Fa-f]+), (buffer|kernel|queue|program|)_id = (?P<id>0x[0-9A-Fa-f]+) }$")
pocl_kernel_create_regex = re.compile(
"^{ context_id = (?P<context_id>0x[0-9A-Fa-f]+), kernel_id = (?P<id>0x[0-9A-Fa-f]+), kernel_name = \"(?P<kernel_name>\S+)\" }$")
pocl_buffer_regex = re.compile(
"^{ event_id = (?P<event_id>0x[0-9A-Fa-f]+), evt_status = (?P<evt_status>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), obj_id = (?P<obj_id>0x[0-9A-Fa-f]+) }$")
pocl_ndrange_kernel_regex = re.compile(
"^{ event_id = (?P<event_id>0x[0-9A-Fa-f]+), evt_status = (?P<evt_status>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), kernel_id = (?P<kernel_id>0x[0-9A-Fa-f]+), kernel_name = \"(?P<kernel_name>\S+)\" }$")
pocl_copy_regex = re.compile(
"^{ event_id = (?P<event_id>0x[0-9A-Fa-f]+), evt_status = (?P<evt_status>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), src_id = (?P<src_id>0x[0-9A-Fa-f]+), dest_id = (?P<dest_id>0x[0-9A-Fa-f]+) }$")
def pocl_parse_status_and_return(dict, event_status):
"""
A function that parses the event status and creates
dictionaries with different phases set based on the status
@param dict The dictionary with most values already set
@param event_status the cl status of the event
@return a list of dictionaries with their phase set
"""
if event_status == 0:
# if the status is cl_finish, also add a line to
# indicate the enqueue bar is done and exit
dict_copy = dict.copy()
dict_copy["ph"] = "e"
dict["name"] = "running"
dict["ph"] = "e"
return [dict, dict_copy]
elif event_status == 1:
dict_copy = dict.copy()
dict_copy["name"] = "running"
dict_copy["ph"] = "b"
dict["name"] = "submitted"
dict["ph"] = "e"
return [dict, dict_copy]
elif event_status == 2:
dict_copy = dict.copy()
dict_copy["name"] = "submitted"
dict_copy["ph"] = "b"
dict["name"] = "queued"
dict["ph"] = "e"
return [dict, dict_copy]
else:
# add an overshadowing bar for the entire
# execution
dict_copy = dict.copy()
dict_copy["ph"] = "b"
dict["name"] = "queued"
dict["ph"] = "b"
return [dict_copy, dict]
def set_dict_values(dict, inp_dict):
"""
function that sets the common values of cl commands
@param dict the dictionary to be filled
@param inp_dict the dictionary with common values
that are copied to the dict
"""
dict["cat"] = "event"
dict["id"] = int(inp_dict["event_id"], 16)
dict["pid"] = int(inp_dict["dev_id"], 16)
dict["args"] = inp_dict
def parse_pocl_trace_data(trace_type, string, dict):
"""
function to parse trace specific data.
@param trace_type: string with the name of the trace
@param string: string to be parsed
@param dict: resulting dictionary with relevant key value pairs
"""
if (trace_type == "msg_received" or
trace_type == "msg_sent"):
res = pocl_msg_regex.match(string)
if res is None:
print("could not parse msg_received trace point, possibly the type has changed?")
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["id"] = int(res_dict["event_id"], 16)
dict["pid"] = int(res_dict["local_did"], 16)
status = int(res_dict["status"], 16)
# set a different phase type depending on
# the message type and status
if trace_type == "msg_received":
if status == 0:
dict["ph"] = "b"
elif status == 1:
dict["ph"] = "n"
elif status == 2:
dict["ph"] = "e"
else:
print("Unknown msg_received status")
return None
else:
status = int(res_dict["status"], 16)
if status == 0:
dict["ph"] = "b"
elif status == 1:
dict["ph"] = "e"
else:
print("Unknown msg_received status")
return None
dict["args"] = res_dict
return [dict]
elif (trace_type == "free_buffer" or
trace_type == "free_queue" or
trace_type == "free_program" or
trace_type == "create_buffer" or
trace_type == "create_queue" or
trace_type == "build_program" or
trace_type == "create_program"):
res = pocl_free_object_regex.match(string)
if res is None:
print("could not parse pocl trace point: " + string)
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["id"] = int(res_dict["id"], 16)
dict["pid"] = int(res_dict["context_id"], 16)
dict["ph"] = "i"
dict["args"] = res_dict
return [dict]
elif (trace_type == "free_kernel" or
trace_type == "create_kernel"):
res = pocl_kernel_create_regex.match(string)
if res is None:
print("could not parse pocl trace point: " + string)
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["name"] = "kernel:" + res_dict["kernel_name"]
dict["id"] = int(res_dict["id"], 16)
dict["pid"] = int(res_dict["context_id"], 16)
dict["ph"] = "i"
dict["args"] = res_dict
return [dict]
elif (trace_type == "read_buffer" or
trace_type == "write_buffer" or
trace_type == "fill_buffer" or
trace_type == "read_buffer_rect" or
trace_type == "write_buffer_rect" or
trace_type == "read_image_rect" or
trace_type == "write_image_rect" or
trace_type == "fill_image" or
trace_type == "map_image" or
trace_type == "map_buffer" or
trace_type == "unmap_memobj"
):
res = pocl_buffer_regex.match(string)
if res is None:
print("could not parse buffer trace point: " + string)
return None
res_dict = res.groupdict()
set_dict_values(dict, res_dict)
event_status = int(res_dict["evt_status"], 16)
return pocl_parse_status_and_return(dict, event_status)
elif trace_type == "ndrange_kernel":
res = pocl_ndrange_kernel_regex.match(string)
if res is None:
print("could not parse ndrange kernel trace point: " + string)
return None
res_dict = res.groupdict()
set_dict_values(dict, res_dict)
dict["name"] = res_dict["kernel_name"]
event_status = int(res_dict["evt_status"], 16)
return pocl_parse_status_and_return(dict, event_status)
elif (trace_type == "copy_image_rect" or
trace_type == "copy_buffer" or
trace_type == "copy_buffer_rect" or
trace_type == "copy_image2buf" or
trace_type == "copy_buf2image"):
res = pocl_copy_regex.match(string)
if res is None:
print("could not parse buffer trace point: " + string)
return None
res_dict = res.groupdict()
set_dict_values(dict, res_dict)
event_status = int(res_dict["evt_status"], 16)
return pocl_parse_status_and_return(dict, event_status)
else:
print("pocl_trace: unknown message type: " + trace_type)
return None
pocld_msg_regex = re.compile(
"^{ msg_id = (?P<msg_id>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), type = (?P<type>[0-9]+) }$")
pocld_buffer_regex = re.compile(
"^{ msg_id = (?P<msg_id>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), buffer_id = (?P<buffer_id>0x[0-9A-Fa-f]+), (write_|read_|)size = (?P<size>0x[0-9A-Fa-f]+), event_status = (?P<event_status>[0-9]+) }$")
pocld_ndrange_kernel_regex = re.compile(
"^{ msg_id = (?P<msg_id>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), queue_id = (?P<queue_id>0x[0-9A-Fa-f]+), kernel_id = (?P<kernel_id>0x[0-9A-Fa-f]+), event_status = (?P<event_status>[0-9]+) }$")
pocld_free_regex = re.compile(
"^{ msg_id = (?P<msg_id>0x[0-9A-Fa-f]+), dev_id = (?P<dev_id>0x[0-9A-Fa-f]+), (buffer|kernel|queue|program|)_id = (?P<id>0x[0-9A-Fa-f]+) }$")
def pocld_parse_event_status(status):
"""
Convert the status number to an appropriate phase
letter.
@param status: pocl status integer
@return: char representing the phase
"""
# for some reason pocld can have the running status
# set to either 1 or 2
if status == 1 or status == 2:
return "b"
# corresponds to cl_finish
elif status == 3:
return "e"
else:
print("unknown event status: {}".format(status))
return "e"
def parse_pocld_trace_data(trace_type, string, dict):
"""
function to parse trace specific data.
@param trace_type: string with the name of the trace
@param string: string to be parsed
@param dict: resulting dictionary with relevant key value pairs
"""
# prefix used to indicate that the device is not local
pocld_pid_prefix = 1000
if (trace_type == "msg_received" or
trace_type == "msg_sent"):
res = pocld_msg_regex.match(string)
if res is None:
print("could not parse pocld msg_received trace point")
return None
res_dict = res.groupdict()
dict["cat"] = "msg"
dict["id"] = int(res_dict["msg_id"], 16)
dict["pid"] = int(res_dict["dev_id"], 16) + pocld_pid_prefix
dict["ph"] = "n"
dict["args"] = res_dict
return [dict]
elif (trace_type == "write_buffer" or
trace_type == "read_buffer" or
trace_type == "fill_buffer"):
res = pocld_buffer_regex.match(string)
if res is None:
print("could not parse pocld write_buffer trace point")
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["id"] = int(res_dict["msg_id"], 16)
dict["pid"] = int(res_dict["dev_id"], 16) + pocld_pid_prefix
dict["ph"] = pocld_parse_event_status(int(res_dict["event_status"]))
dict["args"] = res_dict
return [dict]
elif (trace_type == "ndrange_kernel"):
res = pocld_ndrange_kernel_regex.match(string)
if res is None:
print("could not parse pocld write_buffer trace point")
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["id"] = int(res_dict["msg_id"], 16)
dict["pid"] = int(res_dict["dev_id"], 16) + pocld_pid_prefix
dict["ph"] = pocld_parse_event_status(int(res_dict["event_status"]))
dict["args"] = res_dict
return [dict]
elif (trace_type == "free_buffer" or
trace_type == "free_kernel" or
trace_type == "free_queue" or
trace_type == "free_program" or
trace_type == "create_buffer" or
trace_type == "create_kernel" or
trace_type == "create_queue" or
trace_type == "build_program"):
res = pocld_free_regex.match(string)
if res is None:
print("could not parse pocld trace point: " + string)
return None
res_dict = res.groupdict()
dict["cat"] = "event"
dict["id"] = int(res_dict["msg_id"], 16)
dict["pid"] = int(res_dict["dev_id"], 16) + pocld_pid_prefix
dict["ph"] = "i"
dict["args"] = res_dict
return [dict]
else:
print("pocld_trace: unknown message type: " + trace_type)
return None
line_regex = re.compile(
"^\[(?P<seconds>\d+)\.(?P<nanos>\d+)\]\s+\(\+[0-9?]+\.[0-9?]+\)\s+(?P<hostname>[\S]+)\s+(?P<tracer_name>.*):(?P<trace_type>.*): { cpu_id = \d+ },\s(?P<trace_data>.*)$")
# used to store the earliest timestamp recorded
# which is used as the starting point
start_time = 0
def parse_line(line):
"""
take a raw line that has been parsed by babelparse
and extract all the relevant information from it
@param line: raw line from babelparse
@return: a dictionary with required key value pairs
for the chrome trace format
"""
global start_time
regex_result = line_regex.match(line)
if regex_result:
regex_dict = regex_result.groupdict()
if not (regex_dict["tracer_name"] == "pocl_trace" or
regex_dict["tracer_name"] == "pocld_trace"):
print("other tracer are not supported yet")
return None
ts = int(regex_dict["seconds"]) * 1_000_000_000 + int(regex_dict["nanos"]) - start_time
if start_time == 0:
start_time = ts
ts = 0
# ts divided by 1000 since chrome profiler expects time microseconds
ret_dict = {"ts": ts / 1000, "name": regex_dict["trace_type"]}
if regex_dict["tracer_name"] == "pocl_trace":
parsed_lines = parse_pocl_trace_data(regex_dict["trace_type"], regex_dict["trace_data"], ret_dict)
else:
parsed_lines = parse_pocld_trace_data(regex_dict["trace_type"], regex_dict["trace_data"], ret_dict)
if parsed_lines:
return parsed_lines
return None
else:
print("could not parse line, unknown format")
print(line)
return None
def main():
# parse the command line arguments
parser = argparse.ArgumentParser(description="Parse LTTng text trace files.")
parser.add_argument("input_file", metavar="input_file", nargs="?",
default="trace.txt",
help="the name of the input file that has already been parsed with babel parse.")
parser.add_argument("--out", "-o", metavar="output_file",
default="trace.json", nargs="?",
help="the name of the output file. Default: trace.json")
args = vars(parser.parse_args())
input_file = open(args.get("input_file"), "rt")
output_file = open(args.get("out"), "wt")
parse_count = 0
try:
output_file.write("[ \n")
# read the first line, but don't start it with a ','
first_line = input_file.readline()
result = parse_line(first_line)
if result:
for res in result:
output_file.write(json.dumps(res) + "\n")
parse_count = 1
for line in input_file:
result = parse_line(line)
if result:
for res in result:
output_file.write("," + json.dumps(res) + "\n")
parse_count += 1
output_file.write("] \n")
finally:
input_file.close()
output_file.close()
print("parsed {} lines, exiting".format(parse_count))
if __name__ == "__main__":
main()
|