Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
30f4d9bed09838f241bd845a9c2c9b64f80603ac
[simgrid.git] / src / instr / instr_msg_task.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 "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_msg, instr, "MSG");
12
13 static xbt_dict_t task_containers = NULL;
14
15 static char *TRACE_task_alias_container(m_task_t task, m_process_t process,
16                                  m_host_t host, char *output, int len)
17 {
18   if (output) {
19     snprintf(output, len, "%p-%lld-%p-%p", task, task->counter, process,
20              host);
21     return output;
22   } else {
23     return NULL;
24   }
25 }
26
27 static char *TRACE_host_container(m_host_t host, char *output, int len)
28 {
29   if (output) {
30     snprintf(output, len, "%s", MSG_host_get_name(host));
31     return output;
32   } else {
33     return NULL;
34   }
35 }
36
37 char *TRACE_task_container(m_task_t task, char *output, int len)
38 {
39   if (output) {
40     snprintf(output, len, "%p-%lld", task, task->counter);
41     return output;
42   } else {
43     return NULL;
44   }
45 }
46
47 void TRACE_msg_task_alloc(void)
48 {
49   task_containers = xbt_dict_new();
50 }
51
52 void TRACE_msg_task_release(void)
53 {
54   xbt_dict_free(&task_containers);
55 }
56
57 static void TRACE_task_location(m_task_t task)
58 {
59   char container[200];
60   char name[200], alias[200];
61   char *val_one = NULL;
62   m_process_t process = NULL;
63   m_host_t host = NULL;
64   if (!TRACE_msg_task_is_enabled())
65     return;
66   process = MSG_process_self();
67   host = MSG_process_get_host(process);
68
69   //tasks are grouped by host
70   TRACE_host_container(host, container, 200);
71   TRACE_task_container(task, name, 200);
72   TRACE_task_alias_container(task, process, host, alias, 200);
73   //check if task container is already created
74   if (!xbt_dict_get_or_null(task_containers, alias)) {
75     pajeCreateContainer(MSG_get_clock(), alias, "TASK", container, name);
76     pajeSetState(MSG_get_clock(), "category", alias, task->category);
77     val_one = xbt_strdup("1");
78     xbt_dict_set(task_containers, alias, val_one, xbt_free);
79   }
80 }
81
82 static void TRACE_task_location_present(m_task_t task)
83 {
84   char alias[200];
85   m_process_t process = NULL;
86   m_host_t host = NULL;
87   if (!TRACE_msg_task_is_enabled())
88     return;
89   //updating presence state of this task location
90   process = MSG_process_self();
91   host = MSG_process_get_host(process);
92
93   TRACE_task_alias_container(task, process, host, alias, 200);
94   pajePushState(MSG_get_clock(), "presence", alias, "presence");
95 }
96
97 static void TRACE_task_location_not_present(m_task_t task)
98 {
99   char alias[200];
100   m_process_t process = NULL;
101   m_host_t host = NULL;
102   if (!TRACE_msg_task_is_enabled())
103     return;
104   //updating presence state of this task location
105   process = MSG_process_self();
106   host = MSG_process_get_host(process);
107
108   TRACE_task_alias_container(task, process, host, alias, 200);
109   pajePopState(MSG_get_clock(), "presence", alias);
110 }
111
112 /*
113  * TRACE_msg_set_task_category: tracing interface function
114  */
115 void TRACE_msg_set_task_category(m_task_t task, const char *category)
116 {
117   char name[200];
118   if (!TRACE_is_active())
119     return;
120
121   //set task category
122   task->category = xbt_new(char, strlen(category) + 1);
123   strncpy(task->category, category, strlen(category) + 1);
124
125   //tracing task location based on host
126   TRACE_task_location(task);
127   TRACE_task_location_present(task);
128
129   TRACE_task_container(task, name, 200);
130   //create container of type "task" to indicate behavior
131   if (TRACE_msg_task_is_enabled())
132     pajeCreateContainer(MSG_get_clock(), name, "task", category, name);
133   if (TRACE_msg_task_is_enabled())
134     pajePushState(MSG_get_clock(), "task-state", name, "created");
135 }
136
137 /* MSG_task_create related function*/
138 void TRACE_msg_task_create(m_task_t task)
139 {
140   static long long counter = 0;
141   task->counter = counter++;
142   task->category = NULL;
143 }
144
145 /* MSG_task_execute related functions */
146 void TRACE_msg_task_execute_start(m_task_t task)
147 {
148   char name[200];
149   if (!TRACE_is_active())
150     return;
151
152   if (!task->category)
153     return;
154
155   TRACE_task_container(task, name, 200);
156   if (TRACE_msg_task_is_enabled())
157     pajePushState(MSG_get_clock(), "task-state", name, "execute");
158
159   TRACE_msg_category_set(SIMIX_process_self(), task);
160 }
161
162 void TRACE_msg_task_execute_end(m_task_t task)
163 {
164   char name[200];
165   if (!TRACE_is_active())
166     return;
167
168   if (!task->category)
169     return;
170
171   TRACE_task_container(task, name, 200);
172   if (TRACE_msg_task_is_enabled())
173     pajePopState(MSG_get_clock(), "task-state", name);
174
175   TRACE_category_unset(SIMIX_process_self());
176 }
177
178 /* MSG_task_destroy related functions */
179 void TRACE_msg_task_destroy(m_task_t task)
180 {
181   char name[200];
182   if (!TRACE_is_active())
183     return;
184
185   if (!task->category)
186     return;
187
188   TRACE_task_container(task, name, 200);
189   if (TRACE_msg_task_is_enabled())
190     pajeDestroyContainer(MSG_get_clock(), "task", name);
191
192   //finish the location of this task
193   TRACE_task_location_not_present(task);
194
195   //free category
196   xbt_free(task->category);
197   return;
198 }
199
200 /* MSG_task_get related functions */
201 void TRACE_msg_task_get_start(void)
202 {
203   if (!TRACE_is_active())
204     return;
205 }
206
207 void TRACE_msg_task_get_end(double start_time, m_task_t task)
208 {
209   char name[200];
210   if (!TRACE_is_active())
211     return;
212
213   if (!task->category)
214     return;
215
216   TRACE_task_container(task, name, 200);
217   if (TRACE_msg_task_is_enabled())
218     pajePopState(MSG_get_clock(), "task-state", name);
219
220   TRACE_msg_volume_finish(task);
221
222   TRACE_task_location(task);
223   TRACE_task_location_present(task);
224 }
225
226 /* MSG_task_put related functions */
227 int TRACE_msg_task_put_start(m_task_t task)
228 {
229   char name[200];
230   if (!TRACE_is_active())
231     return 0;
232
233   if (!task->category)
234     return 0;
235
236   TRACE_task_container(task, name, 200);
237   if (TRACE_msg_task_is_enabled())
238     pajePopState(MSG_get_clock(), "task-state", name);
239   if (TRACE_msg_task_is_enabled())
240     pajePushState(MSG_get_clock(), "task-state", name, "communicate");
241
242   TRACE_msg_volume_start(task);
243
244   //trace task location grouped by host
245   TRACE_task_location_not_present(task);
246
247   //set current category
248   TRACE_msg_category_set(SIMIX_process_self(), task);
249   return 1;
250 }
251
252 void TRACE_msg_task_put_end(void)
253 {
254   if (!TRACE_is_active())
255     return;
256
257   TRACE_category_unset(SIMIX_process_self());
258 }
259
260 #endif /* HAVE_TRACING */