1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
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 */
10 /* Create a log channel to have nice outputs. */
12 #include "xbt/asserts.h"
17 /** @addtogroup MSG_examples
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
29 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
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);
38 static void test_host(const char*hostname)
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;
44 const char *noexist = "Unknown";
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);
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)");
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)",
60 xbt_assert(!strcmp(value, "180"),
61 "\tValue of property %s is defined to %s (where it should be 180)",
63 XBT_INFO(" Property: %s old value: %s", exist, value);
65 XBT_INFO("== Trying to modify a host property");
66 MSG_host_set_property_value(thehost, exist, xbt_strdup("250"), xbt_free_f);
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)",
74 XBT_INFO(" Property: %s old value: %s", exist, value);
76 /* Restore the value for the next test */
77 MSG_host_set_property_value(thehost, exist, xbt_strdup("180"), xbt_free_f);
80 int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
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
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");
95 int bob(int argc, char *argv[])
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;
101 const char *noexist = "UnknownProcessProp";
102 _XBT_GNUC_UNUSED const char *value;
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);
108 XBT_INFO("== Try to get a process property that does not exist");
110 value = MSG_process_get_property_value(MSG_process_self(), noexist);
111 xbt_assert(!value, "The property is defined (it shouldnt)");
117 msg_error_t test_all(const char *platform_file,
118 const char *application_file)
126 MSG_function_register("alice", alice);
127 MSG_function_register("bob", bob);
128 MSG_function_register("carole", carole);
129 MSG_function_register("david", david);
131 MSG_create_environment(platform_file);
133 host_number = MSG_get_host_number();
134 XBT_INFO("There are %d hosts in the environment", host_number);
136 hosts = MSG_hosts_as_dynar();
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));
143 MSG_launch_application(application_file);
147 xbt_dynar_free(&hosts);
150 } /* end_of_test_all */
154 int main(int argc, char *argv[])
156 msg_error_t res = MSG_OK;
158 MSG_init(&argc, argv);
160 printf("Usage: %s platform_file deployment_file\n", argv[0]);
161 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
164 res = test_all(argv[1], argv[2]);