Hi fellow wiki editors!

To help newly registered users get more familiar with the wiki (and maybe older users too) there is now a {{Welcome to the wiki}} template. Have a look at it and feel free to add it to new users discussion pages (and perhaps your own).

I have tried to keep the template short, but meaningful. /Johan G

Difference between revisions of "Howto:Exposing Subsystems to Nasal"

From FlightGear wiki
Jump to: navigation, search
(SimGear)
m (Background)
Line 11: Line 11:
 
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.
 
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.
+
Accordingly, this means that Nasal callbacks will be either invoked by the events subsystem (timers) or by whatever subsystem modifies a property that has a Nasal listener bound to it, given that nature of the Nasal garbage collector (GC), this may make frame rates less determinstic/predictable.
 +
 
 +
Unfortunately, these two mechanisms are also 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.
 
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.

Revision as of 16:20, 8 February 2016

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.


Objective

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.

Focus

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.

Background

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.

Accordingly, this means that Nasal callbacks will be either invoked by the events subsystem (timers) or by whatever subsystem modifies a property that has a Nasal listener bound to it, given that nature of the Nasal garbage collector (GC), this may make frame rates less determinstic/predictable.

Unfortunately, these two mechanisms are also 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.

Approach

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

Roadmap

  • 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

Status

Patching

SimGear

For starters, we will just add some debugging statements to the event manager. The event manager maintains two queues for timers, one for timers intended to work with simulation time, and one with realtime (i.e. those also executing when paused). These timers are fields/members of the SGEventMgr class which inherits from SGSubsystem, they are accessible as _simQueue and _rtQueue, using the interface of the SGTimerQueue class, the size of the queue is stored in the private _numEntries field (int), which can be made accessible by adding a public getter method, which in turn can then be used by the SGEventMgr subsystem to access the size of each queue:

diff --git a/simgear/structure/event_mgr.cxx b/simgear/structure/event_mgr.cxx
index 2e4f7b7..97848b4 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 << std::endl <<
+	"\t interval:" << interval <<std::endl <<
+	"\t delay:" << delay <<std::endl <<
+	"\t repeated:" << repeat <<std::endl <<
+	"\t simtime/realtime:" << simtime <<"/"<<!simtime
+	<< std::endl);
+
+    info();
     q->insert(t, delay);
 }
 
+void SGEventMgr::info() {
+    SG_LOG(SG_GENERAL, SG_ALERT, "Total Entries in queues" << std::endl << 
+	"\t simtime queue:" << _simQueue.getQueueSize() << std::endl <<
+	"\t realtime queue:" << _rtQueue.getQueueSize() << std::endl
+	);
+}
+
+
 SGTimer::~SGTimer()
 {
     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;
+	}
 private:
     // 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);
 private:
+
+    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

FlightGear

Base Package