-
Notifications
You must be signed in to change notification settings - Fork 1
/
measureRAMPerformance.py
109 lines (98 loc) · 3.55 KB
/
measureRAMPerformance.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
"""
Script for measuring the RAM performance authomatically for all the mechanisms.
"""
import os
import csv
import numpy as np
# For storing the memory performance data.
folder = "memoryPerformance/"
ciphers = ["SABER=1", "KYBER=1", "NTRU=1", "NTRUP=1", "FRODO=1"]
massiffile = ["saber/saber", "kyber/kyber", "ntru/ntru", "ntrup/ntrup", "frodo/frodo"]
ext = ".out"
rm = "rm test"
make = "make test "
valgrind = "valgrind --tool=massif --stacks=yes --massif-out-file="
N = 1
def measureMemory():
"""
Measure the memory consumption for each cipher.
"""
for i in range(5):
for j in range(N):
# make with the memory option enabled, and desired operation
# Remove test binary
os.system(rm)
cmd = make + "test " + ciphers[i] + " MEMORY=1 DEBUG=1"
os.system(cmd)
# Profile memory with valgrind
cmd = valgrind + folder + massiffile[i] + "_" + str(j) + ext + " ./test"
print(cmd)
os.system(cmd)
def readThreeLines(file):
file.readline()
file.readline()
file.readline()
def getTotalMemory(file):
heap = int(file.readline().split("=")[1])
heapExtra = int(file.readline().split("=")[1])
stack = int(file.readline().split("=")[1])
return heap + heapExtra + stack
def getMemoryUsageAll():
"""
Read the output file generated by valgrind, and get the maximum amount of memory used per KEM
"""
for i in range(4):
# Get the array of values per operation
memUsageAll = []
for k in range(4):
# Get the maximum value for each iteration
memUsage = [] # Stores the max values for all files
for j in range(N):
fileN = folder + massiffile[i] + "1_" + str(j) + ext
memUsageOp = []
with open(fileN, "r") as file:
# Read the first three lines:
readThreeLines(file)
# Find the max value
for line in file:
# Search the following '#' character
if line[0] == "#":
# Read the following three lines
readThreeLines(file)
# Get the total amount of memory
total = getTotalMemory(file)
memUsageOp.append(total)
memUsage.append(memUsageOp.copy())
memUsageAll.append(memUsage.copy())
return memUsageAll
def getMemoryUsageKEM():
"""
Get the data of memory usage per KEM, all the operations
"""
# For each kem
totalValues = []
for i in range(5):
fileN = folder + massiffile[i] + "_TOTAL_0" + ext
values = []
print(fileN)
with open(fileN, "r") as file:
readThreeLines(file)
for line in file:
if line[0] == "#":
readThreeLines(file)
total = getTotalMemory(file)
values.append(total)
totalValues.append(values.copy())
return totalValues
def saveData(data, file, delimiter):
m = np.array(data, dtype=object)
mT = m.transpose()
with open(file, "w") as csvfile:
writer = csv.writer(csvfile, delimiter=delimiter)
cipherS = ["LightSaber", "Kyber512", "NTRUhps2048509", "NTRULPr653", "Frodo640"]
writer.writerow(cipherS)
writer.writerows(mT)
if __name__ == '__main__':
measureMemory()
m = getMemoryUsageKEM()
saveData(m, "memoryPerformance/memoryPerformance.csv", ",")