Skip to content

Instantly share code, notes, and snippets.

@TravMurav
Created July 18, 2021 13:52
Show Gist options
  • Save TravMurav/d5b84825480bab1d56f145979111eeca to your computer and use it in GitHub Desktop.
Save TravMurav/d5b84825480bab1d56f145979111eeca to your computer and use it in GitHub Desktop.
Small tool that converts Linux clk_dump into DOT graph
#!/usr/bin/env python3
"""Small tool that converts clk_dump into DOT graph"""
#
# YOu can use it like this:
# ./clk2dot.py FILE | unflatten -fl 20 | dot -Tpng > graph.png
#
import sys
import json
def dfs_clk(data, src=""):
"""Use DFS to convert clk tree to DOT nodes
and links.
:data: TODO
:returns: TODO
"""
ret = ""
uid = 0
for node in data:
if type(data[node]) is not dict:
continue
rate = data[node]["rate"]
ret += f"{src}_{uid} [shape=box label=<<B>{node}</B><BR/>rate={rate}>];\n"
if src != "":
ret += f"{src} -> {src}_{uid};\n"
ret += dfs_clk(data[node], f"{src}_{uid}")
uid += 1
return ret
def clk_json2dot(data_str):
"""Convert json string from clk_dump to DOT string
representing the clk tree.
:data: json data from linux clk subsystem
:returns: DOT graph
"""
data = json.loads(data_str)
ret = "digraph CLK {\n"
ret += dfs_clk(data)
ret += "}"
return ret
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} FILE ")
exit(1)
with open(sys.argv[1], "r") as json_file:
print(clk_json2dot(json_file.read()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment