File indexing completed on 2025-02-07 23:29:35
0001
0002
0003 import sys
0004 import json
0005
0006 usage = """Usage: mergeResourceJson.py FILE [FILE ...]
0007
0008 Merge the content of multiple "resources.json" files produced by the FastTimerService,
0009 and print the result to standard output.
0010
0011 Example:
0012 mergeResourceJson.py step*/pid*/resources.json > resources.json
0013 """
0014
0015 def merge_into(metrics, data, dest):
0016 dest["events"] += data["events"]
0017 for metric in metrics:
0018 dest[metric] += data[metric]
0019
0020 def convert_old_resources(resources, filename):
0021 mapping = {
0022 "time_real": {"name": "time_real", "description": "real time", "unit": "ms", "title": "Time"},
0023 "time_thread": {"name": "time_thread", "description": "cpu time", "unit": "ms", "title": "Time"},
0024 "mem_alloc": {"name": "mem_alloc", "description": "allocated memory", "unit": "kB", "title": "Memory"},
0025 "mem_free": {"name": "mem_free", "description": "deallocated memory", "unit": "kB", "title": "Memory"}
0026 }
0027 new_resources = []
0028 for resource in resources:
0029
0030 if all(key in resource for key in ["name", "description", "unit", "title"]):
0031 new_resources.append(resource)
0032 elif any(key in resource for key in ["name", "description", "unit", "title"]):
0033 print("Error: incomplete resource description in file " + filename)
0034 sys.exit(1)
0035 else:
0036 for key, _ in resource.items():
0037 if key in mapping:
0038 new_resources.append(mapping[key])
0039 else:
0040 new_resources.append(resource)
0041 return new_resources
0042
0043 if len(sys.argv) == 1:
0044 print(usage)
0045 sys.exit(1)
0046
0047 if '-h' in sys.argv[1:] or '--help' in sys.argv[1:]:
0048 print(usage)
0049 sys.exit(0)
0050
0051 with open(sys.argv[1]) as f:
0052 output = json.load(f)
0053
0054 output["resources"] = convert_old_resources(output["resources"], sys.argv[1])
0055
0056 metrics = []
0057 for resource in output["resources"]:
0058 if "name" in resource:
0059 metrics.append(resource["name"])
0060 else:
0061 for key in resource:
0062 metrics.append(key)
0063
0064 datamap = { module["type"] + '|' + module["label"] : module for module in output["modules"] }
0065
0066 for arg in sys.argv[2:]:
0067 with open(arg) as f:
0068 input = json.load(f)
0069
0070 input["resources"] = convert_old_resources(input["resources"], arg)
0071
0072 if output["resources"] != input["resources"]:
0073 print("Error: input files describe different metrics")
0074 sys.exit(1)
0075
0076 if output["total"]["label"] != input["total"]["label"]:
0077 print("Warning: input files describe different process names")
0078 merge_into(metrics, input["total"], output["total"])
0079
0080 for module in input["modules"]:
0081 key = module["type"] + '|' + module["label"]
0082 if key in datamap:
0083 merge_into(metrics, module, datamap[key])
0084 else:
0085 datamap[key] = module
0086 output["modules"].append(datamap[key])
0087
0088 json.dump(output, sys.stdout, indent = 2 )
0089 sys.stdout.write('\n')