1 // Copyright (C) 2006-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Executor.hxx"
22 #include "AutoLocker.hxx"
23 #include "Scheduler.hxx"
24 #include "Dispatcher.hxx"
25 #include "Container.hxx"
26 #include "HomogeneousPoolContainer.hxx"
27 #include "ComponentInstance.hxx"
29 #include "VisitorSaveState.hxx"
30 #include "ServiceNode.hxx"
31 #include "ComposedNode.hxx"
45 #define usleep(A) _sleep(A/1000)
46 #if !defined(S_ISCHR) || !defined(S_ISREG)
49 # define S_IFMT _S_IFMT
50 # define S_IFCHR _S_IFCHR
51 # define S_IFREG _S_IFREG
54 # define S_IFMT __S_IFMT
55 # define S_IFCHR __S_IFCHR
56 # define S_IFREG __S_IFREG
60 # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
61 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
65 using namespace YACS::ENGINE;
68 using YACS::BASES::Mutex;
69 using YACS::BASES::Thread;
70 using YACS::BASES::Semaphore;
73 #include "YacsTrace.hxx"
75 int Executor::_maxThreads(1000);
76 size_t Executor::_threadStackSize(262144); // Default thread stack size is 256 kB == 2**18 because threads launched by YACS are lightweight
78 Executor::Executor():_nbOfConcurrentThreads(0), _semForMaxThreads(_maxThreads),_keepGoingOnFail(false),_DPLScopeSensitive(false)
83 _stopOnErrorRequested = false;
84 _dumpOnErrorRequested = false;
85 _errorDetected = false;
86 _isRunningunderExternalControl=false;
87 _executorState = YACS::NOTYETINITIALIZED;
88 _execMode = YACS::CONTINUE;
89 _semThreadCnt = _maxThreads;
90 DEBTRACE("Executor initialized with max threads = " << _maxThreads);
95 for(list<Thread *>::iterator iter=_groupOfAllThreadsCreated.begin();iter!=_groupOfAllThreadsCreated.end();iter++)
99 //! Execute a graph waiting for completion
101 * \param graph : schema to execute
102 * \param debug : display the graph with dot if debug == 1
103 * \param fromScratch : if true the graph is reinitialized
105 * Calls Scheduler::getNextTasks and Scheduler::selectRunnableTasks to select tasks to execute
107 * Calls Executor::launchTask to execute a selected Task.
109 * Completion when graph is finished (Scheduler::isFinished)
112 void Executor::RunA(Scheduler *graph,int debug, bool fromScratch)
114 DEBTRACE("Executor::RunW debug: " << debug << " fromScratch: " << fromScratch);
116 _root = dynamic_cast<ComposedNode *>(_mainSched);
117 if (!_root) throw Exception("Executor::Run, Internal Error!");
120 if(debug>1)_displayDot(graph);
124 graph->exUpdateState();
126 if(debug>1)_displayDot(graph);
127 vector<Task *> tasks;
128 vector<Task *>::iterator iter;
130 _execMode = YACS::CONTINUE;
131 _isWaitingEventsFromRunningTasks = false;
132 _numberOfRunningTasks = 0;
133 _runningTasks.clear();
134 _numberOfEndedTasks=0;
137 sleepWhileNoEventsFromAnyRunningTask();
139 if(debug>2)_displayDot(graph);
142 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
143 tasks=graph->getNextTasks(isMore);
144 graph->selectRunnableTasks(tasks);
145 }//End of critical section
147 if(debug>2)_displayDot(graph);
149 for(iter=tasks.begin();iter!=tasks.end();iter++)
150 loadTask(*iter,this);
152 if(debug>1)_displayDot(graph);
156 if(debug>1)_displayDot(graph);
159 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
160 _toContinue=!graph->isFinished();
161 }//End of critical section
162 DEBTRACE("_toContinue: " << _toContinue);
164 if(debug>0)_displayDot(graph);
170 //! Execute a graph with breakpoints or step by step
172 * To be launch in a thread (main thread controls the progression).
173 * \param graph : schema to execute
174 * \param debug : display the graph with dot if debug >0
175 * \param fromScratch : if false, state from a previous partial exection is already loaded
177 * Calls Scheduler::getNextTasks and Scheduler::selectRunnableTasks to select tasks to execute
179 * Calls Executor::checkBreakPoints to verify if a pause is requested
181 * Calls Executor::launchTask to execute a selected Task
183 * Completion when graph is finished (Scheduler::isFinished)
185 * States of execution:
186 * - YACS::NOTYETINITIALIZED
187 * - YACS::INITIALISED
188 * - YACS::RUNNING (to next breakpoint or step)
189 * - YACS::WAITINGTASKS (a breakpoint or step as been reached, but there are still running tasks)
190 * - YACS::PAUSED (a breakpoint or step as been reached, no more running tasks)
191 * - YACS::FINISHED (no more ready tasks, nore running tasks)
192 * - YACS::STOPPED (stopped by user before end)
194 * Modes of Execution:
195 * - YACS::CONTINUE (normal run without breakpoints)
196 * - YACS::STEPBYSTEP (pause at each loop)
197 * - YACS::STOPBEFORENODES (pause when a node is reached)
199 * A breakpoint is defined by a node name. The breakpoint is reached when the node becomes ready.
200 * Step by Step means execution node by node or group of node by group of nodes.
201 * At a given step, the user decides to launch all the ready nodes or only a subset
202 * (Caution: some nodes must run in parallel).
203 * The next event (end of task) may give a new set of ready nodes, and define a new step.
205 * The graph execution may be controled by a pilot which sends requests. Requests are asynchronous.
206 * Requests are taken into account only on certain states, otherwise return the status IgnoredRequest.
207 * - Executor::getCurrentExecMode
208 * - Executor::getExecutorState
209 * - Executor::setExecMode : change the execution mode for next loop
210 * - Executor::setListOfBreakPoints : must be set before setting YACS::STOPBEFORENODES
211 * - Executor::getTasksToLoad : when paused or waiting tasks, get the list of next tasks
212 * - Executor::setStepsToExecute : define a subset of the list given by Executor::getTasksToLoad
213 * - Executor::resumeCurrentBreakPoint : when paused or waiting tasks, resumes execution
214 * - Executor::isNotFinished
215 * - Executor::stopExecution : stop execution asap, i.e. set STEPBYSTEP and wait PAUSED
216 * - Executor::saveState : dump the current state of execution in an xml file
217 * - Executor::loadState : Not yet implemented
218 * - Executor::getNbOfThreads
219 * - Executor::displayDot
220 * - Executor::setStopOnError : ask to stop execution if a node is found in ERROR state
222 * If the pilot wants to wait the state YACS::PAUSED or YACS::WAITINGTASKS, synchronisation is obtained with:
223 * - Executor::waitPause
226 * - Pilot may connect to executor during execution, or deconnect.
227 * - Several Pilots may be connected at the same time (for observation...)
231 void Executor::RunB(Scheduler *graph,int debug, bool fromScratch)
233 DEBTRACE("Executor::RunB debug: "<< graph->getName() <<" "<< debug<<" fromScratch: "<<fromScratch);
235 { // --- Critical section
236 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
238 _root = dynamic_cast<ComposedNode *>(_mainSched);
239 if (!_root) throw Exception("Executor::Run, Internal Error!");
240 _executorState = YACS::NOTYETINITIALIZED;
241 sendEvent("executor");
244 _errorDetected = false;
245 _isWaitingEventsFromRunningTasks = false;
246 _numberOfRunningTasks = 0;
247 _runningTasks.clear();
248 _numberOfEndedTasks = 0;
249 string tracefile = "traceExec_";
250 tracefile += _mainSched->getName();
251 _trace.open(tracefile.c_str());
253 _start = timeGetTime();
255 gettimeofday(&_start, NULL);
258 } // --- End of critical section
260 if (debug > 1) _displayDot(graph);
267 graph->exUpdateState();
271 DEBTRACE("exception: "<< (ex.what()));
272 _executorState = YACS::FINISHED;
273 sendEvent("executor");
277 _executorState = YACS::INITIALISED;
278 sendEvent("executor");
280 if (debug > 1) _displayDot(graph);
282 vector<Task *>::iterator iter;
287 _executorState = YACS::RUNNING;
288 sendEvent("executor");
291 DEBTRACE("--- executor main loop");
292 sleepWhileNoEventsFromAnyRunningTask();
293 DEBTRACE("--- events...");
294 if (debug > 2) _displayDot(graph);
295 { // --- Critical section
296 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
297 _tasks=graph->getNextTasks(isMore);
298 graph->selectRunnableTasks(_tasks);
299 FilterTasksConsideringContainers(_tasks);
300 numberAllTasks=_numberOfRunningTasks+_tasks.size();
301 } // --- End of critical section
302 if (debug > 2) _displayDot(graph);
303 if (_executorState == YACS::RUNNING)
305 if (checkBreakPoints()) break; // end of thread requested, OK to exit at once;
306 if (debug > 0) _displayDot(graph);
309 loadParallelTasks(_tasks,this);
310 if (debug > 1) _displayDot(graph);
315 if (debug > 1) _displayDot(graph);
316 { // --- Critical section
318 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
319 //It is possible that the graph is finished but it remains running tasks (it's an error but we must take it into account)
320 if(_numberOfRunningTasks == 0)
321 _toContinue = !graph->isFinished();
323 DEBTRACE("_numberOfRunningTasks: " << _numberOfRunningTasks);
324 DEBTRACE("_numberOfEndedTasks: " << _numberOfEndedTasks);
325 DEBTRACE("_toContinue: " << _toContinue);
326 if(_toContinue && numberAllTasks==0)
328 //Problem : no running tasks and no task to launch ??
330 std::cerr << "Problem in Executor : no running tasks and no task to launch ?? problemCount=" << problemCount << std::endl;
331 //Pause to give a chance to interrupt
333 if(problemCount > 25)
335 // Too much problems encountered : stop execution
342 _executorState = YACS::FINISHED;
343 sendEvent("executor");
344 _condForPilot.notify_all();
346 } // --- End of critical section
347 if (debug > 0) _displayDot(graph);
348 DEBTRACE("_toContinue: " << _toContinue);
351 DEBTRACE("End of main Loop");
353 { // --- Critical section
354 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
355 if ( _toContinue) // --- break while(): request to stop detected on checkBreakPoints()
357 DEBTRACE("stop requested: End soon");
358 _executorState = YACS::STOPPED;
360 sendEvent("executor");
362 } // --- End of critical section
363 if ( _dumpOnErrorRequested && _errorDetected)
365 saveState(_dumpErrorFile);
368 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForTrace);
371 DEBTRACE("End of RunB thread");
374 YACS::ExecutionMode Executor::getCurrentExecMode()
376 _isRunningunderExternalControl=true;
381 YACS::ExecutorState Executor::getExecutorState()
383 _isRunningunderExternalControl=true;
384 return _executorState;
388 bool Executor::isNotFinished()
390 _isRunningunderExternalControl=true;
394 //! ask to stop execution on the first node found in error
396 * \param dumpRequested produce a state dump when an error is found
397 * \param xmlFile name of file used for state dump
400 void Executor::setStopOnError(bool dumpRequested, std::string xmlFile)
402 { // --- Critical section
403 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
404 _dumpErrorFile=xmlFile;
405 _stopOnErrorRequested=true;
406 _dumpOnErrorRequested = dumpRequested;
407 if (dumpRequested && xmlFile.empty())
408 throw YACS::Exception("dump on error requested and no filename given for dump");
409 DEBTRACE("_dumpErrorFile " << _dumpErrorFile << " " << _dumpOnErrorRequested);
410 } // --- End of critical section
413 //! ask to do not stop execution on nodes found in error
417 void Executor::unsetStopOnError()
419 { // --- Critical section
420 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
421 _stopOnErrorRequested=false;
422 } // --- End of critical section
425 //! Dynamically set the current mode of execution
427 * The mode can be Continue, step by step, or stop before execution of a node
428 * defined in a list of breakpoints.
431 void Executor::setExecMode(YACS::ExecutionMode mode)
433 DEBTRACE("Executor::setExecMode(YACS::ExecutionMode mode) " << mode);
434 { // --- Critical section
435 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
436 _isRunningunderExternalControl=true;
438 } // --- End of critical section
441 //! wake up executor when in pause
443 * When Executor is in state paused or waiting for task completion, the thread
444 * running loop RunB waits on condition _condForStepByStep.
445 * Thread RunB is waken up.
446 * \return true when actually wakes up executor
449 bool Executor::resumeCurrentBreakPoint()
451 DEBTRACE("Executor::resumeCurrentBreakPoint()");
453 //bool doDump = false;
454 { // --- Critical section
455 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
456 _isRunningunderExternalControl=true;
457 DEBTRACE("_executorState: " << _executorState);
458 switch (_executorState)
460 case YACS::WAITINGTASKS:
463 _condForStepByStep.notify_all();
464 _executorState = YACS::RUNNING;
465 sendEvent("executor");
467 //if (_dumpOnErrorRequested && _errorDetected) doDump =true;
473 //if (_dumpOnErrorRequested && _errorDetected) doDump =true;
474 DEBTRACE("Graph Execution finished or stopped !");
479 // debug: no easy way to verify if main loop is acutally waiting on condition
483 //if (doDump) saveState(_dumpErrorFile);
484 } // --- End of critical section
489 //! define a list of nodes names as breakpoints in the graph
492 void Executor::setListOfBreakPoints(std::list<std::string> listOfBreakPoints)
494 DEBTRACE("Executor::setListOfBreakPoints(std::list<std::string> listOfBreakPoints)");
495 { // --- Critical section
496 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
497 _isRunningunderExternalControl=true;
498 _listOfBreakPoints = listOfBreakPoints;
499 } // --- End of critical section
503 //! Get the list of tasks to load, to define a subset to execute in step by step mode
505 * If the executor is not in mode YACS::WAITINGTASKS nor YACS::PAUSED, the list is empty.
506 * Use Executor::waitPause to wait.
508 std::list<std::string> Executor::getTasksToLoad()
510 DEBTRACE("Executor::getTasksToLoad()");
511 list<string> listOfNodesToLoad;
512 listOfNodesToLoad.clear();
513 { // --- Critical section
514 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
515 _isRunningunderExternalControl=true;
516 switch (_executorState)
518 case YACS::WAITINGTASKS:
521 listOfNodesToLoad = _listOfTasksToLoad;
524 case YACS::NOTYETINITIALIZED:
525 case YACS::INITIALISED:
534 } // --- End of critical section
535 return listOfNodesToLoad;
539 //! Define a subset of task to execute in step by step mode
541 * Behaviour is unpredictable if the list is not a subset of the list given by Executor::getTasksToLoad
542 * in the current step.
543 * If some nodes must run in parallel, they must stay together in the list.
546 bool Executor::setStepsToExecute(std::list<std::string> listToExecute)
548 DEBTRACE("Executor::setStepsToExecute(std::list<std::string> listToExecute)");
550 vector<Task *>::iterator iter;
551 vector<Task *> restrictedTasks;
552 { // --- Critical section
553 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
554 _isRunningunderExternalControl=true;
555 switch (_executorState)
557 case YACS::WAITINGTASKS:
560 for (iter=_tasksSave.begin(); iter!=_tasksSave.end(); iter++)
562 string readyNode = _mainSched->getTaskName(*iter);
563 if (find(listToExecute.begin(), listToExecute.end(), readyNode)
564 != listToExecute.end())
566 restrictedTasks.push_back(*iter);
567 DEBTRACE("node to execute " << readyNode);
571 for (iter=restrictedTasks.begin(); iter!=restrictedTasks.end(); iter++)
573 _tasks.push_back(*iter);
577 case YACS::NOTYETINITIALIZED:
578 case YACS::INITIALISED:
587 } // --- End of critical section
590 for (iter=restrictedTasks.begin(); iter!=restrictedTasks.end(); iter++)
592 _tasks.push_back(*iter);
594 for (iter=_tasks.begin(); iter!=_tasks.end(); iter++)
596 string readyNode = _mainSched->getTaskName(*iter);
597 DEBTRACE("selected node to execute " << readyNode);
603 //! suspend pilot execution until Executor is in pause or waiting tasks completion mode.
605 * Do nothing if execution is finished or in pause.
606 * Wait first step if Executor is running or in initialization.
609 void Executor::waitPause()
611 DEBTRACE("Executor::waitPause()" << _executorState);
612 { // --- Critical section
613 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
614 _isRunningunderExternalControl=true;
615 switch (_executorState)
620 case YACS::WAITINGTASKS:
625 case YACS::NOTYETINITIALIZED:
626 case YACS::INITIALISED:
629 _condForPilot.wait(_mutexForSchedulerUpdate); // wait until executor is PAUSED or WAITINGTASKS
633 } // --- End of critical section
637 //! stops the execution as soon as possible
639 void Executor::stopExecution()
641 setExecMode(YACS::STEPBYSTEP);
644 resumeCurrentBreakPoint();
647 //! save the current state of execution in an xml file
649 bool Executor::saveState(const std::string& xmlFile)
651 DEBTRACE("Executor::saveState() in " << xmlFile);
654 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
655 YACS::ENGINE::VisitorSaveState vst(_root);
656 vst.openFileDump(xmlFile.c_str());
661 catch(Exception& ex) {
662 std::cerr << ex.what() << std::endl;
667 //! not yet implemented
669 bool Executor::loadState()
671 DEBTRACE("Executor::loadState()");
672 _isRunningunderExternalControl=true;
677 static int isfile(const char *filename)
680 if (stat(filename, &buf) != 0)
682 if (!S_ISREG(buf.st_mode))
687 //! Display the graph state as a dot display, public method
689 void Executor::displayDot(Scheduler *graph)
691 _isRunningunderExternalControl=true;
695 //! Display the graph state as a dot display
697 * \param graph : the node to display
700 void Executor::_displayDot(Scheduler *graph)
702 std::ofstream g("titi");
703 ((ComposedNode*)graph)->writeDot(g);
705 const char displayScript[]="display.sh";
706 if(isfile(displayScript))
707 system("sh display.sh");
709 system("dot -Tpng titi|display -delay 5");
712 //! Wait reactivation in modes Step By step or with BreakPoints
714 * Check mode of execution (set by main thread):
715 * - YACS::CONTINUE : the graph execution continues.
716 * - YACS::STEPBYSTEP : wait on condition (pilot thread, Executor::resumeCurrentBreakPoint)
717 * - YACS::STOPBEFORENODES : if there are ready nodes in a list of breakpoints,
718 * wait on condition (pilot thread, Executor::resumeCurrentBreakPoint)
719 * else continue the graph execution.
720 * \return true if end of executor thread is requested
723 bool Executor::checkBreakPoints()
725 DEBTRACE("Executor::checkBreakPoints()");
726 vector<Task *>::iterator iter;
727 bool endRequested = false;
735 case YACS::STOPBEFORENODES:
738 { // --- Critical section
739 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
741 for (iter=_tasks.begin(); iter!=_tasks.end(); iter++)
743 string nodeToLoad = _mainSched->getTaskName(*iter);
744 if (find(_listOfBreakPoints.begin(), _listOfBreakPoints.end(), nodeToLoad)
745 != _listOfBreakPoints.end())
753 _listOfTasksToLoad.clear();
754 for (iter=_tasks.begin(); iter!=_tasks.end(); iter++)
756 string nodeToLoad = _mainSched->getTaskName(*iter);
757 _listOfTasksToLoad.push_back(nodeToLoad);
759 if (getNbOfThreads())
760 _executorState = YACS::WAITINGTASKS; // will be paused after completion of running tasks
762 _executorState = YACS::PAUSED;
763 sendEvent("executor");
764 _condForPilot.notify_all();
766 if (stop && !_isOKToEnd) waitResume(); // wait until pilot calls resumeCurrentBreakPoint(), mutex released during wait
767 if (_isOKToEnd) endRequested = true;
768 } // --- End of critical section
769 if (stop) DEBTRACE("wake up from waitResume");
773 case YACS::STEPBYSTEP:
775 { // --- Critical section
776 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
778 _listOfTasksToLoad.clear();
779 for (iter=_tasks.begin(); iter!=_tasks.end(); iter++)
781 string nodeToLoad = _mainSched->getTaskName(*iter);
782 _listOfTasksToLoad.push_back(nodeToLoad);
784 if (getNbOfThreads())
785 _executorState = YACS::WAITINGTASKS; // will be paused after completion of running tasks
787 _executorState = YACS::PAUSED;
788 sendEvent("executor");
789 _condForPilot.notify_all();
791 waitResume(); // wait until pilot calls resumeCurrentBreakPoint(), mutex released during wait
792 // or, if no pilot, wait until no more running tasks (stop on error)
793 if (_isOKToEnd) endRequested = true;
794 } // --- End of critical section
795 DEBTRACE("wake up from waitResume");
799 DEBTRACE("endRequested: " << endRequested);
804 //! in modes Step By step or with BreakPoint, wait until pilot resumes the execution
806 * With the condition Mutex, the mutex is released atomically during the wait.
807 * Pilot calls Executor::resumeCurrentBreakPoint to resume execution.
808 * Must be called while mutex is locked.
811 void Executor::waitResume()
813 DEBTRACE("Executor::waitResume()");
814 _condForStepByStep.wait(_mutexForSchedulerUpdate); // wait until pilot calls resumeCurrentBreakPoint()
819 //! Perform loading of a Task.
821 * \param task : Task to load
824 void Executor::loadTask(Task *task, const Executor *execInst)
826 DEBTRACE("Executor::loadTask(Task *task)");
827 if(task->getState() != YACS::TOLOAD)
829 traceExec(task, "state:TOLOAD", ComputePlacement(task));
831 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
832 _mainSched->notifyFrom(task,YACS::START);
833 }//End of critical section
836 traceExec(task, "load", ComputePlacement(task));
838 traceExec(task, "initService", ComputePlacement(task));
843 std::cerr << ex.what() << std::endl;
845 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
847 _mainSched->notifyFrom(task,YACS::ABORT);
848 traceExec(task, "state:"+Node::getStateName(task->getState()), ComputePlacement(task));
849 }//End of critical section
853 std::cerr << "Load failed" << std::endl;
855 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
857 _mainSched->notifyFrom(task,YACS::ABORT);
858 traceExec(task, "state:"+Node::getStateName(task->getState()), ComputePlacement(task));
859 }//End of critical section
870 void Executor::loadTasks(const std::vector<Task *>& tasks, const Executor *execInst)
872 for(std::vector<Task *>::const_iterator iter = _tasks.begin(); iter != _tasks.end(); iter++)
873 loadTask(*iter,execInst);
876 void Executor::loadParallelTasks(const std::vector<Task *>& tasks, const Executor *execInst)
878 std::vector<Thread> ths(tasks.size());
879 std::size_t ithread(0);
880 for(std::vector<Task *>::const_iterator iter = _tasks.begin(); iter != _tasks.end(); iter++, ithread++)
882 DEBTRACE("Executor::loadParallelTasks(Task *task)");
883 struct threadargs *args(new threadargs);
884 args->task = (*iter);
885 args->sched = _mainSched;
886 args->execInst = this;
887 ths[ithread].go(functionForTaskLoad, args, _threadStackSize);
889 for(ithread=0;ithread<tasks.size();ithread++)
893 //! Execute a list of tasks possibly connected through datastream links
895 * \param tasks : a list of tasks to execute
898 void Executor::launchTasks(const std::vector<Task *>& tasks)
900 //First phase, make datastream connections
901 for(vector<Task *>::const_iterator iter=tasks.begin();iter!=tasks.end();iter++)
903 YACS::StatesForNode state=(*iter)->getState();
904 if(state != YACS::TOLOAD && state != YACS::TORECONNECT)continue;
907 (*iter)->connectService();
908 traceExec(*iter, "connectService",ComputePlacement(*iter));
910 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
911 (*iter)->connected();
912 }//End of critical section
916 std::cerr << ex.what() << std::endl;
919 (*iter)->disconnectService();
920 traceExec(*iter, "disconnectService",ComputePlacement(*iter));
924 // Disconnect has failed
925 traceExec(*iter, "disconnectService failed, ABORT",ComputePlacement(*iter));
928 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
930 _mainSched->notifyFrom(*iter,YACS::ABORT);
931 }//End of critical section
935 std::cerr << "Problem in connectService" << std::endl;
938 (*iter)->disconnectService();
939 traceExec(*iter, "disconnectService",ComputePlacement(*iter));
943 // Disconnect has failed
944 traceExec(*iter, "disconnectService failed, ABORT",ComputePlacement(*iter));
947 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
949 _mainSched->notifyFrom(*iter,YACS::ABORT);
950 }//End of critical section
952 if((*iter)->getState() == YACS::ERROR)
954 //try to put all coupled tasks in error
955 std::set<Task*> coupledSet;
956 (*iter)->getCoupledTasks(coupledSet);
957 for (std::set<Task*>::iterator it = coupledSet.begin(); it != coupledSet.end(); ++it)
960 if(t == *iter)continue;
961 if(t->getState() == YACS::ERROR)continue;
964 t->disconnectService();
965 traceExec(t, "disconnectService",ComputePlacement(*iter));
969 // Disconnect has failed
970 traceExec(t, "disconnectService failed, ABORT",ComputePlacement(*iter));
973 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
975 _mainSched->notifyFrom(t,YACS::ABORT);
976 }//End of critical section
977 traceExec(t, "state:"+Node::getStateName(t->getState()),ComputePlacement(*iter));
980 traceExec(*iter, "state:"+Node::getStateName((*iter)->getState()),ComputePlacement(*iter));
983 //Second phase, execute each task in a thread
984 for(vector<Task *>::const_iterator iter=tasks.begin();iter!=tasks.end();iter++)
990 //! Execute a Task in a thread
992 * \param task : Task to execute
994 * Calls Scheduler::notifyFrom of main node (_mainSched) to notify start
996 * Calls Executor::functionForTaskExecution in Thread
999 void Executor::launchTask(Task *task)
1001 DEBTRACE("Executor::launchTask(Task *task)");
1002 struct threadargs *args;
1003 if(task->getState() != YACS::TOACTIVATE)return;
1005 DEBTRACE("before _semForMaxThreads.wait " << _semThreadCnt);
1006 if(_semThreadCnt == 0)
1008 // --- Critical section
1009 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
1010 //check if we have enough threads to run
1011 std::set<Task*> tmpSet=_runningTasks;
1012 std::set<Task*>::iterator it = tmpSet.begin();
1013 std::string status="running";
1014 std::set<Task*> coupledSet;
1015 while( it != tmpSet.end() )
1019 tt->getCoupledTasks(coupledSet);
1021 for (std::set<Task*>::iterator iter = coupledSet.begin(); iter != coupledSet.end(); ++iter)
1023 if((*iter)->getState() == YACS::TOACTIVATE)status="toactivate";
1024 tmpSet.erase(*iter);
1026 if(status=="running")break;
1027 it = tmpSet.begin();
1030 if(status=="toactivate")
1032 std::cerr << "WARNING: maybe you need more threads to run your schema (current value="<< _maxThreads << ")" << std::endl;
1033 std::cerr << "If it is the case, set the YACS_MAX_THREADS environment variable to a bigger value (export YACS_MAX_THREADS=xxx)" << std::endl;
1035 // --- End of critical section
1038 _semForMaxThreads.wait();
1041 args= new threadargs;
1043 args->sched = _mainSched;
1044 args->execInst = this;
1046 traceExec(task, "launch",ComputePlacement(task));
1048 { // --- Critical section
1049 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
1050 _numberOfRunningTasks++;
1051 _runningTasks.insert(task);
1052 task->begin(); //change state to ACTIVATED
1053 } // --- End of critical section
1054 Thread(functionForTaskExecution, args, _threadStackSize);
1057 //! wait until a running task ends
1059 void Executor::sleepWhileNoEventsFromAnyRunningTask()
1061 DEBTRACE("Executor::sleepWhileNoEventsFromAnyRunningTask()");
1062 // _semForNewTasksToPerform.wait(); //----utiliser pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
1063 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForSchedulerUpdate);
1064 if (_numberOfRunningTasks > 0 && _numberOfEndedTasks==0)
1066 _isWaitingEventsFromRunningTasks = true;
1067 _condForNewTasksToPerform.wait(_mutexForSchedulerUpdate); // mutex released during wait
1069 _numberOfEndedTasks=0;
1075 void Executor::notifyEndOfThread(YACS::BASES::Thread *thread)
1077 /*_mutexForNbOfConcurrentThreads.lock();
1078 _groupOfAllThreadsCreated.remove(thread);
1080 _mutexForNbOfConcurrentThreads.unlock();*/
1084 //! must be used protected by _mutexForSchedulerUpdate!
1086 void Executor::wakeUp()
1088 DEBTRACE("Executor::wakeUp() " << _isWaitingEventsFromRunningTasks);
1089 if (_isWaitingEventsFromRunningTasks)
1091 _isWaitingEventsFromRunningTasks = false;
1092 _condForNewTasksToPerform.notify_all();
1095 _numberOfEndedTasks++;
1098 //! number of running tasks
1100 int Executor::getNbOfThreads()
1103 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForNbOfConcurrentThreads);
1104 _isRunningunderExternalControl=true;
1105 ret = _groupOfAllThreadsCreated.size();
1110 * This thread is NOT supposed to be detached !
1112 void *Executor::functionForTaskLoad(void *arg)
1114 DEBTRACE("Executor::functionForTaskLoad(void *arg)");
1115 struct threadargs *args = (struct threadargs *) arg;
1116 Task *task=args->task;
1117 Scheduler *sched=args->sched;
1118 Executor *execInst=args->execInst;
1120 execInst->loadTask(task,execInst);// no throw of this method - all throw are catched !
1124 //! Function to perform execution of a task in a thread
1126 * \param arg : 3 elements (a Task, a Scheduler, an Executor)
1128 * Calls Task::execute
1130 * Calls Task::finished when the task is finished
1132 * Calls (notify with event YACS::FINISH) Scheduler::notifyFrom when the task is finished
1134 * Calls Executor::wakeUp and Executor::notifyEndOfThread
1137 void *Executor::functionForTaskExecution(void *arg)
1139 DEBTRACE("Executor::functionForTaskExecution(void *arg)");
1141 struct threadargs *args = (struct threadargs *) arg;
1142 Task *task=args->task;
1143 Scheduler *sched=args->sched;
1144 Executor *execInst=args->execInst;
1146 execInst->traceExec(task, "state:"+Node::getStateName(task->getState()),ComputePlacement(task));
1152 if(execInst->getDPLScopeSensitive())
1154 Node *node(dynamic_cast<Node *>(task));
1155 ComposedNode *gfn(dynamic_cast<ComposedNode *>(sched));
1156 if(node!=0 && gfn!=0)
1157 node->applyDPLScope(gfn);
1160 YACS::Event ev=YACS::FINISH;
1163 execInst->traceExec(task, "start execution",ComputePlacement(task));
1165 execInst->traceExec(task, "end execution OK",ComputePlacement(task));
1167 catch(Exception& ex)
1169 std::cerr << "YACS Exception during execute" << std::endl;
1170 std::cerr << ex.what() << std::endl;
1172 string message = "end execution ABORT, ";
1173 message += ex.what();
1174 execInst->traceExec(task, message,ComputePlacement(task));
1178 // Execution has failed
1179 std::cerr << "Execution has failed: unknown reason" << std::endl;
1181 execInst->traceExec(task, "end execution ABORT, unknown reason",ComputePlacement(task));
1187 DEBTRACE("task->disconnectService()");
1188 task->disconnectService();
1189 execInst->traceExec(task, "disconnectService",ComputePlacement(task));
1193 // Disconnect has failed
1194 std::cerr << "disconnect has failed" << std::endl;
1196 execInst->traceExec(task, "disconnectService failed, ABORT",ComputePlacement(task));
1200 std::string placement(ComputePlacement(task));
1202 // container management for HomogeneousPoolOfContainer
1204 HomogeneousPoolContainer *contC(dynamic_cast<HomogeneousPoolContainer *>(task->getContainer()));
1207 YACS::BASES::AutoLocker<Container> alckCont(contC);
1208 contC->release(task);
1211 DEBTRACE("End task->execute()");
1212 { // --- Critical section
1213 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&execInst->_mutexForSchedulerUpdate);
1216 if (ev == YACS::FINISH) task->finished();
1217 if (ev == YACS::ABORT)
1219 execInst->_errorDetected = true;
1220 if (execInst->_stopOnErrorRequested)
1222 execInst->_execMode = YACS::STEPBYSTEP;
1223 execInst->_isOKToEnd = true;
1227 execInst->traceExec(task, "state:"+Node::getStateName(task->getState()),placement);
1228 sched->notifyFrom(task,ev);
1230 catch(Exception& ex)
1232 //notify has failed : it is supposed to have set state
1233 //so no need to do anything
1234 std::cerr << "Error during notification" << std::endl;
1235 std::cerr << ex.what() << std::endl;
1239 //notify has failed : it is supposed to have set state
1240 //so no need to do anything
1241 std::cerr << "Notification failed" << std::endl;
1243 execInst->_numberOfRunningTasks--;
1244 execInst->_runningTasks.erase(task);
1245 DEBTRACE("_numberOfRunningTasks: " << execInst->_numberOfRunningTasks
1246 << " _execMode: " << execInst->_execMode
1247 << " _executorState: " << execInst->_executorState);
1248 if ((execInst->_numberOfRunningTasks == 0) && (execInst->_execMode != YACS::CONTINUE)) // no more running tasks
1250 if (execInst->_executorState == YACS::WAITINGTASKS)
1252 execInst->_executorState = YACS::PAUSED;
1253 execInst->sendEvent("executor");
1254 execInst->_condForPilot.notify_all();
1255 if (execInst->_errorDetected &&
1256 execInst->_stopOnErrorRequested &&
1257 !execInst->_isRunningunderExternalControl)
1258 execInst->_condForStepByStep.notify_all(); // exec thread may be on waitResume
1261 DEBTRACE("before _semForMaxThreads.post " << execInst->_semThreadCnt);
1262 execInst->_semForMaxThreads.post();
1263 execInst->_semThreadCnt += 1;
1264 DEBTRACE("after _semForMaxThreads.post " << execInst->_semThreadCnt);
1265 if (execInst->_executorState != YACS::PAUSED) execInst->wakeUp();
1267 } // --- End of critical section (change state)
1269 //execInst->notifyEndOfThread(0);
1274 void Executor::traceExec(Task *task, const std::string& message, const std::string& placement)
1276 string nodeName = _mainSched->getTaskName(task);
1277 Container *cont = task->getContainer();
1278 string containerName = "---";
1280 containerName = cont->getName();
1283 DWORD now = timeGetTime();
1284 double elapse = (now - _start)/1000.0;
1287 gettimeofday(&now, NULL);
1288 double elapse = (now.tv_sec - _start.tv_sec) + double(now.tv_usec - _start.tv_usec)/1000000.0;
1291 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&_mutexForTrace);
1292 _trace << elapse << " " << containerName << " " << placement << " " << nodeName << " " << message << endl;
1297 //! emit notification to all observers registered with the dispatcher
1299 * The dispatcher is unique and can be obtained by getDispatcher()
1301 void Executor::sendEvent(const std::string& event)
1303 Dispatcher* disp=Dispatcher::getDispatcher();
1306 disp->dispatch(_root,event);
1310 * This method takes in input a list of tasks and selects from that lists a part of it considering only the containers.
1311 * If tasks have no container instance subclass of HomogeneousPoolContainer this method will let the \a tsks untouched.
1313 * \param [in,out] tsks - list of tasks to be
1315 void Executor::FilterTasksConsideringContainers(std::vector<Task *>& tsks)
1317 std::map<HomogeneousPoolContainer *, std::vector<Task *> > m;
1318 for(std::vector<Task *>::const_iterator it=tsks.begin();it!=tsks.end();it++)
1323 Container *cont(cur->getContainer());
1326 m[(HomogeneousPoolContainer *)NULL].push_back(cur);
1329 HomogeneousPoolContainer *contC(dynamic_cast<HomogeneousPoolContainer *>(cont));
1332 m[(HomogeneousPoolContainer *)NULL].push_back(cur);
1335 m[contC].push_back(cur);
1338 std::vector<Task *> ret;
1339 for(std::map<HomogeneousPoolContainer *, std::vector<Task *> >::const_iterator it=m.begin();it!=m.end();it++)
1341 HomogeneousPoolContainer *curhpc((*it).first);
1342 const std::vector<Task *>& curtsks((*it).second);
1345 ret.insert(ret.end(),curtsks.begin(),curtsks.end());
1349 // start of critical section for container curhpc
1350 YACS::BASES::AutoLocker<Container> alckForCont(curhpc);
1351 std::vector<const Task *> vecOfTaskSharingSameHPContToBeRunSimutaneously;
1352 std::size_t sz(curhpc->getNumberOfFreePlace());
1353 std::vector<Task *>::const_iterator it2(curtsks.begin());
1354 for(std::size_t i=0;i<sz && it2!=curtsks.end();i++,it2++)
1356 vecOfTaskSharingSameHPContToBeRunSimutaneously.push_back(*it2);
1357 ret.push_back(*it2);
1359 curhpc->allocateFor(vecOfTaskSharingSameHPContToBeRunSimutaneously);
1360 //end of critical section
1367 std::string Executor::ComputePlacement(Task *zeTask)
1369 std::string placement("---");
1372 if(zeTask->getContainer())
1373 placement=zeTask->getContainer()->getFullPlacementId(zeTask);