1 /* Copyright (c) 2017-2019. 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");
14 static void test_host(std::string hostname)
16 simgrid::s4u::Host* thehost = simgrid::s4u::Host::by_name(hostname);
17 std::unordered_map<std::string, std::string>* props = thehost->get_properties();
18 const char* noexist = "Unknown";
19 const char* exist = "Hdd";
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 : *props)
26 keys.push_back(kv.first);
27 std::sort(keys.begin(), keys.end());
28 for (std::string key : keys)
29 XBT_INFO(" Host property: '%s' -> '%s'", key.c_str(), props->at(key).c_str());
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)");
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"), "\tValue of property %s is defined to %s (where it should be 180)", exist, value);
39 XBT_INFO(" Property: %s old value: %s", exist, value);
41 XBT_INFO("== Trying to modify a host property");
42 thehost->set_property(exist, "250");
44 /* Test if we have changed the value */
45 value = thehost->get_property(exist);
46 xbt_assert(value, "Property %s is undefined (where it should)", exist);
47 xbt_assert(!strcmp(value, "250"), "Value of property %s is defined to %s (where it should be 250)", exist, value);
48 XBT_INFO(" Property: %s old value: %s", exist, value);
50 /* Restore the value for the next test */
51 thehost->set_property(exist, "180");
54 static void alice(std::vector<std::string> /*args*/)
56 /* Dump what we have on the current host */
60 static void carole(std::vector<std::string> /*args*/)
62 /* Dump what we have on a remote host */
63 simgrid::s4u::this_actor::sleep_for(1); // Wait for alice to be done with its experiment
67 static void david(std::vector<std::string> /*args*/)
69 /* Dump what we have on a remote host */
70 simgrid::s4u::this_actor::sleep_for(2); // Wait for alice and carole to be done with its experiment
71 test_host("node-0.simgrid.org");
74 static void bob(std::vector<std::string> /*args*/)
76 /* this host also tests the properties of the AS*/
77 simgrid::s4u::NetZone* root = simgrid::s4u::Engine::get_instance()->get_netzone_root();
78 XBT_INFO("== Print the properties of the zone");
79 XBT_INFO(" Zone property: filename -> %s", root->get_property("filename"));
80 XBT_INFO(" Zone property: date -> %s", root->get_property("date"));
81 XBT_INFO(" Zone property: author -> %s", root->get_property("author"));
83 /* Get the property list of current bob process */
84 std::unordered_map<std::string, std::string>* props = simgrid::s4u::Actor::self()->get_properties();
85 const char* noexist = "UnknownProcessProp";
86 XBT_ATTRIB_UNUSED const char* value;
88 XBT_INFO("== Print the properties of the actor");
89 for (const auto& kv : *props)
90 XBT_INFO(" Actor property: %s -> %s", kv.first.c_str(), kv.second.c_str());
92 XBT_INFO("== Try to get an actor property that does not exist");
94 value = simgrid::s4u::Actor::self()->get_property(noexist);
95 xbt_assert(not value, "The property is defined (it should not)");
98 int main(int argc, char* argv[])
100 simgrid::s4u::Engine e(&argc, argv);
101 e.load_platform(argv[1]);
103 e.register_function("alice", alice);
104 e.register_function("bob", bob);
105 e.register_function("carole", carole);
106 e.register_function("david", david);
108 size_t totalHosts = e.get_host_count();
110 XBT_INFO("There are %zu hosts in the environment", totalHosts);
111 std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
112 for (unsigned int i = 0; i < hosts.size(); i++)
113 XBT_INFO("Host '%s' runs at %.0f flops/s", hosts[i]->get_cname(), hosts[i]->get_speed());
115 e.load_deployment(argv[2]);