Files
DRAMSys/DRAMSys/analyzer/scripts/plots.py
2017-02-28 21:05:24 +01:00

218 lines
8.1 KiB
Python
Executable File

import sys
import sqlite3
from memUtil import *
from math import *
import ntpath
import os
plots = []
def plot(function):
plots.append(function)
return function
@plot
def memory_utilisation_window(connection, tracePath, steps):
# This function determines the average memory bandwidth over time in
# percentage and in Gbit/s. The average bandwidth over time is done
# dividing the time into windows of the same length and getting the average
# bandwidth in each window. Through data from the database, DataStrobeEnd
# and DataStrobeBegin, it is possible to access when a data transfer begins
# or ends. Hence, it is achievable to check when a data transfer happens
# and if it occupies or is inside a time window. Then, it is attainable to
# determine the average bandwidth in percentage. Besides, extracting the
# data from the memory specs, it is feasible to calculate the maximum data
# rate of the memory and then determine the bandwidth in Gbit/s. The
# bandwidth data are then plotted in two graphics.
cursor = connection.cursor()
cursor.execute(" SELECT WindowSize FROM GeneralInfo ")
windowSize = float(cursor.fetchone()[0])
# All possible cases of data transfers inside a time window
queryFull = """ SELECT sum(DataStrobeEnd - DataStrobeBegin) FROM transactions Where DataStrobeBegin > ? and DataStrobeEnd < ?""" # The data transfer begins and ends inside the time window
queryEnd = """ SELECT sum(DataStrobeEnd - ?) FROM transactions Where DataStrobeBegin < ? and DataStrobeEnd > ? and DataStrobeEnd <=?""" # Only the end of the data transfer is inside the time window
queryBegin = """ SELECT sum(? - DataStrobeBegin) FROM transactions Where DataStrobeBegin >= ? and DataStrobeBegin < ? and DataStrobeEnd > ?""" # Only the beginning of the data transfer is inside the time window
queryPart = """ SELECT DataStrobeBegin FROM transactions Where DataStrobeBegin <= ? and DataStrobeEnd >= ?""" # The data transfer occupies all the time window
maxDataRate = maximum_data_rate(connection)
maximumPercentage = 0
bandwidthPercentage = [0] * (steps+1)
bandwidth = [0] * (steps+1)
bandwidthPercentage[0] = 0
bandwidth[0] = 0
for i in range(steps):
bandwidthPercentage[i+1] = 0
cursor.execute(queryPart, (i*windowSize, (i+1)*windowSize))
result = cursor.fetchone()
if(result is None):
cursor.execute(queryFull, (i*windowSize, (i+1)*windowSize))
result = cursor.fetchone()
if(result[0] is not None):
bandwidthPercentage[i+1] += int(result[0])
cursor.execute(queryEnd, (i*windowSize, i*windowSize, i*windowSize, (i+1)*windowSize))
result = cursor.fetchone()
if(result[0] is not None):
bandwidthPercentage[i+1] += int(result[0])
cursor.execute(queryBegin, ((i+1)*windowSize, i*windowSize, (i+1)*windowSize, (i+1)*windowSize))
result = cursor.fetchone()
if(result[0] is not None):
bandwidthPercentage[i+1] += int(result[0])
else:
bandwidthPercentage[i+1] = windowSize
bandwidthPercentage[i+1] = float(bandwidthPercentage[i+1]/windowSize)
bandwidth[i+1] = float(bandwidthPercentage[i+1])*float(maxDataRate)/1024
bandwidthPercentage[i+1] *= 100
if(maximumPercentage < 100 and maximumPercentage < bandwidthPercentage[i+1]):
maximumPercentage = bandwidthPercentage[i+1]
name = ntpath.basename(tracePath)
basename, extension = os.path.splitext(name)
outputFileNameGBPS = 'memory_utilization_gbps_' + basename + '.pdf'
outputFileNamePercent = 'memory_utilization_percent_' + basename + '.pdf'
outputFiles = "Output files are {0},{1}".format(outputFileNameGBPS,outputFileNamePercent)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
#windowSize/1000: picoseconds to nanoseconds conversion
time = np.arange(0, (steps+1)*windowSize/1000, windowSize/1000)
maxBandwidth = [maxDataRate/1024] * (steps+1)
plt.figure()
# Write data to a file for matlab:
outputFileNameBWMatlab = 'memory_utilization_percent_' + basename + '.txt'
f = open(outputFileNameBWMatlab, 'w')
for i in range(steps):
line = "{} {}\n".format(time[i], bandwidthPercentage[i])
f.write(line)
#Plot Bandwidth in Percent
plt.plot(time, bandwidthPercentage)
plt.xlabel('Time [ns]')
plt.ylabel('Bandwidth [%]')
plt.ylim(-1, maximumPercentage + (10 - maximumPercentage%10))
plt.grid(True)
pdf = PdfPages(outputFileNamePercent)
pdf.savefig()
pdf.close()
plt.close()
#Plot absolute bandwidth
plt.plot(time, bandwidth)
plt.plot(time, maxBandwidth)
plt.xlabel('Time [ns]')
plt.ylabel('Bandwidth [Gibit/s]')
#plt.ylim((-0.01)*float(maxDataRate)/1024, ((maximumPercentage + (10 - maximumPercentage%10))/100)*float(maxDataRate)/1024)
plt.grid(True)
pdf = PdfPages(outputFileNameGBPS)
pdf.savefig()
pdf.close()
plt.close()
return outputFiles
@plot
def power_window(connection, tracePath, steps):
outputFile = ""
cursor = connection.cursor()
cursor.execute(" SELECT * FROM Power")
result = cursor.fetchone()
if(result is not None):
time = [0] * (steps+1)
power = [0] * (steps+1)
time[0] = 0
power[0] = 0
#pow(10,9): seconds to nanoseconds conversion
time[1] = float(result[0])*pow(10,9)
power[1] = float(result[1])
for i in range((steps-1)):
result = cursor.fetchone()
time[i+2] = float(result[0])*pow(10,9)
power[i+2] = float(result[1])
name = ntpath.basename(tracePath)
basename, extension = os.path.splitext(name)
outputFileName = 'power_' + basename + '.pdf'
outputFile = "\n" + "Output file is {0}".format(outputFileName)
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
plt.plot(time, power)
plt.xlabel('Time [ns]')
plt.ylabel('Power [mW]')
plt.grid(True)
pdf = PdfPages(outputFileName)
pdf.savefig()
pdf.close()
plt.close()
return outputFile
@plot
def latency_histogram(connection, tracePath, steps):
# This function plots an histogram with access latencys
outputFile = ""
cursor = connection.cursor()
cursor.execute("SELECT ((p2.PhaseEnd - p1.PhaseBegin)/1000) FROM Transactions t, Phases p1, Phases p2 WHERE t.id = p1.Transact and t.id = p2.Transact and p1.PhaseName = \"REQ\" and p2.PhaseName = \"RESP\" ")
result = cursor.fetchall()
name = ntpath.basename(tracePath)
basename, extension = os.path.splitext(name)
outputFileName = 'hist_' + basename + '.pdf'
outputFile = "\n" + "Output file is {0}".format(outputFileName)
numberOfBins=50
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
plt.hist(result, bins=numberOfBins, histtype='barstacked', facecolor='green')
plt.grid(True)
plt.xlabel("Access Time [ns]")
plt.ylabel("Number (Frequency)")
pdf = PdfPages(outputFileName)
pdf.savefig()
pdf.close()
plt.close()
return outputFile
def generatePlots(pathToTrace):
connection = sqlite3.connect(pathToTrace)
#print("================================")
#print("Generating plots for {0}".format(pathToTrace))
outputFiles = ""
cursor = connection.cursor()
cursor.execute(" SELECT WindowSize FROM GeneralInfo")
windowSize = float(cursor.fetchone()[0])
if(windowSize == 0):
outputFiles = "No output file created. Check WindowSize and EnableWindowing configs."
else:
cursor.execute(" SELECT TraceEnd FROM GeneralInfo ")
traceEnd = float(cursor.fetchone()[0])
steps = int(ceil(traceEnd/windowSize))
for p in plots:
outputFiles += p(connection, pathToTrace, steps)
connection.close()
#print(outputFiles)
return outputFiles
if __name__ == "__main__":
path = sys.argv[1]
generatePlots(path)