Logo AND Algorithmique Numérique Distribuée

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