Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge before commiting VM changes - Adrien
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 /** @addtogroup MSG_examples
18  * 
19  * - <b>properties/msg_prop.c</b> Attaching arbitrary informations to
20  *   host, processes and such, and retrieving them with @ref
21  *   MSG_host_get_properties, @ref MSG_host_get_property_value, @ref
22  *   MSG_process_get_properties and @ref
23  *   MSG_process_get_property_value. Also make sure to read the
24  *   platform and deployment XML files to see how to declare these
25  *   data.
26  * 
27  */
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
30
31 int alice(int argc, char *argv[]);
32 int bob(int argc, char *argv[]);
33 int carole(int argc, char *argv[]);
34 int david(int argc, char *argv[]);
35 msg_error_t test_all(const char *platform_file,
36                      const char *application_file);
37
38 static void test_host(const char*hostname) 
39 {
40   msg_host_t thehost = MSG_get_host_by_name(hostname);
41   xbt_dict_t props = MSG_host_get_properties(thehost);
42   xbt_dict_cursor_t cursor = NULL;
43   char *key, *data;
44   const char *noexist = "Unknown";
45   const char *value;
46   char exist[] = "Hdd";
47
48   XBT_INFO("== Print the properties of the host '%s'", hostname);
49   xbt_dict_foreach(props, cursor, key, data)
50       XBT_INFO("  Host property: '%s' -> '%s'", key, data);
51
52   XBT_INFO("== Try to get a host property that does not exist");
53   value = MSG_host_get_property_value(thehost, noexist);
54   xbt_assert(!value, "The key exists (it's not supposed to)");
55
56   XBT_INFO("== Try to get a host property that does exist");
57   value = MSG_host_get_property_value(thehost, exist);
58   xbt_assert(value, "\tProperty %s is undefined (where it should)",
59               exist);
60   xbt_assert(!strcmp(value, "180"),
61               "\tValue of property %s is defined to %s (where it should be 180)",
62               exist, value);
63   XBT_INFO("   Property: %s old value: %s", exist, value);
64
65   XBT_INFO("== Trying to modify a host property");
66   MSG_host_set_property_value(thehost, exist, xbt_strdup("250"), xbt_free_f);
67
68   /* Test if we have changed the value */
69   value = MSG_host_get_property_value(thehost, exist);
70   xbt_assert(value, "Property %s is undefined (where it should)", exist);
71   xbt_assert(!strcmp(value, "250"),
72               "Value of property %s is defined to %s (where it should be 250)",
73               exist, value);
74   XBT_INFO("   Property: %s old value: %s", exist, value);
75
76   /* Restore the value for the next test */
77   MSG_host_set_property_value(thehost, exist, xbt_strdup("180"), xbt_free_f);
78 }
79
80 int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
81   test_host("host1");
82   return 0;
83 }
84 int carole(int argc, char *argv[]) {/* Dump what we have on a remote host */
85   MSG_process_sleep(1); // Wait for alice to be done with its experiment
86   test_host("host1");
87   return 0;
88 }
89 int david(int argc, char *argv[]) {/* Dump what we have on a remote host */
90   MSG_process_sleep(2); // Wait for alice and carole to be done with its experiment
91   test_host("node-0.acme.org");
92   return 0;
93 }
94
95 int bob(int argc, char *argv[])
96 {
97   /* Get the property list of current bob process */
98   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
99   xbt_dict_cursor_t cursor = NULL;
100   char *key, *data;
101   const char *noexist = "UnknownProcessProp";
102   _XBT_GNUC_UNUSED const char *value;
103
104   XBT_INFO("== Print the properties of the process");
105   xbt_dict_foreach(props, cursor, key, data)
106       XBT_INFO("   Process property: %s -> %s", key, data);
107
108   XBT_INFO("== Try to get a process property that does not exist");
109
110   value = MSG_process_get_property_value(MSG_process_self(), noexist);
111   xbt_assert(!value, "The property is defined (it shouldnt)");
112
113   return 0;
114 }
115
116 /** Test function */
117 msg_error_t test_all(const char *platform_file,
118                      const char *application_file)
119 {
120   int host_number;
121   unsigned int i;
122   xbt_dynar_t hosts;
123   msg_host_t host;
124   msg_error_t ret;
125
126   MSG_function_register("alice", alice);
127   MSG_function_register("bob", bob);
128   MSG_function_register("carole", carole);
129   MSG_function_register("david", david);
130
131   MSG_create_environment(platform_file);
132
133   host_number = MSG_get_host_number();
134   XBT_INFO("There are %d hosts in the environment", host_number);
135
136   hosts = MSG_hosts_as_dynar();
137
138   xbt_dynar_foreach(hosts, i, host){
139     XBT_INFO("Host '%s' runs at %.0f flops/s",MSG_host_get_name(host),
140        MSG_get_host_speed(host));
141   }
142
143   MSG_launch_application(application_file);
144
145   ret = MSG_main();
146
147   xbt_dynar_free(&hosts);
148
149   return ret;
150 }                               /* end_of_test_all */
151
152
153 /** Main function */
154 int main(int argc, char *argv[])
155 {
156   msg_error_t res = MSG_OK;
157
158   MSG_init(&argc, argv);
159   if (argc < 3) {
160     printf("Usage: %s platform_file deployment_file\n", argv[0]);
161     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
162     exit(1);
163   }
164   res = test_all(argv[1], argv[2]);
165
166   if (res == MSG_OK)
167     return 0;
168   else
169     return 1;
170 }                               /* end_of_main */