Simulator.cpp 88.2 KB
Newer Older
Ludovic Apvrille's avatar
Ludovic Apvrille committed
1
/*Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Daniel Knorreck,
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
  Ludovic Apvrille, Renaud Pacalet
  *
  * ludovic.apvrille AT telecom-paristech.fr
  *
  * This software is a computer program whose purpose is to allow the
  * edition of TURTLE analysis, design and deployment diagrams, to
  * allow the generation of RT-LOTOS or Java code from this diagram,
  * and at last to allow the analysis of formal validation traces
  * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
  * from INRIA Rhone-Alpes.
  *
  * This software is governed by the CeCILL  license under French law and
  * abiding by the rules of distribution of free software.  You can  use,
  * modify and/ or redistribute the software under the terms of the CeCILL
  * license as circulated by CEA, CNRS and INRIA at the following URL
  * "http://www.cecill.info".
  *
  * As a counterpart to the access to the source code and  rights to copy,
  * modify and redistribute granted by the license, users are provided only
  * with a limited warranty  and the software's author,  the holder of the
  * economic rights,  and the successive licensors  have only  limited
  * liability.
  *
  * In this respect, the user's attention is drawn to the risks associated
  * with loading,  using,  modifying and/or developing or reproducing the
  * software by the user in light of its specific status of free software,
  * that may mean  that it is complicated to manipulate,  and  that  also
  * therefore means  that it is reserved for developers  and  experienced
  * professionals having in-depth computer knowledge. Users are therefore
  * encouraged to load and test the software's suitability as regards their
  * requirements in conditions enabling the security of their systems and/or
  * data to be ensured and,  more generally, to use and operate it in the
  * same conditions as regards security.
  *
  * The fact that you are presently reading this means that you have had
  * knowledge of the CeCILL license and that you accept its terms.
  *
  */
Ludovic Apvrille's avatar
Ludovic Apvrille committed
40
#include <Simulator.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
41 42 43 44 45 46 47 48
//#include <Server.h>
//#include <ServerLocal.h>
#include <SimServSyncInfo.h>
#include <TMLCommand.h>
#include <TMLTransaction.h>
#include <SimComponents.h>
#include <IndeterminismSource.h>
#include <CPU.h>
Siyuan Niu's avatar
Siyuan Niu committed
49
#include <FPGA.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
50 51 52
#include <TMLTask.h>
#include <TMLChannel.h>
#include <ServerIF.h>
53 54
#include <Server.h>
#include <ServerLocal.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
55
#include <TMLEventChannel.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
56
#ifdef EBRDD_ENABLED
Daniel Knorreck's avatar
Daniel Knorreck committed
57 58
#include <EBRDD.h>
#include <EBRDDCommand.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
59
#include <ERC.h>
Daniel Knorreck's avatar
Daniel Knorreck committed
60
#endif
Siyuan Niu's avatar
Siyuan Niu committed
61
class CurrentComponents;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
62

63
Simulator::Simulator(SimServSyncInfo* iSyncInfo):_syncInfo(iSyncInfo), _simComp(_syncInfo->_simComponents), _busy(false), _simTerm(false),  _randChoiceBreak(_syncInfo->_simComponents), _wasReset(true), _longRunTime(0), _shortRunTime(-1), _replyToServer(true), _branchCoverage(60), _commandCoverage(100), _terminateExplore(false), _simDuration(0){
Daniel Knorreck's avatar
Daniel Knorreck committed
64
}
Ludovic Apvrille's avatar
Ludovic Apvrille committed
65 66

Simulator::~Simulator(){
67 68
  //if (_currCmdListener!=0) delete _currCmdListener;
  //if (_randChoiceBreak!=0) delete _randChoiceBreak;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
69 70
}

Siyuan Niu's avatar
Siyuan Niu committed
71
TMLTransaction* Simulator::getTransLowestEndTime(SchedulableDevice*& oResultDevice) const{
72 73 74 75
  //int tmp=0;
  TMLTransaction *aMarker=0, *aTempTrans;
  TMLTime aLowestTime=-1;
  SchedulableDevice* aTempDevice;
76

77
  //static unsigned int aTransitionNo=0;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
78
#ifdef DEBUG_KERNEL
79
  std::cout << "kernel:getTLET: before loop" << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
80
#endif
81 82
  //for(SchedulingList::const_iterator i=_simComp->_cpuList.begin(); i != _simComp->_cpuList.end(); ++i){
  //for(CPUList::const_iterator i=_simComp->getCPUIterator(false); i != _simComp->getCPUIterator(true); ++i){
83
  for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
84 85 86
    aTempDevice=*i;
    aTempTrans=aTempDevice->getNextTransaction();
    if (aTempTrans!=0 && aTempTrans->getVirtualLength()>0){
Ludovic Apvrille's avatar
Ludovic Apvrille committed
87
#ifdef DEBUG_KERNEL
88
      std::cout << "kernel:getTLET: transaction found on " << aTempDevice->toString() << ": " << aTempTrans->toString() << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
89
#endif
90 91 92 93
      //tmp++;
      if (aTempTrans->getEndTime() < aLowestTime){
        aMarker=aTempTrans;
        aLowestTime=aTempTrans->getEndTime();
94
        oResultDevice=aTempDevice;
95 96 97 98 99 100 101 102 103 104 105 106
      }
    }
    //#ifdef DEBUG_KERNEL
    else {
      /*if (!_simComp->couldCPUBeIdle(*i)){
        std::cout << "kernel:getTLET: no transaction found on " << aTempDevice->toString() << std::endl;
        std::cout << "Cry !!!!!!!!";
        //exit(1);
        }*/
    }
    //#endif
  }
Siyuan Niu's avatar
Siyuan Niu committed
107

108
  //if (tmp==1) std::cout << "trans only on one CPU " << oResultDevice->toString() << "\n";
Siyuan Niu's avatar
Siyuan Niu committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  for(FPGAList::const_iterator i=_simComp->getFPGAList().begin(); i != _simComp->getFPGAList().end(); ++i){
    aTempDevice=*i;
    aTempTrans=aTempDevice->getNextTransaction();
    if (aTempTrans!=0 && aTempTrans->getVirtualLength()>0){
#ifdef DEBUG_KERNEL
      std::cout << "kernel:getTLET: transaction found on " << aTempDevice->toString() << ": " << aTempTrans->toString() << std::endl;
#endif
      std::cout<<aTempTrans->toShortString()<<"getEndtime is "<<aTempTrans->getEndTime()<<std::endl;
      std::cout<<"alowest time is "<<aLowestTime<<std::endl;
      if (aTempTrans->getEndTime() < aLowestTime){
	std::cout<<"in!!!"<<std::endl;
        aMarker=aTempTrans;
        aLowestTime=aTempTrans->getEndTime();
        oResultDevice=aTempDevice;     
      }
    }
    //#ifdef DEBUG_KERNEL
    else {

    }
  }
130
  return aMarker;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
131
}
Siyuan Niu's avatar
Siyuan Niu committed
132
/*
Siyuan Niu's avatar
Siyuan Niu committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
TMLTransaction* Simulator::getTransLowestEndTimeFPGA(SchedulableDevice*& oResultDevice) const{
  //int tmp=0;
  TMLTransaction *aMarker=0, *aTempTrans;
  TMLTime aLowestTime=-1;
  SchedulableDevice* aTempDevice;

#ifdef DEBUG_KERNEL
  std::cout << "kernel:getTLET: before loop" << std::endl;
#endif
  for(FPGAList::const_iterator i=_simComp->getFPGAList().begin(); i != _simComp->getFPGAList().end(); ++i){
    aTempDevice=*i;
    aTempTrans=aTempDevice->getNextTransaction();
    if (aTempTrans!=0 && aTempTrans->getVirtualLength()>0){
#ifdef DEBUG_KERNEL
      std::cout << "kernel:getTLET: transaction found on " << aTempDevice->toString() << ": " << aTempTrans->toString() << std::endl;
#endif
      std::cout<<aTempTrans->toShortString()<<"getEndtime is "<<aTempTrans->getEndTime()<<std::endl;
      std::cout<<"alowest time is "<<aLowestTime<<std::endl;
      if (aTempTrans->getEndTime() < aLowestTime){
	std::cout<<"in!!!"<<std::endl;
        aMarker=aTempTrans;
        aLowestTime=aTempTrans->getEndTime();
        oResultDevice=aTempDevice;     
      }
    }
    //#ifdef DEBUG_KERNEL
    else {

    }
  }
  return aMarker;
}
Siyuan Niu's avatar
Siyuan Niu committed
165
*/
apvrille's avatar
apvrille committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

ID Simulator::schedule2GraphAUT(std::ostream& iAUTFile, ID iStartState, unsigned int& oTransCounter) const{
  CPUList::iterator i;
  //std::cout << "entry graph output\n";
  GraphTransactionQueue aQueue;
  TMLTransaction* aTrans, *aTopElement;
  ID aStartState=iStartState, aEndState=0;
  for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
    aTrans = (*i)->getTransactions1By1(true);
    if (aTrans!=0) aQueue.push(aTrans);
  }
  //std::ostringstream aOutp;
  while (!aQueue.empty()){
    CPU* aCPU;
    aTopElement = aQueue.top();
    aCPU = aTopElement->getCommand()->getTask()->getCPU();
    aEndState = aTopElement->getStateID();
    if (aEndState==0){
      aEndState=TMLTransaction::getID();
      TMLTransaction::incID();
    }
    //13 -> 17 [label = "i(CPU0__test1__TMLTask_1__wro__test1__ch<4 ,4>)"];
    oTransCounter++;
    //(20,"i(CPU0__test1__TMLTask_1__wr__test1__ch<4 ,4>)", 24)
190
    //std::cout << "(" << aStartState << "," << "\"i(" << aCPU->toString() << "__" << aTopElement->getCommand()->getTask()->toString() << "__" << aTopElement->getCommand()->getCommandStr();
apvrille's avatar
apvrille committed
191 192 193
    iAUTFile << "(" << aStartState << "," << "\"i(" << aCPU->toString() << "__" << aTopElement->getCommand()->getTask()->toString() << "__" << aTopElement->getCommand()->getCommandStr();
    if (aTopElement->getChannel()!=0){
      iAUTFile << "__" << aTopElement->getChannel()->toShortString();
194
      //std::cout << "__" << aTopElement->getChannel()->toShortString();
apvrille's avatar
apvrille committed
195 196
    }
    iAUTFile << "<" << aTopElement->getVirtualLength() << ">)\"," << aEndState <<")\n";
197
    //std::cout << "<" << aTopElement->getVirtualLength() << ">)\"," << aEndState <<")\n";
apvrille's avatar
apvrille committed
198 199 200 201 202 203 204 205 206 207
    aStartState = aEndState;
    aQueue.pop();
    aTrans = aCPU->getTransactions1By1(false);
    if (aTrans!=0) aQueue.push(aTrans);
  }
  //std::cout << "exit graph output\n";
  return aStartState;
}


Daniel Knorreck's avatar
Daniel Knorreck committed
208
ID Simulator::schedule2GraphDOT(std::ostream& iDOTFile, std::ostream& iAUTFile, ID iStartState, unsigned int& oTransCounter) const{
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  CPUList::iterator i;
  //std::cout << "entry graph output\n";
  GraphTransactionQueue aQueue;
  TMLTransaction* aTrans, *aTopElement;
  ID aStartState=iStartState, aEndState=0;
  for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
    aTrans = (*i)->getTransactions1By1(true);
    if (aTrans!=0) aQueue.push(aTrans);
  }
  //std::ostringstream aOutp;
  while (!aQueue.empty()){
    CPU* aCPU;
    aTopElement = aQueue.top();
    aCPU = aTopElement->getCommand()->getTask()->getCPU();
    aEndState = aTopElement->getStateID();
    if (aEndState==0){
      aEndState=TMLTransaction::getID();
      TMLTransaction::incID();
    }
    //13 -> 17 [label = "i(CPU0__test1__TMLTask_1__wro__test1__ch<4 ,4>)"];
    oTransCounter++;
    iDOTFile << aStartState << " -> " << aEndState << " [label = \"i(" << aCPU->toString() << "__" << aTopElement->getCommand()->getTask()->toString() << "__" << aTopElement->getCommand()->getCommandStr();
    if (aTopElement->getChannel()!=0){
      iDOTFile << "__" << aTopElement->getChannel()->toShortString();
    }
    iDOTFile << "<" << aTopElement->getVirtualLength() << ">)\"]\n";
    //(20,"i(CPU0__test1__TMLTask_1__wr__test1__ch<4 ,4>)", 24)
    iAUTFile << "(" << aStartState << "," << "\"i(" << aCPU->toString() << "__" << aTopElement->getCommand()->getTask()->toString() << "__" << aTopElement->getCommand()->getCommandStr();
    if (aTopElement->getChannel()!=0){
      iAUTFile << "__" << aTopElement->getChannel()->toShortString();
    }
    iAUTFile << "<" << aTopElement->getVirtualLength() << ">)\"," << aEndState <<")\n";
    aStartState = aEndState;
    aQueue.pop();
    aTrans = aCPU->getTransactions1By1(false);
    if (aTrans!=0) aQueue.push(aTrans);
  }
  //std::cout << "exit graph output\n";
  return aStartState;
Daniel Knorreck's avatar
Daniel Knorreck committed
248 249
}

Daniel Knorreck's avatar
Daniel Knorreck committed
250
void Simulator::schedule2Graph(std::string& iTraceFileName) const{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  struct timeval aBegin,aEnd;
  gettimeofday(&aBegin,NULL);
  std::ofstream myfile (iTraceFileName.c_str());
  if (myfile.is_open()){
    CPUList::iterator i;
    GraphTransactionQueue aQueue;
    TMLTransaction* aTrans, *aTopElement;
    unsigned int aTransitionNo=0;
    //for(CPUList::const_iterator i=_simComp->getCPUIterator(false); i != _simComp->getCPUIterator(true); ++i){
    for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
      aTrans = (*i)->getTransactions1By1(true);
      if (aTrans!=0) aQueue.push(aTrans);
    }
    std::ostringstream aOutp;
    while (!aQueue.empty()){
      //std::ostringstream aTempStr;
      CPU* aCPU;
      aTopElement = aQueue.top();
      aCPU = aTopElement->getCommand()->getTask()->getCPU();
      for (TMLLength a=0; a < aTopElement->getVirtualLength(); a++){
        aOutp << "(" << aTransitionNo << ",\"i(" << aCPU->toString() << "__" << aTopElement->getCommand()->getTask()->toString() << "__" << aTopElement->getCommand()->getCommandStr();
        if (aTopElement->getChannel()!=0){
          aOutp << "__" << aTopElement->getChannel()->toShortString();
          //if (dynamic_cast<TMLEventChannel*>(aTopElement->getChannel())==0) aOutp << "<" << aTopElement->getVirtualLength() << ", " << ">";
        }
        aOutp << ")\"," << ++aTransitionNo << ")\n";

        //aOutp << aTempStr.str() << ++aTransitionNo << ")\n";
      }
      //myfile << aTempStr.str();
      aQueue.pop();
      aTrans = aCPU->getTransactions1By1(false);
      if (aTrans!=0) aQueue.push(aTrans);
    }
    myfile << "des (0, " << aTransitionNo+1 << ", " << aTransitionNo+2 << ")\n";
    myfile <<  aOutp.str() << "(" << aTransitionNo << ",\"i(exit)\", " << aTransitionNo+1 << ")\n";
    myfile.close();
  }
  else
    std::cout << "Unable to open Graph output file" << std::endl;
  gettimeofday(&aEnd,NULL);
  std::cout << "The Graph output took " << getTimeDiff(aBegin,aEnd) << "usec. File: " << iTraceFileName << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
293 294
}

Daniel Knorreck's avatar
Daniel Knorreck committed
295
void Simulator::schedule2TXT(std::string& iTraceFileName) const{
296 297
  struct timeval aBegin,aEnd;
  gettimeofday(&aBegin,NULL);
298 299

  if ( !ends_with( iTraceFileName, EXT_TXT ) ) {
300
    iTraceFileName.append( EXT_TXT );
301 302 303
  }

  std::ofstream myfile(iTraceFileName.c_str());
304 305
  if (myfile.is_open()){
    //for(CPUList::const_iterator i=_simComp->getCPUIterator(false); i != _simComp->getCPUIterator(true); ++i){
306
    for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
307
      (*i)->schedule2TXT(myfile);
Siyuan Niu's avatar
Siyuan Niu committed
308 309 310
    }    
    for(FPGAList::const_iterator i=_simComp->getFPGAList().begin(); i != _simComp->getFPGAList().end(); ++i){
      (*i)->schedule2TXT(myfile);
311 312 313 314 315 316 317
    }
    //for(BusList::const_iterator j=_simComp->getBusIterator(false); j != _simComp->getBusIterator(true); ++j){
    for(BusList::const_iterator j=_simComp->getBusList().begin(); j != _simComp->getBusList().end(); ++j){
      (*j)->schedule2TXT(myfile);
    }
    myfile.close();
  }
318
  else {
319
    std::cout << "Unable to open text output file." << std::endl;
320 321
  }

322 323
  gettimeofday(&aEnd,NULL);
  std::cout << "The text output took " << getTimeDiff(aBegin,aEnd) << "usec. File: " << iTraceFileName << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
324 325
}

326 327
int Simulator::allTrans2XML(std::ostringstream& glob, int maxNbOfTrans) const{
  int total = 0;
328
  //glob << TAG_TRANSo << "Transaction" << TAG_TRANSc << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
329
  for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
330
    total += (*i)->allTrans2XML(glob, maxNbOfTrans);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
331
  }
332

Ludovic Apvrille's avatar
Ludovic Apvrille committed
333
  for(BusList::const_iterator j=_simComp->getBusList().begin(); j != _simComp->getBusList().end(); ++j){
334
    total += (*j)->allTrans2XML(glob, maxNbOfTrans);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
335
  }
336 337

  return total;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
338 339
}

Letitia Li's avatar
Letitia Li committed
340
void Simulator::latencies2XML(std::ostringstream& glob, int id1, int id2) {
341
  for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
Letitia Li's avatar
Letitia Li committed
342 343 344 345 346 347 348
    (*i)->latencies2XML(glob, id1, id2);
  }

  for(BusList::const_iterator j=_simComp->getBusList().begin(); j != _simComp->getBusList().end(); ++j){
    (*j)->latencies2XML(glob, id1,id2);
  }
}
Ludovic Apvrille's avatar
Ludovic Apvrille committed
349

350
void Simulator::schedule2HTML(std::string& iTraceFileName) const {
Siyuan Niu's avatar
Siyuan Niu committed
351
#ifdef DEBUG_HTML
Siyuan Niu's avatar
Siyuan Niu committed
352
std::cout<<"schedule2HTML--------------------------------------******************"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
353
#endif
354
  struct timeval aBegin,aEnd;
355 356
  time_t aRawtime;
  struct tm * aTimeinfo;
357
  gettimeofday(&aBegin,NULL);
358 359
  time(&aRawtime);
  aTimeinfo=localtime(&aRawtime);
360 361

  if ( !ends_with( iTraceFileName, EXT_HTML ) ) {
362
    iTraceFileName.append( EXT_HTML );
363
  }
364
  
365 366

  std::ofstream myfile(iTraceFileName.c_str());
Siyuan Niu's avatar
Siyuan Niu committed
367
   //myfile<<"model name: "<<iTraceFileName.c_str();
Siyuan Niu's avatar
Siyuan Niu committed
368
 
369 370

  if (myfile.is_open()) {
371
    // DB: Issue #4
372 373 374
    myfile << SCHED_HTML_DOC; // <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n
    myfile << SCHED_HTML_BEG_HTML; // <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n
    myfile << SCHED_HTML_BEG_HEAD; // <head>\n
375 376
    const std::string::size_type findSlash = iTraceFileName.find_last_of( "/" );
    unsigned int indexSlash;
377

378
    if ( findSlash == std::string::npos ) {
Siyuan Niu's avatar
Siyuan Niu committed
379
      indexSlash = -1;
380
      //myfile<<"indexSlash=0\n";
381 382
    }
    else {
383
      indexSlash = findSlash;
384 385
    }

386
    const std::string ext( EXT_HTML );
Siyuan Niu's avatar
Siyuan Niu committed
387
    const std::string cssFileName = iTraceFileName.substr( indexSlash + 1, iTraceFileName.length() - indexSlash - ext.length() - 1 ) + EXT_CSS; 
Siyuan Niu's avatar
Siyuan Niu committed
388
    const std::string jsFileName = iTraceFileName.substr( indexSlash + 1, iTraceFileName.length() - indexSlash - ext.length() - 1 ) + EXT_JS;
389
    //myfile<<"length is "<< iTraceFileName.length() - indexSlash - ext.length() - 1<<std::endl;
390
    const std::string cssFullFileName = iTraceFileName.substr( 0, indexSlash + 1 ) + cssFileName;
Siyuan Niu's avatar
Siyuan Niu committed
391
    const std::string jsFullFileName =  iTraceFileName.substr( 0, indexSlash + 1 ) + jsFileName;
392
    std::ofstream cssfile( cssFullFileName.c_str() );
Siyuan Niu's avatar
Siyuan Niu committed
393 394
    std::ofstream jsfile( jsFullFileName.c_str() );
    
395
    //myfile<<"full name is "<<cssFullFileName<<std::endl;
396
    if ( cssfile.is_open() ) {
397 398
      cssfile << SCHED_HTML_CSS_CONTENT;
      cssfile.close();
399

400
      myfile << SCHED_HTML_CSS_BEG_LINK;
Siyuan Niu's avatar
Siyuan Niu committed
401
      myfile << cssFileName;     
402
      myfile << SCHED_HTML_CSS_END_LINK;
403 404
    }
    else {
405 406 407
      myfile << SCHED_HTML_BEG_STYLE; // <style>\n";
      myfile << SCHED_HTML_CSS_CONTENT;
      myfile << SCHED_HTML_END_STYLE; // <style>\n";
408 409
    }

410 411 412 413 414 415
    myfile << SCHED_HTML_META; // <meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />\n
    myfile << SCHED_HTML_BEG_TITLE; // <title>
    myfile << "Scheduling";
    myfile << SCHED_HTML_END_TITLE; // </title>\n
    myfile << SCHED_HTML_END_HEAD; // </head>\n
    myfile << SCHED_HTML_BEG_BODY; // <body>\n
Siyuan Niu's avatar
Siyuan Niu committed
416
    
Siyuan Niu's avatar
Siyuan Niu committed
417 418 419 420
    myfile << "<ul>\n";
    myfile << "<li>Model name: "<< _simComp->getModelName() << "</li><br>\n"; //name of model
    myfile << "<li> Date: " << asctime(aTimeinfo) << "</li>\n"; //date and time
    myfile << "</ul>\n";
Siyuan Niu's avatar
Siyuan Niu committed
421 422 423 424 425 426
    
    myfile << SCHED_HTML_JS_TYPE;
    myfile << SCHED_HTML_JS_CONTENT1;
    for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
      (*i)->drawPieChart(myfile);
    }
Siyuan Niu's avatar
Siyuan Niu committed
427
    for(FPGAList::const_iterator i=_simComp->getFPGAList().begin(); i != _simComp->getFPGAList().end(); ++i){
Siyuan Niu's avatar
Siyuan Niu committed
428 429 430 431
      for(TaskList::const_iterator j = (*i)->getTaskList().begin(); j != (*i)->getTaskList().end(); ++j){
      	(*i)->setHtmlCurrTask(*j);
	(*i)->drawPieChart(myfile);
      }
Siyuan Niu's avatar
Siyuan Niu committed
432
    }
Siyuan Niu's avatar
Siyuan Niu committed
433 434 435 436 437 438 439 440 441 442 443
    for(BusList::const_iterator j=_simComp->getBusList().begin(); j != _simComp->getBusList().end(); ++j){
      (*j)->drawPieChart(myfile);
    }
       
    myfile << "}" <<std::endl;
  
   
    myfile << SCHED_HTML_END_JS;
    myfile << SCHED_HTML_JS_LINK;
    myfile << SCHED_HTML_END_JS;
    
444 445
    //for(CPUList::const_iterator i=_simComp->getCPUIterator(false); i != _simComp->getCPUIterator(true); ++i){
    for(CPUList::const_iterator i=_simComp->getCPUList().begin(); i != _simComp->getCPUList().end(); ++i){
Siyuan Niu's avatar
Siyuan Niu committed
446
      for(unsigned int j = 0; j < (*i)->getAmoutOfCore(); j++) {
apvrille's avatar
apvrille committed
447
        //std::cout<<"core number is "<<(*i)->getAmoutOfCore()<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
448
	(*i)->schedule2HTML(myfile);
Siyuan Niu's avatar
Siyuan Niu committed
449
       	(*i)->showPieChart(myfile);
Siyuan Niu's avatar
Siyuan Niu committed
450
	(*i)->setCycleTime((*i)->getCycleTime()+1);
Siyuan Niu's avatar
Siyuan Niu committed
451 452
	
      }
Siyuan Niu's avatar
Siyuan Niu committed
453 454
        if((*i)->getAmoutOfCore() == 1)
	   (*i)->setCycleTime(0);
455
    }
Siyuan Niu's avatar
Siyuan Niu committed
456 457
    jsfile.close();
    
Siyuan Niu's avatar
Siyuan Niu committed
458 459
    
    for(FPGAList::const_iterator j=_simComp->getFPGAList().begin(); j != _simComp->getFPGAList().end(); ++j){     
Siyuan Niu's avatar
Siyuan Niu committed
460
      (*j)->setStartFlagHTML(true);
Siyuan Niu's avatar
Siyuan Niu committed
461 462
      for(TaskList::const_iterator i = (*j)->getTaskList().begin(); i != (*j)->getTaskList().end(); ++i){
      	(*j)->setHtmlCurrTask(*i);
Siyuan Niu's avatar
Siyuan Niu committed
463 464 465 466
#ifdef DEBUG_HTML
	std::cout<<"begin fpga html "<<(*j)->toShortString()<<std::endl;
	std::cout<<"task is !!!!!"<<(*i)->toString()<<std::endl;
#endif
Siyuan Niu's avatar
Siyuan Niu committed
467
	(*j)->schedule2HTML(myfile);
Siyuan Niu's avatar
Siyuan Niu committed
468
	(*j)->setStartFlagHTML(false);
Siyuan Niu's avatar
Siyuan Niu committed
469
      }
Siyuan Niu's avatar
Siyuan Niu committed
470
      myfile << SCHED_HTML_JS_DIV_BEGIN << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
471 472 473 474
      for(TaskList::const_iterator i = (*j)->getTaskList().begin(); i != (*j)->getTaskList().end(); ++i){
	(*j)->setHtmlCurrTask(*i);
	(*j)->showPieChart(myfile);
      }
Siyuan Niu's avatar
Siyuan Niu committed
475 476
      myfile << SCHED_HTML_JS_DIV_END <<std::endl;
      myfile << SCHED_HTML_JS_CLEAR <<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
477 478
    }
    
479
    for(BusList::const_iterator j=_simComp->getBusList().begin(); j != _simComp->getBusList().end(); ++j){
Siyuan Niu's avatar
Siyuan Niu committed
480 481
      (*j)->schedule2HTML(myfile);     
      (*j)->showPieChart(myfile);
482 483
    }
    //for_each(iCPUlist.begin(), iCPUlist.end(),std::bind2nd(std::mem_fun(&CPU::schedule2HTML),myfile));
484 485 486 487

    myfile << SCHED_HTML_END_BODY; // </body>\n
    myfile << SCHED_HTML_END_HTML; // </html>\n

488 489
    myfile.close();
  }
490
  else {
491
    std::cout << "Unable to open HTML output file." << std::endl;
492 493
  }

494 495
  gettimeofday(&aEnd,NULL);
  std::cout << "The HTML output took " << getTimeDiff(aBegin,aEnd) << "usec. File: " << iTraceFileName << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
496
}
Daniel Knorreck's avatar
Daniel Knorreck committed
497
void Simulator::schedule2VCD(std::string& iTraceFileName) const{
Siyuan Niu's avatar
Siyuan Niu committed
498
#ifdef DEBUG_VCD
Siyuan Niu's avatar
Siyuan Niu committed
499
  std::cout<<"schedule2VCD~~~~~~~~~~~~"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
500
#endif
501 502 503 504 505 506
  time_t aRawtime;
  struct tm * aTimeinfo;
  struct timeval aBegin,aEnd;
  gettimeofday(&aBegin,NULL);
  time(&aRawtime);
  aTimeinfo=localtime(&aRawtime);
507 508

  if ( !ends_with( iTraceFileName, EXT_VCD ) ) {
509
    iTraceFileName.append( EXT_VCD );
510 511 512 513
  }

  std::ofstream myfile(iTraceFileName.c_str());

514 515 516
  if (myfile.is_open()){
    //std::cout << "File is open" << std::endl;
    SignalChangeQueue aQueue;
Siyuan Niu's avatar
Siyuan Niu committed
517
    //std::queue<SignalChangeData*> aQueue;
Siyuan Niu's avatar
Siyuan Niu committed
518
    //std::string aSigString;
519 520 521 522 523 524 525 526
    //bool aNoMoreTrans;
    //TraceableDevice* actDevice;
    TMLTime aCurrTime=-1;
    SignalChangeData* aTopElement;
    TMLTime aNextClockEvent=0;
    myfile << "$date\n" << asctime(aTimeinfo) << "$end\n\n$version\nDaniel's TML simulator\n$end\n\n";
    myfile << "$timescale\n5 ns\n$end\n\n$scope module Simulation $end\n";
    //std::cout << "Before 1st loop" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
527
   
528 529 530 531 532 533 534
    for (TraceableDeviceList::const_iterator i=_simComp->getVCDList().begin(); i!= _simComp->getVCDList().end(); ++i){
      //TraceableDevice* a=*i;
      //                        a->streamBenchmarks(std::cout);
      //                        a->toString();
      //std::cout << "in 1st loop " << a << std::endl;
      //std::cout << "device: " << (*i)->toString() << std::endl;
      //myfile << "$var integer 3 " << (*i)->toShortString() << " " << (*i)->toString() << " $end\n";
Siyuan Niu's avatar
Siyuan Niu committed
535 536
      if ((*i)->toShortString().substr(0,3) == "cpu"){
	for(unsigned int j = 0; j < (dynamic_cast<CPU*>(*i))->getAmoutOfCore(); j++) {
Siyuan Niu's avatar
Siyuan Niu committed
537
	  myfile << "$var wire 1 " << (*i)->toShortString() << "_core" << j << " " << (*i)->toString() << "_Core" << j << " $end\n";
Siyuan Niu's avatar
Siyuan Niu committed
538
	  aTopElement = new SignalChangeData();
Siyuan Niu's avatar
Siyuan Niu committed
539
	  aTopElement->_coreNumberVcd=j;
Siyuan Niu's avatar
Siyuan Niu committed
540
	  (*i)->getNextSignalChange(true, aTopElement);
Siyuan Niu's avatar
Siyuan Niu committed
541
	  aQueue.push(aTopElement);
Siyuan Niu's avatar
Siyuan Niu committed
542
	  // (dynamic_cast<CPU*>(*i))->setCycleTime( (dynamic_cast<CPU*>(*i))->getCycleTime()+1);
Siyuan Niu's avatar
Siyuan Niu committed
543
	}
Siyuan Niu's avatar
Siyuan Niu committed
544
      }
Siyuan Niu's avatar
Siyuan Niu committed
545 546 547 548 549 550
       else if((*i)->toShortString().substr(0,4) == "fpga"){
	 for(TaskList::const_iterator j = _simComp->getTaskList().begin(); j != _simComp->getTaskList().end(); j++){
	   aTopElement = new SignalChangeData();
	   aTopElement->_taskFPGA=(*j);
	   (*i)->getNextSignalChange(true, aTopElement);
	   if(aTopElement->_device){
Siyuan Niu's avatar
Siyuan Niu committed
551
#ifdef DEBUG_VCD
Siyuan Niu's avatar
Siyuan Niu committed
552
	     std::cout<<"name of fpga is : "<< (*i)->toShortString() << "_" << (*j)->toString() << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
553
#endif
Siyuan Niu's avatar
Siyuan Niu committed
554 555 556 557 558
	     myfile << "$var wire 1 " << (*i)->toShortString() << "_" << (*j)->toString() << " " << (*i)->toString() << "_" << (*j)->toString() << " $end\n";
	     aQueue.push(aTopElement);
	   }
	 }	 
       }
Siyuan Niu's avatar
Siyuan Niu committed
559
      else{
Siyuan Niu's avatar
Siyuan Niu committed
560 561 562 563
	if(((*i)->toShortString().substr(0,2) == "ta"))
	  myfile << "$var wire 2 " << (*i)->toShortString() << " " << (*i)->toString() << " $end\n";
	else
	  myfile << "$var wire 1 " << (*i)->toShortString() << " " << (*i)->toString() << " $end\n";
Siyuan Niu's avatar
Siyuan Niu committed
564 565 566 567
 	aTopElement = new SignalChangeData();
	(*i)->getNextSignalChange(true, aTopElement);
	aQueue.push(aTopElement);
      }
568
    }
Siyuan Niu's avatar
Siyuan Niu committed
569 570 571 572 573
 
    
    //  (dynamic_cast<CPU*>(*i))->setCycleTime( (dynamic_cast<CPU*>(*i))->getCycleTime()+1);
  
   
574 575
    myfile << "$var integer 32 clk Clock $end\n";
    myfile << "$upscope $end\n$enddefinitions  $end\n\n";
Siyuan Niu's avatar
Siyuan Niu committed
576
    
577
    while (!aQueue.empty()){
Siyuan Niu's avatar
Siyuan Niu committed
578
      // std::cout<<"this is queue"<<std::endl;
579
      aTopElement=aQueue.top();
Siyuan Niu's avatar
Siyuan Niu committed
580
      if( aTopElement->_device->toShortString().substr(0,3) == "cpu")
Siyuan Niu's avatar
Siyuan Niu committed
581
	std::cout<<"the member of queue is "<<aTopElement->_device->toShortString()<< "_core" << aTopElement->_coreNumberVcd<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
582 583
      else if( aTopElement->_device->toShortString().substr(0,4) == "fpga")
        std::cout<<"the member of queue is "<<aTopElement->_device->toShortString()<< "_" << aTopElement->_taskFPGA->toString()<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
584
      else 
Siyuan Niu's avatar
Siyuan Niu committed
585
	 std::cout<<"the member of queue is "<<aTopElement->_device->toShortString() <<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
586

Siyuan Niu's avatar
Siyuan Niu committed
587
      
588
      while (aNextClockEvent < aTopElement->_time){
Siyuan Niu's avatar
Siyuan Niu committed
589 590
	myfile << "#" << aNextClockEvent << "\nr" << aNextClockEvent << " clk\n";
	aNextClockEvent+=CLOCK_INC;
Siyuan Niu's avatar
Siyuan Niu committed
591
	//std::cout<<"aaaa"<<std::endl;
592 593
      }
      if (aCurrTime!=aTopElement->_time){
Siyuan Niu's avatar
Siyuan Niu committed
594
	aCurrTime=aTopElement->_time;
Siyuan Niu's avatar
Siyuan Niu committed
595
	//std::cout<<"bbbbb"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
596
	myfile << "#" << aCurrTime << "\n";
597 598
      }
      if (aNextClockEvent == aTopElement->_time){
Siyuan Niu's avatar
Siyuan Niu committed
599
	myfile << "b" << vcdTimeConvert(aNextClockEvent) << " clk\n";
Siyuan Niu's avatar
Siyuan Niu committed
600
	//std::cout<<"ccccc"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
601
	aNextClockEvent+=CLOCK_INC;
602 603
      }
      //myfile << aTopElement->_sigChange << "\n";
Siyuan Niu's avatar
Siyuan Niu committed
604
      if( aTopElement->_device->toShortString().substr(0,3) == "cpu" )
Siyuan Niu's avatar
Siyuan Niu committed
605
	myfile << vcdValConvert(aTopElement->_sigChange) << aTopElement->_device->toShortString() << "_core" << aTopElement->_coreNumberVcd << "\n"; 
Siyuan Niu's avatar
Siyuan Niu committed
606 607 608 609 610
      
      else if( aTopElement->_device->toShortString().substr(0,4) == "fpga")
	myfile << vcdValConvert(aTopElement->_sigChange) << aTopElement->_device->toShortString() << "_" << aTopElement->_taskFPGA->toString() << "\n";
      
      else if( aTopElement->_device->toShortString().substr(0,2) == "ta" )
Siyuan Niu's avatar
Siyuan Niu committed
611
	myfile <<"b"<< vcdTaskValConvert(aTopElement->_sigChange) <<" "<< aTopElement->_device->toShortString() << "\n";
Siyuan Niu's avatar
Siyuan Niu committed
612
      
Siyuan Niu's avatar
Siyuan Niu committed
613
      else myfile << vcdValConvert(aTopElement->_sigChange) << aTopElement->_device->toShortString() << "\n";
614 615 616
      aQueue.pop();
      TMLTime aTime = aTopElement->_time;
      aTopElement->_device->getNextSignalChange(false, aTopElement);
Siyuan Niu's avatar
Siyuan Niu committed
617
#ifdef DEBUG_VCD
Siyuan Niu's avatar
Siyuan Niu committed
618 619
      std::cout<<"aTime is "<<aTime<<std::endl;
      std::cout<<"top element time is "<<aTopElement->_time<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
620
#endif
Siyuan Niu's avatar
Siyuan Niu committed
621 622
      if (aTopElement->_time == aTime){
	delete aTopElement;
Siyuan Niu's avatar
Siyuan Niu committed
623
	//	std::cout<<"delete"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
624 625 626
      }
      else{
	aQueue.push(aTopElement);
Siyuan Niu's avatar
Siyuan Niu committed
627
	//std::cout<<"no delete"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
628 629
      }
    }
Siyuan Niu's avatar
Siyuan Niu committed
630
  
Siyuan Niu's avatar
Siyuan Niu committed
631
	
632 633 634 635 636
      //actDevice=aTopElement->_device;
      //if (actDevice!=0) aTime = actDevice->getNextSignalChange(false, aSigString, aNoMoreTrans);
      //delete aTopElement;
      //aQueue.pop();
      //if (actDevice!=0) aQueue.push(new SignalChangeData(aSigString, aTime, (aNoMoreTrans)?0:actDevice));
Siyuan Niu's avatar
Siyuan Niu committed
637
     
638 639 640
    myfile << "#" << aCurrTime+1 << "\n";
    std::cout << "Simulated cycles: " << aCurrTime << std::endl;
    //for (TraceableDeviceList::const_iterator i=_simComp->getVCDIterator(false); i!= _simComp->getVCDIterator(true); ++i){
Siyuan Niu's avatar
Siyuan Niu committed
641
    ///////test//////////
642
    for (TraceableDeviceList::const_iterator i=_simComp->getVCDList().begin(); i!= _simComp->getVCDList().end(); ++i){
Siyuan Niu's avatar
Siyuan Niu committed
643 644 645 646
      if ((*i)->toShortString().substr(0,3)=="cpu"){
	for (unsigned int j = 0; j < (dynamic_cast<CPU*>(*i))->getAmoutOfCore();j++){
	  myfile << "0" << (*i)->toShortString() << "\n";
	}
Siyuan Niu's avatar
Siyuan Niu committed
647 648 649 650 651 652
      }	
       else if((*i)->toShortString().substr(0,4) == "FPGA"){
	for(TaskList::const_iterator j=_simComp->getTaskList().begin(); j!=_simComp->getTaskList().end();j++){
	   myfile << "0" << (*i)->toShortString() << "\n";
	}
       }
653
      //myfile << VCD_PREFIX << "100 " << (*i)->toShortString() << "\n";
Siyuan Niu's avatar
Siyuan Niu committed
654
      else  myfile << "0" << (*i)->toShortString() << "\n";
655 656 657 658 659 660 661 662
      //std::cout << "Utilization of component " << (*i)->toString() << ": " << ((float)(*i)->getBusyCycles()) / ((float)aCurrTime) << std::endl;
    }
    myfile.close();
  }
  else
    std::cout << "Unable to open VCD output file." << std::endl;
  gettimeofday(&aEnd,NULL);
  std::cout << "The VCD output took " << getTimeDiff(aBegin,aEnd) << "usec. File: " << iTraceFileName << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
663 664 665

}

Daniel Knorreck's avatar
Daniel Knorreck committed
666
bool Simulator::channelImpactsCommand(TMLChannel* iCh, TMLCommand* iCmd){
667 668 669 670
  unsigned int nbOfChannels = iCmd->getNbOfChannels();
  for (unsigned int i=0; i<nbOfChannels; i++)
    if (iCh==iCmd->getChannel(i)) return true;
  return false;
Daniel Knorreck's avatar
Daniel Knorreck committed
671 672
}

673
bool Simulator::simulate(TMLTransaction*& oLastTrans){
Siyuan Niu's avatar
Siyuan Niu committed
674 675
  TMLTransaction* depTransaction,*depNextTrans,*transLET;
  TMLCommand* commandLET,*depCommand,*depNextCommand;
676
  TMLTask* depTask;
Siyuan Niu's avatar
Siyuan Niu committed
677
  SchedulableDevice* deviceLET;
678
  CPU* depCPU;
Siyuan Niu's avatar
Siyuan Niu committed
679
  FPGA *depFPGA;
Daniel Knorreck's avatar
Daniel Knorreck committed
680
#ifdef DEBUG_KERNEL
681
  std::cout << "kernel:simulate: first schedule" << std::endl;
Daniel Knorreck's avatar
Daniel Knorreck committed
682
#endif
683 684 685 686
  _simComp->setStopFlag(false,"");
  for(TaskList::const_iterator i=_simComp->getTaskList().begin(); i!=_simComp->getTaskList().end();i++){
    if ((*i)->getCurrCommand()!=0) (*i)->getCurrCommand()->prepare(true);
  }
Daniel Knorreck's avatar
Daniel Knorreck committed
687
#ifdef EBRDD_ENABLED
688 689 690
  for(EBRDDList::const_iterator i=_simComp->getEBRDDIterator(false); i!=_simComp->getEBRDDIterator(true);i++){
    if ((*i)->getCurrCommand()!=0) (*i)->getCurrCommand()->prepare();
  }
Daniel Knorreck's avatar
Daniel Knorreck committed
691
#endif
Siyuan Niu's avatar
Siyuan Niu committed
692 693 694
#ifdef DEBUG_SIMULATE
  std::cout<<"simulate"<<std::endl;
#endif
Siyuan Niu's avatar
Siyuan Niu committed
695
  for_each(_simComp->getCPUList().begin(), _simComp->getCPUList().end(),std::mem_fun(&CPU::schedule));
Siyuan Niu's avatar
Siyuan Niu committed
696 697 698 699

  for_each(_simComp->getFPGAList().begin(), _simComp->getFPGAList().end(),std::mem_fun(&FPGA::schedule));

    transLET=getTransLowestEndTime(deviceLET);
700
#ifdef LISTENERS_ENABLED
701 702
  if (_wasReset) NOTIFY_SIM_STARTED();
  _wasReset=false;
703
#endif
Siyuan Niu's avatar
Siyuan Niu committed
704
  while ( transLET!=0 && !_simComp->getStopFlag()){
Siyuan Niu's avatar
Siyuan Niu committed
705 706 707
#ifdef DEBUG_SIMULATE
      std::cout<<"come in cpu"<<std::endl;
#endif
Ludovic Apvrille's avatar
Ludovic Apvrille committed
708
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
709
      std::cout << "kernel:simulate: scheduling decision: " <<  transLET->toString() << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
710
#endif
711

Siyuan Niu's avatar
Siyuan Niu committed
712
	commandLET=transLET->getCommand();
Siyuan Niu's avatar
Siyuan Niu committed
713 714 715
#ifdef DEBUG_SIMULATE
	std::cout<<"device is "<<deviceLET->getName()<<std::endl;
#endif
Siyuan Niu's avatar
Siyuan Niu committed
716
        bool x = deviceLET->addTransaction(0);
Siyuan Niu's avatar
Siyuan Niu committed
717 718 719
#ifdef DEBUG_SIMULATE
	std::cout<<"in simulator end addTransactin"<<std::endl;
#endif
Ludovic Apvrille's avatar
Ludovic Apvrille committed
720
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
721
      std::cout << "kernel:simulate: AFTER add trans: " << x << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
722
#endif
Siyuan Niu's avatar
Siyuan Niu committed
723
      if (x){
Ludovic Apvrille's avatar
Ludovic Apvrille committed
724
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
725
	std::cout << "kernel:simulate: add transaction 0" << commandLET->toString() << std::endl;
726
#endif
Siyuan Niu's avatar
Siyuan Niu committed
727 728 729
	unsigned int nbOfChannels = commandLET->getNbOfChannels();
	for (unsigned int i=0;i<nbOfChannels; i++){
	  if ((depTask=commandLET->getDependentTask(i))==0) continue;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
730
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
731 732 733 734 735 736
	  std::cout << "kernel:simulate: dependent Task found" << std::endl;
#endif
	  depCPU=depTask->getCPU();
	  depFPGA=depTask->getFPGA();
	  
	  if(depCPU){
Siyuan Niu's avatar
Siyuan Niu committed
737 738 739
#ifdef DEBUG_SIMULATE
	    std::cout<<"lets start cpu"<<std::endl;
#endif
Siyuan Niu's avatar
Siyuan Niu committed
740
	    if (depCPU!=deviceLET){
Ludovic Apvrille's avatar
Ludovic Apvrille committed
741
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
742
	      std::cout << "kernel:simulate: Tasks running on different CPUs" << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
743
#endif
Siyuan Niu's avatar
Siyuan Niu committed
744 745
	      depCommand=depTask->getCurrCommand();
	      if (depCommand!=0 && channelImpactsCommand(commandLET->getChannel(i), depCommand)) { //RIGHT one
Daniel Knorreck's avatar
Daniel Knorreck committed
746 747

#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
748
		std::cout << "kernel:simulate: commands are accessing the same channel" << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
749
#endif
Siyuan Niu's avatar
Siyuan Niu committed
750 751
		depTransaction=depCommand->getCurrTransaction();
		if (depTransaction!=0 && depTransaction->getVirtualLength()!=0){
Daniel Knorreck's avatar
Daniel Knorreck committed
752
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
753
		  std::cout << "kernel:simulate: dependent task has a current transaction and is not blocked any more" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
754
#endif
Siyuan Niu's avatar
Siyuan Niu committed
755 756 757
             
		  depNextTrans=depCPU->getNextTransaction();
		  if (depNextTrans!=0){
Siyuan Niu's avatar
Siyuan Niu committed
758
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
759
		    std::cout << "kernel:simulate: transaction scheduled on dependent CPU" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
760
#endif
Siyuan Niu's avatar
Siyuan Niu committed
761 762
		    depNextCommand=depNextTrans->getCommand();
		    if (depNextCommand->getTask()!=depTask){
Siyuan Niu's avatar
Siyuan Niu committed
763
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
764
		      std::cout << "kernel:simulate: dependent task not yet scheduled on dependent CPU" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
765 766
#endif

Siyuan Niu's avatar
Siyuan Niu committed
767
		      depCPU->truncateAndAddNextTransAt(transLET->getEndTime());
Siyuan Niu's avatar
Siyuan Niu committed
768
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
769
		      std::cout << "kernel:simulate: dependent transaction truncated" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
770
#endif
Siyuan Niu's avatar
Siyuan Niu committed
771 772
		    }
		  }else{
Siyuan Niu's avatar
Siyuan Niu committed
773
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
774 775 776 777 778 779 780 781 782
		    std::cout << "kernel:simulate: schedule dependent CPU  " << depCPU->toString() << std::endl;
#endif
		    depCPU->schedule();
		  }
		}
	      }
	    }
	  }
	  else{
Siyuan Niu's avatar
Siyuan Niu committed
783
#ifdef DEBUG_SIMULATE
Siyuan Niu's avatar
Siyuan Niu committed
784
	    std::cout<<"lets start fpga"<<std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
785
#endif
Siyuan Niu's avatar
Siyuan Niu committed
786
	    if (depFPGA!=deviceLET){
Siyuan Niu's avatar
Siyuan Niu committed
787
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
788
	      std::cout << "kernel:simulate: Tasks running on different FPGAs" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
789
#endif
Siyuan Niu's avatar
Siyuan Niu committed
790 791
	      depCommand=depTask->getCurrCommand();
	      if (depCommand!=0 && channelImpactsCommand(commandLET->getChannel(i), depCommand)) { //RIGHT one
Siyuan Niu's avatar
Siyuan Niu committed
792 793

#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
794
		std::cout << "kernel:simulate: commands are accessing the same channel" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
795
#endif
Siyuan Niu's avatar
Siyuan Niu committed
796 797
		depTransaction=depCommand->getCurrTransaction();
		if (depTransaction!=0 && depTransaction->getVirtualLength()!=0){
Siyuan Niu's avatar
Siyuan Niu committed
798
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
799
		  std::cout << "kernel:simulate: dependent task has a current transaction and is not blocked any more" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
800 801
#endif
             
Siyuan Niu's avatar
Siyuan Niu committed
802 803
		  depNextTrans=depFPGA->getNextTransaction();
		  if (depNextTrans!=0){
Siyuan Niu's avatar
Siyuan Niu committed
804
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
805
		    std::cout << "kernel:simulate: transaction scheduled on dependent CPU" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
806
#endif
Siyuan Niu's avatar
Siyuan Niu committed
807 808
		    depNextCommand=depNextTrans->getCommand();
		    if (depNextCommand->getTask()!=depTask){
Siyuan Niu's avatar
Siyuan Niu committed
809
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
810
		      std::cout << "kernel:simulate: dependent task not yet scheduled on dependent CPU" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
811 812
#endif

Siyuan Niu's avatar
Siyuan Niu committed
813
		      depFPGA->truncateAndAddNextTransAt(transLET->getEndTime());
Siyuan Niu's avatar
Siyuan Niu committed
814
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
815
		      std::cout << "kernel:simulate: dependent transaction truncated" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
816
#endif
Siyuan Niu's avatar
Siyuan Niu committed
817 818
		    }
		  }else{
Siyuan Niu's avatar
Siyuan Niu committed
819
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
820 821 822 823 824 825 826 827 828 829
		    std::cout << "kernel:simulate: schedule dependent CPU  " << depFPGA->toString() << std::endl;
#endif
		    depFPGA->schedule();
		  }
		}
	      }
	    }
	  }
	}
	
Siyuan Niu's avatar
Siyuan Niu committed
830
#ifdef DEBUG_KERNEL
Siyuan Niu's avatar
Siyuan Niu committed
831
	std::cout << "kernel:simulate: invoke schedule on executing CPU" << std::endl;
Siyuan Niu's avatar
Siyuan Niu committed
832
#endif
Siyuan Niu's avatar
Siyuan Niu committed
833
	deviceLET->schedule();
Siyuan Niu's avatar
Siyuan Niu committed
834
#ifdef LISTENERS_ENABLED
Siyuan Niu's avatar
Siyuan Niu committed
835
	NOTIFY_TIME_ADVANCES(transLET->getEndTime());
836
#endif
Siyuan Niu's avatar
Siyuan Niu committed
837 838
      }
      oLastTrans=transLET;
Siyuan Niu's avatar
Siyuan Niu committed
839 840 841
#ifdef DEBUG_SIMULATE
      std::cout<<"task is !!!!!"<<oLastTrans->toString()<<std::endl;
#endif
Siyuan Niu's avatar
Siyuan Niu committed
842
	transLET=getTransLowestEndTime(deviceLET);	
843
    }
Daniel Knorreck's avatar
Daniel Knorreck committed
844

Siyuan Niu's avatar
Siyuan Niu committed
845
  bool aSimCompleted = ( transLET==0  && !_simComp->getStoppedOnAction());
Daniel Knorreck's avatar
Daniel Knorreck committed
846

847
  if (aSimCompleted){
Daniel Knorreck's avatar
Daniel Knorreck committed
848
#ifdef LISTENERS_ENABLED
849 850
    NOTIFY_SIM_STOPPED();
    NOTIFY_EVALUATE();
851
#endif
852 853 854
    _longRunTime = max(_longRunTime, SchedulableDevice::getSimulatedTime());
    _shortRunTime = min(_shortRunTime, SchedulableDevice::getSimulatedTime());
  }
855
  return (aSimCompleted);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
856 857 858
}

const std::string Simulator::getArgs(const std::string& iComp, const std::string& iDefault, int iLen, char** iArgs){
859 860 861 862 863 864 865 866 867 868 869
  int aPosition=0;
  while (aPosition < iLen){
    if (iComp.compare(iArgs[aPosition])==0){
      if (aPosition+1 < iLen && iArgs[aPosition+1][0]!='-'){
        return std::string(iArgs[aPosition+1]);
      }else
        return iDefault;
    }
    aPosition++;
  }
  return std::string("");
Ludovic Apvrille's avatar
Ludovic Apvrille committed
870 871 872
}

void Simulator::printHelp(){
873 874 875 876 877 878 879 880 881
  std::cout <<  "\n************************** Command line arguments *************************\n"
    "-gpath                 specify path for graph output\n"
    "-server                launch simulator in server mode\n"
    "-file                  read simulation commands from file\n"
    "-help                  display this help text\n"
    "-ohtml ofile           simulate and write traces to ofile in html format\n"
    "-otxt ofile            simulate and write traces to ofile in text format\n"
    "-ovcd ofile            simulate and write traces to ofile in vcd format\n"
    "-ograph ofile          simulate and write traces to ofile in aut format\n"
apvrille's avatar
apvrille committed
882
    "-gname ofile           name of the file WITHOUT extension storing the reachability graph\n"
883 884 885 886
    "-explo                 generate the reachability graph                 \n"
    "-cmd \'c1 p1 p2;c2\'     execute commands c1 with parameters p1 and p2 and c2\n"
    "-oxml ofile            xml reply is written to ofile, in case the -cmd option is used\n"
    "***************************************************************************\n\n";
Ludovic Apvrille's avatar
Ludovic Apvrille committed
887 888 889
}

void Simulator::run(){
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
  std::string* aNewCmd;
  std::cout << "Running in server mode.\n";
  while (!_syncInfo->_terminate){
    //pthread_mutex_lock (&_syncInfo->_mutexConsume);
    //std::cout << "Simulator Waiting for cmd\n";
    aNewCmd=_syncInfo->popCommand();
    //decodeCommand(_syncInfo->_command);
    //std::cout << "Let's crash.\n";
    decodeCommand(*aNewCmd);
    //std::cout << "Returned from decode.\n";
    //std::cout << "Before delete.\n";
    delete aNewCmd;
    //pthread_mutex_unlock (&_syncInfo->_mutexProduce);
  }
  std::cout << "Simulator loop terminated." << std::endl;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
905 906
}

907
ServerIF* Simulator::run(int iLen, char ** iArgs){
908
  std::string aArgString;
909
  std::string graphName = "";
910
  std::cout << "Starting up...\n";
911 912 913 914
  graphName = getArgs("-gname", "", iLen, iArgs);
  if (graphName.empty()) {
    graphName = "graph";
  }
915 916 917 918 919 920 921 922 923
  _graphOutPath = getArgs("-gpath", "", iLen, iArgs);
  if (_graphOutPath.length()>0 && _graphOutPath[_graphOutPath.length()-1]!='/')
    _graphOutPath+="/";
  aArgString =getArgs("-server", "server", iLen, iArgs);
  if (!aArgString.empty()) return new Server();
  aArgString =getArgs("-file", "file", iLen, iArgs);
  if (!aArgString.empty()) return new ServerLocal(aArgString);
  aArgString =getArgs("-explo", "file", iLen, iArgs);
  std::cout << "Just analyzed explo 1->" + aArgString + "<-\n";
924 925 926 927 928
  if (!aArgString.empty()) {
    std::string command = "1 7 100 100 " + graphName;
    std::cout << "Just analyzed explo 1->" + aArgString + "<- with command: " + command + "\n";
    decodeCommand(command);
  }
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
  std::cout << "Just analyzed explo 2\n";
  //if (!aArgString.empty()) return new ServerExplore();
  std::cout << "Running in command line mode.\n";
  _replyToServer = false;
  aArgString =getArgs("-help", "help", iLen, iArgs);
  if (aArgString.empty()){
    //aArgString =getArgs("-explo", "explo", iLen, iArgs);
    aArgString =getArgs("-cmd", "1 0", iLen, iArgs);
    if (aArgString.empty()){
      TMLTransaction* oLastTrans;
      simulate(oLastTrans);
      aArgString=getArgs("-ohtml", "scheduling.html", iLen, iArgs);
      if (!aArgString.empty()) schedule2HTML(aArgString);
      aArgString=getArgs("-otxt", "scheduling.txt", iLen, iArgs);
      if (!aArgString.empty()) schedule2TXT(aArgString);
      aArgString=getArgs("-ovcd", "scheduling.vcd", iLen, iArgs);
      if (!aArgString.empty()) schedule2VCD(aArgString);
      aArgString=getArgs("-ograph", "scheduling.aut", iLen, iArgs);
      if (!aArgString.empty()) schedule2Graph(aArgString);
      _simComp->streamBenchmarks(std::cout);
      std::cout << "Simulated time: " << SchedulableDevice::getSimulatedTime() << " time units.\n";
    }else{
      std::ofstream aXmlOutFile;
      std::string aXmlFileName = getArgs("-oxml", "reply.xml", iLen, iArgs);
      if (aXmlFileName.empty()) aXmlOutFile.open("/dev/null"); else aXmlOutFile.open(aXmlFileName.c_str());
      if (aXmlOutFile.is_open()){
        std::string aNextCmd;
        std::istringstream iss(aArgString+";");
        getline(iss, aNextCmd, ';');
        while (!(iss.eof() || aNextCmd.empty())){
          std::cout << "next cmd to execute: \"" << aNextCmd << "\"\n";
          decodeCommand(aNextCmd, aXmlOutFile);
          getline(iss, aNextCmd, ';');
        }
        aXmlOutFile.close();
      }else
        std::cout << "XML output file could not be opened, aborting.\n";
    }
    rusage res;
    getrusage(RUSAGE_SELF, &res);
    //std::cerr << res.ru_utime.tv_sec << "," << res.ru_utime.tv_usec << "," << res.ru_stime.tv_sec << "," << res.ru_stime.tv_usec << "\n";
    double aRunTime = ((double)((res.ru_utime.tv_sec + res.ru_stime.tv_sec) *1000000 + res.ru_utime.tv_usec + res.ru_stime.tv_usec))/1000000;
    std::cerr << "trans/sec: " << ((double)SchedulableDevice::getOverallTransNo())/aRunTime << "\n";
    std::cerr << "cycles/trans: " << ((double)SchedulableDevice::getOverallTransSize())/((double)SchedulableDevice::getOverallTransNo()) << "\n";
    std::cerr << "Trans size: " << SchedulableDevice::getOverallTransSize() << "  trans no: " << SchedulableDevice::getOverallTransNo() << "\n";
    std::cerr << "Statement coverage of application: " << TMLCommand::getCmdCoverage() << "%\n";
    std::cerr << "Branch coverage of application: " << TMLCommand::getBranchCoverage() << "%\n";
  }else{
    printHelp();
  }
  //clock_t tick =sysconf(_SC_CLK_TCK);
  //tms test;
  //times(&test);
  //std::cout << "user time: " << test.tms_utime << "  system time: " << test.tms_stime + test.tms_cstime << "  tick: " << tick << "\n";
  return 0;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
984 985
}

Daniel Knorreck's avatar
Daniel Knorreck committed
986
void Simulator::decodeCommand(std::string iCmd, std::ostream& iXmlOutStream){
987 988 989 990 991 992 993 994
  //std::cout << "Not crashed. I: " << iCmd << std::endl;
  //std::cout << iCmd << std::endl;
  unsigned int aCmd, aParam1, aParam2, anErrorCode=0;
  //std::string anIssuedCmd(iCmd);
  std::istringstream aInpStream(iCmd);
  //std::cout << "Not crashed. II\n";
  std::ostringstream aGlobMsg, anEntityMsg, anAckMsg;
  std::string aStrParam;
995
  int returnedNbOfTransactions = 0;
996 997 998 999 1000 1001 1002 1003
  //bool aSimTerminated=false;
  //std::cout << "Not crashed. III\n";
  //std::cout << "Not crashed. I: " << iCmd << std::endl;
  _simComp->setStopFlag(false,"");
  //anEntityMsg.str("");
  aGlobMsg << TAG_HEADER << std::endl << TAG_STARTo << std::endl << TAG_GLOBALo << std::endl /*<< TAG_REPLYo << anIssuedCmd << TAG_REPLYc << std::endl*/;
  aInpStream >> aCmd;
  //std::cout << "Not crashed. I: " << iCmd << std::endl;
1004
  //std::cout << "Decoding command: d" << iCmd << " " << aCmd<<std::endl;
Letitia Li's avatar
Letitia Li committed
1005
  TMLTransaction* oLastTrans;
1006
  switch (aCmd){
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  case 0: //Quit simulation
    std::cout << "QUIT SIMULATION from Decode Command"  << std::endl;
    break;
  case 1:{
    struct timeval aBegin,aEnd;
    gettimeofday(&aBegin,NULL);
    _busy=true;
    //std::cout << "Not crashed. I: " << iCmd << std::endl;
    anAckMsg << TAG_HEADER << std::endl << TAG_STARTo << std::endl << TAG_GLOBALo << std::endl << /*TAG_REPLYo << anIssuedCmd << TAG_REPLYc << std::endl<< */ TAG_MSGo << "Command received" << TAG_MSGc << TAG_ERRNOo << 0 << TAG_ERRNOc << std::endl << TAG_STATUSo << SIM_BUSY << TAG_STATUSc << std::endl << TAG_GLOBALc << std::endl << TAG_STARTc << std::endl;
    if (_replyToServer) {
      if (_syncInfo != NULL)
        if (_syncInfo->_server != NULL)
          _syncInfo->_server->sendReply(anAckMsg.str());
    }
    aInpStream >> aParam1;
1022
    //std::cout << "Not crashed. I: " << iCmd << " param= " << aParam1 << std::endl;
Letitia Li's avatar
Letitia Li committed
1023

1024
    switch (aParam1){
1025
      //_end =oLastTrans->printEnd();
1026 1027 1028 1029 1030
    case 0:     //Run to next breakpoint
      std::cout << "Run to next breakpoint." << std::endl;
      aGlobMsg << TAG_MSGo << "Run to next breakpoint" << TAG_MSGc << std::endl;
      _simTerm=runToNextBreakpoint(oLastTrans);
      std::cout << "End Run to next breakpoint." << std::endl;
Letitia Li's avatar
Letitia Li committed
1031
      _end =oLastTrans->printEnd();
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
      break;
    case 1:     //Run up to trans x
      std::cout << "Run to transaction x." << std::endl;
      aGlobMsg << TAG_MSGo << MSG_CMDNIMPL << TAG_MSGc << std::endl;
      anErrorCode=1;
      std::cout << "End Run to transaction x." << std::endl;
      break;
    case 2:     //Run x transactions
      std::cout << "Run x transactions." << std::endl;
      aInpStream >> aParam2;
      //_currCmdListener=new RunXTransactions(_simComp,aParam2);
      aGlobMsg << TAG_MSGo << "Created listener run " << aParam2 << " transactions" << TAG_MSGc << std::endl;
apvrille's avatar
apvrille committed
1044
      _simTerm = runXTransactions(aParam2, oLastTrans);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
      std::cout << "Run x transactions." << std::endl;
      break;
    case 3:     //Run up to command x
      std::cout << "Run to command x." << std::endl;
      aGlobMsg << TAG_MSGo << MSG_CMDNIMPL << TAG_MSGc << std::endl;
      anErrorCode=1;
      std::cout << "End Run to command x." << std::endl;
      break;
    case 4:     //Run x commands
      std::cout << "Run x commands." << std::endl;
      aInpStream >> aParam2;
      //_currCmdListener=new RunXCommands(_simComp,aParam2);
      aGlobMsg << TAG_MSGo << "Created listener run " << aParam2 << " commands" << TAG_MSGc << std::endl;
apvrille's avatar
apvrille committed
1058
      _simTerm = runXCommands(aParam2, oLastTrans);
1059 1060 1061 1062 1063 1064 1065
      std::cout << "End Run x commands." << std::endl;
      break;
    case 5: //Run up to time x
      std::cout << "Run to time x." << std::endl;
      aInpStream >> aParam2;
      //_currCmdListener=new RunXTimeUnits(_simComp,aParam2);
      aGlobMsg << TAG_MSGo << "Created listener run to time " << aParam2 << TAG_MSGc << std::endl;
apvrille's avatar
apvrille committed
1066
      _simTerm = runTillTimeX(aParam2, oLastTrans);
1067 1068 1069 1070 1071 1072 1073
      std::cout << "End Run to time x." << std::endl;
      break;
    case 6:     //Run for x time units
      std::cout << "Run for x time units." << std::endl;
      aInpStream >> aParam2;
      //_currCmdListener=new RunXTimeUnits(_simComp,aParam2+SchedulableDevice::getSimulatedTime());
      aGlobMsg << TAG_MSGo  << "Created listener run " << aParam2 << " time units" << TAG_MSGc << std::endl;
apvrille's avatar
apvrille committed
1074
      _simTerm = runXTimeUnits(aParam2, oLastTrans);
1075 1076 1077 1078 1079 1080 1081 1082
      std::cout << "End Run for x time units." << std::endl;
      break;
    case 7: {//Explore Tree
      //for (int i=0; i<RECUR_DEPTH; i++) leafsForLevel[i]=0;
      std::cout << "Explore tree." << std::endl;
      _commandCoverage=100; _branchCoverage=100;