Skip to content

Commit

Permalink
Adding divided stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakayorihiro committed Nov 19, 2024
1 parent 46aeb9b commit e69630d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
9 changes: 7 additions & 2 deletions tools/profiler/new-profiler-approach.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ FSM_JSON=${TMP_DIR}/fsm.json
CELLS_JSON=${TMP_DIR}/cells.json
GROUPS_JSON=${TMP_DIR}/groups.json
OUT_DIR=${TMP_DIR}/out
FLAME_OUT_DIR=${TMP_DIR}/flame-out
rm -rf ${TREES_OUT_DIR}
OUT_JSON=${TMP_DIR}/dump.json
TIMELINE_VIEW_JSON=${TMP_DIR}/timeline.json
Expand Down Expand Up @@ -79,7 +80,7 @@ fi
echo "[${SCRIPT_NAME}] Using FSM info and VCD file to obtain cycle level counts"
(
set -o xtrace
python3 ${SCRIPT_DIR}/profiler-process.py ${VCD_FILE} ${CELLS_JSON} ${OUT_DIR} ${OUT_DIR}/flame.folded
python3 ${SCRIPT_DIR}/profiler-process.py ${VCD_FILE} ${CELLS_JSON} ${OUT_DIR} ${OUT_DIR}/flame.folded ${FLAME_OUT_DIR}
set +o xtrace
) &> ${LOGS_DIR}/gol-process

Expand All @@ -90,4 +91,8 @@ for f in $( ls ${OUT_DIR} | grep dot$ ); do
dot -Tpng ${OUT_DIR}/${f} > ${TREES_PDF_DIR}/${f}.png
done

${FLAMEGRAPH_DIR}/flamegraph.pl --countname="cycles" ${OUT_DIR}/flame.folded > ${OUT_DIR}/flame.svg
for folded in $( ls ${FLAME_OUT_DIR}/*.folded ); do
echo "Writing flame graph for ${folded}"
base_name=$( echo ${folded} | rev | cut -d. -f2- | rev )
${FLAMEGRAPH_DIR}/flamegraph.pl --countname="cycles" ${folded} > ${base_name}.svg
done
48 changes: 38 additions & 10 deletions tools/profiler/profiler-process.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import csv
import json
import os
import re
import shutil
import sys
import vcdvcd

DELIMITER = "__"
INVISIBLE = "gray"
TREE_PICTURE_LIMIT=300
DIVIDED_FLAME_MULTIPLIER=1000

def remove_size_from_name(name: str) -> str:
""" changes e.g. "state[2:0]" to "state" """
Expand Down Expand Up @@ -421,9 +419,32 @@ def create_path_dot_str_dict(path_dict):

return path_to_dot_str

def create_output(timeline_map, dot_out_dir, flame_out_file):
# create a tree where we divide cycles via par arms
# FIXME: rename?
def tree_helper(timeline_map):
stacks = {}
for i in timeline_map:
num_stacks = len(timeline_map[i])
cycle_slice = round(1 / num_stacks, 3)
last_cycle_slice = 1 - (cycle_slice * (num_stacks - 1))
acc = 0
for stack_list in timeline_map[i]:
stack_id = ";".join(stack_list)
slice_to_add = cycle_slice if acc < num_stacks - 1 else last_cycle_slice
if stack_id not in stacks:
stacks[stack_id] = slice_to_add * DIVIDED_FLAME_MULTIPLIER
else:
stacks[stack_id] += slice_to_add * DIVIDED_FLAME_MULTIPLIER
acc += 1

return stacks

def create_output(timeline_map, dot_out_dir, flame_out_file, flames_out_dir):

os.mkdir(dot_out_dir)
if not os.path.exists(dot_out_dir):
os.mkdir(dot_out_dir)
if not os.path.exists(flames_out_dir):
os.mkdir(flames_out_dir)

# make flame graph folded file
stacks = {} # stack to number of cycles
Expand All @@ -439,6 +460,11 @@ def create_output(timeline_map, dot_out_dir, flame_out_file):
for stack in stacks:
flame_out.write(f"{stack} {stacks[stack]}\n")

divided_stacks = tree_helper(timeline_map)
with open(os.path.join(flames_out_dir, "divided-flame.folded"), "w") as div_flame_out:
for stack in divided_stacks:
div_flame_out.write(f"{stack} {divided_stacks[stack]}\n")

# probably wise to not have a billion dot files.
if len(timeline_map) > TREE_PICTURE_LIMIT:
print(f"Simulation exceeds {TREE_PICTURE_LIMIT} cycles, skipping trees...")
Expand Down Expand Up @@ -474,30 +500,32 @@ def create_output(timeline_map, dot_out_dir, flame_out_file):
f.write(f'\t{path_to_dot_str[path_id]} [color="{INVISIBLE}"];\n')
f.write("}")

def main(vcd_filename, cells_json_file, dot_out_dir, flame_out):
def main(vcd_filename, cells_json_file, dot_out_dir, flame_out, flames_out_dir):
main_component, cells_to_components = read_component_cell_names_json(cells_json_file)
converter = VCDConverter(main_component, cells_to_components)
vcdvcd.VCDVCD(vcd_filename, callbacks=converter)
converter.postprocess()

new_timeline_map = create_traces(converter.active_elements_info, converter.call_stack_probe_info, converter.cell_invoke_caller_probe_info, converter.clock_cycles, cells_to_components, main_component)

create_output(new_timeline_map, dot_out_dir, flame_out)
create_output(new_timeline_map, dot_out_dir, flame_out, flames_out_dir)


if __name__ == "__main__":
if len(sys.argv) > 4:
if len(sys.argv) > 5:
vcd_filename = sys.argv[1]
cells_json = sys.argv[2]
dot_out_dir = sys.argv[3]
flame_out = sys.argv[4]
main(vcd_filename, cells_json, dot_out_dir, flame_out)
flames_out_dir = sys.argv[5] # tmp folder until I figure out how to get multiple outputs from fud2
main(vcd_filename, cells_json, dot_out_dir, flame_out, flames_out_dir)
else:
args_desc = [
"VCD_FILE",
"CELLS_JSON",
"DOT_FILE_DIR",
"FLAME_OUT"
"FLAME_OUT",
"FLAME_OUT_DIR"
]
print(f"Usage: {sys.argv[0]} {' '.join(args_desc)}")
print("CELLS_JSON: Run the `component_cells` tool")
Expand Down

0 comments on commit e69630d

Please sign in to comment.