1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 msg_error_t test_all(const char *platform_file,
35 const char *application_file);
37 static void test_host(const char*hostname)
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;
43 const char *noexist = "Unknown";
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);
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)");
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)",
59 xbt_assert(!strcmp(value, "180"),
60 "\tValue of property %s is defined to %s (where it should be 180)",
62 XBT_INFO(" Property: %s old value: %s", exist, value);
64 XBT_INFO("== Trying to modify a host property");
65 MSG_host_set_property_value(thehost, exist, xbt_strdup("250"), NULL);
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)",
73 XBT_INFO(" Property: %s old value: %s", exist, value);
75 /* Restore the value for the next test */
76 MSG_host_set_property_value(thehost, exist, xbt_strdup("180"), NULL);
79 int alice(int argc, char *argv[]) { /* Dump what we have on the current host */
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
89 int bob(int argc, char *argv[])
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;
95 const char *noexist = "UnknownProcessProp";
96 _XBT_GNUC_UNUSED const char *value;
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);
102 XBT_INFO("== Try to get a process property that does not exist");
104 value = MSG_process_get_property_value(MSG_process_self(), noexist);
105 xbt_assert(!value, "The property is defined (it shouldnt)");
111 msg_error_t test_all(const char *platform_file,
112 const char *application_file)
120 MSG_function_register("alice", alice);
121 MSG_function_register("bob", bob);
122 MSG_function_register("carole", carole);
124 MSG_create_environment(platform_file);
126 host_number = MSG_get_host_number();
127 XBT_INFO("There are %d hosts in the environment", host_number);
129 hosts = MSG_hosts_as_dynar();
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));
136 MSG_launch_application(application_file);
140 xbt_dynar_free(&hosts);
143 } /* end_of_test_all */
147 int main(int argc, char *argv[])
149 msg_error_t res = MSG_OK;
151 MSG_init(&argc, argv);
153 printf("Usage: %s platform_file deployment_file\n", argv[0]);
154 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
157 res = test_all(argv[1], argv[2]);