-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreuse.py
168 lines (138 loc) · 4.72 KB
/
reuse.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
"""
plot reuse time distribution
usage:
1. run traceAnalyzer: `./traceAnalyzer /path/trace trace_format --common`,
this will generate some output, including reuse distribution result, trace.reuse
2. plot reuse distribution using this script:
`python3 reuse.py trace.reuse`
"""
import os
import sys
import re
import numpy as np
import matplotlib.pyplot as plt
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
from utils.data_utils import conv_to_cdf
logger = logging.getLogger("reuse")
def _load_reuse_data(datapath: str) -> Tuple[dict, dict]:
"""load reuse distribution plot data from C++ computation
Args:
datapath (str): the path of reuse data file
Returns:
Tuple[dict, dict]: reuse_rtime_count, reuse_vtime_count
"""
ifile = open(datapath)
data_line = ifile.readline()
desc_line = ifile.readline()
m = re.match(r"# reuse real time: freq \(time granularity (?P<tg>\d+)\)", desc_line)
assert m is not None, (
"the input file might not be reuse data file, desc line "
+ desc_line
+ " data "
+ datapath
)
rtime_granularity = int(m.group("tg"))
log_base = 1.5
reuse_rtime_count, reuse_vtime_count = {}, {}
for line in ifile:
if line[0] == "#" and "virtual time" in line:
m = re.match(
r"# reuse virtual time: freq \(log base (?P<lb>\d+\.?\d*)\)", line
)
assert m is not None, (
"the input file might not be reuse data file, desc line "
+ line
+ " data "
+ datapath
)
log_base = float(m.group("lb"))
break
elif len(line.strip()) == 0:
continue
else:
reuse_time, count = [int(i) for i in line.split(":")]
if reuse_time < -1:
print("find negative reuse time " + line)
reuse_rtime_count[reuse_time * rtime_granularity] = count
for line in ifile:
if len(line.strip()) == 0:
continue
else:
reuse_time, count = [int(i) for i in line.split(":")]
if reuse_time < -1:
print("find negative reuse time " + line)
reuse_vtime_count[log_base**reuse_time] = count
ifile.close()
return reuse_rtime_count, reuse_vtime_count
def plot_reuse(datapath: str, figname_prefix: str = "") -> None:
"""
plot reuse time distribution
Args:
datapath (str): the path of reuse data file
figname_prefix (str, optional): the prefix of figname. Defaults to "".
Returns:
None: None
"""
if len(figname_prefix) == 0:
figname_prefix = extract_dataname(datapath)
reuse_rtime_count, reuse_vtime_count = _load_reuse_data(datapath)
x, y = conv_to_cdf(None, data_dict=reuse_rtime_count)
if x[0] < 0:
x = x[1:]
y = [y[i] - y[0] for i in range(1, len(y))]
plt.plot([i / 3600 for i in x], y)
plt.grid(linestyle="--")
plt.ylim(0, 1)
plt.xlabel("Time (Hour)")
plt.ylabel("Fraction of requests (CDF)")
plt.savefig(
"{}/{}_reuse_rt.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.xscale("log")
plt.xticks(
np.array([60, 300, 3600, 86400, 86400 * 2, 86400 * 4]) / 3600,
["1 min", "5 min", "1 hour", "1 day", "", "4 day"],
rotation=28,
)
plt.savefig(
"{}/{}_reuse_rt_log.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
x, y = conv_to_cdf(None, data_dict=reuse_vtime_count)
if x[0] < 0:
x = x[1:]
y = [y[i] - y[0] for i in range(1, len(y))]
plt.plot([i for i in x], y)
plt.grid(linestyle="--")
plt.xlabel("Virtual time (# requests)")
plt.ylabel("Fraction of requests (CDF)")
plt.savefig(
"{}/{}_reuse_vt.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.xscale("log")
plt.savefig(
"{}/{}_reuse_vt_log.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
logger.info(
"reuse time plot saved to {}/{}_reuse_rt.{} and {}/{}_reuse_vt.{}".format(
FIG_DIR, figname_prefix, FIG_TYPE, 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()
plot_reuse(p.datapath, p.figname_prefix)