Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 int forwarder(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");
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"), NULL);
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"), NULL);
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
90 int bob(int argc, char *argv[])
91 {
92   /* Get the property list of current bob process */
93   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
94   xbt_dict_cursor_t cursor = NULL;
95   char *key, *data;
96   const char *noexist = "UnknownProcessProp";
97   _XBT_GNUC_UNUSED const char *value;
98
99   XBT_INFO("== Print the properties of the process");
100   xbt_dict_foreach(props, cursor, key, data)
101       XBT_INFO("   Process property: %s -> %s", key, data);
102
103   XBT_INFO("== Try to get a process property that does not exist");
104
105   value = MSG_process_get_property_value(MSG_process_self(), noexist);
106   xbt_assert(!value, "The property is defined (it shouldnt)");
107
108   return 0;
109 }
110
111 /** Test function */
112 msg_error_t test_all(const char *platform_file,
113                      const char *application_file)
114 {
115   int host_number;
116   unsigned int i;
117   xbt_dynar_t hosts;
118   msg_host_t host;
119   msg_error_t ret;
120
121   MSG_function_register("alice", alice);
122   MSG_function_register("bob", bob);
123   MSG_function_register("carole", carole);
124
125   MSG_create_environment(platform_file);
126
127   host_number = MSG_get_host_number();
128   XBT_INFO("There are %d hosts in the environment", host_number);
129
130   hosts = MSG_hosts_as_dynar();
131
132   xbt_dynar_foreach(hosts, i, host){
133     XBT_INFO("Host '%s' runs at %.0f flops/s",MSG_host_get_name(host),
134        MSG_get_host_speed(host));
135   }
136
137   MSG_launch_application(application_file);
138
139   ret = MSG_main();
140
141   xbt_dynar_free(&hosts);
142
143   return ret;
144 }                               /* end_of_test_all */
145
146
147 /** Main function */
148 int main(int argc, char *argv[])
149 {
150   msg_error_t res = MSG_OK;
151
152   MSG_init(&argc, argv);
153   if (argc < 3) {
154     printf("Usage: %s platform_file deployment_file\n", argv[0]);
155     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
156     exit(1);
157   }
158   res = test_all(argv[1], argv[2]);
159
160   if (res == MSG_OK)
161     return 0;
162   else
163     return 1;
164 }                               /* end_of_main */