1 // TimeManager.cxx -- simulation-wide time management
3 // Written by James Turner, started July 2010.
5 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "TimeManager.hxx"
28 # define WIN32_LEAN_AND_MEAN
29 # include <windows.h> // for Sleep()
31 # include <unistd.h> // for usleep()
34 #include <simgear/timing/sg_time.hxx>
35 #include <simgear/structure/event_mgr.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/timing/lowleveltime.h>
39 #include <Main/fg_props.hxx>
40 #include <Main/globals.hxx>
41 #include <Time/sunsolver.hxx>
43 TimeManager::TimeManager() :
50 void TimeManager::init()
53 // time manager has to be initialised early, so needs to be defensive
54 // about multiple initialisation
61 _adjustWarpOnUnfreeze = false;
63 _maxDtPerFrame = fgGetNode("/sim/max-simtime-per-frame", true);
64 _clockFreeze = fgGetNode("/sim/freeze/clock", true);
65 _timeOverride = fgGetNode("/sim/time/cur-time-override", true);
66 _warp = fgGetNode("/sim/time/warp", true);
67 _warp->addChangeListener(this);
69 _warpDelta = fgGetNode("/sim/time/warp-delta", true);
71 _longitudeDeg = fgGetNode("/position/longitude-deg", true);
72 _latitudeDeg = fgGetNode("/position/latitude-deg", true);
74 SGPath zone(globals->get_fg_root());
75 zone.append("Timezone");
76 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
77 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
79 _impl = new SGTime(lon, lat, zone.str(), _timeOverride->getLongValue());
81 _warpDelta->setIntValue(0);
83 globals->get_event_mgr()->addTask("updateLocalTime", this,
84 &TimeManager::updateLocalTime, 30*60 );
87 _impl->update(lon, lat, _timeOverride->getLongValue(),
88 _warp->getIntValue());
89 globals->set_time_params(_impl);
91 // frame-rate / worst-case latency / update-rate counters
92 _frameRate = fgGetNode("/sim/frame-rate", true);
93 _frameLatency = fgGetNode("/sim/frame-latency-max-ms", true);
95 _frameLatencyMax = 0.0;
99 void TimeManager::postinit()
104 void TimeManager::reinit()
111 void TimeManager::shutdown()
113 _warp->removeChangeListener(this);
115 globals->set_time_params(NULL);
119 globals->get_event_mgr()->removeTask("updateLocalTime");
122 void TimeManager::valueChanged(SGPropertyNode* aProp)
124 if (aProp == _warp) {
125 if (_clockFreeze->getBoolValue()) {
126 // if the warp is changed manually while frozen, don't modify it when
127 // un-freezing - the user wants to unfreeze with exactly the warp
129 _adjustWarpOnUnfreeze = false;
132 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
133 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
134 _impl->update(lon, lat,
135 _timeOverride->getLongValue(),
136 _warp->getIntValue());
140 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
142 // Update the elapsed time.
145 _firstUpdate = false;
146 _lastClockFreeze = _clockFreeze->getBoolValue();
149 bool scenery_loaded = fgGetBool("sim/sceneryloaded");
150 bool wait_for_scenery = !(scenery_loaded || fgGetBool("sim/sceneryloaded-override"));
152 if (!wait_for_scenery) {
153 throttleUpdateRate();
157 // suppress framerate while initial scenery isn't loaded yet (splash screen still active)
162 SGTimeStamp currentStamp;
163 currentStamp.stamp();
164 double dt = (currentStamp - _lastStamp).toSecs();
165 if (dt > _frameLatencyMax)
166 _frameLatencyMax = dt;
168 // Limit the time we need to spend in simulation loops
169 // That means, if the /sim/max-simtime-per-frame value is strictly positive
170 // you can limit the maximum amount of time you will do simulations for
171 // one frame to display. The cpu time spent in simulations code is roughly
172 // at least O(real_delta_time_sec). If this is (due to running debug
173 // builds or valgrind or something different blowing up execution times)
174 // larger than the real time you will no longer get any response
175 // from flightgear. This limits that effect. Just set to property from
176 // your .fgfsrc or commandline ...
177 double dtMax = _maxDtPerFrame->getDoubleValue();
178 if (0 < dtMax && dtMax < dt) {
182 int model_hz = fgGetInt("/sim/model-hz");
184 SGSubsystemGroup* fdmGroup =
185 globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
186 fdmGroup->set_fixed_update_time(1.0 / model_hz);
188 // round the real time down to a multiple of 1/model-hz.
189 // this way all systems are updated the _same_ amount of dt.
191 int multiLoop = long(floor(dt * model_hz));
192 multiLoop = SGMisc<long>::max(0, multiLoop);
193 _dtRemainder = dt - double(multiLoop)/double(model_hz);
194 dt = double(multiLoop)/double(model_hz);
197 if (_clockFreeze->getBoolValue() || wait_for_scenery) {
203 _lastStamp = currentStamp;
204 globals->inc_sim_time_sec(simDt);
206 // These are useful, especially for Nasal scripts.
207 fgSetDouble("/sim/time/delta-realtime-sec", realDt);
208 fgSetDouble("/sim/time/delta-sec", simDt);
211 void TimeManager::update(double dt)
213 bool freeze = _clockFreeze->getBoolValue();
214 time_t now = time(NULL);
217 // clock freeze requested
218 if (_timeOverride->getLongValue() == 0) {
219 _timeOverride->setLongValue(now);
220 _adjustWarpOnUnfreeze = true;
223 // no clock freeze requested
224 if (_lastClockFreeze) {
225 if (_adjustWarpOnUnfreeze) {
226 // clock just unfroze, let's set warp as the difference
227 // between frozen time and current time so we don't get a
228 // time jump (and corresponding sky object and lighting
230 int adjust = _timeOverride->getLongValue() - now;
231 SG_LOG(SG_GENERAL, SG_INFO, "adjusting on un-freeze:" << adjust);
232 _warp->setIntValue(_warp->getIntValue() + adjust);
234 _timeOverride->setLongValue(0);
237 int warpDelta = _warpDelta->getIntValue();
238 if (warpDelta != 0) {
239 _warp->setIntValue(_warp->getIntValue() + warpDelta);
243 _lastClockFreeze = freeze;
244 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
245 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
246 _impl->update(lon, lat,
247 _timeOverride->getLongValue(),
248 _warp->getIntValue());
253 void TimeManager::computeFrameRate()
255 // Calculate frame rate average
256 if ((_impl->get_cur_time() != _lastFrameTime)) {
257 _frameRate->setIntValue(_frameCount);
258 _frameLatency->setDoubleValue(_frameLatencyMax*1000);
260 _frameLatencyMax = 0.0;
263 _lastFrameTime = _impl->get_cur_time();
267 void TimeManager::throttleUpdateRate()
269 double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
270 SGTimeStamp currentStamp;
272 // common case, no throttle requested
273 if (throttle_hz <= 0.0) {
277 double frame_us = 1000000.0 / throttle_hz;
278 #define FG_SLEEP_BASED_TIMING 1
279 #if defined(FG_SLEEP_BASED_TIMING)
280 // sleep based timing loop.
282 // Calling sleep, even usleep() on linux is less accurate than
283 // we like, but it does free up the cpu for other tasks during
284 // the sleep so it is desirable. Because of the way sleep()
285 // is implemented in consumer operating systems like windows
286 // and linux, you almost always sleep a little longer than the
289 // To combat the problem of sleeping too long, we calculate the
290 // desired wait time and shorten it by 2000us (2ms) to avoid
291 // [hopefully] over-sleep'ing. The 2ms value was arrived at
292 // via experimentation. We follow this up at the end with a
293 // simple busy-wait loop to get the final pause timing exactly
296 // Assuming we don't oversleep by more than 2000us, this
297 // should be a reasonable compromise between sleep based
298 // waiting, and busy waiting.
300 // sleep() will always overshoot by a bit so undersleep by
301 // 2000us in the hopes of never oversleeping.
303 if ( frame_us < 0.0 ) {
307 currentStamp.stamp();
309 double elapsed_us = (currentStamp - _lastStamp).toUSecs();
310 if ( elapsed_us < frame_us ) {
311 double requested_us = frame_us - elapsed_us;
314 Sleep ((int)(requested_us / 1000.0)) ;
316 usleep(requested_us) ;
321 // busy wait timing loop.
323 // This yields the most accurate timing. If the previous
324 // ulMilliSecondSleep() call is omitted this will peg the cpu
325 // (which is just fine if FG is the only app you care about.)
326 currentStamp.stamp();
327 SGTimeStamp next_time_stamp = _lastStamp;
328 next_time_stamp += SGTimeStamp::fromSec(1e-6*frame_us);
329 while ( currentStamp < next_time_stamp ) {
330 currentStamp.stamp();
334 // periodic time updater wrapper
335 void TimeManager::updateLocalTime()
337 SGPath zone(globals->get_fg_root());
338 zone.append("Timezone");
340 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
341 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
343 SG_LOG(SG_GENERAL, SG_INFO, "updateLocal(" << lon << ", " << lat << ", " << zone.str() << ")");
344 _impl->updateLocal(lon, lat, zone.str());
347 void TimeManager::initTimeOffset()
349 // Handle potential user specified time offsets
350 int orig_warp = _warp->getIntValue();
351 time_t cur_time = _impl->get_cur_time();
352 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
353 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
354 time_t aircraftLocalTime =
355 sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
357 // Okay, we now have several possible scenarios
358 int offset = fgGetInt("/sim/startup/time-offset");
359 string offset_type = fgGetString("/sim/startup/time-offset-type");
360 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
361 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
364 if ( offset_type == "real" ) {
366 } else if ( offset_type == "dawn" ) {
367 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, true );
368 } else if ( offset_type == "morning" ) {
369 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 75.0, true );
370 } else if ( offset_type == "noon" ) {
371 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 0.0, true );
372 } else if ( offset_type == "afternoon" ) {
373 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 60.0, false );
374 } else if ( offset_type == "dusk" ) {
375 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, false );
376 } else if ( offset_type == "evening" ) {
377 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 100.0, false );
378 } else if ( offset_type == "midnight" ) {
379 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 180.0, false );
380 } else if ( offset_type == "system-offset" ) {
383 } else if ( offset_type == "gmt-offset" ) {
384 warp = offset - (currGMT - systemLocalTime);
386 } else if ( offset_type == "latitude-offset" ) {
387 warp = offset - (aircraftLocalTime - systemLocalTime);
389 } else if ( offset_type == "system" ) {
390 warp = offset - (systemLocalTime - currGMT) - cur_time;
391 } else if ( offset_type == "gmt" ) {
392 warp = offset - cur_time;
393 } else if ( offset_type == "latitude" ) {
394 warp = offset - (aircraftLocalTime - currGMT)- cur_time;
396 SG_LOG( SG_GENERAL, SG_ALERT,
397 "TimeManager::initTimeOffset: unsupported offset: " << offset_type );
401 _warp->setIntValue( orig_warp + warp );
403 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
404 << _warp->getIntValue() );