Logo AND Algorithmique Numérique Distribuée

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