Skip to content

Commit

Permalink
Make cycle.sh and resources.sh generate JSONs
Browse files Browse the repository at this point in the history
  • Loading branch information
polybeandip committed Sep 24, 2024
1 parent 5406ab6 commit 5eff826
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 96 deletions.
20 changes: 18 additions & 2 deletions frontends/queues/cycles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ shopt -s globstar

cd "$(dirname "$0")/../.." # move to root

for file in frontends/queues/tests/**/*.py; do
declare -a files=(frontends/queues/tests/**/*.py)
num_files=${#files[@]}

echo "{"

for (( i=0; i<${num_files}; i++ )); do
file="${files[$i]}"
name="$(basename $file .py)"
dir="$(dirname $file)"

Expand All @@ -15,5 +21,15 @@ for file in frontends/queues/tests/**/*.py; do
-s verilog.data "$dir/$name.data" \
-s jq.expr ".cycles" \
-q)"
echo "${file#*tests/}: $cycles"

echo -n "\"${file#*tests/}\" : $cycles"

# because JSON doesn't allow trailing ','s
if [ $i -ne $(( num_files - 1 )) ]; then
echo ","
else
echo ""
fi
done

echo "}"
177 changes: 87 additions & 90 deletions frontends/queues/plot.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,119 @@
import os
import sys
import json
import numpy as np
from enum import Enum
import matplotlib.pyplot as plt

stat = sys.argv[1]

class Logic(Enum):
RR = 1
STRICT = 2

def append_path_prefix(file):
path_to_script = os.path.dirname(__file__)
path_to_file = os.path.join(path_to_script, file)
return path_to_file

def parse(stat, file):
binheap_rr = []
specialized_rr = []
binheap_strict = []
specialized_strict = []
out = {
"binheap" : {
"round_robin" : {},
"strict" : {}
},
"specialized" : {
"round_robin" : {},
"strict" : {}
}
}

with open(file) as file:
if stat == "cycles":
for line in file:
split = line.strip().split(":")
tup = (split[0].split("flow")[0][-1], int(split[1]))
if "round_robin" in line:
if "binheap" in line:
binheap_rr.append(tup)
else:
specialized_rr.append(tup)
if "strict" in line:
if "binheap" in line:
binheap_strict.append(tup)
else:
specialized_strict.append(tup)
else:
data = file.read().strip()
for d in data.split("\n\n"):
split = d.split("\n")
name = split[0]
stats = json.loads("\n".join(split[1:]))
tup = (name.split("flow")[0][-1], stats[stat])
if "round_robin" in name:
if "binheap" in name:
binheap_rr.append(tup)
else:
specialized_rr.append(tup)
if "strict" in name:
if "binheap" in name:
binheap_strict.append(tup)
else:
specialized_strict.append(tup)

return (specialized_rr,
binheap_rr,
specialized_strict,
binheap_strict)

def draw(specialized, binheap, name, filename, details=None):
data = json.load(file)
for file, data in data.items():
if isinstance(data, dict):
data = data[stat]

flow_no = file.split('flow')[0][-1]

if "round_robin" in file:
if "binheap" in file:
out["binheap"]["round_robin"][flow_no] = data
else:
out["specialized"]["round_robin"][flow_no] = data
if "strict" in file:
if "binheap" in file:
out["binheap"]["strict"][flow_no] = data
else:
out["specialized"]["strict"][flow_no] = data

return out

def draw(data, stat, logic, unit):
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(20, 10, forward=True)
ax.set_title(name,
fontweight='bold',
fontsize=20)
ax.set_xlabel("number of flows",
fontsize=20)
if details is None:
ax.set_ylabel(stat,
fontsize=20)
ax.set_xlabel("number of flows", fontsize=20)
if unit is None:
ax.set_ylabel(stat, fontsize=20)
else:
ax.set_ylabel(f"{stat} ({details})",
fontsize=20)
specialized = ax.scatter(
list(map(lambda x: x[0], specialized)),
list(map(lambda x: x[1], specialized)),
c='b')
binheap = ax.scatter(
list(map(lambda x: x[0], binheap)),
list(map(lambda x: x[1], binheap)),
c='g')
ax.set_ylabel(f"{stat} ({unit})", fontsize=20)

file = ""

if logic == Logic.RR:
specialized = ax.scatter(
data["specialized"]["round_robin"].keys(),
data["specialized"]["round_robin"].values(),
c='b')
binheap = ax.scatter(
data["binheap"]["round_robin"].keys(),
data["binheap"]["round_robin"].values(),
c='g')

ax.set_title("Round Robin Queues", fontweight='bold', fontsize=20)
file = append_path_prefix(f"{stat}_round_robin")

elif logic == Logic.STRICT:
specialized = ax.scatter(
data["specialized"]["strict"].keys(),
data["specialized"]["strict"].values(),
c='b')
binheap = ax.scatter(
data["binheap"]["strict"].keys(),
data["binheap"]["strict"].values(),
c='g')

ax.set_title("Strict Queues", fontweight='bold', fontsize=20)
file = append_path_prefix(f"{stat}_strict")

plt.legend((specialized, binheap),
("Specialized (i.e. Cassandra style PIFO)", "Binary Heap"),
("Specialized (i.e. Cassandra style)", "Binary Heap"),
fontsize=12)
file = append_path_prefix(f"{stat}_{filename}")

plt.savefig(file)

print(f"Generated {file}.png")

# Parse data for round_robin and strict queues
(specialized_rr, binheap_rr, specialized_strict, binheap_strict) = ([], [], [], [])
stat = sys.argv[1]
data = {}
if stat == "total_time":
file1 = sys.argv[2]
file2 = sys.argv[3]

(specialized_cycles_rr,
binheap_cycles_rr,
specialized_cycles_strict,
binheap_cycles_strict) = parse("cycles", file1)
(specialized_slacks_rr,
binheap_slacks_rr,
specialized_slacks_strict,
binheap_slacks_strict) = parse("worst_slack", file2)

def map2(cycles, slacks):
cycles.sort(key=lambda c: c[0])
slacks.sort(key=lambda s: s[0])

def f(c,s):
return (c[0], (1000*c[1])/(7 - s[1]))

return list(map(f, cycles, slacks))
cycle_data = parse("cycles", file1)
slack_data = parse("worst_slack", file2)

specialized_rr = map2(specialized_cycles_rr, specialized_slacks_rr)
binheap_rr = map2(binheap_cycles_rr, binheap_slacks_rr)
specialized_strict = map2(specialized_cycles_strict, specialized_slacks_strict)
binheap_strict = map2(binheap_cycles_strict, binheap_slacks_strict)
data = cycle_data.copy()
for impl in data.keys():
for logic in data[impl].keys():
for flow_no in data[impl][logic].keys():
cycles = cycle_data[impl][logic][flow_no]
slack = slack_data[impl][logic][flow_no]
data[impl][logic][flow_no] = (1000 * cycles) / (7 - slack)
else:
file = sys.argv[2]
(specialized_rr, binheap_rr, specialized_strict, binheap_strict) = parse(stat, file)
data = parse(stat, file)

# Draw results
details = "μs" if stat == "total_time" else None
draw(specialized_rr, binheap_rr, "Round Robin Queues", "round_robin", details)
draw(specialized_strict, binheap_strict, "Strict Queues", "strict", details)
unit = "μs" if stat == "total_time" else None
draw(data, stat, Logic.RR, unit)
draw(data, stat, Logic.STRICT, unit)
23 changes: 19 additions & 4 deletions frontends/queues/resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ fi

cd "$(dirname "$0")/../.." # move to root

for file in frontends/queues/tests/**/*.py; do
declare -a files=(frontends/queues/tests/**/*.py)
num_files=${#files[@]}

echo "{"

for (( i=0; i<${num_files}; i++ )); do
file="${files[$i]}"
name="$(basename $file .py)"
dir="$(dirname $file)"

Expand All @@ -18,9 +24,18 @@ for file in frontends/queues/tests/**/*.py; do

if [ "$#" -eq 1 ]; then
resource=$(jq ".$1" <<< "$resources")
echo "${file#*tests/}: $resource"
echo -n "\"${file#*tests/}\" : $resource"
else
echo "\"${file#*tests/}\" :"
echo -n "$resources"
fi

# because JSON doesn't allow trailing ','s
if [ $i -ne $(( num_files - 1 )) ]; then
echo ","
else
newline=$'\n'
echo "${file#*tests/}${newline}$resources${newline}"
echo ""
fi
done

echo "}"

0 comments on commit 5eff826

Please sign in to comment.