Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
today is doomsday: platform.xml is sacrificed for the greater good
[simgrid.git] / examples / msg / platform-properties / platform-properties.c
1 /* Copyright (c) 2007-2016. 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/msg.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
9
10 static void test_host(const char*hostname) 
11 {
12   msg_host_t thehost = MSG_host_by_name(hostname);
13   xbt_dict_t props = MSG_host_get_properties(thehost);
14   xbt_dict_cursor_t cursor = NULL;
15   char *key, *data;
16   const char *noexist = "Unknown";
17   const char *value;
18   char exist[] = "Hdd";
19
20   XBT_INFO("== Print the properties of the host '%s'", hostname);
21   xbt_dict_foreach(props, cursor, key, data)
22     XBT_INFO("  Host property: '%s' -> '%s'", key, data);
23
24   XBT_INFO("== Try to get a host property that does not exist");
25   value = MSG_host_get_property_value(thehost, noexist);
26   xbt_assert(!value, "The key exists (it's not supposed to)");
27
28   XBT_INFO("== Try to get a host property that does exist");
29   value = MSG_host_get_property_value(thehost, exist);
30   xbt_assert(value, "\tProperty %s is undefined (where it should)", exist);
31   xbt_assert(!strcmp(value, "180"), "\tValue of property %s is defined to %s (where it should be 180)", exist, value);
32   XBT_INFO("   Property: %s old value: %s", exist, value);
33
34   XBT_INFO("== Trying to modify a host property");
35   MSG_host_set_property_value(thehost, exist, xbt_strdup("250"), xbt_free_f);
36
37   /* Test if we have changed the value */
38   value = MSG_host_get_property_value(thehost, exist);
39   xbt_assert(value, "Property %s is undefined (where it should)", exist);
40   xbt_assert(!strcmp(value, "250"), "Value of property %s is defined to %s (where it should be 250)", exist, value);
41   XBT_INFO("   Property: %s old value: %s", exist, value);
42
43   /* Restore the value for the next test */
44   MSG_host_set_property_value(thehost, exist, xbt_strdup("180"), xbt_free_f);
45 }
46
47 static int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
48   test_host("host1");
49   return 0;
50 }
51
52 static int carole(int argc, char *argv[]) {/* Dump what we have on a remote host */
53   MSG_process_sleep(1); // Wait for alice to be done with its experiment
54   test_host("host1");
55   return 0;
56 }
57
58 static int david(int argc, char *argv[]) {/* Dump what we have on a remote host */
59   MSG_process_sleep(2); // Wait for alice and carole to be done with its experiment
60   test_host("node-0.acme.org");
61   return 0;
62 }
63
64 static int bob(int argc, char *argv[])
65 {
66   /* Get the property list of current bob process */
67   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
68   xbt_dict_cursor_t cursor = NULL;
69   char *key, *data;
70   const char *noexist = "UnknownProcessProp";
71   XBT_ATTRIB_UNUSED const char *value;
72
73   XBT_INFO("== Print the properties of the process");
74   xbt_dict_foreach(props, cursor, key, data)
75     XBT_INFO("   Process property: %s -> %s", key, data);
76
77   XBT_INFO("== Try to get a process property that does not exist");
78
79   value = MSG_process_get_property_value(MSG_process_self(), noexist);
80   xbt_assert(!value, "The property is defined (it shouldnt)");
81
82   return 0;
83 }
84
85 int main(int argc, char *argv[])
86 {
87   unsigned int i;
88   msg_host_t host;
89
90   MSG_init(&argc, argv);
91   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
92              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
93
94   MSG_function_register("alice", alice);
95   MSG_function_register("bob", bob);
96   MSG_function_register("carole", carole);
97   MSG_function_register("david", david);
98
99   MSG_create_environment(argv[1]);
100
101   XBT_INFO("There are %d hosts in the environment", MSG_get_host_number());
102
103   xbt_dynar_t hosts = MSG_hosts_as_dynar();
104   xbt_dynar_foreach(hosts, i, host){
105     XBT_INFO("Host '%s' runs at %.0f flops/s",MSG_host_get_name(host), MSG_host_get_speed(host));
106   }
107   xbt_dynar_free(&hosts);
108
109   MSG_launch_application(argv[2]);
110
111   msg_error_t res =  MSG_main();
112
113   return res!=MSG_OK;
114 }