#!/usr/bin/python -u
import os
import xenprototypeModulesCarrier
from xenprototype.VirtualManagementLib.virtualManagement import PhysicalHost
from xenprototype.CpuAllocation.cpuFeedbackControl import *
from xenprototype.CpuAllocation.statistics import *

testName = "ControllerConfigurations"

rounds = 10
count = 1
prefix = "eXpErImEnT"
testName = prefix + testName

os.popen("rm " + str(testName) + "*")

#Set pin parameters

os.popen("ssh root@floresta \" xm vcpu-pin vm01 0 7 \"")
os.popen("ssh root@floresta \" xm vcpu-set 0 2 \"")
os.popen("ssh root@floresta \" xm vcpu-pin 0 0 0 \"")
os.popen("ssh root@floresta \" xm vcpu-pin 0 1 1 \"")

while (count <= rounds):

	#Perform the experiment using Controller Off

	physicalHost = PhysicalHost("floresta","xen+ssh")
	physicalHost.setSchedParametersCap(14,domainID)
	physicalHost.printSchedParameters()

	os.popen("ssh root@floresta \" killall measureThroughputServer.py \"")

	physicalHost = PhysicalHost("floresta","xen+ssh")

	cmd1 = "ssh root@tartaruga" +  " \" " + "iperf -su -l 64"  " \" "
	
	cmd2 = "iperf -c 10.0.1.3 -b 51200k -f k -l 64 -t 115 "

	subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)

	subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)
	

	medida = MeasureClient("floresta",50012,lista,100,str(str(testName) + "OffVM1_" + str(count)))

	MeasureClientThread("3",medida).start()	

	while (medida.getExperimentStatus() == True):
		os.popen("sleep 1")

	averageThroughputList=[]

	os.popen("sleep 5")

	#Compute statistics for VM1	

	statistic = Statistics(str(str(testName) + "OffVM1_" + str(count)),20)
	output = open(str(testName + "Average"),'a')
	output.write("1 " + str(statistic.average) + "\n")
	output.close()
	output = open(str(testName + "RMSE"),'a')
	output.write("1 " + str(statistic.rmse) + "\n")
	output.close()	


	averageThroughputList.append(statistic.average)

	del statistic
	del medida
	del physicalHost	
	
	#Perform the experiment using Theoretical Parameters

	os.popen("ssh root@floresta \" killall measureThroughputServer.py \"")

	physicalHost = PhysicalHost("floresta","xen+ssh")

	cmd1 = "ssh root@tartaruga" +  " \" " + "iperf -su -l 64"  " \" "
	
	cmd2 = "iperf -c 10.0.1.3 -b 51200k -f k -l 64 -t 115 "

	subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)

	subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)

	medida = MeasureClient("floresta",50012,lista,100,str(str(testName) + "OnTPVM1_"" + str(count)))

	desiredThroughput = 20000

	adpativeOn = True

	domainID = 1

	control = ThroughputControl(desiredThroughput,domainID,physicalHost,medida,adpativeOn)

	control.runFeedackControl()

	statistic = Statistics(str(str(testName) + "OnTPVM1_" + str(count)),20)

	output = open(str(testName + "Average"),'a')
	output.write("2 " + str(statistic.average) + "\n")
	output.close()
	output = open(str(testName + "RMSE"),'a')
	output.write("2 " + str(statistic.rmse) + "\n")
	output.close()	

	print "Controller Adpative 20k" + str(count) + " done"

	cmd1 = "killall iperf"

	cmd2 = "ssh root@tartaruga" +  " \" " + "killall iperf"  " \" "

	subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)

	subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)

	del statistic
	del physicalHost
	del control
	del medida

	os.popen("sleep 10")


###3#

	test = IperfTest(100,1,64,51007,"10.0.1.3",str(str(testName) + "OnTPVM1_" + str(count)),"146.164.69.149","5001")

	kp=float(-0.000003422)
	ki=float(0.000022158)

	physicalHost = PhysicalHost("floresta","xen+ssh")
	desiredThroughput = 20000
	samplePeriod = 1
	errorPercentageAcceptable = 0
	minPercentageSamplePeriod = 1
	constantCap = 0
	operationalPointThroughput = 40007
	control = ThroughputControl	(kp,ki,operationalPointThroughput,desiredThroughput,samplePeriod,errorPercentageAcceptable,minPercentageSamplePeriod,constantCap,3,physicalHost,test)
	control.runFeedackControl()

	statistic = Statistics(str(str(testName) + "OnTPVM1_" + str(count)),20)


	output = open(str(testName + "Average"),'a')
	output.write("2 " + str(statistic.average) + "\n")
	output.close()
	output = open(str(testName + "RMSE"),'a')
	output.write("2 " + str(statistic.rmse) + "\n")
	output.close()	

	print "Controller Theoretical Parameters " + str(count) + " done"

	del statistic
	del physicalHost
	del control
	del test

	os.popen("sleep 5")



	#Perform the experiment using Adjusted Parameters

	test = IperfTest(100,1,64,51007,"10.0.1.3",str(str(testName) + "OnAPVM1_" + str(count)),"146.164.69.149","5001")

	kp=float(-0.000003422)
	ki=float(0.000010158)

	physicalHost = PhysicalHost("floresta","xen+ssh")
	desiredThroughput = 20000
	samplePeriod = 1
	errorPercentageAcceptable = 0
	minPercentageSamplePeriod = 1
	constantCap = 0
	operationalPointThroughput = 40007
	control = ThroughputControl	(kp,ki,operationalPointThroughput,desiredThroughput,samplePeriod,errorPercentageAcceptable,minPercentageSamplePeriod,constantCap,3,physicalHost,test)
	control.runFeedackControl()

	statistic = Statistics(str(str(testName) + "OnAPVM1_" + str(count)),20)


	output = open(str(testName + "Average"),'a')
	output.write("3 " + str(statistic.average) + "\n")
	output.close()
	output = open(str(testName + "RMSE"),'a')
	output.write("3 " + str(statistic.rmse) + "\n")
	output.close()	

	print "Controller Adjusted Parameters " + str(count) + " done"

	del statistic
	del physicalHost
	del control
	del test

	os.popen("sleep 5")

	#Perform the experiment using Practical Solution

	test = IperfTest(100,1,64,51007,"10.0.1.3",str(str(testName) + "OnPSVM1_" + str(count)),"146.164.69.149","5001")

	kp=float(-0.000003422)
	ki=float(0.000010158)

	physicalHost = PhysicalHost("floresta","xen+ssh")
	desiredThroughput = 20000
	samplePeriod = 1
	errorPercentageAcceptable = 0.1
	minPercentageSamplePeriod = 1
	constantCap = 0
	operationalPointThroughput = 40007
	control = ThroughputControl	(kp,ki,operationalPointThroughput,desiredThroughput,samplePeriod,errorPercentageAcceptable,minPercentageSamplePeriod,constantCap,3,physicalHost,test)
	control.runFeedackControl()

	statistic = Statistics(str(str(testName) + "OnPSVM1_" + str(count)),20)


	output = open(str(testName + "Average"),'a')
	output.write("4 " + str(statistic.average) + "\n")
	output.close()
	output = open(str(testName + "RMSE"),'a')
	output.write("4 " + str(statistic.rmse) + "\n")
	output.close()	

	print "Controller Practical Solution " + str(count) + " done"

	del statistic
	del physicalHost
	del control

	os.popen("sleep 5")

	count = count + 1

os.popen('./ic.awk nrvar=1 ic=95 ' + str(testName + "Average") + ' >> ' + str(testName + "Average.dt") + "") 

os.popen('./ic.awk nrvar=1 ic=95 ' + str(testName + "RMSE") + ' >> ' + str(testName + "RMSE.dt") + "") 

