-
Notifications
You must be signed in to change notification settings - Fork 0
/
reuse_heatmap.py
199 lines (163 loc) · 5.86 KB
/
reuse_heatmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""
plot reuse time heatmap
usage:
1. run traceAnalyzer: `./traceAnalyzer /path/trace trace_format --common`,
this will generate some output, including reuse distribution result per-window, trace.reuseWindow_w300_rt
2. plot reuse heatmap using this script:
`python3 reuse_heatmap.py trace.reuseWindow_w300_rt`
"""
import os, sys
import re
import numpy as np
import copy
import numpy.ma as ma
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
from typing import List, Dict, Tuple
import logging
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from utils.trace_utils import extract_dataname
from utils.plot_utils import FIG_DIR, FIG_TYPE
logger = logging.getLogger("reuse_heatmap")
def _load_reuse_heatmap_data(datapath: str) -> Tuple[np.ndarray, int, int, float]:
"""load reuse heatmap plot data from C++ computation
Args:
datapath (str): the path of reuse heatmap data file
Returns:
Tuple[np.ndarray, int, int, float]: plot_data, time_granularity, time_window, log_base
"""
ifile = open(datapath)
data_line = ifile.readline()
desc_line = ifile.readline()
time_granularity, log_base = 0, 0
if "real time" in desc_line:
m = re.search(
r"# reuse real time distribution per window \(time granularity (?P<tg>\d+), time window (?P<tw>\d+)\)",
desc_line,
)
assert m is not None, (
"the input file might not be reuse heatmap data file, desc line "
+ desc_line
+ "data "
+ datapath
)
time_granularity = int(m.group("tg"))
time_window = int(m.group("tw"))
elif "virtual time" in desc_line:
m = re.search(
r"# reuse virtual time distribution per window \(log base (?P<lb>\d+\.?\d*), time window (?P<tw>\d+)\)",
desc_line,
)
assert m is not None, (
"the input file might not be reuse heatmap data file, desc line "
+ desc_line
+ "data "
+ datapath
)
log_base = float(m.group("lb"))
time_window = int(m.group("tw"))
else:
raise RuntimeError(
"the input file might not be reuse heatmap data file, desc line "
+ desc_line
+ "data "
+ datapath
)
reuse_time_distribution_list = []
for line in ifile:
if len(line.strip()) == 0:
continue
else:
reuse_time_distribution_list.append(
[int(i) for i in line.strip(",\n").split(",")]
)
ifile.close()
dim = max([len(l) for l in reuse_time_distribution_list])
plot_data = np.ones((len(reuse_time_distribution_list), dim))
for idx, l in enumerate(reuse_time_distribution_list):
plot_data[idx][: len(l)] = np.cumsum(np.array(l) / sum(l))
# plot_data = ma.array(plot_data, mask=plot_data<1e-12).T
# print(np.sum(plot_data, axis=1))
plot_data = plot_data.T
# print(plot_data)
# print(plot_data.shape)
# time_granularity is for real_time, log_base is for virtual time
# the time in real_time data is time_granularity * time, the time in virtual_time is log_base ** time
return plot_data, time_granularity, time_window, log_base
def plot_reuse_heatmap(datapath: str, figname_prefix: str = "") -> None:
"""
plot reuse heatmap
Args:
datapath (str): the path of reuse heatmap data file
Returns:
None
"""
if len(figname_prefix) == 0:
figname_prefix = extract_dataname(datapath)
plot_data_rt, time_granularity, time_window, log_base = _load_reuse_heatmap_data(
datapath + "_rt"
)
assert log_base == 0
# plot heatmap
cmap = copy.copy(plt.cm.jet)
# cmap = copy.copy(plt.cm.viridis)
cmap.set_bad(color="white", alpha=1.0)
img = plt.imshow(plot_data_rt, origin="lower", cmap=cmap, aspect="auto")
cb = plt.colorbar(img)
plt.gca().xaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(x * time_window / 3600))
)
plt.gca().yaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(x * time_granularity / 3600))
)
plt.xlabel("Time (hour)")
plt.ylabel("Reuse time (hour)")
plt.savefig(
"{}/{}_reuse_heatmap_rt.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
logger.info(
"plot saved to {}/{}_reuse_heatmap_rt.{}".format(
FIG_DIR, figname_prefix, FIG_TYPE
)
)
plot_data_vt, time_granularity, time_window, log_base = _load_reuse_heatmap_data(
datapath + "_vt"
)
assert time_granularity == 0
# plot heatmap
cmap = copy.copy(plt.cm.jet)
# cmap = copy.copy(plt.cm.viridis)
cmap.set_bad(color="white", alpha=1.0)
img = plt.imshow(plot_data_vt, origin="lower", cmap=cmap, aspect="auto")
cb = plt.colorbar(img)
plt.gca().xaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(x * time_window / 3600))
)
plt.gca().yaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(log_base**x))
)
plt.xlabel("Time (hour)")
plt.ylabel("Reuse time (# request)")
plt.savefig(
"{}/{}_reuse_heatmap_vt.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
logger.info(
"plot saved to {}/{}_reuse_heatmap_vt.{}".format(
FIG_DIR, figname_prefix, FIG_TYPE
)
)
if __name__ == "__main__":
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("datapath", type=str, help="data path")
ap.add_argument(
"--figname-prefix", type=str, default="", help="the prefix of figname"
)
p = ap.parse_args()
if p.datapath.endswith("_rt") or p.datapath.endswith("_vt"):
p.datapath = p.datapath[:-3]
plot_reuse_heatmap(p.datapath, p.figname_prefix)