Howto:Exposing Subsystems to Nasal

From FlightGear wiki
Revision as of 12:37, 7 February 2016 by Hooray (Talk | contribs) (SimGear)

Jump to: navigation, search
WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
See history for the latest developments.


Document how existing FlightGear subsystems (i.e. those inheriting from SGSubsystem) can be exposed to Nasal space to make certain information available to Nasal space.


This article will specifically cover how to access the FlightGear event manager (SGEventMgr) and the listener API (SGPropertyChangeListener) to track registration and invocation of Nasal callbacks per frame.


The main mechanism for triggering scripted Nasal code to be executed within the FlightGear main loop are so called listeners (Nasal callbacks that are stored in a property-specific vector/list, that get executed in a for-each loop whenever a property is modified), and timers, which represent "delays" (typically in seconds or milliseconds) after which a timer expires and fires its callback to be executed.

Unfortunately, these two mechanisms are rather low-level and tedious/error-prone to use correctly, so that code (Nasal callbacks) may be triggered accidently, and unnecessarily, which ultimately eats up considerable resources while creating a frame, sooner or later causing frame stuttering.

Even more unfortunate is the fact that FlightGear does not currently provide any mechanisms/APIs to look behind the scenes, i.e. to see how often a callback is actually executed, and where the callback is registered to be executed.

This article intends to document the steps required to expose meta information about timers and listeners to scripting space so that a Nasal/Canvas bsed UI (dialog) can be displayed with a list of active callbacks, and their runtime characteristics.

However, the underlying timer/listener APIs are not specific to Nasal scripting but also commonly used elsewhere (in fact, in most other subsystems), and they are also infamous for buggy C++ code that similarly affected FlightGear performance.

This will include patches to all 3 repositories of FlightGear, i.e. SimGear, FlightGear and fgdata.


We will be providing additional methods to obtain access to otherwise private fields/data (members) and make those available to FGNasalSys.


  • extend the destructors of the timer/listener objects to dump creation/destruction and invocation info to the log via SG_LOG Not done Not done
  • extend SGTimerQueue to provide methods for getting the size of the queue Not done Not done
  • extend SGEventMgr to dump that info to the console via the SG_LOG macro Not done Not done
  • add a counter to keep track of timers registered via settimer/maketimer Not done Not done
  • add a counter to keep track of listeners registered via setlistener Not done Not done
  • add a per-frame resetting counter to keep track of callback invocations per frame Not done Not done




For starters, we will just add some debugging statements to the event manager

diff --git a/simgear/structure/event_mgr.cxx b/simgear/structure/event_mgr.cxx
index 2e4f7b7..eb892ad 100644
--- a/simgear/structure/event_mgr.cxx
+++ b/simgear/structure/event_mgr.cxx
@@ -24,9 +24,26 @@ void SGEventMgr::add(const std::string& name, SGCallback* cb,
     SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
+    SG_LOG(SG_GENERAL, SG_ALERT, "== Inserting timer into queue" <<
+	"\t type:" << name <<
+	"\t interval:" << interval <<
+	"\t delay:" << delay <<
+	"\t repeated:" << repeat <<
+	"\t simtime/realtime:" << simtime <<"/"<<!simtime
+	<< std::endl);
+    info();
     q->insert(t, delay);
+void info() {
+    SG_LOG(SG_GENERAL, SG_ALERT, "Total Entries in queues" << 
+	"\t simtime queue:" << _simQueue->getQueueSize() << std::endl
+	"\t realtime queue:" << _rtQueue->getQueueSize() << std::endl
+	);
     delete callback;
diff --git a/simgear/structure/event_mgr.hxx b/simgear/structure/event_mgr.hxx
index 699522b..0cab8b7 100644
--- a/simgear/structure/event_mgr.hxx
+++ b/simgear/structure/event_mgr.hxx
@@ -38,6 +38,9 @@ public:
     double   nextTime()  { return -_table[0].pri; }
     SGTimer* findByName(const std::string& name) const;
+    unsigned int getQueueSize() const {
+		return _numEntries;
+	}
     // The "priority" is stored as a negative time.  This allows the
     // implementation to treat the "top" of the heap as the largest
@@ -76,6 +79,7 @@ public:
     virtual void update(double delta_time_sec);
     virtual void unbind();
     virtual void shutdown();
+    virtual void info(); // dumps queue info to the console
     void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
@@ -120,6 +124,11 @@ public:
     void removeTask(const std::string& name);
+    typedef std::pair<std::string, int> CallbackInfo; //callback name & number of instances
+    typedef std::map<SGCallback*, CallbackInfo> CallbackMap;
+    CallbackMap _registeredCallbacks;
     friend class SGTimer;
     void add(const std::string& name, SGCallback* cb,

However, permanently logging all that info to the console/log file can obviously become heavy - so the next thing we are going to do is to use so called property objects to directly expose this information in the form of properties, for each event queue - so that this needs to be implemented at the SGTimerQueue level.

In particular, we need to expose the following information for each queue:

  • number of total entries
  • number of Nasal callbacks (settimer, maketimer)
  • number of other callbacks


Base Package