Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fb08720f88f8073819889f7863e3a42da9f690cb
[simgrid.git] / examples / s4u / exec-monitor / s4u-exec-monitor.cpp
1 /* Copyright (c) 2017. 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 #include "simgrid/forward.h"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9 #include "simgrid/s4u/forward.hpp"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
12
13 static void monitor(simgrid::s4u::ExecPtr activity)
14 {
15   while (activity->getRemains() > 0) { // FIXME: use a test() here once that function is implemented
16     XBT_INFO("activity remaining duration: %g (%.0f%%)", activity->getRemains(), 100 * activity->getRemainingRatio());
17     simgrid::s4u::this_actor::sleep_for(5);
18   }
19   XBT_INFO("My task is over.");
20 }
21
22 static void executor()
23 {
24   XBT_INFO("Create one monitored task, and wait for it");
25   simgrid::s4u::ExecPtr activity = simgrid::s4u::Actor::self()->exec_async(1e9);
26   simgrid::s4u::Actor::createActor("monitor 1", simgrid::s4u::Host::by_name("Tremblay"), monitor, activity);
27   activity->wait(); // This blocks until the activity is over
28   XBT_INFO("The monitored task is over. Let's start 3 of them now.");
29   simgrid::s4u::Actor::createActor("monitor 2", simgrid::s4u::Host::by_name("Jupiter"), monitor,
30                                    simgrid::s4u::Actor::self()->exec_async(1e9));
31   simgrid::s4u::Actor::createActor("monitor 3", simgrid::s4u::Host::by_name("Ginette"), monitor,
32                                    simgrid::s4u::Actor::self()->exec_async(1e9));
33   simgrid::s4u::Actor::createActor("monitor 4", simgrid::s4u::Host::by_name("Bourassa"), monitor,
34                                    simgrid::s4u::Actor::self()->exec_async(1e9));
35   XBT_INFO("All activities are started; finish now");
36   // Waiting execution activities is not mandatory: they go to completion once started
37
38   // No memory is leaked here: activities are automatically refcounted, thanks to C++ smart pointers
39 }
40
41 int main(int argc, char* argv[])
42 {
43   simgrid::s4u::Engine e(&argc, argv);
44   e.loadPlatform(argv[1]);
45
46   simgrid::s4u::Actor::createActor("executor", simgrid::s4u::Host::by_name("Fafard"), executor);
47
48   e.run();
49
50   return 0;
51 }