Skip to content

Commit fc7a6a5

Browse files
committed
fix startup for cyclic enclave programs
1 parent fe5e6c2 commit fc7a6a5

File tree

8 files changed

+37
-21
lines changed

8 files changed

+37
-21
lines changed

include/reactor-cpp/connection.hh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,11 @@ public:
197197
inline auto acquire_tag(const Tag& tag, std::unique_lock<std::mutex>& lock, std::condition_variable& cv,
198198
const std::function<bool(void)>& abort_waiting) -> bool override {
199199
// Since this is a delayed connection, we can go back in time and need to
200-
// acquire the latest tag upstream tag that can create an event at the given
200+
// acquire the latest upstream tag that can create an event at the given
201201
// tag. We also need to consider that given a delay d and a tag g=(t, n),
202202
// for any value of n, g + d = (t, 0). Hence, we need to quire a tag with
203203
// the highest possible microstep value.
204-
auto time_point = tag.time_point() - this->min_delay();
205-
auto upstream_tag = Tag::max_for_timepoint(time_point);
204+
auto upstream_tag = tag.subtract(this->min_delay());
206205
return EnclaveConnection<T>::acquire_tag(upstream_tag, lock, cv, abort_waiting);
207206
}
208207
};

include/reactor-cpp/environment.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private:
5555

5656
Scheduler scheduler_;
5757
Phase phase_{Phase::Construction};
58-
TimePoint start_time_{};
58+
Tag start_tag_{};
5959

6060
const Duration timeout_{};
6161

@@ -96,7 +96,7 @@ public:
9696
auto scheduler() noexcept -> Scheduler* { return &scheduler_; }
9797

9898
[[nodiscard]] auto logical_time() const noexcept -> const LogicalTime& { return scheduler_.logical_time(); }
99-
[[nodiscard]] auto start_time() const noexcept -> const TimePoint& { return start_time_; }
99+
[[nodiscard]] auto start_tag() const noexcept -> const Tag& { return start_tag_; }
100100
[[nodiscard]] auto timeout() const noexcept -> const Duration& { return timeout_; }
101101

102102
static auto physical_time() noexcept -> TimePoint { return get_physical_time(); }

include/reactor-cpp/logical_time.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public:
4141
[[nodiscard]]static auto max_for_timepoint(TimePoint time_point) noexcept -> Tag;
4242

4343
[[nodiscard]] auto delay(Duration offset = Duration::zero()) const noexcept -> Tag;
44+
[[nodiscard]] auto subtract(Duration offset = Duration::zero()) const noexcept -> Tag;
45+
[[nodiscard]] auto decrement() const noexcept -> Tag;
4446
};
4547

4648
// define all the comparison operators

lib/action.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ void Timer::startup() {
4141
return;
4242
}
4343

44-
Tag tag_zero = Tag::from_physical_time(environment()->start_time());
44+
const Tag& start_tag = environment()->start_tag();
4545
if (offset_ != Duration::zero()) {
46-
environment()->scheduler()->schedule_sync(this, tag_zero.delay(offset_));
46+
environment()->scheduler()->schedule_sync(this, start_tag.delay(offset_));
4747
} else {
48-
environment()->scheduler()->schedule_sync(this, tag_zero);
48+
environment()->scheduler()->schedule_sync(this, start_tag);
4949
}
5050
}
5151

lib/environment.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ void Environment::calculate_indexes() {
254254

255255
auto Environment::startup() -> std::thread {
256256
validate(this == top_environment_, "startup() may only be called on the top environment");
257-
auto start_time = get_physical_time();
258-
return startup(start_time);
257+
return startup(get_physical_time());
259258
}
260259

261260
auto Environment::startup(const TimePoint& start_time) -> std::thread {
@@ -264,7 +263,7 @@ auto Environment::startup(const TimePoint& start_time) -> std::thread {
264263
log_.debug() << "Starting the execution";
265264
phase_ = Phase::Startup;
266265

267-
start_time_ = start_time;
266+
this->start_tag_ = Tag::from_physical_time(start_time);
268267
// start up initialize all reactors
269268
for (auto* reactor : top_level_reactors_) {
270269
reactor->startup();
@@ -273,12 +272,12 @@ auto Environment::startup(const TimePoint& start_time) -> std::thread {
273272
// start processing events
274273
phase_ = Phase::Execution;
275274

276-
return std::thread([this]() {
275+
return std::thread([this, start_time]() {
277276
std::vector<std::thread> threads;
278277
threads.reserve(contained_environments_.size());
279278
// startup all contained environments recursively
280279
for (auto* env : contained_environments_) {
281-
threads.emplace_back(env->startup(start_time_));
280+
threads.emplace_back(env->startup(start_time));
282281
}
283282
// start the local scheduler and wait until it returns
284283
this->scheduler_.start();

lib/logical_time.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,26 @@ auto Tag::from_logical_time(const LogicalTime& logical_time) noexcept -> Tag {
2828
return {logical_time.time_point(), logical_time.micro_step()};
2929
}
3030

31-
auto Tag::max_for_timepoint(TimePoint time_point) noexcept -> Tag {
32-
return {time_point, std::numeric_limits<mstep_t>::max()};
31+
auto Tag::delay(Duration offset) const noexcept -> Tag {
32+
if (offset == Duration::zero()) {
33+
validate(this->micro_step_ != std::numeric_limits<mstep_t>::max(), "Microstep overflow detected!");
34+
return {this->time_point_, this->micro_step_ + 1};
35+
}
36+
return {this->time_point_ + offset, 0};
3337
}
3438

35-
auto Tag::delay(Duration offset) const noexcept -> Tag {
39+
auto Tag::subtract(Duration offset) const noexcept -> Tag {
3640
if (offset == Duration::zero()) {
37-
return Tag{this->time_point_, this->micro_step_ + 1};
41+
return decrement();
42+
}
43+
return {time_point_ - offset, std::numeric_limits<mstep_t>::max()};
44+
}
45+
46+
auto Tag::decrement() const noexcept -> Tag {
47+
if (micro_step_ == 0) {
48+
return {time_point_ - Duration{1}, std::numeric_limits<mstep_t>::max()};
3849
}
39-
return Tag{this->time_point_ + offset, 0};
50+
return {time_point_, micro_step_ - 1};
4051
}
4152

4253
void LogicalTime::advance_to(const Tag& tag) {
@@ -46,7 +57,7 @@ void LogicalTime::advance_to(const Tag& tag) {
4657
}
4758

4859
void LogicalTime::advance_to(const LogicalTime& time) {
49-
reactor_assert(*this <= Tag::from_logical_time(time));
60+
reactor_assert(*this < Tag::from_logical_time(time));
5061
time_point_ = time.time_point();
5162
micro_step_ = time.micro_step();
5263
}

lib/reactor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ auto Reactor::get_tag() const noexcept -> Tag {
169169
}
170170

171171
auto Reactor::get_elapsed_logical_time() const noexcept -> Duration {
172-
return get_logical_time() - environment()->start_time();
172+
return get_logical_time() - environment()->start_tag().time_point();
173173
}
174174

175175
auto Reactor::get_elapsed_physical_time() const noexcept -> Duration {
176-
return get_physical_time() - environment()->start_time();
176+
return get_physical_time() - environment()->start_tag().time_point();
177177
}
178178

179179
} // namespace reactor

lib/scheduler.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ auto Scheduler::schedule_ready_reactions() -> bool {
202202
void Scheduler::start() {
203203
log_.debug() << "Starting the scheduler...";
204204

205+
// Initialize our logical time to the value right before the start tag. This
206+
// is important for usage with enclaves/federates, to indicate, that no events
207+
// before the start tag ca be generated.
208+
logical_time_.advance_to(environment_->start_tag().decrement());
209+
205210
auto num_workers = environment_->num_workers();
206211
// initialize the reaction queue, set ports vector, and triggered reactions
207212
// vector

0 commit comments

Comments
 (0)