Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dabffc1553f77fa407ccc297c9e46f88b66c80d4
[simgrid.git] / examples / s4u / platform-properties / s4u-platform-properties.cpp
1 /* Copyright (c) 2017. 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 <simgrid/s4u.hpp>
9 #include <string>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Property test");
12
13 static void test_host(std::string hostname)
14 {
15   simgrid::s4u::Host* thehost = simgrid::s4u::Host::by_name(hostname);
16   std::map<std::string, std::string>* props = thehost->getProperties();
17   const char* noexist = "Unknown";
18   const char* exist   = "Hdd";
19   const char* value;
20
21   XBT_INFO("== Print the properties of the host '%s'", hostname.c_str());
22   for (const auto& kv : *props)
23     XBT_INFO("  Host property: '%s' -> '%s'", kv.first.c_str(), kv.second.c_str());
24
25   XBT_INFO("== Try to get a host property that does not exist");
26   value = thehost->getProperty(noexist);
27   xbt_assert(not value, "The key exists (it's not supposed to)");
28
29   XBT_INFO("== Try to get a host property that does exist");
30   value = thehost->getProperty(exist);
31   xbt_assert(value, "\tProperty %s is undefined (where it should)", exist);
32   xbt_assert(!strcmp(value, "180"), "\tValue of property %s is defined to %s (where it should be 180)", exist, value);
33   XBT_INFO("   Property: %s old value: %s", exist, value);
34
35   XBT_INFO("== Trying to modify a host property");
36   thehost->setProperty(exist, "250");
37
38   /* Test if we have changed the value */
39   value = thehost->getProperty(exist);
40   xbt_assert(value, "Property %s is undefined (where it should)", exist);
41   xbt_assert(!strcmp(value, "250"), "Value of property %s is defined to %s (where it should be 250)", exist, value);
42   XBT_INFO("   Property: %s old value: %s", exist, value);
43
44   /* Restore the value for the next test */
45   thehost->setProperty(exist, "180");
46 }
47
48 static int alice(int argc, char* argv[])
49 {
50   /* Dump what we have on the current host */
51   test_host("host1");
52   return 0;
53 }
54
55 static int carole(int argc, char* argv[])
56 {
57   /* Dump what we have on a remote host */
58   simgrid::s4u::this_actor::sleep_for(1); // Wait for alice to be done with its experiment
59   test_host("host1");
60   return 0;
61 }
62
63 static int david(int argc, char* argv[])
64 {
65   /* Dump what we have on a remote host */
66   simgrid::s4u::this_actor::sleep_for(2); // Wait for alice and carole to be done with its experiment
67   test_host("node-0.acme.org");
68   return 0;
69 }
70
71 static int bob(int argc, char* argv[])
72 {
73   /* this host also tests the properties of the AS*/
74   simgrid::s4u::NetZone* root = simgrid::s4u::Engine::getInstance()->getNetRoot();
75   XBT_INFO("== Print the properties of the zone");
76   XBT_INFO("   Zone property: filename -> %s", root->getProperty("filename"));
77   XBT_INFO("   Zone property: date -> %s", root->getProperty("date"));
78   XBT_INFO("   Zone property: author -> %s", root->getProperty("author"));
79
80   /* Get the property list of current bob process */
81   std::map<std::string, std::string>* props = simgrid::s4u::Actor::self()->getProperties();
82   const char* noexist = "UnknownProcessProp";
83   XBT_ATTRIB_UNUSED const char* value;
84
85   XBT_INFO("== Print the properties of the actor");
86   for (const auto& kv : *props)
87     XBT_INFO("   Actor property: %s -> %s", kv.first.c_str(), kv.second.c_str());
88
89   XBT_INFO("== Try to get an actor property that does not exist");
90
91   value = simgrid::s4u::Actor::self()->getProperty(noexist);
92   xbt_assert(not value, "The property is defined (it shouldnt)");
93   return 0;
94 }
95
96 int main(int argc, char* argv[])
97 {
98   simgrid::s4u::Engine e(&argc, argv);
99   e.loadPlatform(argv[1]);
100
101   e.registerFunction("alice", alice);
102   e.registerFunction("bob", bob);
103   e.registerFunction("carole", carole);
104   e.registerFunction("david", david);
105
106   size_t totalHosts = sg_host_count();
107
108   XBT_INFO("There are %zu hosts in the environment", totalHosts);
109   std::vector<simgrid::s4u::Host*> hosts;
110   e.getHostList(&hosts);
111   for (unsigned int i = 0; i < hosts.size(); i++)
112     XBT_INFO("Host '%s' runs at %.0f flops/s", hosts[i]->getCname(), hosts[i]->getSpeed());
113
114   e.loadDeployment(argv[2]);
115   e.run();
116
117   return 0;
118 }