Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / platform-properties / platform-properties.c
1 /* Copyright (c) 2007-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 #include "simgrid/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/host.h"
9 #include "simgrid/zone.h"
10
11 #include "xbt/asserts.h"
12 #include "xbt/dict.h"
13 #include "xbt/log.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
16
17 static void test_host(const char* hostname)
18 {
19   sg_host_t thehost        = sg_host_by_name(hostname);
20   xbt_dict_t props         = sg_host_get_properties(thehost);
21   xbt_dict_cursor_t cursor = NULL;
22   char* key;
23   char* data;
24   const char* noexist = "Unknown";
25   const char* value;
26   char exist[] = "Hdd";
27
28   XBT_INFO("== Print the properties of the host '%s'", hostname);
29   xbt_dict_foreach (props, cursor, key, data)
30     XBT_INFO("  Host property: '%s' -> '%s'", key, data);
31
32   XBT_INFO("== Try to get a host property that does not exist");
33   value = sg_host_get_property_value(thehost, noexist);
34   xbt_assert(!value, "The key exists (it's not supposed to)");
35
36   XBT_INFO("== Try to get a host property that does exist");
37   value = sg_host_get_property_value(thehost, exist);
38   xbt_assert(value, "\tProperty %s is undefined (where it should)", exist);
39   xbt_assert(!strcmp(value, "180"), "\tValue of property %s is defined to %s (where it should be 180)", exist, value);
40   XBT_INFO("   Property: %s old value: %s", exist, value);
41
42   XBT_INFO("== Trying to modify a host property");
43   sg_host_set_property_value(thehost, exist, (char*)"250");
44
45   /* Test if we have changed the value */
46   value = sg_host_get_property_value(thehost, exist);
47   xbt_assert(value, "Property %s is undefined (where it should)", exist);
48   xbt_assert(!strcmp(value, "250"), "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   sg_host_set_property_value(thehost, exist, (char*)"180");
53
54   xbt_dict_free(&props);
55 }
56
57 static void alice(int argc, char* argv[])
58 { /* Dump what we have on the current host */
59   test_host("host1");
60 }
61
62 static void carole(int argc, char* argv[])
63 {                        /* Dump what we have on a remote host */
64   sg_actor_sleep_for(1); // Wait for alice to be done with its experiment
65   test_host("host1");
66 }
67
68 static void david(int argc, char* argv[])
69 {                        /* Dump what we have on a remote host */
70   sg_actor_sleep_for(2); // Wait for alice and carole to be done with its experiment
71   test_host("node-0.simgrid.org");
72 }
73
74 static void bob(int argc, char* argv[])
75 {
76   /* this host also tests the properties of the AS*/
77   const_sg_netzone_t root = sg_zone_get_root();
78   XBT_INFO("== Print the properties of the AS");
79   XBT_INFO("   Process property: filename -> %s", sg_zone_get_property_value(root, "filename"));
80   XBT_INFO("   Process property: date -> %s", sg_zone_get_property_value(root, "date"));
81   XBT_INFO("   Process property: author -> %s", sg_zone_get_property_value(root, "author"));
82
83   /* Get the property list of current bob process */
84   xbt_dict_t props         = sg_actor_get_properties(sg_actor_self());
85   xbt_dict_cursor_t cursor = NULL;
86   char* key;
87   char* data;
88   const char* noexist = "UnknownProcessProp";
89   const char* value;
90
91   XBT_INFO("== Print the properties of the process");
92   xbt_dict_foreach (props, cursor, key, data)
93     XBT_INFO("   Process property: %s -> %s", key, data);
94
95   XBT_INFO("== Try to get a process property that does not exist");
96
97   value = sg_actor_get_property_value(sg_actor_self(), noexist);
98   xbt_assert(!value, "The property is defined (it shouldn't)");
99   xbt_dict_free(&props);
100 }
101
102 int main(int argc, char* argv[])
103 {
104   simgrid_init(&argc, argv);
105   xbt_assert(argc > 2,
106              "Usage: %s platform_file deployment_file\n"
107              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
108              argv[0], argv[0]);
109
110   simgrid_register_function("alice", alice);
111   simgrid_register_function("bob", bob);
112   simgrid_register_function("carole", carole);
113   simgrid_register_function("david", david);
114
115   simgrid_load_platform(argv[1]);
116   size_t host_count = sg_host_count();
117   XBT_INFO("There are %zu hosts in the environment", host_count);
118
119   sg_host_t* hosts = sg_host_list();
120   for (size_t i = 0; i < host_count; i++)
121     XBT_INFO("Host '%s' runs at %.0f flops/s", sg_host_get_name(hosts[i]), sg_host_get_speed(hosts[i]));
122
123   free(hosts);
124
125   simgrid_load_deployment(argv[2]);
126
127   simgrid_run();
128
129   return 0;
130 }