1 #include <simgrid/Exception.hpp>
2 #include <simgrid/plugins/operation.hpp>
3 #include <simgrid/s4u/Comm.hpp>
4 #include <simgrid/s4u/Exec.hpp>
5 #include <simgrid/s4u/Io.hpp>
6 #include <simgrid/simix.hpp>
8 #include "src/simgrid/module.hpp"
10 SIMGRID_REGISTER_PLUGIN(operation, "Battery management", nullptr)
11 /** @defgroup plugin_operation plugin_operation Plugin Operation
15 This is the operation plugin, enabling management of Operations.
16 To activate this plugin, first call :cpp:func:`Operation::init`.
18 Operations are designed to represent workflows, i.e, graphs of Operations.
19 Operations can only be instancied using either
20 :cpp:func:`simgrid::plugins::ExecOp::init` or :cpp:func:`simgrid::plugins::CommOp::init`
21 An ExecOp is an Execution Operation. Its underlying Activity is an :ref:`Exec <API_s4u_Exec>`.
22 A CommOp is a Communication Operation. Its underlying Activity is a :ref:`Comm <API_s4u_Comm>`.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Operation, kernel, "Logging specific to the operation plugin");
28 namespace simgrid::plugins {
30 xbt::signal<void(Operation*)> Operation::on_start;
31 xbt::signal<void(Operation*)> Operation::on_end;
33 Operation::Operation(const std::string& name) : name_(name) {}
36 * @param predecessor The Operation to add.
37 * @brief Add a predecessor to this Operation.
39 void Operation::add_predecessor(Operation* predecessor)
41 if (predecessors_.find(predecessor) == predecessors_.end())
42 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_[predecessor] = 0; });
46 * @param predecessor The Operation to remove.
47 * @brief Remove a predecessor from this Operation.
49 void Operation::remove_predecessor(Operation* predecessor)
51 simgrid::kernel::actor::simcall_answered([this, predecessor] { predecessors_.erase(predecessor); });
55 * @brief Return True if the Operation can start a new Activity.
56 * @note The Operation is ready if not already doing something and there is at least one execution waiting in queue.
58 bool Operation::ready_to_run() const
60 return not working_ && queued_execs_ > 0;
64 * @param source The sender.
65 * @brief Receive a token from another Operation.
66 * @note Check upon reception if the Operation has received a token from each of its predecessors,
67 * and in this case consumes those tokens and enqueue an execution.
69 void Operation::receive(Operation* source)
71 XBT_DEBUG("Operation %s received a token from %s", name_.c_str(), source->name_.c_str());
72 auto it = predecessors_.find(source);
73 simgrid::kernel::actor::simcall_answered([this, it] {
75 bool enough_tokens = true;
76 for (auto const& [key, val] : predecessors_)
78 enough_tokens = false;
82 for (auto& [key, val] : predecessors_)
90 * @brief Operation routine when finishing an execution.
91 * @note Set its working status as false.
92 * Add 1 to its count of finished executions.
93 * Call the on_this_end func.
94 * Fire on_end callback.
95 * Send a token to each of its successors.
96 * Start a new execution if possible.
98 void Operation::complete()
100 simgrid::kernel::actor::simcall_answered([this] {
104 for (auto end_func : end_func_handlers_)
106 Operation::on_end(this);
107 for (auto const& op : successors_)
113 /** @ingroup plugin_operation
114 * @brief Init the Operation plugin.
115 * @note Add a completion callback to all Activities to call Operation::complete().
117 void Operation::init()
119 if (Operation::inited_)
121 Operation::inited_ = true;
122 ExtendedAttributeActivity::EXTENSION_ID = simgrid::s4u::Activity::extension_create<ExtendedAttributeActivity>();
123 simgrid::s4u::Exec::on_completion_cb([](simgrid::s4u::Exec const& exec) {
124 exec.extension<ExtendedAttributeActivity>()->operation_->complete();
126 simgrid::s4u::Comm::on_completion_cb([](simgrid::s4u::Comm const& comm) {
127 comm.extension<ExtendedAttributeActivity>()->operation_->complete();
129 simgrid::s4u::Io::on_completion_cb([](simgrid::s4u::Io const& io) {
130 io.extension<ExtendedAttributeActivity>()->operation_->complete();
134 /** @ingroup plugin_operation
135 * @param n The number of executions to enqueue.
136 * @brief Enqueue executions.
137 * @note Immediatly starts an execution if possible.
139 void Operation::enqueue_execs(int n)
141 simgrid::kernel::actor::simcall_answered([this, n] {
148 /** @ingroup plugin_operation
149 * @param amount The amount to set.
150 * @brief Set the amout of work to do.
151 * @note Amount in flop for ExecOp and in bytes for CommOp.
153 void Operation::set_amount(double amount)
155 simgrid::kernel::actor::simcall_answered([this, amount] { amount_ = amount; });
158 /** @ingroup plugin_operation
159 * @param successor The Operation to add.
160 * @brief Add a successor to this Operation.
161 * @note It also adds this as a predecessor of successor.
163 void Operation::add_successor(OperationPtr successor)
165 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.insert(successor.get()); });
166 successor->add_predecessor(this);
169 /** @ingroup plugin_operation
170 * @param successor The Operation to remove.
171 * @brief Remove a successor from this Operation.
172 * @note It also remove this from the predecessors of successor.
174 void Operation::remove_successor(OperationPtr successor)
176 simgrid::kernel::actor::simcall_answered([this, successor] { successors_.erase(successor.get()); });
177 successor->remove_predecessor(this);
180 void Operation::remove_all_successors()
182 simgrid::kernel::actor::simcall_answered([this] {
183 while (not successors_.empty()) {
184 auto* successor = *(successors_.begin());
185 successor->predecessors_.erase(this);
186 successors_.erase(successor);
191 /** @ingroup plugin_operation
192 * @param func The function to set.
193 * @brief Set a function to be called before each execution.
194 * @note The function is called before the underlying Activity starts.
196 void Operation::on_this_start(const std::function<void(Operation*)>& func)
198 simgrid::kernel::actor::simcall_answered([this, &func] { start_func_handlers_.push_back(func); });
201 /** @ingroup plugin_operation
202 * @param func The function to set.
203 * @brief Set a function to be called after each execution.
204 * @note The function is called after the underlying Activity ends, but before sending tokens to successors.
206 void Operation::on_this_end(const std::function<void(Operation*)>& func)
208 simgrid::kernel::actor::simcall_answered([this, &func] { end_func_handlers_.push_back(func); });
211 /** @ingroup plugin_operation
212 * @brief Return the number of completed executions.
214 int Operation::get_count() const
220 * @brief Default constructor.
222 ExecOp::ExecOp(const std::string& name) : Operation(name) {}
224 /** @ingroup plugin_operation
225 * @brief Smart Constructor.
227 ExecOpPtr ExecOp::init(const std::string& name)
229 return ExecOpPtr(new ExecOp(name));
232 /** @ingroup plugin_operation
233 * @brief Smart Constructor.
235 ExecOpPtr ExecOp::init(const std::string& name, double flops, s4u::Host* host)
237 return init(name)->set_flops(flops)->set_host(host);
241 * @brief Do one execution of the Operation.
242 * @note Call the on_this_start() func. Set its working status as true.
243 * Init and start the underlying Activity.
245 void ExecOp::execute()
247 for (auto start_func : start_func_handlers_)
249 Operation::on_start(this);
250 kernel::actor::simcall_answered([this] {
252 queued_execs_ = std::max(queued_execs_ - 1, 0);
254 s4u::ExecPtr exec = s4u::Exec::init();
255 exec->set_name(name_);
256 exec->set_flops_amount(amount_);
257 exec->set_host(host_);
259 exec->extension_set(new ExtendedAttributeActivity());
260 exec->extension<ExtendedAttributeActivity>()->operation_ = this;
261 kernel::actor::simcall_answered([this, exec] { current_activity_ = exec; });
264 /** @ingroup plugin_operation
265 * @param host The host to set.
266 * @brief Set a new host.
268 ExecOpPtr ExecOp::set_host(s4u::Host* host)
270 kernel::actor::simcall_answered([this, host] { host_ = host; });
274 /** @ingroup plugin_operation
275 * @param flops The amount of flops to set.
277 ExecOpPtr ExecOp::set_flops(double flops)
279 kernel::actor::simcall_answered([this, flops] { amount_ = flops; });
284 * @brief Default constructor.
286 CommOp::CommOp(const std::string& name) : Operation(name) {}
288 /** @ingroup plugin_operation
289 * @brief Smart constructor.
291 CommOpPtr CommOp::init(const std::string& name)
293 return CommOpPtr(new CommOp(name));
296 /** @ingroup plugin_operation
297 * @brief Smart constructor.
299 CommOpPtr CommOp::init(const std::string& name, double bytes, s4u::Host* source,
300 s4u::Host* destination)
302 return init(name)->set_bytes(bytes)->set_source(source)->set_destination(destination);
306 * @brief Do one execution of the Operation.
307 * @note Call the on_this_start() func. Set its working status as true.
308 * Init and start the underlying Activity.
310 void CommOp::execute()
312 for (auto start_func : start_func_handlers_)
314 Operation::on_start(this);
315 kernel::actor::simcall_answered([this] {
317 queued_execs_ = std::max(queued_execs_ - 1, 0);
319 s4u::CommPtr comm = s4u::Comm::sendto_init(source_, destination_);
320 comm->set_name(name_);
321 comm->set_payload_size(amount_);
323 comm->extension_set(new ExtendedAttributeActivity());
324 comm->extension<ExtendedAttributeActivity>()->operation_ = this;
325 kernel::actor::simcall_answered([this, comm] { current_activity_ = comm; });
328 /** @ingroup plugin_operation
329 * @param source The host to set.
330 * @brief Set a new source host.
332 CommOpPtr CommOp::set_source(s4u::Host* source)
334 kernel::actor::simcall_answered([this, source] { source_ = source; });
338 /** @ingroup plugin_operation
339 * @param destination The host to set.
340 * @brief Set a new destination host.
342 CommOpPtr CommOp::set_destination(s4u::Host* destination)
344 kernel::actor::simcall_answered([this, destination] { destination_ = destination; });
348 /** @ingroup plugin_operation
349 * @param bytes The amount of bytes to set.
351 CommOpPtr CommOp::set_bytes(double bytes)
353 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
358 * @brief Default constructor.
360 IoOp::IoOp(const std::string& name) : Operation(name) {}
362 /** @ingroup plugin_operation
363 * @brief Smart Constructor.
365 IoOpPtr IoOp::init(const std::string& name)
367 return IoOpPtr(new IoOp(name));
370 /** @ingroup plugin_operation
371 * @brief Smart Constructor.
373 IoOpPtr IoOp::init(const std::string& name, double bytes, s4u::Disk* disk, s4u::Io::OpType type)
375 return init(name)->set_bytes(bytes)->set_disk(disk)->set_op_type(type);
378 /** @ingroup plugin_operation
379 * @param disk The disk to set.
380 * @brief Set a new disk.
382 IoOpPtr IoOp::set_disk(s4u::Disk* disk)
384 kernel::actor::simcall_answered([this, disk] { disk_ = disk; });
388 /** @ingroup plugin_operation
389 * @param bytes The amount of bytes to set.
391 IoOpPtr IoOp::set_bytes(double bytes)
393 kernel::actor::simcall_answered([this, bytes] { amount_ = bytes; });
397 /** @ingroup plugin_operation */
398 IoOpPtr IoOp::set_op_type(s4u::Io::OpType type)
400 kernel::actor::simcall_answered([this, type] { type_ = type; });
406 for (auto start_func : start_func_handlers_)
408 Operation::on_start(this);
409 kernel::actor::simcall_answered([this] {
411 queued_execs_ = std::max(queued_execs_ - 1, 0);
413 s4u::IoPtr io = s4u::Io::init();
415 io->set_size(amount_);
417 io->set_op_type(type_);
419 io->extension_set(new ExtendedAttributeActivity());
420 io->extension<ExtendedAttributeActivity>()->operation_ = this;
421 kernel::actor::simcall_answered([this, io] { current_activity_ = io; });
425 } // namespace simgrid::plugins
427 simgrid::xbt::Extension<simgrid::s4u::Activity, simgrid::plugins::ExtendedAttributeActivity>
428 simgrid::plugins::ExtendedAttributeActivity::EXTENSION_ID;
429 bool simgrid::plugins::Operation::inited_ = false;