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[])
59 double task_comm_size = 0;
62 xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
64 XBT_DEBUG ("Master started");
68 read = sscanf(argv[1], "%lg", &task_comm_size);
69 xbt_assert(read, "Invalid argument %s\n", argv[1]);
72 char *slavename = argv[2];
73 int id = atoi(argv[3]); //unique id to control statistics
74 char *id_alias = bprintf("flow_%d", id);
75 slavenames[id] = slavename;
76 TRACE_category(id_alias);
78 masternames[id] = MSG_host_get_name(MSG_host_self());
80 { /* Task creation. */
81 todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
82 MSG_task_set_category(todo, id_alias);
83 //keep track of running tasks
84 gl_task_array[id] = todo;
85 gl_data_size[id] = task_comm_size;
88 { /* Process organization */
89 MSG_get_host_by_name(slavename);
95 /* time measurement */
96 sprintf(id_alias, "%d", id);
97 start_time = MSG_get_clock();
98 MSG_task_send(todo, id_alias);
99 end_time = MSG_get_clock();
101 XBT_DEBUG ("Finished");
104 } /* end_of_master */
107 /** Timer function */
108 int timer(int argc, char *argv[])
113 xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
115 sscanf(argv[1], "%lf", &first_sleep);
116 sscanf(argv[2], "%lf", &sleep_time);
118 XBT_DEBUG ("Timer started");
121 MSG_process_sleep(first_sleep);
125 XBT_DEBUG ("Get sleep");
126 MSG_process_sleep(sleep_time);
127 } while(timer_start);
129 XBT_DEBUG ("Finished");
133 /** Receiver function */
134 int slave(int argc, char *argv[])
137 msg_task_t task = NULL;
142 xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
144 XBT_DEBUG ("Slave started");
147 sprintf(id_alias, "%d", id);
149 a = MSG_task_receive(&(task), id_alias);
152 if(count_finished == 0){
159 XBT_INFO("Hey?! What's up?");
160 xbt_die("Unexpected behavior.");
163 elapsed_time = MSG_get_clock() - start_time;
165 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
167 MSG_task_get_bytes_amount(task),
170 // MSG_task_execute(task);
171 MSG_task_destroy(task);
173 XBT_DEBUG ("Finished");
178 msg_error_t test_all(const char *platform_file,
179 const char *application_file)
181 msg_error_t res = MSG_OK;
183 { /* Simulation setting */
184 MSG_create_environment(platform_file);
187 TRACE_declare_mark("endmark");
189 { /* Application deployment */
190 MSG_function_register("master", master);
191 MSG_function_register("slave", slave);
192 MSG_function_register("timer", timer);
194 MSG_launch_application(application_file);
198 } /* end_of_test_all */
201 int main(int argc, char *argv[])
203 msg_error_t res = MSG_OK;
206 MSG_init(&argc, argv);
207 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
208 "\tExample: %s platform.xml deployment.xml\n",
212 res = test_all(argv[1], argv[2]);
214 return res != MSG_OK;