Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into mc++
[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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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 msg_error_t test_all(const char *platform_file,
35                      const char *application_file);
36
37 static void test_host(const char*hostname) 
38 {
39   msg_host_t thehost = MSG_get_host_by_name(hostname);
40   xbt_dict_t props = MSG_host_get_properties(thehost);
41   xbt_dict_cursor_t cursor = NULL;
42   char *key, *data;
43   const char *noexist = "Unknown";
44   const char *value;
45   char exist[] = "Hdd";
46
47   XBT_INFO("== Print the properties of the host");
48   xbt_dict_foreach(props, cursor, key, data)
49       XBT_INFO("  Host property: '%s' -> '%s'", key, data);
50
51   XBT_INFO("== Try to get a host property that does not exist");
52   value = MSG_host_get_property_value(thehost, noexist);
53   xbt_assert(!value, "The key exists (it's not supposed to)");
54
55   XBT_INFO("== Try to get a host property that does exist");
56   value = MSG_host_get_property_value(thehost, exist);
57   xbt_assert(value, "\tProperty %s is undefined (where it should)",
58               exist);
59   xbt_assert(!strcmp(value, "180"),
60               "\tValue of property %s is defined to %s (where it should be 180)",
61               exist, value);
62   XBT_INFO("   Property: %s old value: %s", exist, value);
63
64   XBT_INFO("== Trying to modify a host property");
65   MSG_host_set_property_value(thehost, exist, xbt_strdup("250"), NULL);
66
67   /* Test if we have changed the value */
68   value = MSG_host_get_property_value(thehost, exist);
69   xbt_assert(value, "Property %s is undefined (where it should)", exist);
70   xbt_assert(!strcmp(value, "250"),
71               "Value of property %s is defined to %s (where it should be 250)",
72               exist, value);
73   XBT_INFO("   Property: %s old value: %s", exist, value);
74
75   /* Restore the value for the next test */
76   MSG_host_set_property_value(thehost, exist, xbt_strdup("180"), NULL);
77 }
78
79 int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
80   test_host("host1");
81   return 0;
82 }
83 int carole(int argc, char *argv[]) {/* Dump what we have on a remote host */
84   MSG_process_sleep(1); // Wait for alice to be done with its experiment
85   test_host("host1");
86   return 0;
87 }
88
89 int bob(int argc, char *argv[])
90 {
91   /* Get the property list of current bob process */
92   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
93   xbt_dict_cursor_t cursor = NULL;
94   char *key, *data;
95   const char *noexist = "UnknownProcessProp";
96   _XBT_GNUC_UNUSED const char *value;
97
98   XBT_INFO("== Print the properties of the process");
99   xbt_dict_foreach(props, cursor, key, data)
100       XBT_INFO("   Process property: %s -> %s", key, data);
101
102   XBT_INFO("== Try to get a process property that does not exist");
103
104   value = MSG_process_get_property_value(MSG_process_self(), noexist);
105   xbt_assert(!value, "The property is defined (it shouldnt)");
106
107   return 0;
108 }
109
110 /** Test function */
111 msg_error_t test_all(const char *platform_file,
112                      const char *application_file)
113 {
114   int host_number;
115   unsigned int i;
116   xbt_dynar_t hosts;
117   msg_host_t host;
118   msg_error_t ret;
119
120   MSG_function_register("alice", alice);
121   MSG_function_register("bob", bob);
122   MSG_function_register("carole", carole);
123
124   MSG_create_environment(platform_file);
125
126   host_number = MSG_get_host_number();
127   XBT_INFO("There are %d hosts in the environment", host_number);
128
129   hosts = MSG_hosts_as_dynar();
130
131   xbt_dynar_foreach(hosts, i, host){
132     XBT_INFO("Host '%s' runs at %.0f flops/s",MSG_host_get_name(host),
133        MSG_get_host_speed(host));
134   }
135
136   MSG_launch_application(application_file);
137
138   ret = MSG_main();
139
140   xbt_dynar_free(&hosts);
141
142   return ret;
143 }                               /* end_of_test_all */
144
145
146 /** Main function */
147 int main(int argc, char *argv[])
148 {
149   msg_error_t res = MSG_OK;
150
151   MSG_init(&argc, argv);
152   if (argc < 3) {
153     printf("Usage: %s platform_file deployment_file\n", argv[0]);
154     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
155     exit(1);
156   }
157   res = test_all(argv[1], argv[2]);
158
159   if (res == MSG_OK)
160     return 0;
161   else
162     return 1;
163 }                               /* end_of_main */