-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
67 lines (51 loc) · 1.71 KB
/
generator.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
"""
Trace file generator.
"""
import argparse
import os
import random
OUTPUT_DIRECTORY = 'data/'
class Generator:
"""
Generates random output file representing memory for given number of pages.
"""
def __init__(self, pages: int):
self.pages = pages
def generate(self):
"""
Generates output file `<number of pages>.trace` in `data` directory.
"""
self.create_data_dir()
filename: str = OUTPUT_DIRECTORY + str(self.pages) + '.trace'
with open(filename, 'w+') as file:
for _ in range(self.pages):
file.write(RandomPage().__repr__())
@staticmethod
def create_data_dir():
"""
Creates directory `data` in project root.
"""
if not os.path.exists(OUTPUT_DIRECTORY):
os.makedirs(OUTPUT_DIRECTORY)
VPN_UPPER_BOUND = 80 # max: 20**2-1 = 399
READ_PROBABILITY = 85 # in %
class RandomPage:
"""
Representation of page in memory consisting of random page address and read or write access.
"""
def __init__(self):
self.vpn = '{:05x}'.format(random.randint(0, VPN_UPPER_BOUND))
self.ppn = '{:03x}'.format(random.getrandbits(12))
self.r_w = 'W' if random.randint(0, 100) > READ_PROBABILITY else 'R'
def __repr__(self):
return self.vpn + self.ppn + ' ' + self.r_w + '\n'
def main():
"""
Allows to invoke generator from CLI and passing file size by `--pages <number>` argument.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--pages", default=250000, help="number of pages (output file size in lines)")
args = parser.parse_args()
Generator(int(args.pages)).generate()
if __name__ == "__main__":
main()