1 /* Copyright (c) 2017-2023. 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 // TODO: also test the properties attached to links
9 #include <simgrid/s4u.hpp>
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Property test");
13 namespace sg4 = simgrid::s4u;
15 static void test_host(const std::string& hostname)
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";
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& [key, _] : *hostprops)
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());
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)");
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,
41 XBT_INFO(" Property: %s old value: %s", exist, value);
43 XBT_INFO("== Trying to modify a host property");
44 thehost->set_property(exist, "250");
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);
52 /* Restore the value for the next test */
53 thehost->set_property(exist, "180");
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();
59 for (auto const& [key, _] : *zoneprops)
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());
68 /* Dump what we have on the current host */
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
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");
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"));
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";
99 XBT_INFO("== Print the properties of the actor");
100 for (const auto& [key, value] : *props)
101 XBT_INFO(" Actor property: %s -> %s", key.c_str(), value.c_str());
103 XBT_INFO("== Try to get an actor property that does not exist");
105 const char* value = sg4::Actor::self()->get_property(noexist);
106 xbt_assert(not value, "The property is defined (it should not)");
109 int main(int argc, char* argv[])
111 sg4::Engine e(&argc, argv);
112 e.load_platform(argv[1]);
113 auto* host1 = e.host_by_name("host1");
114 auto* host2 = e.host_by_name("host2");
116 size_t totalHosts = e.get_host_count();
118 XBT_INFO("There are %zu hosts in the environment", totalHosts);
119 std::vector<sg4::Host*> hosts = e.get_all_hosts();
120 for (sg4::Host const* host : hosts)
121 XBT_INFO("Host '%s' runs at %.0f flops/s", host->get_cname(), host->get_speed());
123 sg4::Actor::create("alice", host1, alice);
124 sg4::Actor::create("bob", host1, bob)->set_property("SomeProp", "SomeValue");
125 sg4::Actor::create("carole", host2, carole);
126 sg4::Actor::create("david", host2, david);