Repository navigation
Expand file tree
/
Copy pathStagingEngine.cpp
More file actions
202 lines (171 loc) · 8.19 KB
/
Copy pathStagingEngine.cpp
File metadata and controls
202 lines (171 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright (c) 2022-2026. The SWAT Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <fsmod/FileSystem.hpp>
#include <fsmod/PathUtil.hpp>
#include <simgrid/s4u/Actor.hpp>
#include <simgrid/s4u/Engine.hpp>
#include <simgrid/s4u/MessageQueue.hpp>
#include "dtlmod/DTL.hpp"
#include "dtlmod/DTLException.hpp"
#include "dtlmod/StagingEngine.hpp"
#include "dtlmod/StagingMboxTransport.hpp"
#include "dtlmod/StagingMqTransport.hpp"
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dtlmod_staging_engine, dtlmod_engine, "DTL logging about staging Engines");
namespace dtlmod {
/// \cond EXCLUDE_FROM_DOCUMENTATION
void StagingEngine::create_transport(const Transport::Method& transport_method)
{
XBT_DEBUG("Create a new Staging Engine");
if (transport_method == Transport::Method::Mailbox) {
XBT_DEBUG("Creating mailbox '%s'", get_cname());
set_transport(std::make_shared<StagingMboxTransport>(this));
} else if (transport_method == Transport::Method::MQ) {
XBT_DEBUG("Creating Message Queue '%s'", get_cname());
set_transport(std::make_shared<StagingMqTransport>(this));
}
}
std::shared_ptr<StagingTransport> StagingEngine::get_staging_transport() const
{
auto transport = std::dynamic_pointer_cast<StagingTransport>(get_transport());
if (!transport) // LCOV_EXCL_LINE
throw TransportEngineMismatchException(XBT_THROW_POINT, "Transport is not a StagingTransport"); // LCOV_EXCL_LINE
return transport;
}
void StagingEngine::begin_pub_transaction()
{
if (!pub_transaction_in_progress_) {
pub_transaction_in_progress_ = true;
current_pub_transaction_id_++;
XBT_DEBUG("Publish Transaction %u started by %s", current_pub_transaction_id_, sg4::Actor::self()->get_cname());
// Start the first transaction by notifying subscribers that all publishers are here
if (current_pub_transaction_id_ == 1) {
XBT_DEBUG("Notify subscribers that they can create their rendez-vous points");
first_pub_transaction_started_->notify_all();
}
}
// Only one publisher has to do this
std::unique_lock lock(*get_publishers().get_mutex());
if (current_pub_transaction_id_ > 1) { // This is not the first transaction.
// Wait for the completion of the Publish activities from the previous transaction
XBT_DEBUG("[T %d] (%d) Wait for the completion of %u publish activities from the previous transaction",
current_pub_transaction_id_, current_sub_transaction_id_, get_pub_transaction().size());
get_pub_transaction().wait_all();
XBT_DEBUG("All on-flight publish activities are completed. Proceed with the current transaction.");
XBT_DEBUG("%u sub activities pending", get_sub_transaction().size());
get_pub_transaction().clear();
}
// Then we wait for all subscribers to be at the same transaction
while (get_subscribers().is_empty() || current_pub_transaction_id_ > current_sub_transaction_id_) {
XBT_DEBUG("Wait for subscribers");
sub_transaction_started_->wait(lock);
}
// Publisher has been notified by subscribers, it can proceed with the transaction");
}
void StagingEngine::end_pub_transaction()
{
// This is the end of the first transaction, create a barrier
if (auto pub_barrier = get_publishers().get_or_create_barrier())
XBT_DEBUG("Barrier created for %zu publishers", get_publishers().count());
// A new pub transaction has been completed, notify subscribers that they can starting getting variables
if (get_publishers().is_last_at_barrier() && (completed_pub_transaction_id_ < current_pub_transaction_id_)) {
completed_pub_transaction_id_++;
pub_transaction_completed_->notify_all();
}
// Wait for the put requests and actually put (asynchrously) comm/mess in Mbox/MQ
get_staging_transport()->get_requests_and_do_put(sg4::Actor::self());
XBT_DEBUG("Start publish activities for the transaction");
if (get_publishers().is_last_at_barrier()) // Mark this transaction as over
pub_transaction_in_progress_ = false;
}
void StagingEngine::pub_close()
{
auto self = sg4::Actor::self();
XBT_DEBUG("Publisher '%s' is closing the engine '%s'", self->get_cname(), get_cname());
if (!pub_closing_) {
// I'm the first to close
pub_closing_ = true;
XBT_DEBUG("[%s] Wait for the completion of %u publish activities from the previous transaction", get_cname(),
get_pub_transaction().size());
get_pub_transaction().wait_all();
get_pub_transaction().clear();
XBT_DEBUG("[%s] last publish transaction is over", get_cname());
current_pub_transaction_id_++;
}
get_publishers().remove(self);
if (get_publishers().is_last_at_barrier()) {
XBT_DEBUG("All publishers have called the Engine::close() function");
close_stream();
XBT_DEBUG("Engine '%s' is now closed for all publishers ", get_cname());
get_stream()->export_metadata_to_file();
}
}
void StagingEngine::begin_sub_transaction()
{
if (current_sub_transaction_id_ == 0) { // This is the first transaction
// Wait for at least one publisher to start a tran
std::unique_lock lock(*get_subscribers().get_mutex());
while (current_pub_transaction_id_ == 0)
first_pub_transaction_started_->wait(lock);
XBT_DEBUG("Publishers have started a transaction, create rendez-vous points");
// We now know the number of publishers, subscriber can create mailboxes/mqs with publishers
get_staging_transport()->create_rendez_vous_points();
}
if (!sub_transaction_in_progress_) {
current_sub_transaction_id_++;
sub_transaction_in_progress_ = true;
}
num_subscribers_starting_++;
XBT_DEBUG("Subscribe Transaction %u started by %s (%u/%lu)", current_sub_transaction_id_,
sg4::Actor::self()->get_cname(), num_subscribers_starting_.load(), get_subscribers().count());
// The last subscriber to start a transaction notifies the publishers
if (num_subscribers_starting_.load() == get_subscribers().count() &&
current_pub_transaction_id_ == current_sub_transaction_id_) {
XBT_DEBUG("Notify Publishers that they can start their transaction");
sub_transaction_started_->notify_all();
}
std::unique_lock lock(*get_subscribers().get_mutex());
while (completed_pub_transaction_id_ < current_sub_transaction_id_)
pub_transaction_completed_->wait(lock);
}
void StagingEngine::end_sub_transaction()
{
// This is the end of the first transaction, create a barrier
if (auto sub_barrier = get_subscribers().get_or_create_barrier())
XBT_DEBUG("Barrier created for %zu subscribers", get_subscribers().count());
if (get_subscribers().is_last_at_barrier()) {
XBT_DEBUG("Wait for the %d subscribe activities for the transaction", get_sub_transaction().size());
get_sub_transaction().wait_all();
XBT_DEBUG("All on-flight subscribe activities are completed. Proceed with the current transaction.");
get_sub_transaction().clear();
}
// Prevent subscribers to start a new transaction before this one is really over
if (get_subscribers().is_last_at_barrier())
// Mark this transaction as over
sub_transaction_in_progress_ = false;
// Decrease counter for next iteration
num_subscribers_starting_--;
XBT_DEBUG("Subscribe Transaction %u end by %s (%u/%lu)", current_sub_transaction_id_, sg4::Actor::self()->get_cname(),
num_subscribers_starting_.load(), get_subscribers().count());
}
void StagingEngine::sub_close()
{
auto self = sg4::Actor::self();
XBT_DEBUG("Subscriber '%s' is closing the engine", self->get_cname());
if (!sub_closing_) {
// I'm the first to close
sub_closing_ = true;
XBT_DEBUG("Wait for the %d subscribe activities for the transaction", get_sub_transaction().size());
get_sub_transaction().wait_all();
XBT_DEBUG("All on-flight subscribe activities are completed. Proceed with the current transaction.");
get_sub_transaction().clear();
}
get_subscribers().remove(self);
if (get_subscribers().is_last_at_barrier()) {
XBT_DEBUG("All subscribers have called the Engine::close() function");
close_stream();
XBT_DEBUG("Engine '%s' is now closed for all subscribers ", get_cname());
}
}
/// \endcond
} // namespace dtlmod