Files
rocm-systems/bin/merge_jsons.py
T

118 lines
4.7 KiB
Python
Raw Normal View History

2020-03-06 11:17:13 -05:00
#!/usr/bin/python
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
import os, sys, re
import argparse
shift = 100
2020-03-18 12:36:07 -04:00
def parse_json(jsonfile, fo, index, lastjson):
2020-03-06 11:17:13 -05:00
if not re.search(r'\.json$', jsonfile):
raise Exception('wrong input file type: "' + jsonfile + '"' )
2020-03-18 12:36:07 -04:00
metadata = ''
2020-03-06 11:17:13 -05:00
with open(jsonfile) as fp:
for line in fp:
2020-03-13 15:43:57 -04:00
ms = re.match(r'^{ "traceEvents":\[{}\n|^\]}\n|^\],\n',line)
2020-03-06 11:17:13 -05:00
if ms:
continue
2020-03-13 15:43:57 -04:00
md = re.match(r'.*otherData.*',line)
if md:
2020-03-17 12:14:51 -04:00
# collect metadata in variable 'metadata'
2020-03-13 15:43:57 -04:00
metadata = ' '
continue
if metadata != '':
minfo = re.match(r'(.*)"(.*)":(.*)',line)
2020-03-17 12:14:51 -04:00
# minfo catch pattern like: "version": "my app V1.0"
2020-03-13 15:43:57 -04:00
if minfo:
2020-03-18 12:36:07 -04:00
metadata = metadata + minfo.group(1) + '"' + minfo.group(2) + '(' + os.path.splitext(jsonfile)[0] + ')":' + minfo.group(3) + '\n'
else:
metadata = metadata + line
2020-03-13 15:43:57 -04:00
continue
2020-03-06 11:17:13 -05:00
mo = re.match(r'(.*"pid"\s*:\s*)(\d+)(.*)',line)
2020-03-17 12:14:51 -04:00
# mo catch pattern like: ,{"ts":4258451581657,"ph":"s","cat":"DataFlow","id":0,"pid":2,"tid":83583,"name":"dep"}
# grp2 is pid number that needs shifting, grp1 is what comes before pid number and grp3 is what comes after
2020-03-06 11:17:13 -05:00
mp = re.match(r'(.*)"name"\s*:\s*"([\w,\s]+)"(.*)"pid"\s*:\s*(\d+)(.*)',line)
2020-03-17 12:14:51 -04:00
# mp catch pattern like: ,{"args":{"name":"0 CPU HIP API"},"ph":"M","pid":2,"name":"process_name"}
# Grp 1. 0-10 ,{"args":{
# Grp 2. 18-31 0 CPU HIP API
# Grp 3. 32-43 },"ph":"M",
# Grp 4. 49-50 2
# Grp 5. 50-73 ,"name":"process_name"}
2020-03-06 11:17:13 -05:00
mp2 = re.match(r'(.*"pid"\s*:\s*")(\d+)(".*)',line)
2020-03-17 12:14:51 -04:00
# mo2 catch pattern like: "pid":"3",
# where grp2 is the pid number to shift
2020-03-06 11:17:13 -05:00
if mp:
2020-03-13 15:43:57 -04:00
laneName = mp.group(2) + '(' + os.path.splitext(jsonfile)[0] + ')'
2020-03-06 11:17:13 -05:00
mpid = int(str(mp.group(4)))
2020-03-18 12:36:07 -04:00
mpid = mpid + index*shift
2020-03-06 11:17:13 -05:00
fo.write(mp.group(1) + '"name":"' + laneName + '"' + mp.group(3) + '"pid":' + str(mpid) + mp.group(5) + '\n')
elif mo:
mpid = int(str(mo.group(2)))
2020-03-18 12:36:07 -04:00
mpid = mpid + index*shift
2020-03-06 11:17:13 -05:00
fo.write(mo.group(1) + str(mpid) + mo.group(3) + '\n')
elif mp2:
mpid = int(str(mp2.group(2)))
2020-03-18 12:36:07 -04:00
mpid = mpid + index*shift
2020-03-06 11:17:13 -05:00
fo.write(mp2.group(1) + str(mpid) + mp2.group(3) + '\n')
else:
fo.write(line)
2020-03-18 12:36:07 -04:00
metadata = metadata[:metadata.rfind('\n')]
metadata = metadata[:metadata.rfind('\n')]
if lastjson == 0:
metadata = metadata + ',\n'
else:
metadata = metadata + '\n'
2020-03-13 15:43:57 -04:00
return metadata
2020-03-06 11:17:13 -05:00
def merge_jsons(jsons,outfile):
ljsons = jsons.split(',')
fo = open(outfile, mode='w')
fo.write('{ "traceEvents":[{}\n')
2020-03-13 15:43:57 -04:00
metadata = ''
res=''
2020-03-17 12:14:51 -04:00
# res will contain all metadata for all jsons files provided as input
2020-03-06 11:17:13 -05:00
for i in range(0, len(ljsons)):
2020-03-13 15:43:57 -04:00
if i == len(ljsons)-1:
res=res+parse_json(ljsons[i],fo,i,1)
else:
res=res+parse_json(ljsons[i],fo,i,0)
fo.write('],\n')
2020-03-17 12:14:51 -04:00
# write metadata at the end of output json file
2020-03-13 15:43:57 -04:00
fo.write(' "otherData": {\n')
fo.write(res)
fo.write(' }\n}\n')
2020-03-06 11:17:13 -05:00
fo.close()
parser = argparse.ArgumentParser(description='merge_jsons.py: merges list of jsons into one json file.')
requiredNamed=parser.add_argument_group('Required arguments')
requiredNamed.add_argument('-in','--in', help='comma separated list of json files', required=True)
requiredNamed.add_argument('-out','--out', help='Output file (.json)', required=True)
args = vars(parser.parse_args())
if __name__ == '__main__':
merge_jsons(args['in'],args['out'])