This repository was archived by the owner on Oct 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmsim
More file actions
executable file
·68 lines (50 loc) · 1.99 KB
/
vmsim
File metadata and controls
executable file
·68 lines (50 loc) · 1.99 KB
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
#!/usr/bin/env python3
import argparse
from pathlib import Path
from typing import Literal, get_args
from src import VMSimBase, VMSimClock, VMSimLRU, VMSimOpt, VMSimRand
AlgorithmStr = Literal["opt", "rand", "clock", "lru"] # for type hinting
# map to get the algorithm from a string input
ALGORITHM_MAP: dict[AlgorithmStr, VMSimBase] = {
"opt": VMSimOpt,
"rand": VMSimRand,
"clock": VMSimClock,
"lru": VMSimLRU,
}
class VMSimArgs(argparse.Namespace):
"""This just adds type hints to the `args` variable in the main function"""
n: int
a: AlgorithmStr
nv: bool # Additional arg I added for 'not verbose' when testing
tracefile: str
def main():
"""VMSim CLI entrypoint
Args:
n (int): Number of frames
a (Algorithms): Algorithm to use (clock, lru, opt, rand)
nv (bool): Enable non-verbose output
tracefile (str): Path to trace trace_file
Usage:
usage: vmsim [-h] -n N -a {opt,rand,clock,lru} [-nv] tracefile
Raises:
FileNotFoundError: If the trace trace_file does not exist
ValueError: If an non-positive integer is passed for the number of frames
"""
parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, required=True)
parser.add_argument(
"-a", type=str, choices=get_args(AlgorithmStr), required=True
) # `get_args` converts the literal type to a list of strings
parser.add_argument("-nv", action="store_false")
parser.add_argument("tracefile", type=str)
args: VMSimArgs = parser.parse_args(namespace=VMSimArgs())
if args.n < 1:
raise ValueError("Number of frames must be a positive integer")
trace_file_path = Path(args.tracefile)
if trace_file_path.is_file() is False:
raise FileNotFoundError(f"Trace trace_file does not exist: {trace_file_path}")
vmsim: VMSimBase = ALGORITHM_MAP[args.a](args.n)
vmsim.sim(trace_file_path.absolute(), verbose=args.nv)
print(str(vmsim))
if __name__ == "__main__":
main()