Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
14e166f63f75062ebfc577c9a7dd90aeb8de8f52
[simgrid.git] / examples / cpp / platform-properties / s4u-platform-properties.cpp
1 /* Copyright (c) 2017-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 // TODO: also test the properties attached to links
7
8 #include <algorithm>
9 #include <simgrid/s4u.hpp>
10 #include <string>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Property test");
13 namespace sg4 = simgrid::s4u;
14
15 static void test_host(const std::string& hostname)
16 {
17   sg4::Host* thehost                                            = sg4::Host::by_name(hostname);
18   const std::unordered_map<std::string, std::string>* hostprops = thehost->get_properties();
19   const char* noexist = "Unknown";
20   const char* exist   = "Hdd";
21   const char* value;
22
23   XBT_INFO("== Print the properties of the host '%s'", hostname.c_str());
24   // Sort the properties before displaying them, so that the tests are perfectly reproducible
25   std::vector<std::string> keys;
26   for (auto const& kv : *hostprops)
27     keys.push_back(kv.first);
28   std::sort(keys.begin(), keys.end());
29   for (const std::string& key : keys)
30     XBT_INFO("  Host property: '%s' -> '%s'", key.c_str(), hostprops->at(key).c_str());
31
32   XBT_INFO("== Try to get a host property that does not exist");
33   value = thehost->get_property(noexist);
34   xbt_assert(not value, "The key exists (it's not supposed to)");
35
36   XBT_INFO("== Try to get a host property that does exist");
37   value = thehost->get_property(exist);
38   xbt_assert(value, "\tProperty %s is undefined (where it should)", exist);
39   xbt_assert(strcmp(value, "180") == 0, "\tValue of property %s is defined to %s (where it should be 180)", exist,
40              value);
41   XBT_INFO("   Property: %s old value: %s", exist, value);
42
43   XBT_INFO("== Trying to modify a host property");
44   thehost->set_property(exist, "250");
45
46   /* Test if we have changed the value */
47   value = thehost->get_property(exist);
48   xbt_assert(value, "Property %s is undefined (where it should)", exist);
49   xbt_assert(strcmp(value, "250") == 0, "Value of property %s is defined to %s (where it should be 250)", exist, value);
50   XBT_INFO("   Property: %s old value: %s", exist, value);
51
52   /* Restore the value for the next test */
53   thehost->set_property(exist, "180");
54
55   const auto* thezone = thehost->get_englobing_zone();
56   XBT_INFO("== Print the properties of the zone '%s' that contains '%s'", thezone->get_cname(), hostname.c_str());
57   const std::unordered_map<std::string, std::string>* zoneprops = thezone->get_properties();
58   keys.clear();
59   for (auto const& kv : *zoneprops)
60     keys.push_back(kv.first);
61   std::sort(keys.begin(), keys.end());
62   for (const std::string& key : keys)
63     XBT_INFO("  Zone property: '%s' -> '%s'", key.c_str(), zoneprops->at(key).c_str());
64 }
65
66 static void alice()
67 {
68   /* Dump what we have on the current host */
69   test_host("host1");
70 }
71
72 static void carole()
73 {
74   /* Dump what we have on a remote host */
75   sg4::this_actor::sleep_for(1); // Wait for alice to be done with its experiment
76   test_host("host1");
77 }
78
79 static void david()
80 {
81   /* Dump what we have on a remote host */
82   sg4::this_actor::sleep_for(2); // Wait for alice and carole to be done with its experiment
83   test_host("node-0.simgrid.org");
84 }
85
86 static void bob()
87 {
88   /* this host also tests the properties of the AS*/
89   const sg4::NetZone* root = sg4::Engine::get_instance()->get_netzone_root();
90   XBT_INFO("== Print the properties of the root zone");
91   XBT_INFO("   Zone property: filename -> %s", root->get_property("filename"));
92   XBT_INFO("   Zone property: date -> %s", root->get_property("date"));
93   XBT_INFO("   Zone property: author -> %s", root->get_property("author"));
94
95   /* Get the property list of current bob actor */
96   const std::unordered_map<std::string, std::string>* props = sg4::Actor::self()->get_properties();
97   const char* noexist = "UnknownProcessProp";
98   XBT_ATTRIB_UNUSED const char* value;
99
100   XBT_INFO("== Print the properties of the actor");
101   for (const auto& kv : *props)
102     XBT_INFO("   Actor property: %s -> %s", kv.first.c_str(), kv.second.c_str());
103
104   XBT_INFO("== Try to get an actor property that does not exist");
105
106   value = sg4::Actor::self()->get_property(noexist);
107   xbt_assert(not value, "The property is defined (it should not)");
108 }
109
110 int main(int argc, char* argv[])
111 {
112   sg4::Engine e(&argc, argv);
113   e.load_platform(argv[1]);
114   auto* host1 = e.host_by_name("host1");
115   auto* host2 = e.host_by_name("host2");
116
117   size_t totalHosts = e.get_host_count();
118
119   XBT_INFO("There are %zu hosts in the environment", totalHosts);
120   std::vector<sg4::Host*> hosts = e.get_all_hosts();
121   for (sg4::Host const* host : hosts)
122     XBT_INFO("Host '%s' runs at %.0f flops/s", host->get_cname(), host->get_speed());
123
124   sg4::Actor::create("alice", host1, alice);
125   sg4::Actor::create("bob", host1, bob)->set_property("SomeProp", "SomeValue");
126   sg4::Actor::create("carole", host2, carole);
127   sg4::Actor::create("david", host2, david);
128
129   e.run();
130
131   return 0;
132 }