1 // Written by David Megginson, started 2000-12
3 // Copyright (C) 2000 David Megginson, david@megginson.com
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # include <simgear_config.h>
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/timing/timestamp.hxx>
28 #include "exception.hxx"
29 #include "subsystem_mgr.hxx"
31 #include <simgear/math/SGMath.hxx>
32 #include "SGSmplstat.hxx"
34 const int SG_MAX_SUBSYSTEM_EXCEPTIONS = 4;
38 ////////////////////////////////////////////////////////////////////////
39 // Implementation of SGSubsystem
40 ////////////////////////////////////////////////////////////////////////
42 SGSubsystemTimingCb SGSubsystem::reportTimingCb = NULL;
43 void* SGSubsystem::reportTimingUserData = NULL;
45 SGSubsystem::SGSubsystem ()
50 SGSubsystem::~SGSubsystem ()
59 SGSubsystem::InitStatus
60 SGSubsystem::incrementalInit ()
67 SGSubsystem::postinit ()
72 SGSubsystem::reinit ()
77 SGSubsystem::shutdown ()
87 SGSubsystem::unbind ()
92 SGSubsystem::suspend ()
98 SGSubsystem::suspend (bool suspended)
100 _suspended = suspended;
104 SGSubsystem::resume ()
110 SGSubsystem::is_suspended () const
115 void SGSubsystem::stamp(const string& name)
117 timingInfo.push_back(TimingInfo(name, SGTimeStamp::now()));
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of SGSubsystemGroup.
122 ////////////////////////////////////////////////////////////////////////
124 class SGSubsystemGroup::Member
127 Member (const Member &member);
132 virtual void update (double delta_time_sec);
134 void reportTiming(void) { if (reportTimingCb) reportTimingCb(reportTimingUserData, name, &timeStat); }
135 void updateExecutionTime(double time) { timeStat += time;}
137 SampleStatistic timeStat;
139 SGSharedPtr<SGSubsystem> subsystem;
142 bool collectTimeStats;
149 SGSubsystemGroup::SGSubsystemGroup () :
150 _fixedUpdateTime(-1.0),
151 _updateTimeRemainder(0.0),
156 SGSubsystemGroup::~SGSubsystemGroup ()
158 // reverse order to prevent order dependency problems
159 for( size_t i = _members.size(); i > 0; i-- )
161 delete _members[i-1];
166 SGSubsystemGroup::init ()
168 for( size_t i = 0; i < _members.size(); i++ )
169 _members[i]->subsystem->init();
172 SGSubsystem::InitStatus
173 SGSubsystemGroup::incrementalInit()
175 if (_initPosition >= _members.size())
180 InitStatus memberStatus = _members[_initPosition]->subsystem->incrementalInit();
181 _members[_initPosition]->initTime += st.elapsedMSec();
183 if (memberStatus == INIT_DONE)
186 return INIT_CONTINUE;
190 SGSubsystemGroup::postinit ()
192 for( size_t i = 0; i < _members.size(); i++ )
193 _members[i]->subsystem->postinit();
197 SGSubsystemGroup::reinit ()
199 for( size_t i = 0; i < _members.size(); i++ )
200 _members[i]->subsystem->reinit();
204 SGSubsystemGroup::shutdown ()
206 // reverse order to prevent order dependency problems
207 for( size_t i = _members.size(); i > 0; i-- )
208 _members[i-1]->subsystem->shutdown();
213 SGSubsystemGroup::bind ()
215 for( size_t i = 0; i < _members.size(); i++ )
216 _members[i]->subsystem->bind();
220 SGSubsystemGroup::unbind ()
222 // reverse order to prevent order dependency problems
223 for( size_t i = _members.size(); i > 0; i-- )
224 _members[i-1]->subsystem->unbind();
228 SGSubsystemGroup::update (double delta_time_sec)
231 // if dt == 0.0, we are paused, so we need to run one iteration
232 // of our members; if we have a fixed update time, we compute a
233 // loop count, and locally adjust dt
234 if ((delta_time_sec > 0.0) && (_fixedUpdateTime > 0.0)) {
235 double localDelta = delta_time_sec + _updateTimeRemainder;
236 loopCount = SGMiscd::roundToInt(localDelta / _fixedUpdateTime);
237 _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime);
238 delta_time_sec = _fixedUpdateTime;
241 bool recordTime = (reportTimingCb != NULL);
242 SGTimeStamp timeStamp;
243 while (loopCount-- > 0) {
244 for( size_t i = 0; i < _members.size(); i++ )
247 timeStamp = SGTimeStamp::now();
249 _members[i]->update(delta_time_sec); // indirect call
251 if ((recordTime)&&(reportTimingCb))
253 timeStamp = SGTimeStamp::now() - timeStamp;
254 _members[i]->updateExecutionTime(timeStamp.toUSecs());
257 } // of multiple update loop
261 SGSubsystemGroup::reportTiming(void)
263 for( size_t i = _members.size(); i > 0; i-- )
265 _members[i-1]->reportTiming();
270 SGSubsystemGroup::suspend ()
272 for( size_t i = 0; i < _members.size(); i++ )
273 _members[i]->subsystem->suspend();
277 SGSubsystemGroup::resume ()
279 for( size_t i = 0; i < _members.size(); i++ )
280 _members[i]->subsystem->resume();
284 SGSubsystemGroup::member_names() const
287 for( size_t i = 0; i < _members.size(); i++ )
288 result.push_back( _members[i]->name );
294 SGSubsystemGroup::is_suspended () const
300 SGSubsystemGroup::set_subsystem (const string &name, SGSubsystem * subsystem,
303 Member * member = get_member(name, true);
304 if (member->subsystem != 0)
305 delete member->subsystem;
307 member->subsystem = subsystem;
308 member->min_step_sec = min_step_sec;
312 SGSubsystemGroup::get_subsystem (const string &name)
314 Member * member = get_member(name);
316 return member->subsystem;
322 SGSubsystemGroup::remove_subsystem (const string &name)
324 MemberVec::iterator it = _members.begin();
325 for (; it != _members.end(); ++it) {
326 if (name == (*it)->name) {
333 SG_LOG(SG_GENERAL, SG_WARN, "remove_subsystem: missing:" << name);
337 SGSubsystemGroup::set_fixed_update_time(double dt)
339 _fixedUpdateTime = dt;
343 SGSubsystemGroup::has_subsystem (const string &name) const
345 return (((SGSubsystemGroup *)this)->get_member(name) != 0);
348 SGSubsystemGroup::Member *
349 SGSubsystemGroup::get_member (const string &name, bool create)
351 for( size_t i = 0; i < _members.size(); i++ ) {
352 if (_members[i]->name == name)
356 Member * member = new Member;
357 _members.push_back(member);
365 ////////////////////////////////////////////////////////////////////////
366 // Implementation of SGSubsystemGroup::Member
367 ////////////////////////////////////////////////////////////////////////
370 SGSubsystemGroup::Member::Member ()
380 // This shouldn't be called due to subsystem pointer ownership issues.
381 SGSubsystemGroup::Member::Member (const Member &)
385 SGSubsystemGroup::Member::~Member ()
390 SGSubsystemGroup::Member::update (double delta_time_sec)
392 elapsed_sec += delta_time_sec;
393 if (elapsed_sec < min_step_sec) {
397 if (subsystem->is_suspended()) {
402 subsystem->update(elapsed_sec);
404 } catch (sg_exception& e) {
405 SG_LOG(SG_GENERAL, SG_ALERT, "caught exception processing subsystem:" << name
406 << "\nmessage:" << e.getMessage());
408 if (++exceptionCount > SG_MAX_SUBSYSTEM_EXCEPTIONS) {
409 SG_LOG(SG_GENERAL, SG_ALERT, "(exceptionCount=" << exceptionCount <<
411 subsystem->suspend();
417 ////////////////////////////////////////////////////////////////////////
418 // Implementation of SGSubsystemMgr.
419 ////////////////////////////////////////////////////////////////////////
422 SGSubsystemMgr::SGSubsystemMgr () :
425 for (int i = 0; i < MAX_GROUPS; i++) {
426 _groups[i] = new SGSubsystemGroup;
430 SGSubsystemMgr::~SGSubsystemMgr ()
432 // ensure get_subsystem returns NULL from now onwards,
433 // before the SGSubsystemGroup destructors are run
434 _subsystem_map.clear();
436 for (int i = 0; i < MAX_GROUPS; i++) {
442 SGSubsystemMgr::init ()
444 for (int i = 0; i < MAX_GROUPS; i++)
448 SGSubsystem::InitStatus
449 SGSubsystemMgr::incrementalInit()
451 if (_initPosition >= MAX_GROUPS)
454 InitStatus memberStatus = _groups[_initPosition]->incrementalInit();
455 if (memberStatus == INIT_DONE)
458 return INIT_CONTINUE;
462 SGSubsystemMgr::postinit ()
464 for (int i = 0; i < MAX_GROUPS; i++)
465 _groups[i]->postinit();
469 SGSubsystemMgr::reinit ()
471 for (int i = 0; i < MAX_GROUPS; i++)
472 _groups[i]->reinit();
476 SGSubsystemMgr::shutdown ()
478 // reverse order to prevent order dependency problems
479 for (int i = MAX_GROUPS-1; i >= 0; i--)
480 _groups[i]->shutdown();
487 SGSubsystemMgr::bind ()
489 for (int i = 0; i < MAX_GROUPS; i++)
494 SGSubsystemMgr::unbind ()
496 // reverse order to prevent order dependency problems
497 for (int i = MAX_GROUPS-1; i >= 0; i--)
498 _groups[i]->unbind();
502 SGSubsystemMgr::update (double delta_time_sec)
504 for (int i = 0; i < MAX_GROUPS; i++) {
505 _groups[i]->update(delta_time_sec);
510 SGSubsystemMgr::suspend ()
512 for (int i = 0; i < MAX_GROUPS; i++)
513 _groups[i]->suspend();
517 SGSubsystemMgr::resume ()
519 for (int i = 0; i < MAX_GROUPS; i++)
520 _groups[i]->resume();
524 SGSubsystemMgr::is_suspended () const
530 SGSubsystemMgr::add (const char * name, SGSubsystem * subsystem,
531 GroupType group, double min_time_sec)
533 SG_LOG(SG_GENERAL, SG_INFO, "Adding subsystem " << name);
534 get_group(group)->set_subsystem(name, subsystem, min_time_sec);
536 if (_subsystem_map.find(name) != _subsystem_map.end()) {
537 SG_LOG(SG_GENERAL, SG_ALERT, "Adding duplicate subsystem " << name);
538 throw sg_exception("duplicate subsystem:" + std::string(name));
540 _subsystem_map[name] = subsystem;
544 SGSubsystemMgr::remove(const char* name)
546 SubsystemDict::iterator s =_subsystem_map.find(name);
547 if (s == _subsystem_map.end()) {
551 _subsystem_map.erase(s);
553 // tedious part - we don't know which group the subsystem belongs too
554 for (int i = 0; i < MAX_GROUPS; i++) {
555 if (_groups[i]->get_subsystem(name) != NULL) {
556 _groups[i]->remove_subsystem(name);
559 } // of groups iteration
564 SGSubsystemMgr::get_group (GroupType group)
566 return _groups[group];
570 SGSubsystemMgr::get_subsystem (const string &name) const
572 SubsystemDict::const_iterator s =_subsystem_map.find(name);
574 if (s == _subsystem_map.end())
580 /** Trigger the timing callback to report data for all subsystems. */
582 SGSubsystemMgr::reportTiming()
584 for (int i = 0; i < MAX_GROUPS; i++) {
585 _groups[i]->reportTiming();
586 } // of groups iteration
589 // end of subsystem_mgr.cxx