Logo AND Algorithmique Numérique Distribuée

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