Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
994ec37ec1fc694d8e3d33925fad0a8855288593
[simgrid.git] / examples / s4u / exec-monitor / s4u-exec-monitor.cpp
1 /* Copyright (c) 2017-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u.hpp"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
9
10 static void monitor(simgrid::s4u::ExecPtr activity)
11 {
12   while (not activity->test()) {
13     XBT_INFO("activity remaining duration: %g (%.0f%%)", activity->get_remaining(),
14              100 * activity->getRemainingRatio());
15     simgrid::s4u::this_actor::sleep_for(5);
16   }
17   XBT_INFO("My task is over.");
18 }
19
20 static void executor()
21 {
22   XBT_INFO("Create one monitored task, and wait for it");
23   simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_async(1e9);
24   simgrid::s4u::Actor::create("monitor 1", simgrid::s4u::Host::by_name("Tremblay"), monitor, activity);
25   activity->wait(); // This blocks until the activity is over
26   XBT_INFO("The monitored task is over. Let's start 3 of them now.");
27   simgrid::s4u::Actor::create("monitor 2", simgrid::s4u::Host::by_name("Jupiter"), monitor,
28                               simgrid::s4u::this_actor::exec_async(1e9));
29   simgrid::s4u::Actor::create("monitor 3", simgrid::s4u::Host::by_name("Ginette"), monitor,
30                               simgrid::s4u::this_actor::exec_async(1e9));
31   simgrid::s4u::Actor::create("monitor 4", simgrid::s4u::Host::by_name("Bourassa"), monitor,
32                               simgrid::s4u::this_actor::exec_async(1e9));
33   XBT_INFO("All activities are started; finish now");
34   // Waiting execution activities is not mandatory: they go to completion once started
35
36   // No memory is leaked here: activities are automatically refcounted, thanks to C++ smart pointers
37 }
38
39 int main(int argc, char* argv[])
40 {
41   simgrid::s4u::Engine e(&argc, argv);
42   e.loadPlatform(argv[1]);
43
44   simgrid::s4u::Actor::create("executor", simgrid::s4u::Host::by_name("Fafard"), executor);
45
46   e.run();
47
48   return 0;
49 }