-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.py
84 lines (68 loc) · 2.2 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
import time
import json
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
class Node():
def __init__(self, id, BeladyFreq):
self.cache = []
self.cpu = 100
self.id = id
self.data = None
self.policies = []
self.file_size = None
self.BeladyFreq = BeladyFreq
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 = 1000 * 1024 * 1024
self.policies = [FIFO(cache_size, self.file_size),
LFU(cache_size, self.file_size),
LRU(cache_size, self.file_size),
RR(cache_size, self.file_size),
Belady(cache_size, self.file_size, self.BeladyFreq)]
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):
swap_count_list = []
for policy in self.policies:
swap_count_list.append(policy.get_swap_count())
return swap_count_list
def get_hit(self):
hit_count_list = []
for policy in self.policies:
hit_count_list.append(policy.get_hit_count())
return hit_count_list
def get_miss(self):
miss_count_list = []
for policy in self.policies:
miss_count_list.append(policy.get_miss_count())
return miss_count_list
def execute(self, job_id, msg):
job = job_id.split(":")
sleep_time = self.data[str(job[2])]["time"]
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"))
time.sleep(sleep_time)
for file in msg.get("outs"):
if file == "length":
break
for policy in self.policies:
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"]