1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
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. */
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.
9 #include <simgrid/s4u.hpp>
10 #include <xbt/config.hpp>
12 namespace sg4 = simgrid::s4u;
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_monkey, "Simple test of the semaphore");
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 */
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)",
24 int todo; // remaining amount of items to exchange
26 static void producer()
28 static bool inited = false;
29 static std::vector<sg4::Semaphore*> to_release;
30 XBT_INFO("Producer %s", inited ? "rebooting" : "booting");
33 sg4::this_actor::on_exit(
34 [](bool forcefully) { XBT_INFO("Producer dying %s.", forcefully ? "forcefully" : "peacefully"); });
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();
40 XBT_INFO("Released a semaphore on reboot. It's now %d", sem->get_capacity());
41 to_release.pop_back();
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);
48 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
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");
55 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
57 XBT_INFO("Pushing item %d", todo - 1);
60 to_release.pop_back();
61 XBT_INFO("sem_empty removed from to_release");
65 static void consumer()
67 static std::vector<sg4::Semaphore*> to_release;
69 static bool inited = false;
70 XBT_INFO("Consumer %s", inited ? "rebooting" : "booting");
72 sg4::this_actor::on_exit(
73 [](bool forcefully) { XBT_INFO("Consumer dying %s.", forcefully ? "forcefully" : "peacefully"); });
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();
79 XBT_INFO("Released a semaphore on reboot. It's now %d", sem->get_capacity());
80 to_release.pop_back();
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);
88 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
90 while (sem_full->acquire_timeout(10))
91 XBT_INFO("Timeouted");
92 to_release.push_back(sem_full.get());
94 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
97 XBT_INFO("Receiving item %d", item);
99 to_release.pop_back();
105 int main(int argc, char** argv)
107 sg4::Engine e(&argc, argv);
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);
116 sg4::Actor::create("producer", paul, producer)->set_auto_restart();
117 sg4::Actor::create("consumer", carol, consumer)->set_auto_restart();