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");
12 /** @addtogroup MSG_examples
14 * @section MSG_ex_models Models-related examples
16 * @subsection MSG_ex_PLS Packet level simulators
18 * These examples demonstrate how to use the bindings to classicalPacket-Level Simulators (PLS), as explained in
19 * \ref pls. The most interesting is probably not the C files since they are unchanged from the other simulations,
20 * but the associated files, such as the platform files to see how to declare a platform to be used with the PLS
21 * bindings of SimGrid and the tesh files to see how to actually start a simulation in these settings.
23 * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models.
26 int timer_start; //set as 1 in the master process
28 //keep a pointer to all surf running tasks.
31 double start_time, end_time, elapsed_time;
32 double gl_data_size[NTASKS];
33 msg_task_t gl_task_array[NTASKS];
34 const char *slavenames[NTASKS];
35 const char *masternames[NTASKS];
36 int gl_task_array_id = 0;
37 int count_finished = 0;
39 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
41 static int master(int argc, char *argv[])
45 xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
47 XBT_DEBUG ("Master started");
50 double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
53 char *slavename = argv[2];
54 int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s"); //unique id to control statistics
55 char *id_alias = bprintf("flow_%d", id);
56 slavenames[id] = slavename;
57 TRACE_category(id_alias);
59 masternames[id] = MSG_host_get_name(MSG_host_self());
61 { /* Task creation. */
62 todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
63 MSG_task_set_category(todo, id_alias);
64 //keep track of running tasks
65 gl_task_array[id] = todo;
66 gl_data_size[id] = task_comm_size;
69 MSG_host_by_name(slavename);
74 /* time measurement */
75 sprintf(id_alias, "%d", id);
76 start_time = MSG_get_clock();
77 MSG_task_send(todo, id_alias);
78 end_time = MSG_get_clock();
80 XBT_DEBUG ("Finished");
85 static int timer(int argc, char *argv[])
90 xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
92 sscanf(argv[1], "%lf", &first_sleep);
93 sscanf(argv[2], "%lf", &sleep_time);
95 XBT_DEBUG ("Timer started");
98 MSG_process_sleep(first_sleep);
102 XBT_DEBUG ("Get sleep");
103 MSG_process_sleep(sleep_time);
104 } while(timer_start);
106 XBT_DEBUG ("Finished");
110 static int slave(int argc, char *argv[])
112 msg_task_t task = NULL;
115 xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
117 XBT_DEBUG ("Slave started");
119 int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
120 sprintf(id_alias, "%d", id);
122 msg_error_t a = MSG_task_receive(&(task), id_alias);
125 if(count_finished == 0){
130 XBT_INFO("Hey?! What's up?");
131 xbt_die("Unexpected behavior.");
134 elapsed_time = MSG_get_clock() - start_time;
136 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, MSG_task_get_bytes_amount(task), masternames[id],
138 // MSG_task_execute(task);
139 MSG_task_destroy(task);
141 XBT_DEBUG ("Finished");
145 int main(int argc, char *argv[])
147 msg_error_t res = MSG_OK;
150 MSG_init(&argc, argv);
151 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
152 "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
154 MSG_create_environment(argv[1]);
155 TRACE_declare_mark("endmark");
157 MSG_function_register("master", master);
158 MSG_function_register("slave", slave);
159 MSG_function_register("timer", timer);
161 MSG_launch_application(argv[2]);
165 return res != MSG_OK;