]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/subsystem_mgr.cxx
Add optional attribute condition to "copyProperties".
[simgear.git] / simgear / structure / subsystem_mgr.cxx
index 0ed6b43623913bb6d72dd3193805526f82123f4e..39598abeb8f3cb83768ca92309217bff382f9907 100644 (file)
@@ -1,3 +1,22 @@
+// Written by David Megginson, started 2000-12
+//
+// Copyright (C) 2000  David Megginson, david@megginson.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/timing/timestamp.hxx>
@@ -8,7 +27,7 @@
 #include <simgear/math/SGMath.hxx>
 
 
-\f
+const int SG_MAX_SUBSYSTEM_EXCEPTIONS = 4;\f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of SGSubsystem
 ////////////////////////////////////////////////////////////////////////
@@ -38,6 +57,11 @@ SGSubsystem::reinit ()
 {
 }
 
+void
+SGSubsystem::shutdown ()
+{
+}
+
 void
 SGSubsystem::bind ()
 {
@@ -79,7 +103,7 @@ SGSubsystem::printTimingInformation ()
    SGTimeStamp startTime;
    for ( eventTimeVecIterator i = timingInfo.begin();
           i != timingInfo.end();
-          i++) {
+          ++i) {
        if (i == timingInfo.begin()) {
            startTime = i->getTime();
        } else {
@@ -104,16 +128,20 @@ void SGSubsystem::stamp(const string& name)
 // Implementation of SGSubsystemGroup.
 ////////////////////////////////////////////////////////////////////////
 
-SGSubsystemGroup::SGSubsystemGroup ()
+SGSubsystemGroup::SGSubsystemGroup () :
+  _fixedUpdateTime(-1.0),
+  _updateTimeRemainder(0.0)
 {
 }
 
 SGSubsystemGroup::~SGSubsystemGroup ()
 {
-    for (unsigned int i = 0; i < _members.size(); i++)
+    printTimingStatistics();
+
+    // reverse order to prevent order dependency problems
+    for (unsigned int i = _members.size(); i > 0; i--)
     {
-        _members[i]->printTimingStatistics();
-        delete _members[i];
+        delete _members[i-1];
     }
 }
 
@@ -138,6 +166,14 @@ SGSubsystemGroup::reinit ()
         _members[i]->subsystem->reinit();
 }
 
+void
+SGSubsystemGroup::shutdown ()
+{
+    // reverse order to prevent order dependency problems
+    for (unsigned int i = _members.size(); i > 0; i--)
+        _members[i-1]->subsystem->shutdown();
+}
+
 void
 SGSubsystemGroup::bind ()
 {
@@ -148,25 +184,39 @@ SGSubsystemGroup::bind ()
 void
 SGSubsystemGroup::unbind ()
 {
-    for (unsigned int i = 0; i < _members.size(); i++)
-        _members[i]->subsystem->unbind();
+    // reverse order to prevent order dependency problems
+    for (unsigned int i = _members.size(); i > 0; i--)
+       _members[i-1]->subsystem->unbind();
 }
 
 void
 SGSubsystemGroup::update (double delta_time_sec)
 {
-    for (unsigned int i = 0; i < _members.size(); i++)
-    {
-         SGTimeStamp timeStamp = SGTimeStamp::now();
-         _members[i]->update(delta_time_sec); // indirect call
-         timeStamp = timeStamp - SGTimeStamp::now();
-         double b = timeStamp.toUSecs();
-         _members[i]->updateExecutionTime(b);
-         double threshold = _members[i]->getTimeWarningThreshold();
-         if (( b > threshold ) && (b > 10000)) {
-             _members[i]->printTimingInformation(b);
-         }
+    int loopCount = 1;
+    // if dt == 0.0, we are paused, so we need to run one iteration
+    // of our members; if we have a fixed update time, we compute a
+    // loop count, and locally adjust dt
+    if ((delta_time_sec > 0.0) && (_fixedUpdateTime > 0.0)) {
+      double localDelta = delta_time_sec + _updateTimeRemainder;
+      loopCount = SGMiscd::roundToInt(localDelta / _fixedUpdateTime);
+      _updateTimeRemainder = delta_time_sec - (loopCount * _fixedUpdateTime);
+      delta_time_sec = _fixedUpdateTime;
     }
+
+    while (loopCount-- > 0) {
+      for (unsigned int i = 0; i < _members.size(); i++)
+      {
+           SGTimeStamp timeStamp = SGTimeStamp::now();
+           _members[i]->update(delta_time_sec); // indirect call
+           timeStamp = SGTimeStamp::now() - timeStamp;
+           double b = timeStamp.toUSecs();
+           _members[i]->updateExecutionTime(b);
+           double threshold = _members[i]->getTimeWarningThreshold();
+           if (( b > threshold ) && (b > 10000)) {
+               _members[i]->printTimingInformation(b);
+           }
+      }
+    } // of multiple update loop
 }
 
 void 
@@ -178,6 +228,16 @@ SGSubsystemGroup::collectDebugTiming(bool collect)
     }
 }
 
+void 
+SGSubsystemGroup::printTimingStatistics(double minMaxTime,double minJitter)
+{
+    for (unsigned int i = _members.size(); i > 0; i--)
+    {
+        _members[i-1]->printTimingStatistics(minMaxTime, minJitter);
+        _members[i-1]->timeStat.reset();
+    }
+}
+
 void
 SGSubsystemGroup::suspend ()
 {
@@ -232,7 +292,18 @@ SGSubsystemGroup::remove_subsystem (const string &name)
 }
 
 void
-SGSubsystemGroup::Member::printTimingStatistics ()
+SGSubsystemGroup::set_fixed_update_time(double dt)
+{
+  _fixedUpdateTime = dt;
+}
+
+/**
+ * Print timing statistics.
+ * Only show data if jitter exceeds minJitter or
+ * maximum time exceeds minMaxTime. 
+ */
+void
+SGSubsystemGroup::Member::printTimingStatistics(double minMaxTime,double minJitter)
 {
     if (collectTimeStats) {
         double minTime = timeStat.min()   / 1000;
@@ -240,13 +311,17 @@ SGSubsystemGroup::Member::printTimingStatistics ()
         double meanTime = timeStat.mean() / 1000;
         double stddev   = timeStat.stdDev()   / 1000;
 
-        char buffer[256];
-        snprintf(buffer, 256, "Timing summary for %20s.\n"
-                              "-  mean time: %04.2f ms.\n"
-                              "-  min time : %04.2f ms.\n"
-                              "-  max time : %04.2f ms.\n"
-                              "- stddev    : %04.2f ms.\n", name.c_str(), meanTime, minTime, maxTime, stddev);
-        SG_LOG(SG_GENERAL, SG_ALERT, buffer);
+        if ((maxTime - minTime >= minJitter)||
+            (maxTime >= minMaxTime))
+        {
+            char buffer[256];
+            snprintf(buffer, 256, "Timing summary for %20s.\n"
+                                  "-  mean time: %04.2f ms.\n"
+                                  "-  min time : %04.2f ms.\n"
+                                  "-  max time : %04.2f ms.\n"
+                                  "-  stddev   : %04.2f ms.\n", name.c_str(), meanTime, minTime, maxTime, stddev);
+            SG_LOG(SG_GENERAL, SG_ALERT, buffer);
+        }
     }
 }
 
@@ -285,7 +360,8 @@ SGSubsystemGroup::Member::Member ()
       subsystem(0),
       min_step_sec(0),
       elapsed_sec(0),
-      collectTimeStats(false)
+      collectTimeStats(false),
+      exceptionCount(0)
 {
 }
 
@@ -303,11 +379,26 @@ void
 SGSubsystemGroup::Member::update (double delta_time_sec)
 {
     elapsed_sec += delta_time_sec;
-    if (elapsed_sec >= min_step_sec) {
-        if (!subsystem->is_suspended()) {
-            subsystem->update(elapsed_sec);
-            elapsed_sec = 0;
-        }
+    if (elapsed_sec < min_step_sec) {
+        return;
+    }
+    
+    if (subsystem->is_suspended()) {
+        return;
+    }
+    
+    try {
+      subsystem->update(elapsed_sec);
+      elapsed_sec = 0;
+    } catch (sg_exception& e) {
+      SG_LOG(SG_GENERAL, SG_ALERT, "caught exception processing subsystem:" << name
+        << "\nmessage:" << e.getMessage());
+      
+      if (++exceptionCount > SG_MAX_SUBSYSTEM_EXCEPTIONS) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "(exceptionCount=" << exceptionCount <<
+          ", suspending)");
+        subsystem->suspend();
+      }
     }
 }
 
@@ -316,7 +407,7 @@ void
 SGSubsystemGroup::Member::printTimingInformation(double time)
 {
      if (collectTimeStats) {
-         SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem Timing Alert : " << time << " " << name);
+         SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem Timing Alert, subsystem \"" << name << "\": " << time/1000.0 << "ms");
          subsystem->printTimingInformation();
      }
 }
@@ -344,52 +435,72 @@ void SGSubsystemGroup::Member::updateExecutionTime(double time)
 
 SGSubsystemMgr::SGSubsystemMgr ()
 {
+  for (int i = 0; i < MAX_GROUPS; i++) {
+    _groups[i] = new SGSubsystemGroup;
+  }
 }
 
 SGSubsystemMgr::~SGSubsystemMgr ()
 {
+  // ensure get_subsystem returns NULL from now onwards,
+  // before the SGSubsystemGroup destructors are run
+  _subsystem_map.clear();
+  
+  for (int i = 0; i < MAX_GROUPS; i++) {
+    delete _groups[i];
+  }
 }
 
 void
 SGSubsystemMgr::init ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-            _groups[i].init();
+            _groups[i]->init();
 }
 
 void
 SGSubsystemMgr::postinit ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-            _groups[i].postinit();
+            _groups[i]->postinit();
 }
 
 void
 SGSubsystemMgr::reinit ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-            _groups[i].reinit();
+            _groups[i]->reinit();
+}
+
+void
+SGSubsystemMgr::shutdown ()
+{
+    // reverse order to prevent order dependency problems
+    for (int i = MAX_GROUPS-1; i >= 0; i--)
+        _groups[i]->shutdown();
 }
 
+
 void
 SGSubsystemMgr::bind ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-        _groups[i].bind();
+        _groups[i]->bind();
 }
 
 void
 SGSubsystemMgr::unbind ()
 {
-    for (int i = 0; i < MAX_GROUPS; i++)
-        _groups[i].unbind();
+    // reverse order to prevent order dependency problems
+    for (int i = MAX_GROUPS-1; i >= 0; i--)
+        _groups[i]->unbind();
 }
 
 void
 SGSubsystemMgr::update (double delta_time_sec)
 {
     for (int i = 0; i < MAX_GROUPS; i++) {
-        _groups[i].update(delta_time_sec);
+        _groups[i]->update(delta_time_sec);
     }
 }
 
@@ -397,7 +508,7 @@ void
 SGSubsystemMgr::collectDebugTiming(bool collect)
 {
     for (int i = 0; i < MAX_GROUPS; i++) {
-        _groups[i].collectDebugTiming(collect);
+        _groups[i]->collectDebugTiming(collect);
     }
 }
 
@@ -405,14 +516,14 @@ void
 SGSubsystemMgr::suspend ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-        _groups[i].suspend();
+        _groups[i]->suspend();
 }
 
 void
 SGSubsystemMgr::resume ()
 {
     for (int i = 0; i < MAX_GROUPS; i++)
-        _groups[i].resume();
+        _groups[i]->resume();
 }
 
 bool
@@ -435,16 +546,39 @@ SGSubsystemMgr::add (const char * name, SGSubsystem * subsystem,
     _subsystem_map[name] = subsystem;
 }
 
+SGSubsystem* 
+SGSubsystemMgr::remove(const char* name)
+{
+  SubsystemDict::iterator s =_subsystem_map.find(name);
+  if (s == _subsystem_map.end()) {
+    return NULL;
+  }
+  
+  SGSubsystem* sub = s->second;
+  _subsystem_map.erase(s);
+  
+// tedious part - we don't know which group the subsystem belongs too
+  for (int i = 0; i < MAX_GROUPS; i++) {
+    if (_groups[i]->get_subsystem(name) == sub) {
+      _groups[i]->remove_subsystem(name);
+      break;
+    }
+  } // of groups iteration
+  
+  return sub;
+}
+
+
 SGSubsystemGroup *
 SGSubsystemMgr::get_group (GroupType group)
 {
-    return &(_groups[group]);
+    return _groups[group];
 }
 
 SGSubsystem *
-SGSubsystemMgr::get_subsystem (const string &name)
+SGSubsystemMgr::get_subsystem (const string &name) const
 {
-    map<string,SGSubsystem *>::iterator s =_subsystem_map.find(name);
+    SubsystemDict::const_iterator s =_subsystem_map.find(name);
 
     if (s == _subsystem_map.end())
         return 0;
@@ -452,4 +586,12 @@ SGSubsystemMgr::get_subsystem (const string &name)
         return s->second;
 }
 
-// end of fgfs.cxx
+void
+SGSubsystemMgr::printTimingStatistics(double minMaxTime,double minJitter)
+{
+    for (int i = 0; i < MAX_GROUPS; i++) {
+        _groups[i]->printTimingStatistics(minMaxTime, minJitter);
+    } // of groups iteration
+}
+
+// end of subsystem_mgr.cxx