-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.py
130 lines (109 loc) · 3.91 KB
/
node.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
import time
import json
import yaml
from algorithms.FIFO import FIFO
from algorithms.LFU import LFU
from algorithms.LRU import LRU
from algorithms.RR import RR
from algorithms.Belady import Belady
from helpers.mock_download import mock_download
config = yaml.safe_load(open("./config.yml"))
class Node():
def __init__(self, id, BeladyFreq, downloader):
self.cache = []
self.cpu = config['simulator']['vcpu'] * 100
self.id = id
self.data = None
self.policies = []
self.file_size = None
self.BeladyFreq = BeladyFreq
self.downloader = downloader
self.delay = config['simulator']['delay']
self.load_data()
def load_data(self):
with open('data.json') as json_file:
data = json.load(json_file)
self.data = data
with open('file_size.json') as json_file:
file_size = json.load(json_file)
self.file_size = file_size
cache_size = config['simulator']['cache']['size'] * 1024 * 1024
policy = config['simulator']['cache']['policy']
if policy == 'FIFO':
self.policy =FIFO(cache_size, self.file_size, self.downloader)
elif policy == 'LFU':
self.policy = LFU(cache_size, self.file_size, self.downloader)
elif policy == 'LRU':
self.policy = LRU(cache_size, self.file_size, self.downloader)
elif policy == 'RR':
self.policy = RR(cache_size, self.file_size, self.downloader)
elif policy == 'Belady':
self.policy = Belady(cache_size, self.file_size, self.downloader, self.BeladyFreq)
else:
print('Wrong policy')
os.exit(1)
def calucate_cache_score(self, files):
score = 0
for file in files:
if file in self.cache:
score += 1
return score / len(files)
def get_avalaible_cpu(self):
return self.cpu
def mock_execute(self):
print('Start mocking exectuion %d' % self.env.now)
yield self.env.timeout(5000)
print('End mocking exectuion %d' % self.env.now)
def get_swap(self):
return self.policy.get_swap_count()
# swap_count_list = []
# for policy in self.policies:
# swap_count_list.append(policy.get_swap_count())
# return swap_count_list
def get_hit(self):
return self.policy.get_hit_count()
# hit_count_list = []
# for policy in self.policies:
# hit_count_list.append(policy.get_hit_count())
# return hit_count_list
def get_miss(self):
return self.policy.get_miss_count()
# miss_count_list = []
# for policy in self.policies:
# miss_count_list.append(policy.get_miss_count())
# return miss_count_list
def get_download_size(self):
return self.policy.get_download_size()
# time_save_count_list = []
# for policy in self.policies:
# time_save_count_list.append(policy.get_saved_time())
# return time_save_count_list
def get_full_download_size(self):
return self.policy.get_full_download_size()
# download_time_count_list = []
# for policy in self.policies:
# download_time_count_list.append(policy.get_full_download_time())
# return download_time_count_list
def execute(self, job_id, msg):
job = job_id.split(":")
sleep_time = self.data[str(job[2])]["time"] / config['simulator']['divisor']
self.cpu -= self.data[str(job[2])]["cpu"]
for file in msg.get("ins"):
if file == "length":
break
# for policy in self.policies:
# policy.process(msg.get("ins").get(file).get("name"), True)
name = msg.get("ins").get(file).get("name")
if config['simulator']['cache']['enabled']:
self.policy.process(name, True)
else:
mock_download(self.file_size[name], self.downloader)
time.sleep(self.delay)
time.sleep(sleep_time)
for file in msg.get("outs"):
if file == "length":
break
if config['simulator']['cache']['enabled']:
self.policy.process(msg.get("outs").get(file).get("name"))
self.cpu += self.data[str(job[2])]["cpu"]
#self.memory += self.data[str(job[2])]["cpu"]