Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50c7cd226a50d8bdd373e7b8441c4f4774dbc1fc
[simgrid.git] / src / mc / mc_request.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc_private.h"
7
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
9                                 "Logging specific to MC (request)");
10
11 static char* pointer_to_string(void* pointer);
12 static char* buff_size_to_string(size_t size);
13
14 int MC_request_depend(smx_simcall_t r1, smx_simcall_t r2) {
15   if(mc_reduce_kind == e_mc_reduce_none)
16     return TRUE;
17
18
19   if (r1->issuer == r2->issuer)
20     return FALSE;
21
22   if(r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
23     return FALSE;
24
25   if(r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
26     return FALSE;
27
28   if(   (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
29         &&  r2->call == SIMCALL_COMM_WAIT){
30
31     if(r2->comm_wait.comm->comm.rdv == NULL)
32       return FALSE;
33
34     smx_rdv_t rdv = r1->call == SIMCALL_COMM_ISEND ? r1->comm_isend.rdv : r1->comm_irecv.rdv;
35
36     if(r2->comm_wait.comm->comm.rdv != rdv)
37       return FALSE;
38
39     if(r2->comm_wait.comm->comm.type == SIMIX_COMM_SEND && r1->call == SIMCALL_COMM_ISEND)
40       return FALSE;
41
42     if(r2->comm_wait.comm->comm.type == SIMIX_COMM_RECEIVE && r1->call == SIMCALL_COMM_IRECV)
43       return FALSE;
44   }
45
46   if(   (r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
47         &&  r1->call == SIMCALL_COMM_WAIT){
48
49     if(r1->comm_wait.comm->comm.rdv != NULL)
50       return FALSE;
51
52     smx_rdv_t rdv = r2->call == SIMCALL_COMM_ISEND ? r2->comm_isend.rdv : r2->comm_irecv.rdv;
53
54     if(r1->comm_wait.comm->comm.rdv != rdv)
55       return FALSE;
56
57     if(r1->comm_wait.comm->comm.type == SIMIX_COMM_SEND && r2->call == SIMCALL_COMM_ISEND)
58       return FALSE;
59
60     if(r1->comm_wait.comm->comm.type == SIMIX_COMM_RECEIVE && r2->call == SIMCALL_COMM_IRECV)
61       return FALSE;
62   }
63
64   /* FIXME: the following rule assumes that the result of the
65    * isend/irecv call is not stored in a buffer used in the
66    * test call. */
67   if(   (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
68         &&  r2->call == SIMCALL_COMM_TEST)
69     return FALSE;
70
71   /* FIXME: the following rule assumes that the result of the
72    * isend/irecv call is not stored in a buffer used in the
73    * test call.*/
74   if(   (r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
75         && r1->call == SIMCALL_COMM_TEST)
76     return FALSE;
77
78   if(r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_ISEND
79      && r1->comm_isend.rdv != r2->comm_isend.rdv)
80     return FALSE;
81
82   if(r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_IRECV
83      && r1->comm_irecv.rdv != r2->comm_irecv.rdv)
84     return FALSE;
85
86   if(r1->call == SIMCALL_COMM_WAIT && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
87      && (r1->comm_wait.comm->comm.src_proc == NULL
88          || r1->comm_wait.comm->comm.dst_proc == NULL))
89     return FALSE;
90
91   if(r2->call == SIMCALL_COMM_WAIT && (r1->call == SIMCALL_COMM_WAIT || r1->call == SIMCALL_COMM_TEST)
92      && (r2->comm_wait.comm->comm.src_proc == NULL
93          || r2->comm_wait.comm->comm.dst_proc == NULL))
94     return FALSE;
95
96   if(r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
97      && r1->comm_wait.comm->comm.src_buff == r2->comm_wait.comm->comm.src_buff
98      && r1->comm_wait.comm->comm.dst_buff == r2->comm_wait.comm->comm.dst_buff)
99     return FALSE;
100
101   if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
102       && r1->comm_wait.comm->comm.src_buff != NULL
103       && r1->comm_wait.comm->comm.dst_buff != NULL
104       && r2->comm_wait.comm->comm.src_buff != NULL
105       && r2->comm_wait.comm->comm.dst_buff != NULL
106       && r1->comm_wait.comm->comm.dst_buff != r2->comm_wait.comm->comm.src_buff
107       && r1->comm_wait.comm->comm.dst_buff != r2->comm_wait.comm->comm.dst_buff
108       && r2->comm_wait.comm->comm.dst_buff != r1->comm_wait.comm->comm.src_buff)
109     return FALSE;
110
111   if(r1->call == SIMCALL_COMM_TEST &&
112      (r1->comm_test.comm == NULL
113       || r1->comm_test.comm->comm.src_buff == NULL
114       || r1->comm_test.comm->comm.dst_buff == NULL))
115     return FALSE;
116
117   if(r2->call == SIMCALL_COMM_TEST &&
118      (r2->comm_test.comm == NULL
119       || r2->comm_test.comm->comm.src_buff == NULL
120       || r2->comm_test.comm->comm.dst_buff == NULL))
121     return FALSE;
122
123   if(r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
124      && r1->comm_test.comm->comm.src_buff == r2->comm_wait.comm->comm.src_buff
125      && r1->comm_test.comm->comm.dst_buff == r2->comm_wait.comm->comm.dst_buff)
126     return FALSE;
127
128   if(r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
129      && r1->comm_wait.comm->comm.src_buff == r2->comm_test.comm->comm.src_buff
130      && r1->comm_wait.comm->comm.dst_buff == r2->comm_test.comm->comm.dst_buff)
131     return FALSE;
132
133   if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
134       && r1->comm_wait.comm->comm.src_buff != NULL
135       && r1->comm_wait.comm->comm.dst_buff != NULL
136       && r2->comm_test.comm->comm.src_buff != NULL
137       && r2->comm_test.comm->comm.dst_buff != NULL
138       && r1->comm_wait.comm->comm.dst_buff != r2->comm_test.comm->comm.src_buff
139       && r1->comm_wait.comm->comm.dst_buff != r2->comm_test.comm->comm.dst_buff
140       && r2->comm_test.comm->comm.dst_buff != r1->comm_wait.comm->comm.src_buff)
141     return FALSE;
142
143   if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
144       && r1->comm_test.comm->comm.src_buff != NULL
145       && r1->comm_test.comm->comm.dst_buff != NULL
146       && r2->comm_wait.comm->comm.src_buff != NULL
147       && r2->comm_wait.comm->comm.dst_buff != NULL
148       && r1->comm_test.comm->comm.dst_buff != r2->comm_wait.comm->comm.src_buff
149       && r1->comm_test.comm->comm.dst_buff != r2->comm_wait.comm->comm.dst_buff
150       && r2->comm_wait.comm->comm.dst_buff != r1->comm_test.comm->comm.src_buff)
151     return FALSE;
152
153
154   return TRUE;
155 }
156
157 static char* pointer_to_string(void* pointer) {
158
159   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
160     return bprintf("%p", pointer);
161
162   return xbt_strdup("(verbose only)");
163 }
164
165 static char* buff_size_to_string(size_t buff_size) {
166
167   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
168     return bprintf("%zu", buff_size);
169
170   return xbt_strdup("(verbose only)");
171 }
172
173
174 char *MC_request_to_string(smx_simcall_t req, int value)
175 {
176   char *type = NULL, *args = NULL, *str = NULL, *p = NULL, *bs = NULL;
177   smx_action_t act = NULL;
178   size_t size = 0;
179
180   switch(req->call){
181   case SIMCALL_COMM_ISEND:
182     type = xbt_strdup("iSend");
183     p = pointer_to_string(req->comm_isend.src_buff);
184     bs = buff_size_to_string(req->comm_isend.src_buff_size);
185     args = bprintf("src=%s, buff=%s, size=%s", req->issuer->name, p, bs);
186     break;
187   case SIMCALL_COMM_IRECV:
188     size = req->comm_irecv.dst_buff_size ? *req->comm_irecv.dst_buff_size : 0;
189     type = xbt_strdup("iRecv");
190     p = pointer_to_string(req->comm_irecv.dst_buff); 
191     bs = buff_size_to_string(size);
192     args = bprintf("dst=%s, buff=%s, size=%s", req->issuer->name, p, bs);
193     break;
194   case SIMCALL_COMM_WAIT:
195     act = req->comm_wait.comm;
196     if(value == -1){
197       type = xbt_strdup("WaitTimeout");
198       p = pointer_to_string(act);
199       args = bprintf("comm=%p", p);
200     }else{
201       type = xbt_strdup("Wait");
202       p = pointer_to_string(act);
203       args  = bprintf("comm=%s [(%lu)%s -> (%lu)%s]", p,
204                       act->comm.src_proc ? act->comm.src_proc->pid : 0,
205                       act->comm.src_proc ? act->comm.src_proc->name : "",
206                       act->comm.dst_proc ? act->comm.dst_proc->pid : 0,
207                       act->comm.dst_proc ? act->comm.dst_proc->name : "");
208     }
209     break;
210   case SIMCALL_COMM_TEST:
211     act = req->comm_test.comm;
212     if(act->comm.src_proc == NULL || act->comm.dst_proc == NULL){
213       type = xbt_strdup("Test FALSE");
214       p = pointer_to_string(act);
215       args = bprintf("comm=%s", p);
216     }else{
217       type = xbt_strdup("Test TRUE");
218       p = pointer_to_string(act);
219       args  = bprintf("comm=%s [(%lu)%s -> (%lu)%s]", p,
220                       act->comm.src_proc->pid, act->comm.src_proc->name,
221                       act->comm.dst_proc->pid, act->comm.dst_proc->name);
222     }
223     break;
224
225   case SIMCALL_COMM_WAITANY:
226     type = xbt_strdup("WaitAny");
227     p = pointer_to_string(xbt_dynar_get_as(req->comm_waitany.comms, value, smx_action_t));
228     args = bprintf("comm=%s (%d of %lu)", p,
229                    value+1, xbt_dynar_length(req->comm_waitany.comms));
230     break;
231
232   case SIMCALL_COMM_TESTANY:
233     if(value == -1){
234       type = xbt_strdup("TestAny FALSE");
235       args = xbt_strdup("-");
236     }else{
237       type = xbt_strdup("TestAny");
238       args = bprintf("(%d of %lu)", value+1, xbt_dynar_length(req->comm_testany.comms));
239     }
240     break;
241
242   case SIMCALL_MC_SNAPSHOT:
243     type = xbt_strdup("MC_SNAPSHOT");
244     args = '\0';
245     break;
246
247   case SIMCALL_MC_COMPARE_SNAPSHOTS:
248     type = xbt_strdup("MC_COMPARE_SNAPSHOTS");
249     args = '\0';
250     break;
251
252   default:
253     THROW_UNIMPLEMENTED;
254   }
255
256   str = bprintf("[(%lu)%s] %s (%s)", req->issuer->pid ,req->issuer->name, type, args);
257   xbt_free(type);
258   xbt_free(args);
259   xbt_free(p);
260   xbt_free(bs);
261   return str;
262 }
263
264 unsigned int MC_request_testany_fail(smx_simcall_t req)
265 {
266   unsigned int cursor;
267   smx_action_t action;
268
269   xbt_dynar_foreach(req->comm_testany.comms, cursor, action){
270     if(action->comm.src_proc && action->comm.dst_proc)
271       return FALSE;
272   }
273
274   return TRUE;
275 }
276
277 int MC_request_is_visible(smx_simcall_t req)
278 {
279   return req->call == SIMCALL_COMM_ISEND
280     || req->call == SIMCALL_COMM_IRECV
281     || req->call == SIMCALL_COMM_WAIT
282     || req->call == SIMCALL_COMM_WAITANY
283     || req->call == SIMCALL_COMM_TEST
284     || req->call == SIMCALL_COMM_TESTANY
285     || req->call == SIMCALL_MC_SNAPSHOT
286     || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS;
287 }
288
289 int MC_request_is_enabled(smx_simcall_t req)
290 {
291   unsigned int index = 0;
292   smx_action_t act;
293
294   switch (req->call) {
295
296   case SIMCALL_COMM_WAIT:
297     /* FIXME: check also that src and dst processes are not suspended */
298
299     /* If it has a timeout it will be always be enabled, because even if the
300      * communication is not ready, it can timeout and won't block.
301      * On the other hand if it hasn't a timeout, check if the comm is ready.*/
302     if(req->comm_wait.timeout >= 0){
303       return TRUE;
304     }else{
305       act = req->comm_wait.comm;
306       return (act->comm.src_proc && act->comm.dst_proc);
307     }
308     break;
309
310   case SIMCALL_COMM_WAITANY:
311     /* Check if it has at least one communication ready */
312     xbt_dynar_foreach(req->comm_waitany.comms, index, act) {
313       if (act->comm.src_proc && act->comm.dst_proc){
314         return TRUE;
315       }
316     }
317     return FALSE;
318     break;
319
320   default:
321     /* The rest of the request are always enabled */
322     return TRUE;
323   }
324 }
325
326 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
327 {
328   smx_action_t act;
329
330   switch (req->call) {
331
332   case SIMCALL_COMM_WAIT:
333     /* FIXME: check also that src and dst processes are not suspended */
334     act = req->comm_wait.comm;
335     return (act->comm.src_proc && act->comm.dst_proc);
336     break;
337
338   case SIMCALL_COMM_WAITANY:
339     act = xbt_dynar_get_as(req->comm_waitany.comms, idx, smx_action_t);
340     return (act->comm.src_proc && act->comm.dst_proc);
341     break;
342
343   case SIMCALL_COMM_TESTANY:
344     act = xbt_dynar_get_as(req->comm_testany.comms, idx, smx_action_t);
345     return (act->comm.src_proc && act->comm.dst_proc);
346     break;
347
348   default:
349     return TRUE;
350   }
351 }
352
353 int MC_process_is_enabled(smx_process_t process)
354 {
355   if (process->simcall.call != SIMCALL_NONE && MC_request_is_enabled(&process->simcall))
356     return TRUE;
357
358   return FALSE;
359 }