Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add time sync logs #205

Merged
merged 9 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public abstract class AbstractSumoAmbassador extends AbstractFederateAmbassador
*/
protected boolean receivedSimulationStep = false;

/**
* First time when the sumo ambassador is called to progress to the next simulation step
*
*/
protected boolean firstAttemptToAdvanceToNextStep = true;

/**
* CARLA federate is enabled
*/
Expand Down Expand Up @@ -1193,6 +1199,16 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
return;
}

// A script to validate time synchronization of tools in CDASim currently relies on the following
// log line. TODO: This line is meant to be removed in the future upon completion of this work:
// https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
if (log.isDebugEnabled() && (!receivedSimulationStep && firstAttemptToAdvanceToNextStep))
{
long millis = System.currentTimeMillis();
log.info("Simulation Time: {} here current system time is: {} and nextTimeStep: {} and ambasador id: {}", (int) (time/1e6), millis, nextTimeStep, getId());
firstAttemptToAdvanceToNextStep = false;
}

if (time > lastAdvanceTime) {
// actually add vehicles in sumo, before we reach the next advance time
flushNotYetAddedVehicles(lastAdvanceTime);
Expand Down Expand Up @@ -1229,6 +1245,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
rti.triggerInteraction(simulationStepResult.getTrafficDetectorUpdates());
this.rti.triggerInteraction(simulationStepResult.getTrafficLightUpdates());
receivedSimulationStep = false;
firstAttemptToAdvanceToNextStep = true;
}

// System.out.println("Sumo request time advance at time: " + nextTimeStep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package org.eclipse.mosaic.rti.time;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.mosaic.rti.MosaicComponentParameters;
import org.eclipse.mosaic.rti.api.ComponentProvider;
import org.eclipse.mosaic.rti.api.FederateAmbassador;
Expand All @@ -31,6 +34,26 @@ public class SequentialTimeManagement extends AbstractTimeManagement {

private final int realtimeBrake;

// Debugging & Logging
HashMap<String, Long> loggingMap = new HashMap<>();

/**
* Prints log for time synchronization monitor script. Only meant to be printed for debugging purposes.
* Please see https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
*
* @param event FederateEvent requested by one of the ambassadaor
* @param startTime current system time when the event request was received
*/

private void printTimeSyncDebugLogs(FederateEvent event, long startTime){
if (!loggingMap.containsKey(event.getFederateId()) ||
(loggingMap.containsKey(event.getFederateId()) && loggingMap.get(event.getFederateId()) != event.getRequestedTime()))
{
loggingMap.put(event.getFederateId(), event.getRequestedTime());
this.logger.debug("Simulation Time: {} where current system time is: {} and requested from id: {}", (int) (event.getRequestedTime()/1e6), startTime, event.getFederateId());
}
}

/**
* Creates a new instance of the sequential time management.
*
Expand Down Expand Up @@ -87,6 +110,15 @@ public void runSimulation() throws InternalFederateException, IllegalValueExcept
if (ambassador != null) {
federation.getMonitor().onBeginActivity(event);
long startTime = System.currentTimeMillis();

// A script to validate time synchronization of tools in CDASim currently relies on the following
// log line. TODO: This line is meant to be removed in the future upon completion of this work:
// https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
if (this.logger.isDebugEnabled())
{
printTimeSyncDebugLogs(event, startTime);
}

ambassador.advanceTime(event.getRequestedTime());
federation.getMonitor().onEndActivity(event, System.currentTimeMillis() - startTime);

Expand Down
Loading