Logo AND Algorithmique Numérique Distribuée

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