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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * @section MSG_ex_models Models-related examples
15 * @subsection MSG_ex_PLS Packet level simulators
17 * These examples demonstrate how to use the bindings to classicalPacket-Level Simulators (PLS), as explained in
18 * \ref pls. The most interesting is probably not the C files since they are unchanged from the other simulations,
19 * but the associated files, such as the platform files to see how to declare a platform to be used with the PLS
20 * bindings of SimGrid and the tesh files to see how to actually start a simulation in these settings.
22 * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models.
25 int timer_start; //set as 1 in the master process
27 //keep a pointer to all surf running tasks.
30 double start_time, end_time, elapsed_time;
31 double gl_data_size[NTASKS];
32 msg_task_t gl_task_array[NTASKS];
33 const char *slavenames[NTASKS];
34 const char *masternames[NTASKS];
35 int gl_task_array_id = 0;
36 int count_finished = 0;
38 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
40 static int master(int argc, char *argv[])
44 xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
46 XBT_DEBUG ("Master started");
49 double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
52 char *slavename = argv[2];
53 int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s"); //unique id to control statistics
54 char *id_alias = bprintf("flow_%d", id);
55 slavenames[id] = slavename;
56 TRACE_category(id_alias);
58 masternames[id] = MSG_host_get_name(MSG_host_self());
60 { /* Task creation. */
61 todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
62 MSG_task_set_category(todo, id_alias);
63 //keep track of running tasks
64 gl_task_array[id] = todo;
65 gl_data_size[id] = task_comm_size;
68 MSG_host_by_name(slavename);
73 /* time measurement */
74 sprintf(id_alias, "%d", id);
75 start_time = MSG_get_clock();
76 MSG_task_send(todo, id_alias);
77 end_time = MSG_get_clock();
79 XBT_DEBUG ("Finished");
84 static int timer(int argc, char *argv[])
89 xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
91 sscanf(argv[1], "%lf", &first_sleep);
92 sscanf(argv[2], "%lf", &sleep_time);
94 XBT_DEBUG ("Timer started");
97 MSG_process_sleep(first_sleep);
101 XBT_DEBUG ("Get sleep");
102 MSG_process_sleep(sleep_time);
103 } while(timer_start);
105 XBT_DEBUG ("Finished");
109 static int slave(int argc, char *argv[])
111 msg_task_t task = NULL;
114 xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
116 XBT_DEBUG ("Slave started");
118 int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
119 sprintf(id_alias, "%d", id);
121 msg_error_t a = MSG_task_receive(&(task), id_alias);
124 if(count_finished == 0){
128 xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
130 elapsed_time = MSG_get_clock() - start_time;
132 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, MSG_task_get_bytes_amount(task), masternames[id],
134 // MSG_task_execute(task);
135 MSG_task_destroy(task);
137 XBT_DEBUG ("Finished");
141 int main(int argc, char *argv[])
143 msg_error_t res = MSG_OK;
146 MSG_init(&argc, argv);
147 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
148 "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
150 MSG_create_environment(argv[1]);
151 TRACE_declare_mark("endmark");
153 MSG_function_register("master", master);
154 MSG_function_register("slave", slave);
155 MSG_function_register("timer", timer);
157 MSG_launch_application(argv[2]);
161 return res != MSG_OK;