From 0f0a9ff0fe5f2541e0dc6b36e46844cc87fd2e48 Mon Sep 17 00:00:00 2001
From: niusiyuan <siyuan.niu@telecom-paristech.fr>
Date: Mon, 22 Jul 2019 12:30:29 +0200
Subject: [PATCH] daemon seems to work

---
 .../c++2/src_simulator/TMLTransaction.cpp     |  2 +-
 .../c++2/src_simulator/TMLTransaction.h       |  1 +
 simulators/c++2/src_simulator/app/TMLTask.cpp |  4 +--
 simulators/c++2/src_simulator/app/TMLTask.h   |  7 +++--
 simulators/c++2/src_simulator/arch/CPU.cpp    | 11 ++++++--
 simulators/c++2/src_simulator/arch/FPGA.cpp   | 17 ++++++++---
 .../c++2/src_simulator/arch/MultiCoreCPU.cpp  |  4 +--
 .../src_simulator/arch/OrderScheduler.cpp     |  4 ++-
 .../src_simulator/arch/SchedulableDevice.h    |  3 +-
 .../c++2/src_simulator/arch/SingleCoreCPU.cpp | 12 ++++++--
 simulators/c++2/src_simulator/definitions.cpp |  1 +
 .../c++2/src_simulator/sim/Simulator.cpp      | 28 ++++++++++++++-----
 12 files changed, 69 insertions(+), 25 deletions(-)

diff --git a/simulators/c++2/src_simulator/TMLTransaction.cpp b/simulators/c++2/src_simulator/TMLTransaction.cpp
index 66f2d39c8a..51c2b2eaf1 100755
--- a/simulators/c++2/src_simulator/TMLTransaction.cpp
+++ b/simulators/c++2/src_simulator/TMLTransaction.cpp
@@ -56,7 +56,7 @@ TMLTransaction::TMLTransaction():_runnableTime(0), _startTime(0), _length(0), _v
   
 }
 
-TMLTransaction::TMLTransaction(TMLCommand* iCommand, TMLLength iVirtualLength, TMLTime iRunnableTime, TMLChannel* iChannel):_runnableTime(iRunnableTime), _startTime(0), _length(0), _virtualLength(iVirtualLength), _command(iCommand),
+TMLTransaction::TMLTransaction(TMLCommand* iCommand, TMLLength iVirtualLength, TMLTime iRunnableTime, TMLChannel* iChannel):_runnableTime(iRunnableTime), _startTime(0), _length(0), _virtualLength(iVirtualLength), _command(iCommand),_endState(false),
 #ifdef PENALTIES_ENABLED
                                                                                                                             _idlePenalty(0), _taskSwitchingPenalty(0), //, _branchingPenalty(0),
 #endif
diff --git a/simulators/c++2/src_simulator/TMLTransaction.h b/simulators/c++2/src_simulator/TMLTransaction.h
index f316cc112e..7fcebbdd81 100644
--- a/simulators/c++2/src_simulator/TMLTransaction.h
+++ b/simulators/c++2/src_simulator/TMLTransaction.h
@@ -166,6 +166,7 @@ class TMLTransaction {
     return _startTime  + _length;
 #endif
   }
+
   ///Returns the idle panalty of the transaction
   /**
      \return Idle penalty
diff --git a/simulators/c++2/src_simulator/app/TMLTask.cpp b/simulators/c++2/src_simulator/app/TMLTask.cpp
index ad659e5e1e..b1d077de33 100755
--- a/simulators/c++2/src_simulator/app/TMLTask.cpp
+++ b/simulators/c++2/src_simulator/app/TMLTask.cpp
@@ -48,7 +48,7 @@ Ludovic Apvrille, Renaud Pacalet
 
 unsigned int TMLTask::_instanceCount=1;
 
-TMLTask::TMLTask(ID iID, Priority iPriority, std::string iName, CPU** iCPU, unsigned int iNoOfCPUs): WorkloadSource(iPriority), _ID(iID), _name(iName), _endLastTransaction(0), _currCommand(0), _firstCommand(0), _currentCPU(0), _cpus(iCPU), _noOfCPUs(iNoOfCPUs), _nextCellIndex(0), _comment(0), _busyCycles(0), _CPUContentionDelay(0), _noCPUTransactions(0), _justStarted(true), _myInstance(_instanceCount), /*_isScheduled(false),*/ _stateHash(0, 30) , _liveVarList(0), _hashInvalidated(true){
+TMLTask::TMLTask(ID iID, Priority iPriority, std::string iName, CPU** iCPU, unsigned int iNoOfCPUs, bool isDaemon): WorkloadSource(iPriority), _ID(iID), _name(iName), _endLastTransaction(0), _currCommand(0), _firstCommand(0), _currentCPU(0), _cpus(iCPU), _noOfCPUs(iNoOfCPUs), _isDaemon(isDaemon), _nextCellIndex(0), _comment(0), _busyCycles(0), _CPUContentionDelay(0), _noCPUTransactions(0), _justStarted(true), _myInstance(_instanceCount), /*_isScheduled(false),*/ _stateHash(0, 30) , _liveVarList(0), _hashInvalidated(true){
 	for (unsigned int i=0; i< _noOfCPUs; i++)
 		_cpus[i]->registerTask(this);
 #ifdef ADD_COMMENTS
@@ -59,7 +59,7 @@ TMLTask::TMLTask(ID iID, Priority iPriority, std::string iName, CPU** iCPU, unsi
 	if (_noOfCPUs==1) _currentCPU = _cpus[0];
 }
 
-TMLTask::TMLTask(ID iID, Priority iPriority, std::string iName, FPGA** iFPGA, unsigned int iNoOfFPGAs): WorkloadSource(iPriority), _ID(iID), _name(iName), _endLastTransaction(0), _currCommand(0), _firstCommand(0), _currentFPGA(0), _fpgas(iFPGA), _noOfFPGAs(iNoOfFPGAs), _nextCellIndex(0),_comment(0), _busyCycles(0), _FPGAContentionDelay(0), _noFPGATransactions(0), _justStarted(true), _myInstance(_instanceCount), 
+TMLTask::TMLTask(ID iID, Priority iPriority, std::string iName, FPGA** iFPGA, unsigned int iNoOfFPGAs, bool isDaemon): WorkloadSource(iPriority), _ID(iID), _name(iName), _endLastTransaction(0), _currCommand(0), _firstCommand(0), _currentFPGA(0), _fpgas(iFPGA), _noOfFPGAs(iNoOfFPGAs), _isDaemon(isDaemon), _nextCellIndex(0),_comment(0), _busyCycles(0), _FPGAContentionDelay(0), _noFPGATransactions(0), _justStarted(true), _myInstance(_instanceCount), 
  _stateHash(0, 30) , _liveVarList(0), _hashInvalidated(true){
 	for (unsigned int i=0; i< _noOfFPGAs; i++)
 		_fpgas[i]->registerTask(this);
diff --git a/simulators/c++2/src_simulator/app/TMLTask.h b/simulators/c++2/src_simulator/app/TMLTask.h
index 7dcc017b36..6516c55bb1 100755
--- a/simulators/c++2/src_simulator/app/TMLTask.h
+++ b/simulators/c++2/src_simulator/app/TMLTask.h
@@ -74,8 +74,8 @@ public:
 	\param iCPU Pointer to the CPUs the task is mapped onto
 	\param iNoOfCPUs Number of CPUs
     	*/
-	TMLTask(ID iID, Priority iPriority, std::string iName, CPU** iCPU, unsigned int iNoOfCPUs);
-	TMLTask(ID iID, Priority iPriority, std::string iName, FPGA** iFPGA, unsigned int iNoOfFPGAs);
+	TMLTask(ID iID, Priority iPriority, std::string iName, CPU** iCPU, unsigned int iNoOfCPUs, bool isDaemon);
+	TMLTask(ID iID, Priority iPriority, std::string iName, FPGA** iFPGA, unsigned int iNoOfFPGAs, bool isDaemon);
 	///Destructor
 	virtual ~TMLTask();
 	///Returns the priority of the task
@@ -235,6 +235,7 @@ public:
 	void setNextCellIndex(unsigned int n) {_nextCellIndex=n;}
 	unsigned int getNextCellIndex() const {return _nextCellIndex;}
 	void schedule2HTML(std::ofstream& myfile) const;
+	bool getIsDaemon() {return _isDaemon;};
 protected:
 	///ID of the task
 	ID _ID;
@@ -258,7 +259,9 @@ protected:
 	FPGA** _fpgas;
 	///Number of cores assigned to the task
 	unsigned int _noOfFPGAs;
+	bool _isDaemon;
 	unsigned int _nextCellIndex;
+
 #ifdef ADD_COMMENTS
 	///Comment list
 	CommentList _commentList;
diff --git a/simulators/c++2/src_simulator/arch/CPU.cpp b/simulators/c++2/src_simulator/arch/CPU.cpp
index 200ea8532e..f852c4bff6 100644
--- a/simulators/c++2/src_simulator/arch/CPU.cpp
+++ b/simulators/c++2/src_simulator/arch/CPU.cpp
@@ -209,8 +209,10 @@ void CPU::HW2HTML(std::ofstream& myfile) const {
 	unsigned int indexTrans=aCurrTransName.find_first_of(":");
 	std::string aCurrContent=aCurrTransName.substr(indexTrans+1,2);
 	writeHTMLColumn( myfile, aLength, cellClass, aCurrTrans->toShortString(), aCurrContent );
-
-	aCurrTime = aCurrTrans->getEndTime();
+        if(aCurrTrans->getCommand()->getTask()->getIsDaemon() == true && aCurrTrans->getEndTime() > _simulatedTime)
+	  aCurrTime = _simulatedTime;
+	else
+	  aCurrTime = aCurrTrans->getEndTime();
       }
     }
 		
@@ -276,7 +278,10 @@ void CPU::schedule2HTML(std::ofstream& myfile) const {
 
 	writeHTMLColumn( myfile, aLength, cellClass, aCurrTrans->toShortString() );
 
-	aCurrTime = aCurrTrans->getEndTime();
+	if(aCurrTrans->getCommand()->getTask()->getIsDaemon() == true && aCurrTrans->getEndTime() > _simulatedTime)
+	  aCurrTime = _simulatedTime;
+	else
+	  aCurrTime = aCurrTrans->getEndTime();
       }
     }
 		
diff --git a/simulators/c++2/src_simulator/arch/FPGA.cpp b/simulators/c++2/src_simulator/arch/FPGA.cpp
index b57ba1154a..56588d0fe7 100644
--- a/simulators/c++2/src_simulator/arch/FPGA.cpp
+++ b/simulators/c++2/src_simulator/arch/FPGA.cpp
@@ -236,8 +236,11 @@ std::cout<<"fpga addTransaction"<<std::endl;
 #ifdef DEBUG_FPGA
     std::cout<<"I am in finish!!!"<<std::endl;
 #endif
- 
-    if(_endSchedule == 0) _maxEndTime=max(_maxEndTime,_nextTransaction->getEndTime());
+    //_endSchedule=0;
+    // _maxEndTime=max(_maxEndTime,_nextTransaction->getEndTime())
+    //std::cout<<"end schedule is ~~~~~~~"<<_endSchedule<<std::endl;
+    if(_endSchedule == 0 && (!(_nextTransaction->getCommand()->getTask()->getIsDaemon()==true && _nextTransaction->getCommand()->getTask()->getNextTransaction(0)==0))) 
+      _maxEndTime=max(_maxEndTime,_nextTransaction->getEndTime());
     if(_reconfigNumber>0)
       _endSchedule=_maxEndTime+_reconfigNumber*_reconfigTime;
     else{
@@ -592,7 +595,10 @@ void FPGA::HW2HTML(std::ofstream& myfile)  {
 	std::string aCurrContent=aCurrTransName.substr(indexTrans+1,2);
 	writeHTMLColumn( myfile, aLength, cellClass, aCurrTrans->toShortString(), aCurrContent );
 
-	aCurrTime = aCurrTrans->getEndTime();
+	if(aCurrTrans->getCommand()->getTask()->getIsDaemon() == true && aCurrTrans->getEndTime() > _simulatedTime)
+	  aCurrTime = _simulatedTime;
+	else
+	  aCurrTime = aCurrTrans->getEndTime();
       }
     }	
 
@@ -656,7 +662,10 @@ void FPGA::schedule2HTML(std::ofstream& myfile)  {
 
 	writeHTMLColumn( myfile, aLength, cellClass, aCurrTrans->toShortString() );
 
-	aCurrTime = aCurrTrans->getEndTime();
+	if(aCurrTrans->getCommand()->getTask()->getIsDaemon() == true && aCurrTrans->getEndTime() > _simulatedTime)
+	  aCurrTime = _simulatedTime;
+	else
+	  aCurrTime = aCurrTrans->getEndTime();
       }
     }
 		
diff --git a/simulators/c++2/src_simulator/arch/MultiCoreCPU.cpp b/simulators/c++2/src_simulator/arch/MultiCoreCPU.cpp
index 85b05e636c..eb448b5e1d 100644
--- a/simulators/c++2/src_simulator/arch/MultiCoreCPU.cpp
+++ b/simulators/c++2/src_simulator/arch/MultiCoreCPU.cpp
@@ -357,8 +357,8 @@ std::cout << "CPU:calcSTL: addtransaction of CPU " << _name << ": " << _nextTran
  	 	
     }
     time++;
-
-    _simulatedTime=max(_simulatedTime,_nextTransaction->getEndTime());
+    if(!(_nextTransaction->getCommand()->getTask()->getIsDaemon()==true && _nextTransaction->getCommand()->getTask()->getNextTransaction(0)==0))
+      _simulatedTime=max(_simulatedTime,_nextTransaction->getEndTime());
     _overallTransNo++; //NEW!!!!!!!!
     _overallTransSize+=_nextTransaction->getOperationLength();  //NEW!!!!!!!!
     //std::cout << "lets crash execute\n";
diff --git a/simulators/c++2/src_simulator/arch/OrderScheduler.cpp b/simulators/c++2/src_simulator/arch/OrderScheduler.cpp
index b0e11cfce8..64d948f4e2 100644
--- a/simulators/c++2/src_simulator/arch/OrderScheduler.cpp
+++ b/simulators/c++2/src_simulator/arch/OrderScheduler.cpp
@@ -54,7 +54,7 @@ TMLTime OrderScheduler::schedule(TMLTime iEndSchedule){
 	WorkloadSource *aSourcePast=0, *aSourceFuture=0;  //NEW
 	for(WorkloadList::iterator i=_workloadList.begin(); i != _workloadList.end(); ++i){
 		aTempTrans=(*i)->getNextTransaction(iEndSchedule);
-		if(aTempTrans) std::cout<<"show temp trans"<<aTempTrans->toShortString()<<std::endl;
+		//	if(aTempTrans) std::cout<<"show temp trans"<<aTempTrans->toShortString()<<std::endl;
 		if (aTempTrans!=0 && aTempTrans->getVirtualLength()!=0 && aTempTrans->getCommand()->getTask()->getFPGA()){
 			aRunnableTime=aTempTrans->getRunnableTime();	
 			if (aRunnableTime<=iEndSchedule){
@@ -82,9 +82,11 @@ TMLTime OrderScheduler::schedule(TMLTime iEndSchedule){
 		_nextTransaction=aMarkerPast;
 		_lastSource=aSourcePast; //NEW
 	}
+#ifdef DEBUG_FPGA
 	if(_nextTransaction) std::cout<<"order next trans is "<<_nextTransaction->toShortString()<<std::endl;
 	else std::cout<<"order next trans is 0"<<std::endl;
 	std::cout<<"end order scheduler"<<std::endl;
+#endif
 	return 0;
 }
 
diff --git a/simulators/c++2/src_simulator/arch/SchedulableDevice.h b/simulators/c++2/src_simulator/arch/SchedulableDevice.h
index 7cb94897c8..475408c0a8 100644
--- a/simulators/c++2/src_simulator/arch/SchedulableDevice.h
+++ b/simulators/c++2/src_simulator/arch/SchedulableDevice.h
@@ -106,7 +106,7 @@ public:
 	\return Number of simulated clock cycles
 	*/
 	static TMLTime getSimulatedTime() { return _simulatedTime; }
-
+	static TMLTime getNonDaemonSimulatedTime() { return _nonDaemonSimulatedTime;}
 	///Sets the number of simulated clock cycles
 	/**
 	\param iSimulatedTime Number of simulated clock cycles
@@ -163,6 +163,7 @@ protected:
 	std::string _name;
 	///Class variable holding the simulation time
 	static TMLTime _simulatedTime;
+	static TMLTime _nonDaemonSimulatedTime;
 	///End time of the last scheduled transaction
 	TMLTime _endSchedule;
 
diff --git a/simulators/c++2/src_simulator/arch/SingleCoreCPU.cpp b/simulators/c++2/src_simulator/arch/SingleCoreCPU.cpp
index 3b93a80d89..3d9fc7ca63 100644
--- a/simulators/c++2/src_simulator/arch/SingleCoreCPU.cpp
+++ b/simulators/c++2/src_simulator/arch/SingleCoreCPU.cpp
@@ -295,9 +295,17 @@ std::cout<<"addTransaction"<<std::endl;
     //#endif
     //_nextTransaction->getCommand()->execute();  //NEW!!!!
     //std::cout << "CPU:addt: to be started" << std::endl;
-    _endSchedule=_nextTransaction->getEndTime();
+     _endSchedule=_nextTransaction->getEndTime();
+    std::cout<<"end schedule is"<<_endSchedule<<std::endl;
+ 
+    std::cout<<"simulated time is ---------"<<_simulatedTime<<std::endl;
+    
     //std::cout << "set end schedule CPU: " << _endSchedule << " startTime of trans:" << _nextTransaction->getStartTime() << " length of trans=" << _nextTransaction->getLength() <<"\n";
-    _simulatedTime=max(_simulatedTime,_endSchedule);
+    
+    if(!(_nextTransaction->getCommand()->getTask()->getIsDaemon()==true && _nextTransaction->getCommand()->getTask()->getNextTransaction(0)==0))
+      _simulatedTime=max(_simulatedTime,_endSchedule);
+    // else
+    //  _nextTransaction->setEndTime(_simulatedTime);
     _overallTransNo++; //NEW!!!!!!!!
     _overallTransSize+=_nextTransaction->getOperationLength();  //NEW!!!!!!!!
     //std::cout << "lets crash execute\n";
diff --git a/simulators/c++2/src_simulator/definitions.cpp b/simulators/c++2/src_simulator/definitions.cpp
index c968bc4b38..a022035aeb 100644
--- a/simulators/c++2/src_simulator/definitions.cpp
+++ b/simulators/c++2/src_simulator/definitions.cpp
@@ -53,6 +53,7 @@ Ludovic Apvrille, Renaud Pacalet
 #include <unistd.h>
 
 TMLTime SchedulableDevice::_simulatedTime=0;
+TMLTime SchedulableDevice::_nonDaemonSimulatedTime=0;
 TMLTime SchedulableDevice::_overallTransNo=0;
 TMLTime SchedulableDevice::_overallTransSize=0;
 ID TMLTransaction::_ID=1;
diff --git a/simulators/c++2/src_simulator/sim/Simulator.cpp b/simulators/c++2/src_simulator/sim/Simulator.cpp
index 8168118f94..f6d0d8c095 100644
--- a/simulators/c++2/src_simulator/sim/Simulator.cpp
+++ b/simulators/c++2/src_simulator/sim/Simulator.cpp
@@ -808,7 +808,7 @@ bool Simulator::channelImpactsCommand(TMLChannel* iCh, TMLCommand* iCmd){
 bool Simulator::simulate(TMLTransaction*& oLastTrans){
   TMLTransaction* depTransaction,*depNextTrans,*transLET;
   TMLCommand* commandLET,*depCommand,*depNextCommand;
-  TMLTask* depTask;
+  TMLTask* depTask,*lastNonDaemonTask=0;
   SchedulableDevice* deviceLET;
   CPU* depCPU;
   FPGA* depFPGA;
@@ -844,8 +844,23 @@ bool Simulator::simulate(TMLTransaction*& oLastTrans){
 #ifdef DEBUG_KERNEL
       std::cout << "kernel:simulate: scheduling decision: " <<  transLET->toString() << std::endl;
 #endif
-
 	commandLET=transLET->getCommand();
+
+	if(commandLET->getTask()->getIsDaemon()==false){
+	  lastNonDaemonTask=commandLET->getTask();
+	}
+
+	std::cout<<"transLET is********"<<transLET->toString()<<std::endl;
+	if(transLET!=0 && transLET->getCommand()->getTask()->getIsDaemon()==true){
+	  if(transLET->getStartTime() >= deviceLET->getSimulatedTime()){
+	    if(lastNonDaemonTask!=0 && lastNonDaemonTask->getNextTransaction(0)!=0)
+	      std::cout<<"next trans no 0********** "<< lastNonDaemonTask->getNextTransaction(0)->toString()<<std::endl;
+	    if(lastNonDaemonTask!=0 && lastNonDaemonTask->getNextTransaction(0)==0){
+	      break;
+	    }
+	  }
+	}
+       		
 #ifdef DEBUG_SIMULATE
 	std::cout<<"device is "<<deviceLET->getName()<<std::endl;
 #endif
@@ -856,6 +871,7 @@ bool Simulator::simulate(TMLTransaction*& oLastTrans){
 #ifdef DEBUG_KERNEL
       std::cout << "kernel:simulate: AFTER add trans: " << x << std::endl;
 #endif
+    
       if (x){
 #ifdef DEBUG_KERNEL
 	std::cout << "kernel:simulate: add transaction 0" << commandLET->toString() << std::endl;
@@ -975,11 +991,9 @@ bool Simulator::simulate(TMLTransaction*& oLastTrans){
 #ifdef DEBUG_SIMULATE
       std::cout<<"task is !!!!!"<<oLastTrans->toString()<<std::endl;
 #endif
-	transLET=getTransLowestEndTime(deviceLET);
-	//	if(transLET==0) std::cout<<"translet is 0~~~"<<std::endl;
-	//	if(_simComp->getStopFlag()==true) std::cout<<"stop flag is true"<<std::endl;
-	//	else std::cout<<"stop flag is false"<<std::endl;
-    }
+    
+      transLET=getTransLowestEndTime(deviceLET);
+  }
 
   bool aSimCompleted = ( transLET==0  && !_simComp->getStoppedOnAction());
 
-- 
GitLab