-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessDistace.py
More file actions
62 lines (56 loc) · 2.14 KB
/
accessDistace.py
File metadata and controls
62 lines (56 loc) · 2.14 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
import matplotlib.pyplot as plt
import sys
import matplotlib
matplotlib.use('Agg')
#validation
if(len(sys.argv) != 2):
print("Invalid Arguments")
exit(0)
else:
fileName = str(sys.argv[1])
counter = 0
#we also count total acccess - the first access to each block
totalAccessCount = 0
#We maintain a hashmap of blocks vs their access number
AccessesSofar = {}
#We also maintain a hashmap of access distance vs its count
AccessesDist = {}
with open(fileName) as infile:
for line in infile:
line = line.rstrip().split(" ")
if(line[0] == "#eof"):
break
memAddr = int(line[2])
#for getting block number, we could use the starting address of the block as the unique identifier that would be setting the last 6 bits to 0
#or we could also divide by 64 to get the block number ie if its the nth block what is the value of n.
#dividing by 64 since its a power of two, can be done by right shifting by 6 bits.
blockNumber = memAddr >> 6
#print("%d %d %d" % (counter, memAddr, blockNumber))
prevAccess = AccessesSofar.get(blockNumber, -1)
if prevAccess == -1:
#If we are accessing the block for the first time
AccessesSofar[blockNumber] = counter
else:
dist = counter - prevAccess
distCount = AccessesDist.setdefault(dist, 0)
AccessesDist[dist] = distCount+1
AccessesSofar[blockNumber] = counter
#adding here sums up total number of access except the very first ones to each block.
totalAccessCount += 1
counter += 1
#plotting
CumulativeFnXaxis = []
CumulativeFnYaxis = []
countSofar = 0
#we need to sort the AccessesDistances by their value as hashmaps dont have an order.
for key, value in sorted(AccessesDist.items()):
countSofar += value
CumulativeFnXaxis.append(key)
CumulativeFnYaxis.append(float(countSofar)/totalAccessCount)
plt.plot(CumulativeFnXaxis, CumulativeFnYaxis)
plt.grid()
plt.xscale('log', base=10)
plt.xlabel("Access Distance in log10")
plt.ylabel("cumulative density function")
graphfile = "graph_"+fileName.split(".")[0]
plt.savefig(graphfile)