对应异步社区资源HW-Paging-Policy:
1.
- ./paging-policy.py -s 0 -n 10 -p FIFO -c
策略过多本处便不全部给出了,以FIFO为例,结果如下:
其余的策略和随机因子也可尝试,主要观察hitrate
2.
FIFO:1,2,3,4,5,6,1、、、
LRU:1,2,3,4,5,6,1、、、
MRU:1,2,3,4,5,6,5,4、、、
当缓存大小>=可能被访问到的页数的时候,可大幅提升性能,达到OPT。
3.
参考代码如下:
#!/usr/bin/env python
import random
Max = 10
num = 10
arr = []
for i in range(0, num):
t = random.randint(0, Max - 1)
arr.append(t)
testFile = open('./test.txt', 'w')
for i in arr:
testFile.write(str(i) + '\n')
testFile.close()
FIFO:
LRU:
OPT:
LRU和FIFO已经接近OPT了。
4.
随机序列生成代码参考代码如下:
#!/usr/bin/env python
import random
Max = 100
num = 100
randCite = 0.8
randomHotPage = 0.2
arr = []
numList = set(range(0, Max))
hotPage = set()
coldPage = set()
while len(hotPage) < int(Max * randomHotPage):
i = random.choice(list(numList))
hotPage.add(i)
for i in range(0, num):
t = random.randint(0, Max - 1)
arr.append(t)
testFile = open('./test.txt', 'w')
for i in arr:
testFile.write(str(i) + '\n')
testFile.close()
LRU测试结果如下:
RAND测试结果如下:
CLOCK测试结果如下:
总的来说测试结果有随机性,个人认为理论上LRU的表现在局部序列上会好一些。
5.
拿书中这个例子为例:
此功能能够跟踪到内存的引用记录,要使用必须要缓存住每个虚拟内存页对应的虚拟页码。