forked from chipsalliance/riscv-dv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cov.py
344 lines (313 loc) · 14.4 KB
/
cov.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Regression script for RISC-V random instruction generator
"""
import argparse
import os
import re
import sys
import logging
from scripts.lib import *
from scripts.spike_log_to_trace_csv import *
from scripts.ovpsim_log_to_trace_csv import *
from scripts.sail_log_to_trace_csv import *
from types import SimpleNamespace
LOGGER = logging.getLogger()
def build_cov(out, cfg, cwd, opts_vec, opts_cov):
"""Building the coverage collection framework
Args:
out : Output directory
cfg : Loaded configuration dictionary.
cwd : Filesystem path to RISCV-DV repo
opts_vec : Vector options
opts_cov : Coverage options
"""
# Convert key dictionary to argv variable
argv = SimpleNamespace(**cfg)
logging.info("Building the coverage collection framework")
build_cmd = ("python3 {}/run.py --simulator {} --simulator_yaml {} "
" --co -o {} --cov -tl {} {} --cmp_opts \"{} {}\" --noclean".format(
cwd, argv.simulator, argv.simulator_yaml, out,
argv.testlist, argv.opts, opts_vec, opts_cov))
if argv.target:
build_cmd += (" --target {}".format(argv.target))
if argv.custom_target:
build_cmd += (" --custom_target {}".format(argv.custom_target))
if argv.stop_on_first_error:
build_cmd += " --stop_on_first_error"
if argv.lsf_cmd != "":
build_cmd += (" --lsf_cmd \"{}\"".format(argv.lsf_cmd))
run_parallel_cmd([build_cmd], argv.timeout, debug_cmd=argv.debug)
else:
run_cmd(build_cmd, debug_cmd=argv.debug)
def sim_cov(out, cfg, cwd, opts_vec, opts_cov, csv_list):
"""Simulation the coverage collection
Args:
out : Output directory
cfg : Loaded configuration dictionary.
cwd : Filesystem path to RISCV-DV repo
opts_vec : Vector options
opts_cov : Coverage options
csv_list : The list of trace csv
"""
# Convert key dictionary to argv variable
argv = SimpleNamespace(**cfg)
logging.info(
"Collecting functional coverage from {} trace CSV".format(len(csv_list)))
test_name = "riscv_instr_cov_test"
base_sim_cmd = (
"python3 {}/run.py --simulator {} --simulator_yaml {} --noclean "
"--so -o {} --cov -tl {} {} "
"-tn {} --steps gen --sim_opts \"<trace_csv_opts> {} {} <visualization>\" "
.format(cwd, argv.simulator, argv.simulator_yaml, out,
argv.testlist,
argv.opts, test_name, opts_vec, opts_cov))
if argv.simulator == "pyflow" and argv.enable_visualization:
base_sim_cmd = re.sub("<visualization>", "--enable_visualization",
base_sim_cmd)
else:
base_sim_cmd = re.sub("<visualization>", "", base_sim_cmd)
if argv.target:
base_sim_cmd += (" --target {}".format(argv.target))
if argv.custom_target:
base_sim_cmd += (" --custom_target {}".format(argv.custom_target))
if argv.stop_on_first_error:
base_sim_cmd += " --stop_on_first_error"
trace_csv_opts = ""
batch_cnt = 1
sim_cmd_list = []
if argv.batch_size > 0:
batch_cnt = (len(csv_list) + argv.batch_size - 1) / argv.batch_size
logging.info(
"Batch size: {}, Batch cnt: {}".format(argv.batch_size, batch_cnt))
for i in range(len(csv_list)):
file_idx = 0
trace_idx = i
if argv.batch_size > 0:
file_idx = i / argv.batch_size
trace_idx = i % argv.batch_size
if argv.simulator == "pyflow":
if i == 0:
trace_csv_opts += (" --trace_csv={}".format(csv_list[i]))
else:
trace_csv_opts += (",{}".format(csv_list[i]))
else:
trace_csv_opts += (" +trace_csv_{}={}".format(trace_idx, csv_list[i]))
if (i == len(csv_list) - 1) or (
(argv.batch_size > 0) and (trace_idx == argv.batch_size - 1)):
sim_cmd = base_sim_cmd.replace("<trace_csv_opts>", trace_csv_opts)
sim_cmd += (" --log_suffix _{}".format(file_idx))
if argv.lsf_cmd == "":
logging.info(
"Processing batch {}/{}".format(file_idx + 1, batch_cnt))
run_cmd(sim_cmd, debug_cmd=argv.debug)
else:
sim_cmd += (" --lsf_cmd \"{}\"".format(argv.lsf_cmd))
sim_cmd_list.append(sim_cmd)
trace_csv_opts = ""
if argv.lsf_cmd != "":
run_parallel_cmd(sim_cmd_list, argv.timeout)
logging.info(
"Collecting functional coverage from {} trace CSV...done".format(len(
csv_list)))
def collect_cov(out, cfg, cwd):
"""Collect functional coverage from the instruction trace
Args:
out : Output directory
cfg : Loaded configuration dictionary.
cwd : Filesystem path to RISCV-DV repo
"""
# Convert key dictionary to argv variable
argv = SimpleNamespace(**cfg)
log_list = []
csv_list = []
if not argv.dir:
logging.error("Missing directory of trace log files")
sys.exit(RET_FAIL)
logging.info("Processing trace log under {}".format(argv.dir))
if not os.path.isdir(argv.dir) or not os.listdir(argv.dir):
if not argv.debug:
logging.error("Cannot find {} directory, or it is empty".format(argv.dir))
sys.exit(RET_FAIL)
if argv.core:
"""If functional coverage is being collected from an RTL core
implementation, the flow assumes that the core's trace logs have
already been converted to CSV files by the post_compare step of the
flow. """
trace_log = ("{}/{}_trace_log".format(out, argv.core))
run_cmd("find {} -name \"*.csv\" | sort > {}".format(argv.dir, trace_log))
else:
trace_log = ("{}/{}_trace_log".format(out, argv.iss))
run_cmd("find {} -name \"*.log\" | sort > {}".format(argv.dir, trace_log))
with open(trace_log) as f:
for line in f:
line = line.rstrip()
log_list.append(line)
csv = line[0:-4] + ".csv"
csv_list.append(csv)
if argv.steps == "all" or re.match("csv", argv.steps):
for i in range(len(log_list)):
log = log_list[i]
csv = csv_list[i]
# If a core target is defined, prioritize over ISS
if argv.core:
logging.info("Process {} log[{}/{}] : {}".format(
argv.core, i + 1, len(log_list), log))
else:
logging.info("Process {} log[{}/{}] : {}".format(
argv.iss, i + 1, len(log_list), log))
if argv.iss == "spike":
process_spike_sim_log(log, csv, 1)
elif argv.iss == "ovpsim":
process_ovpsim_sim_log(log, csv, argv.stop_on_first_error,
argv.dont_truncate_after_first_ecall,
1)
else:
logging.error(
"Full trace for {} is not supported yet".format(argv.iss))
sys.exit(RET_FAIL)
if argv.steps == "all" or re.match("cov", argv.steps):
opts_vec = ""
opts_cov = ""
if argv.vector_options:
opts_vec = ("{}".format(argv.vector_options))
if argv.coverage_options:
opts_cov = ("{}".format(argv.coverage_options))
if argv.compliance_mode:
opts_cov += " +define+COMPLIANCE_MODE"
# Building the coverage collection framework
if argv.simulator != "pyflow":
build_cov(out, cfg, cwd, opts_vec, opts_cov)
# Simulation the coverage collection
sim_cov(out, cfg, cwd, opts_vec, opts_cov, csv_list)
def setup_parser():
"""Create a command line parser.
Returns: The created parser.
"""
# Parse input arguments
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", type=str,
help="Output directory name", dest="o")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
default=False,
help="Verbose logging")
parser.add_argument("--dir", type=str,
help="Directory of trace log files")
parser.add_argument("-bz", "--batch_size", dest="batch_size", type=int,
default=0,
help="Number of CSV to process per run")
parser.add_argument("--compliance_mode", action="store_true", default=False,
help="Run the coverage model in compliance test mode")
parser.add_argument("-i", "--instr_cnt", dest="instr_cnt", type=int,
default=0,
help="Random instruction count for debug mode")
parser.add_argument("-to", "--timeout", dest="timeout", type=int,
default=1000,
help="Number of CSV to process per run")
parser.add_argument("-s", "--steps", type=str, default="all",
help="Run steps: csv,cov", dest="steps")
parser.add_argument("--core", type=str, default="",
help="Name of target core")
parser.add_argument("--isa", type=str, default="",
help="RISC-V ISA variant")
parser.add_argument("--iss", type=str, default="spike",
help="RISC-V instruction set simulator: spike,ovpsim,sail")
parser.add_argument("-tl", "--testlist", type=str, default="",
help="Regression testlist", dest="testlist")
parser.add_argument("--opts", type=str, default="",
help="Additional options for the instruction generator")
parser.add_argument("--lsf_cmd", type=str, default="",
help="LSF command. Run in local sequentially if lsf \
command is not specified")
parser.add_argument("--target", type=str, default="rv32imc",
help="Run the generator with pre-defined targets: \
rv32imc, rv32i, rv64imc, rv64gc")
parser.add_argument("-si", "--simulator", type=str, default="vcs",
help="Simulator used to run the generator, \
default VCS", dest="simulator")
parser.add_argument("--simulator_yaml", type=str, default="",
help="RTL simulator setting YAML")
parser.add_argument("-ct", "--custom_target", type=str, default="",
help="Directory name of the custom target")
parser.add_argument("-cs", "--core_setting_dir", type=str, default="",
help="Path for the riscv_core_setting.sv")
parser.add_argument("--stop_on_first_error", dest="stop_on_first_error",
action="store_true", default=False,
help="Stop on detecting first error")
parser.add_argument("--dont_truncate_after_first_ecall",
dest="dont_truncate_after_first_ecall",
action="store_true", default=False,
help="Do not truncate log and csv file on first ecall")
parser.add_argument("--noclean", action="store_true", default=False,
help="Do not clean the output of the previous runs")
parser.add_argument("--vector_options", type=str, default="",
help="Enable Vectors and set options")
parser.add_argument("--coverage_options", type=str, default="",
help="Controlling coverage coverpoints")
parser.add_argument("--exp", action="store_true", default=False,
help="Run generator with experimental features")
parser.add_argument("-d", "--debug", type=str, default="",
help="Generate debug command log file")
parser.add_argument("--enable_visualization", action="store_true",
default=False,
help="Enabling coverage report visualization for pyflow")
return parser
def load_config(args, cwd):
"""
Load configuration from the command line and the configuration file.
Args:
args: Parsed command-line configuration
Returns:
Loaded configuration dictionary.
"""
if args.debug:
args.debug = open(args.debug, "w")
if args.verbose:
args.opts += "-v"
if not args.simulator_yaml:
args.simulator_yaml = cwd + "/yaml/simulator.yaml"
# Disable ISS coverage if a core is passed in
if args.core:
args.iss = ""
# Keep the core_setting_dir option to be backward compatible, suggest to use
# --custom_target
if args.core_setting_dir:
if not args.custom_target:
args.custom_target = args.core_setting_dir
else:
args.core_setting_dir = args.custom_target
if not args.custom_target:
args.core_setting_dir = cwd + "/target/" + args.target
args.testlist = cwd + "/yaml/cov_testlist.yaml" ## needed if need to force
# Create loaded configuration dictionary.
cfg = vars(args)
return cfg
def main():
"""This is the main entry point."""
try:
parser = setup_parser()
args = parser.parse_args()
cwd = os.path.dirname(os.path.realpath(__file__))
setup_logging(args.verbose)
# Load configuration from the command line and the configuration file.
cfg = load_config(args, cwd)
# Create output directory
output_dir = create_output(args.o, args.noclean, "cov_out_")
# Collect coverage for the trace CSV
collect_cov(output_dir, cfg, cwd)
logging.info("Coverage results are saved to {}".format(output_dir))
sys.exit(RET_SUCCESS)
except KeyboardInterrupt:
logging.info("\nExited Ctrl-C from user request.")
sys.exit(130)
if __name__ == "__main__":
main()