1 /* Copyright (c) 2007-2015. 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. */
9 #include "simgrid/msg.h"
11 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
17 /** @addtogroup MSG_examples
19 * @section MSG_ex_models Models-related examples
21 * @subsection MSG_ex_PLS Packet level simulators
23 * These examples demonstrate how to use the bindings to classical
24 * Packet-Level Simulators (PLS), as explained in \ref pls. The most
25 * interesting is probably not the C files since they are unchanged
26 * from the other simulations, but the associated files, such as the
27 * platform files to see how to declare a platform to be used with
28 * the PLS bindings of SimGrid and the tesh files to see how to
29 * actually start a simulation in these settings.
31 * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models.
35 int master(int argc, char *argv[]);
36 int slave(int argc, char *argv[]);
37 int timer(int argc, char *argv[]);
38 msg_error_t test_all(const char *platform_file,
39 const char *application_file);
41 int timer_start; //set as 1 in the master process
43 //keep a pointer to all surf running tasks.
46 double start_time, end_time, elapsed_time;
47 double gl_data_size[NTASKS];
48 msg_task_t gl_task_array[NTASKS];
49 const char *slavenames[NTASKS];
50 const char *masternames[NTASKS];
51 int gl_task_array_id = 0;
52 int count_finished = 0;
54 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
57 int master(int argc, char *argv[])
61 xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
63 XBT_DEBUG ("Master started");
66 double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
69 char *slavename = argv[2];
70 int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s"); //unique id to control statistics
71 char *id_alias = bprintf("flow_%d", id);
72 slavenames[id] = slavename;
73 TRACE_category(id_alias);
75 masternames[id] = MSG_host_get_name(MSG_host_self());
77 { /* Task creation. */
78 todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
79 MSG_task_set_category(todo, id_alias);
80 //keep track of running tasks
81 gl_task_array[id] = todo;
82 gl_data_size[id] = task_comm_size;
85 { /* Process organization */
86 MSG_host_by_name(slavename);
92 /* time measurement */
93 sprintf(id_alias, "%d", id);
94 start_time = MSG_get_clock();
95 MSG_task_send(todo, id_alias);
96 end_time = MSG_get_clock();
98 XBT_DEBUG ("Finished");
101 } /* end_of_master */
104 /** Timer function */
105 int timer(int argc, char *argv[])
110 xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
112 sscanf(argv[1], "%lf", &first_sleep);
113 sscanf(argv[2], "%lf", &sleep_time);
115 XBT_DEBUG ("Timer started");
118 MSG_process_sleep(first_sleep);
122 XBT_DEBUG ("Get sleep");
123 MSG_process_sleep(sleep_time);
124 } while(timer_start);
126 XBT_DEBUG ("Finished");
130 /** Receiver function */
131 int slave(int argc, char *argv[])
134 msg_task_t task = NULL;
139 xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
141 XBT_DEBUG ("Slave started");
143 id = xbt_str_parse_int(argv[1], "Invalid id: %s");
144 sprintf(id_alias, "%d", id);
146 a = MSG_task_receive(&(task), id_alias);
149 if(count_finished == 0){
156 XBT_INFO("Hey?! What's up?");
157 xbt_die("Unexpected behavior.");
160 elapsed_time = MSG_get_clock() - start_time;
162 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
164 MSG_task_get_bytes_amount(task),
167 // MSG_task_execute(task);
168 MSG_task_destroy(task);
170 XBT_DEBUG ("Finished");
175 msg_error_t test_all(const char *platform_file,
176 const char *application_file)
178 msg_error_t res = MSG_OK;
180 { /* Simulation setting */
181 MSG_create_environment(platform_file);
184 TRACE_declare_mark("endmark");
186 { /* Application deployment */
187 MSG_function_register("master", master);
188 MSG_function_register("slave", slave);
189 MSG_function_register("timer", timer);
191 MSG_launch_application(application_file);
195 } /* end_of_test_all */
198 int main(int argc, char *argv[])
200 msg_error_t res = MSG_OK;
203 MSG_init(&argc, argv);
204 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
205 "\tExample: %s platform.xml deployment.xml\n",
209 res = test_all(argv[1], argv[2]);
211 return res != MSG_OK;