Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
semaphore monkey
[simgrid.git] / teshsuite / s4u / monkey-semaphore / monkey-semaphore.cpp
1 /* Copyright (c) 2006-2022. 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 // This example implements a simple producer/consumer schema, passing a bunch of items from one to the other,
7 // hopefully implemented in a way that resists resource failures.
8
9 #include <simgrid/s4u.hpp>
10 #include <xbt/config.hpp>
11
12 namespace sg4 = simgrid::s4u;
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_monkey, "Simple test of the semaphore");
15
16 int buffer;                                              /* Where the data is exchanged */
17 sg4::SemaphorePtr sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */
18 sg4::SemaphorePtr sem_full  = sg4::Semaphore::create(0); /* indicates whether the buffer is full */
19
20 static simgrid::config::Flag<int> cfg_item_count{"item-count", "Amount of items that must be exchanged to succeed", 2};
21 static simgrid::config::Flag<double> cfg_deadline{"deadline", "When to fail the simulation (infinite loop detection)",
22                                                   120};
23
24 int todo; // remaining amount of items to exchange
25
26 static void producer()
27 {
28   static bool inited = false;
29   static std::vector<sg4::Semaphore*> to_release;
30   XBT_INFO("Producer %s", inited ? "rebooting" : "booting");
31
32   if (not inited) {
33     sg4::this_actor::on_exit(
34         [](bool forcefully) { XBT_INFO("Producer dying %s.", forcefully ? "forcefully" : "peacefully"); });
35     inited = true;
36   }
37   while (not to_release.empty()) { // Clean up a previous run. Cannot be done in on_exit, as it entails a simcall
38     auto* sem = to_release.back();
39     sem->release();
40     XBT_INFO("Released a semaphore on reboot. It's now %d", sem->get_capacity());
41     to_release.pop_back();
42   }
43
44   while (todo > 0) {
45     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
46                "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
47
48     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
49
50     while (sem_empty->acquire_timeout(10))
51       XBT_INFO("Timeouted");
52     to_release.push_back(sem_empty.get());
53     XBT_INFO("sem_empty acquired");
54
55     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
56
57     XBT_INFO("Pushing item %d", todo - 1);
58     buffer = todo - 1;
59     sem_full->release();
60     to_release.pop_back();
61     XBT_INFO("sem_empty removed from to_release");
62     todo--;
63   }
64 }
65 static void consumer()
66 {
67   static std::vector<sg4::Semaphore*> to_release;
68
69   static bool inited = false;
70   XBT_INFO("Consumer %s", inited ? "rebooting" : "booting");
71   if (not inited) {
72     sg4::this_actor::on_exit(
73         [](bool forcefully) { XBT_INFO("Consumer dying %s.", forcefully ? "forcefully" : "peacefully"); });
74     inited = true;
75   }
76   while (not to_release.empty()) { // Clean up a previous run. Cannot be done in on_exit, as it entails a simcall
77     auto* sem = to_release.back();
78     sem->release();
79     XBT_INFO("Released a semaphore on reboot. It's now %d", sem->get_capacity());
80     to_release.pop_back();
81   }
82
83   int item;
84   do {
85     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
86                "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
87
88     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
89
90     while (sem_full->acquire_timeout(10))
91       XBT_INFO("Timeouted");
92     to_release.push_back(sem_full.get());
93
94     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
95
96     item = buffer;
97     XBT_INFO("Receiving item %d", item);
98     sem_empty->release();
99     to_release.pop_back();
100   } while (item != 0);
101
102   XBT_INFO("Bye!");
103 }
104
105 int main(int argc, char** argv)
106 {
107   sg4::Engine e(&argc, argv);
108
109   todo           = cfg_item_count;
110   auto* rootzone = sg4::create_full_zone("root");
111   auto* paul     = rootzone->create_host("Paul", 1e9);
112   auto* carol    = rootzone->create_host("Carol", 1e9);
113   sg4::LinkInRoute link(rootzone->create_link("link", "1MBps")->set_latency("24us")->seal());
114   rootzone->add_route(paul->get_netpoint(), carol->get_netpoint(), nullptr, nullptr, {link}, true);
115
116   sg4::Actor::create("producer", paul, producer)->set_auto_restart();
117   sg4::Actor::create("consumer", carol, consumer)->set_auto_restart();
118   e.run();
119
120   return 0;
121 }