1 /* $Id: msg_test.c,v 1.24 2007/03/16 13:18:24 cherierm Exp $ */
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. 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(msg_test,"Messages specific for this msg example");
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 = calloc(number_of_tasks, sizeof(m_task_t));
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 = calloc(slaves_count, sizeof(m_host_t));
67 for (i = 4; i < argc; i++) {
68 slaves[i-4] = MSG_get_host_by_name(argv[i]);
69 /* Get the property list of the host */
70 props = MSG_host_get_properties(slaves[i-4]);
71 xbt_dict_cursor_t cursor=NULL;
74 /* Print the properties of the host */
75 xbt_dict_foreach(props,cursor,key,data) {
76 INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
79 /* Try to get a property that does not exist */
80 char *noexist=xbt_strdup("Unknown");
81 const char *value = MSG_host_get_property_value(slaves[i-4],noexist);
83 INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
85 INFO3("Property: %s for host %s has value: %s", noexist, argv[i], value);
87 if(slaves[i-4]==NULL) {
88 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
94 for (i = 0; i < number_of_tasks; i++) {
95 if(MSG_host_self()==slaves[i % slaves_count]) {
98 MSG_task_put(todo[i], slaves[i % slaves_count],
102 for (i = 0; i < slaves_count; i++)
103 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
109 } /* end_of_master */
111 /** Receiver function */
112 int slave(int argc, char *argv[])
115 m_task_t task = NULL;
117 a = MSG_task_get(&(task), PORT_22);
119 INFO1("Received \"%s\" ", MSG_task_get_name(task));
120 if(MSG_task_get_data(task)==FINALIZE) {
121 MSG_task_destroy(task);
125 /* Get the property list of current slave process */
126 xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
127 xbt_dict_cursor_t cursor=NULL;
130 /* Print the properties of the process */
131 xbt_dict_foreach(props,cursor,key,data) {
132 INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
135 /* Try to get a property that does not exist */
136 char *noexist=xbt_strdup("UnknownProcessProp");
137 const char *value = MSG_process_get_property_value(MSG_process_self(),noexist);
139 INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
141 INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
143 MSG_task_execute(task);
144 MSG_task_destroy(task);
146 INFO0("Hey ?! What's up ? ");
147 xbt_assert0(0,"Unexpected behavior");
153 /** Forwarder function */
154 int forwarder(int argc, char *argv[])
160 { /* Process organisation */
161 slaves_count = argc - 1;
162 slaves = calloc(slaves_count, sizeof(m_host_t));
164 for (i = 1; i < argc; i++) {
165 slaves[i-1] = MSG_get_host_by_name(argv[i]);
166 if(slaves[i-1]==NULL) {
167 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
175 m_task_t task = NULL;
177 a = MSG_task_get(&(task), PORT_22);
179 INFO1("Received \"%s\" ", MSG_task_get_name(task));
180 if(MSG_task_get_data(task)==FINALIZE) {
181 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
182 for (i = 0; i < slaves_count; i++)
183 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
185 MSG_task_destroy(task);
188 MSG_task_put(task, slaves[i % slaves_count],
192 INFO0("Hey ?! What's up ? ");
193 xbt_assert0(0,"Unexpected behavior");
197 } /* end_of_forwarder */
200 MSG_error_t test_all(const char *platform_file,
201 const char *application_file)
203 MSG_error_t res = MSG_OK;
205 /* MSG_config("surf_workstation_model","KCCFLN05"); */
206 { /* Simulation setting */
207 MSG_set_channel_number(MAX_CHANNEL);
208 MSG_paje_output("msg_test.trace");
209 MSG_create_environment(platform_file);
211 { /* Application deployment */
212 MSG_function_register("master", master);
213 MSG_function_register("slave", slave);
214 MSG_function_register("forwarder", forwarder);
215 MSG_launch_application(application_file);
219 INFO1("Simulation time %g",MSG_get_clock());
221 } /* end_of_test_all */
225 int main(int argc, char *argv[])
227 MSG_error_t res = MSG_OK;
229 MSG_global_init(&argc,argv);
231 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
232 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
235 res = test_all(argv[1],argv[2]);