Skip to content

Commit 8b6c6d4

Browse files
Change TimeAdvanceMode::ByMinimalDuration to trigger SimTasks with timestamps of all other sync. participants; Add Integration Tests; Remove automatic creation of TimeSynService in SimTestHarness
Signed-off-by: Konrad Breitsprecher <Konrad.Breitsprecher@vector.com>
1 parent 7a4364e commit 8b6c6d4

12 files changed

Lines changed: 484 additions & 51 deletions

File tree

Demos/api/Orchestration/DynSimStep.cpp

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <iostream>
66
#include <random>
7+
#include <thread>
78

89
#include "silkit/SilKit.hpp"
910

@@ -17,13 +18,74 @@ std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp)
1718

1819
int main(int argc, char** argv)
1920
{
20-
if (argc != 2)
21+
if (argc != 5 && argc != 6)
2122
{
22-
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl;
23+
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] <<
24+
" <ParticipantName> "
25+
"<StepSize> "
26+
"[-A (Autonomous) | -C (Coordinated)] "
27+
"[-M (ByMinimalDuration) | -D (ByOwnDuration)] "
28+
"[-R (Optional; Randomize StepSize (1ms to 10ms) with 10% probability every step)]" << std::endl;
2329
return -1;
2430
}
31+
// Arg 1: Participant Name
2532
std::string participantName(argv[1]);
2633

34+
// Arg 2: Step Size
35+
auto stepSize = std::chrono::milliseconds(std::stoi(argv[2]));
36+
std::cout << "Starting with stepSize=" << stepSize << std::endl;
37+
38+
// Arg 3: Operation Mode
39+
auto operationMode = SilKit::Services::Orchestration::OperationMode::Coordinated;
40+
if (std::string(argv[3]) == "-A")
41+
{
42+
std::cout << "Using OperationMode::Autonomous" << std::endl;
43+
operationMode = SilKit::Services::Orchestration::OperationMode::Autonomous;
44+
}
45+
else if (std::string(argv[3]) == "-C")
46+
{
47+
std::cout << "Using OperationMode::Coordinated" << std::endl;
48+
}
49+
else
50+
{
51+
std::cerr << "Unknown third argument '" << argv[3] << "'. Did you mean '-A' for autonomous mode or '-C' for coordinated mode?" << std::endl;
52+
return -1;
53+
}
54+
55+
// Arg 4: Time Advance Mode
56+
auto timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration;
57+
if (std::string(argv[4]) == "-M")
58+
{
59+
std::cout << "Using TimeAdvanceMode::ByMinimalDuration" << std::endl;
60+
}
61+
else if (std::string(argv[4]) == "-D")
62+
{
63+
timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByOwnDuration;
64+
std::cout << "Using TimeAdvanceMode::ByOwnDuration" << std::endl;
65+
}
66+
else
67+
{
68+
std::cerr << "Unknown argument '" << argv[4]
69+
<< "'. Did you mean '-M' for TimeAdvanceMode::ByMinimalDuration or '-D' for TimeAdvanceMode::ByOwnDuration?" << std::endl;
70+
return -1;
71+
}
72+
73+
// Arg 5: Optional Randomize Step Size
74+
bool randomizeStepSize = false;
75+
if (argc == 6)
76+
{
77+
if (std::string(argv[5]) == "-R")
78+
{
79+
randomizeStepSize = true;
80+
std::cout << "Randomizing step size every 10 steps." << std::endl << std::endl;
81+
}
82+
else
83+
{
84+
std::cerr << "Unknown argument '" << argv[5] << "'. Did you mean '-R' to randomize the step size every 10 steps?" << std::endl;
85+
return -1;
86+
}
87+
}
88+
2789
try
2890
{
2991
// Setup participant, lifecycle, time synchronization and logging.
@@ -34,23 +96,20 @@ int main(int argc, char** argv)
3496
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri);
3597
auto logger = participant->GetLogger();
3698

37-
auto* lifecycleService =
38-
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated});
99+
auto* lifecycleService = participant->CreateLifecycleService({operationMode});
39100

40-
auto* timeSyncService = lifecycleService->CreateTimeSyncService(SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration);
101+
auto* timeSyncService = lifecycleService->CreateTimeSyncService(timeAdvanceMode);
41102

42-
const auto stepSize = 10ms;
43-
static int stepCounter = 0;
44103
std::random_device rd;
45104
std::mt19937 rng(rd());
46105
auto bounded_rand = [&rng](unsigned range) {
47106
std::uniform_int_distribution<unsigned> dist(1, range);
48107
return dist(rng);
49108
};
50109

51-
52110
timeSyncService->SetSimulationStepHandler(
53-
[logger, timeSyncService, participantName, bounded_rand](std::chrono::nanoseconds now,
111+
[randomizeStepSize, logger, timeSyncService, participantName, bounded_rand](
112+
std::chrono::nanoseconds now,
54113
std::chrono::nanoseconds duration) {
55114
// The invocation of this handler marks the beginning of a simulation step.
56115
{
@@ -59,7 +118,7 @@ int main(int argc, char** argv)
59118
logger->Info(ss.str());
60119
}
61120

62-
if (bounded_rand(10) == 1)// && participantName == "P1")
121+
if (randomizeStepSize && bounded_rand(10) == 1)
63122
{
64123
auto rndStepDuration = bounded_rand(10);
65124
timeSyncService->SetStepDuration(std::chrono::milliseconds(rndStepDuration));
@@ -69,12 +128,7 @@ int main(int argc, char** argv)
69128
}
70129

71130
std::this_thread::sleep_for(500ms);
72-
// All messages sent here are guaranteed to arrive at other participants before their next simulation step is called.
73-
// So here, we can rely on having received all messages from the past (< now).
74-
// Note that this guarantee only holds for messages sent within a simulation step,
75-
// not for messages send outside of this handler (e.g. directly in a reception handler).
76131

77-
// Returning from the handler marks the end of a simulation step.
78132
}, stepSize);
79133

80134
auto finalStateFuture = lifecycleService->StartLifecycle();

Demos/api/Orchestration/SimStep.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
#include <iostream>
6+
#include <thread>
67

78
#include "silkit/SilKit.hpp"
89

@@ -38,7 +39,7 @@ int main(int argc, char** argv)
3839

3940
auto* timeSyncService = lifecycleService->CreateTimeSyncService();
4041

41-
const auto stepSize = 2ms;
42+
const auto stepSize = 5ms;
4243

4344
timeSyncService->SetSimulationStepHandler(
4445
[logger](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) {

SilKit/IntegrationTests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ add_silkit_test_to_executable(SilKitIntegrationTests
114114
SOURCES ITest_SimTask.cpp
115115
)
116116

117+
add_silkit_test_to_executable(SilKitIntegrationTests
118+
SOURCES ITest_DynStepSizes.cpp
119+
)
120+
121+
117122
add_silkit_test_to_executable(SilKitFunctionalTests
118123
SOURCES FTest_WallClockCoupling.cpp
119124
)

SilKit/IntegrationTests/Hourglass/Test_HourglassOrchestration.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ TEST_F(Test_HourglassOrchestration, SilKit_TimeSyncService_SetStepDuration)
447447
timeSyncService.SetStepDuration(stepDuration);
448448
}
449449

450-
451450
TEST_F(Test_HourglassOrchestration, SilKit_Experimental_TimeSyncService_AddOtherSimulationStepsCompletedHandler)
452451
{
453452
using testing::_;

SilKit/IntegrationTests/ITest_AsyncSimTask.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ auto MakeCompletionThread(SimParticipant* p, ParticipantData* d) -> std::thread
302302

303303
TEST(ITest_AsyncSimTask, test_async_simtask_other_simulation_steps_completed_handler)
304304
{
305-
SimTestHarness testHarness({"A", "B", "C"}, "silkit://localhost:0");
305+
SimTestHarness testHarness({"A", "B", "C", "D"}, "silkit://localhost:0");
306306

307307
const auto a = testHarness.GetParticipant("A");
308308
const auto b = testHarness.GetParticipant("B");
309309
const auto c = testHarness.GetParticipant("C");
310+
const auto d = testHarness.GetParticipant("D");
310311

311312
ParticipantData ad, bd, cd;
312313

@@ -316,6 +317,8 @@ TEST(ITest_AsyncSimTask, test_async_simtask_other_simulation_steps_completed_han
316317
b->GetOrCreateLifecycleService()->SetStopHandler([&bd] { bd.running = false; });
317318
c->GetOrCreateLifecycleService()->SetStopHandler([&cd] { cd.running = false; });
318319

320+
d->GetOrCreateTimeSyncService()->SetSimulationStepHandler([](auto, auto) {}, 1ms);
321+
319322
const auto aLifecycleService = a->GetOrCreateLifecycleService();
320323

321324
a->GetOrCreateTimeSyncService()->SetSimulationStepHandlerAsync([aLifecycleService, &ad](auto now, auto) {

0 commit comments

Comments
 (0)