Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] cosmetics on user variables tracing, simpler interface
[simgrid.git] / examples / msg / tracing / ms.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 /** Emitter function  */
23 int master(int argc, char *argv[])
24 {
25   long number_of_tasks = atol(argv[1]);
26   double task_comp_size = atof(argv[2]);
27   double task_comm_size = atof(argv[3]);
28   long slaves_count = atol(argv[4]);
29
30   //setting the variable "is_master" (previously declared) to value 1
31   TRACE_host_variable_set(MSG_host_self()->name, "is_master", 1);
32
33   TRACE_mark("msmark", "start_send_tasks");
34   int i;
35   for (i = 0; i < number_of_tasks; i++) {
36     m_task_t task = NULL;
37     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
38
39     //setting the variable "task_creation" to value i
40     TRACE_host_variable_set(MSG_host_self()->name, "task_creation", i);
41
42     //setting the category of task to "compute"
43     //the category of a task must be defined before it is sent or executed
44     TRACE_msg_set_task_category(task, "compute");
45     MSG_task_send(task, "master_mailbox");
46   }
47   TRACE_mark("msmark", "finish_send_tasks");
48
49   for (i = 0; i < slaves_count; i++) {
50     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
51     TRACE_msg_set_task_category(finalize, "finalize");
52     MSG_task_send(finalize, "master_mailbox");
53   }
54
55   return 0;
56 }
57
58 /** Receiver function  */
59 int slave(int argc, char *argv[])
60 {
61   m_task_t task = NULL;
62   int res;
63
64   TRACE_host_variable_set(MSG_host_self()->name, "is_slave", 1);
65   while (1) {
66     res = MSG_task_receive(&(task), "master_mailbox");
67
68     if (!strcmp(MSG_task_get_name(task), "finalize")) {
69       MSG_task_destroy(task);
70       break;
71     }
72     //adding the value returned by MSG_task_get_compute_duration(task)
73     //to the variable "task_computation"
74     TRACE_host_variable_add(MSG_host_self()->name,
75                             "task_computation",
76                             MSG_task_get_compute_duration(task));
77     MSG_task_execute(task);
78     MSG_task_destroy(task);
79     task = NULL;
80   }
81   return 0;
82 }
83
84 /** Test function */
85 MSG_error_t test_all(const char *platform_file,
86                      const char *application_file)
87 {
88   MSG_error_t res = MSG_OK;
89
90   {                             /*  Simulation setting */
91     MSG_set_channel_number(0);
92     MSG_create_environment(platform_file);
93   }
94   {
95     /* declaring tracing categories */
96
97     //declaring user variables
98     TRACE_host_variable_declare("is_slave");
99     TRACE_host_variable_declare("is_master");
100     TRACE_host_variable_declare("task_creation");
101     TRACE_host_variable_declare("task_computation");
102
103     //declaring user markers
104     TRACE_declare_mark("msmark");
105
106     //declaring user categories with RGB colors (values from 0 to 1)
107     TRACE_category_with_color ("compute", "1 0 0");  //compute is red
108     TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
109     //categories without user-defined colors receive
110     //random colors generated by the tracing system
111     TRACE_category ("request");
112     TRACE_category_with_color ("report", NULL);
113   }
114   {                             /*   Application deployment */
115     MSG_function_register("master", master);
116     MSG_function_register("slave", slave);
117     MSG_launch_application(application_file);
118   }
119   res = MSG_main();
120
121   XBT_INFO("Simulation time %g", MSG_get_clock());
122   return res;
123 }
124
125
126 /** Main function */
127 int main(int argc, char *argv[])
128 {
129   MSG_error_t res = MSG_OK;
130
131   MSG_global_init(&argc, argv);
132   if (argc < 3) {
133     printf("Usage: %s platform_file deployment_file\n", argv[0]);
134     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
135     exit(1);
136   }
137
138   res = test_all(argv[1], argv[2]);
139   MSG_clean();
140
141   if (res == MSG_OK)
142     return 0;
143   else
144     return 1;
145 }                               /* end_of_main */