3 /* Copyright (c) 2006. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Property test");
20 int master(int argc, char *argv[]);
21 int slave(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
30 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
32 /** Emitter function */
33 int master(int argc, char *argv[])
36 m_host_t *slaves = NULL;
37 m_task_t *todo = NULL;
38 int number_of_tasks = 0;
39 double task_comp_size = 0;
40 double task_comm_size = 0;
45 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
46 "Invalid argument %s\n",argv[1]);
47 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
48 "Invalid argument %s\n",argv[2]);
49 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
50 "Invalid argument %s\n",argv[3]);
53 char sprintf_buffer[64];
55 todo = xbt_new(m_task_t, sizeof(m_task_t) * number_of_tasks);
57 for (i = 0; i < number_of_tasks; i++) {
58 sprintf(sprintf_buffer, "Task_%d", i);
59 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
63 { /* Process organisation */
64 slaves_count = argc - 4;
65 slaves = xbt_new(m_host_t, sizeof(m_host_t) * slaves_count);
67 for (i = 4; i < argc; i++) {
68 slaves[i-4] = MSG_get_host_by_name(argv[i]);
69 xbt_assert1(slaves[i-4]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
71 /* Get the property list of the host */
72 props = MSG_host_get_properties(slaves[i-4]);
73 xbt_dict_cursor_t cursor=NULL;
76 /* Print the properties of the host */
77 xbt_dict_foreach(props,cursor,key,data) {
78 INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
81 /* Try to get a property that does not exist */
82 char *noexist="Unknown";
83 const char*value = MSG_host_get_property_value(slaves[i-4],noexist);
85 INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
87 INFO3("Property: %s for host %s has value: %s", noexist, argv[i], value);
89 /* Modify an existing property test. First check it exists */\
90 INFO0("Trying to modify a host property");
92 value = MSG_host_get_property_value(slaves[i-4],exist);
94 INFO1("\tProperty: %s is undefined", exist);
96 INFO2("\tProperty: %s old value: %s", exist, value);
97 xbt_dict_set(props, exist, strdup("250"), free);
100 /* Test if we have changed the value */
101 value = MSG_host_get_property_value(slaves[i-4],exist);
103 INFO1("\tProperty: %s is undefined", exist);
105 INFO2("\tProperty: %s new value: %s", exist, value);
109 for (i = 0; i < number_of_tasks; i++) {
110 if(MSG_host_self()==slaves[i % slaves_count]) {
112 MSG_task_put(todo[i], slaves[i % slaves_count],
116 for (i = 0; i < slaves_count; i++)
117 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
123 } /* end_of_master */
125 /** Receiver function */
126 int slave(int argc, char *argv[])
129 m_task_t task = NULL;
131 a = MSG_task_get(&(task), PORT_22);
133 INFO1("Received \"%s\" ", MSG_task_get_name(task));
134 if(MSG_task_get_data(task)==FINALIZE) {
135 MSG_task_destroy(task);
139 /* Get the property list of current slave process */
140 xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
141 xbt_dict_cursor_t cursor=NULL;
144 /* Print the properties of the process */
145 xbt_dict_foreach(props,cursor,key,data) {
146 INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
149 /* Try to get a property that does not exist */
150 char *noexist="UnknownProcessProp";
151 const char *value = MSG_process_get_property_value(MSG_process_self(),noexist);
153 INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
155 INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
157 MSG_task_execute(task);
158 MSG_task_destroy(task);
160 xbt_die("Hey ?! What's up ?");
166 /** Forwarder function */
167 int forwarder(int argc, char *argv[])
173 { /* Process organisation */
174 slaves_count = argc - 1;
175 slaves = calloc(slaves_count, sizeof(m_host_t));
177 for (i = 1; i < argc; i++) {
178 slaves[i-1] = MSG_get_host_by_name(argv[i]);
179 xbt_assert1(slaves[i-1]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
185 m_task_t task = NULL;
187 a = MSG_task_get(&(task), PORT_22);
189 INFO1("Received \"%s\" ", MSG_task_get_name(task));
190 if(MSG_task_get_data(task)==FINALIZE) {
191 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
192 for (i = 0; i < slaves_count; i++)
193 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
195 MSG_task_destroy(task);
198 MSG_task_put(task, slaves[i % slaves_count],
202 xbt_die("Hey ?! What's up ?");
206 } /* end_of_forwarder */
209 MSG_error_t test_all(const char *platform_file,
210 const char *application_file)
212 MSG_error_t res = MSG_OK;
214 /* MSG_config("surf_workstation_model","KCCFLN05"); */
215 { /* Simulation setting */
216 MSG_set_channel_number(MAX_CHANNEL);
217 MSG_paje_output("msg_test.trace");
218 MSG_create_environment(platform_file);
220 { /* Application deployment */
221 MSG_function_register("master", master);
222 MSG_function_register("slave", slave);
223 MSG_function_register("forwarder", forwarder);
224 MSG_launch_application(application_file);
228 INFO1("Simulation time %g",MSG_get_clock());
230 } /* end_of_test_all */
234 int main(int argc, char *argv[])
236 MSG_error_t res = MSG_OK;
238 MSG_global_init(&argc,argv);
240 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
241 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
244 res = test_all(argv[1],argv[2]);