import csv
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def parse_overhead_file(file_path):
	data = {}
	valid_values = ['CXS', "LOCK", "RELEASE", "SCHED-TIMER", "SCHED", "SCHED2", "UNLOCK"]
	with open(file_path, mode='r') as file:
		csv_reader = csv.reader(file)
		header = next(csv_reader)

		max_sched = 0

		for row in csv_reader:
			category = row[2].lstrip()
			value = round(float(row[7]) / 2200,1) #cycles to us

			if category == "SCHED-TIMER" or category == "SCHED":
				if value > max_sched:
					max_sched = value
			elif category in valid_values:
				data[category] = value

		data['SCHED'] = max_sched
	return data

def main():
	fs = 20
	ns = []
	njlp_cxs = []
	njlp_lock = []
	njlp_release = []
	njlp_sched = []
	njlp_unlock = []

	fmlp_cxs = []
	fmlp_lock = []
	fmlp_release = []
	fmlp_sched = []
	fmlp_unlock = []

	for i in range(10):
		n = 10*(i+1)
		njlp_filename = str(n) + '_EDZL-NJLP_stats.csv'
		fmlp_filename = str(n) + '_EDZL_stats.csv'
		
		njlp_data = parse_overhead_file(njlp_filename)
		fmlp_data = parse_overhead_file(fmlp_filename)

		ns.append(n)
		njlp_cxs.append(njlp_data['CXS'])
		fmlp_cxs.append(fmlp_data['CXS'])
		njlp_lock.append(njlp_data['LOCK'])
		fmlp_lock.append(fmlp_data['LOCK'])
		njlp_release.append(njlp_data['RELEASE'])
		fmlp_release.append(fmlp_data['RELEASE'])
		njlp_sched.append(njlp_data['SCHED'])
		fmlp_sched.append(fmlp_data['SCHED'])
		njlp_unlock.append(njlp_data['UNLOCK'])
		fmlp_unlock.append(fmlp_data['UNLOCK'])

	print(ns)
	print('CXS')
	print(njlp_cxs)
	print(fmlp_cxs)
	print('LOCK')
	print(njlp_lock)
	print(fmlp_lock)
	print('RELEASE')
	print(njlp_release)
	print(fmlp_release)
	print('SCHED')
	print(njlp_sched)
	print(fmlp_sched)
	print('UNLOCK')
	print(njlp_unlock)
	print(fmlp_unlock)

	plt.figure()
	plt.plot(ns, njlp_lock, marker='^', label='NJLP Lock')
	plt.plot(ns, njlp_unlock, marker='s', label='NJLP Unlock')
	plt.plot(ns, fmlp_lock, marker='^', linestyle=':', label='FMLP Lock')
	plt.plot(ns, fmlp_unlock, marker='s', linestyle=':', label='FMLP Unlock')

	plt.xlabel('# of tasks', fontsize = fs+4)
	plt.ylabel('microseconds', fontsize = fs+4)
	plt.xticks(fontsize=fs)
	plt.yticks(fontsize=fs)
	plt.ylim(5, 25)
	plt.legend(fontsize=fs-2, loc='upper center', bbox_to_anchor=(0.45, 1.3), ncol=4)
	fig = plt.gcf()
	fig.set_size_inches(12, 4)
	plt.savefig('lock_overhead_plot.pdf', bbox_inches='tight')

	plt.figure()
	plt.plot(ns, njlp_release, marker='o', linestyle=':', label='Track Release')
	plt.plot(ns, njlp_sched, marker='o', label='Track Sched')
	plt.plot(ns, fmlp_release, marker='x', linestyle=':', label='Untrack Release')
	plt.plot(ns, fmlp_sched, marker='x', label='Untrack Sched')

	plt.xlabel('# of tasks', fontsize = fs+4)
	plt.ylabel('microseconds', fontsize = fs+4)
	plt.xticks(fontsize=fs)
	plt.yticks(fontsize=fs)
	plt.ylim(5, 25)
	plt.legend(fontsize=fs-2, loc='upper center', bbox_to_anchor=(0.45, 1.3), ncol=4)
	fig = plt.gcf()
	fig.set_size_inches(12, 4)
	plt.savefig('sched_overhead_plot.pdf', bbox_inches='tight')

if __name__ == "__main__":
	main()
