Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Read MPI_Request from MCed in mc_comm_determinism.c
[simgrid.git] / src / mc / mc_comm_determinism.c
1 /* Copyright (c) 2008-2014. 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 "mc_state.h"
8 #include "mc_comm_pattern.h"
9 #include "mc_request.h"
10 #include "mc_safety.h"
11 #include "mc_private.h"
12 #include "mc_record.h"
13 #include "mc_smx.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
16                                 "Logging specific to MC communication determinism detection");
17
18 /********** Global variables **********/
19
20 xbt_dynar_t initial_communications_pattern;
21 xbt_dynar_t incomplete_communications_pattern;
22
23 /********** Static functions ***********/
24
25 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
26   if(comm1->type != comm2->type)
27     return TYPE_DIFF;
28   if (strcmp(comm1->rdv, comm2->rdv) != 0)
29     return RDV_DIFF;
30   if (comm1->src_proc != comm2->src_proc)
31     return SRC_PROC_DIFF;
32   if (comm1->dst_proc != comm2->dst_proc)
33     return DST_PROC_DIFF;
34   if (comm1->tag != comm2->tag)
35     return TAG_DIFF;
36   if (comm1->data_size != comm2->data_size)
37     return DATA_SIZE_DIFF;
38   if(comm1->data == NULL && comm2->data == NULL)
39     return 0;
40   if(comm1->data != NULL && comm2->data !=NULL) {
41     if (!memcmp(comm1->data, comm2->data, comm1->data_size))
42       return 0;
43     return DATA_DIFF;
44   }else{
45     return DATA_DIFF;
46   }
47   return 0;
48 }
49
50 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
51   char *type, *res;
52
53   if(comm->type == SIMIX_COMM_SEND)
54     type = bprintf("The send communications pattern of the process %d is different!", process - 1);
55   else
56     type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
57
58   switch(diff) {
59   case TYPE_DIFF:
60     res = bprintf("%s Different type for communication #%d", type, cursor);
61     break;
62   case RDV_DIFF:
63     res = bprintf("%s Different rdv for communication #%d", type, cursor);
64     break;
65   case TAG_DIFF:
66     res = bprintf("%s Different tag for communication #%d", type, cursor);
67     break;
68   case SRC_PROC_DIFF:
69       res = bprintf("%s Different source for communication #%d", type, cursor);
70     break;
71   case DST_PROC_DIFF:
72       res = bprintf("%s Different destination for communication #%d", type, cursor);
73     break;
74   case DATA_SIZE_DIFF:
75     res = bprintf("%s\n Different data size for communication #%d", type, cursor);
76     break;
77   case DATA_DIFF:
78     res = bprintf("%s\n Different data for communication #%d", type, cursor);
79     break;
80   default:
81     res = NULL;
82     break;
83   }
84
85   return res;
86 }
87
88 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
89 {
90   smx_process_t src_proc = MC_smx_resolve_process(comm->comm.src_proc);
91   smx_process_t dst_proc = MC_smx_resolve_process(comm->comm.dst_proc);
92   comm_pattern->src_proc = src_proc->pid;
93   comm_pattern->dst_proc = dst_proc->pid;
94   comm_pattern->src_host = MC_smx_process_get_host_name(src_proc);
95   comm_pattern->dst_host = MC_smx_process_get_host_name(dst_proc);
96   if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
97     comm_pattern->data_size = *(comm->comm.dst_buff_size);
98     comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
99     MC_process_read_simple(&mc_model_checker->process,
100       comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
101   }
102 }
103
104 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
105
106   mc_list_comm_pattern_t list_comm_pattern = (mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, process, mc_list_comm_pattern_t);
107
108   if(!backtracking){
109     mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, list_comm_pattern->index_comm, mc_comm_pattern_t);
110     e_mc_comm_pattern_difference_t diff;
111     
112     if((diff = compare_comm_pattern(initial_comm, comm)) != NONE_DIFF){
113       if (comm->type == SIMIX_COMM_SEND){
114         initial_global_state->send_deterministic = 0;
115         if(initial_global_state->send_diff != NULL)
116           xbt_free(initial_global_state->send_diff);
117         initial_global_state->send_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
118       }else{
119         initial_global_state->recv_deterministic = 0;
120         if(initial_global_state->recv_diff != NULL)
121           xbt_free(initial_global_state->recv_diff);
122         initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
123       }
124       if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
125         XBT_INFO("*********************************************************");
126         XBT_INFO("***** Non-send-deterministic communications pattern *****");
127         XBT_INFO("*********************************************************");
128         XBT_INFO("%s", initial_global_state->send_diff);
129         xbt_free(initial_global_state->send_diff);
130         initial_global_state->send_diff = NULL;
131         MC_print_statistics(mc_stats);
132         xbt_abort(); 
133       }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
134         XBT_INFO("****************************************************");
135         XBT_INFO("***** Non-deterministic communications pattern *****");
136         XBT_INFO("****************************************************");
137         XBT_INFO("%s", initial_global_state->send_diff);
138         XBT_INFO("%s", initial_global_state->recv_diff);
139         xbt_free(initial_global_state->send_diff);
140         initial_global_state->send_diff = NULL;
141         xbt_free(initial_global_state->recv_diff);
142         initial_global_state->recv_diff = NULL;
143         MC_print_statistics(mc_stats);
144         xbt_abort();
145       } 
146     }
147   }
148     
149   MC_comm_pattern_free(comm);
150
151 }
152
153 /********** Non Static functions ***********/
154
155 void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
156 {
157   mc_comm_pattern_t pattern = xbt_new0(s_mc_comm_pattern_t, 1);
158   pattern->data_size = -1;
159   pattern->data = NULL;
160
161   // Fill initial_pattern->index_comm:
162   const smx_process_t issuer = MC_smx_simcall_get_issuer(request);
163   mc_list_comm_pattern_t initial_pattern =
164     (mc_list_comm_pattern_t) xbt_dynar_get_as(initial_communications_pattern, issuer->pid, mc_list_comm_pattern_t);
165   xbt_dynar_t incomplete_pattern =
166     (xbt_dynar_t) xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
167   pattern->index =
168     initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
169
170   
171   if (call_type == MC_CALL_TYPE_SEND) {
172     /* Create comm pattern */
173     pattern->type = SIMIX_COMM_SEND;
174     pattern->comm = simcall_comm_isend__get__result(request);
175
176     s_smx_synchro_t synchro;
177     MC_process_read_simple(&mc_model_checker->process,
178       &synchro, pattern->comm, sizeof(synchro));
179
180     // TODO, remote access to rdv->name and rdv_copy->name
181     pattern->rdv = (synchro.comm.rdv != NULL) ? strdup(synchro.comm.rdv->name) : strdup(synchro.comm.rdv_cpy->name);
182     pattern->src_proc = MC_smx_resolve_process(synchro.comm.src_proc)->pid;
183     pattern->src_host = MC_smx_process_get_host_name(issuer);
184
185     struct s_smpi_mpi_request mpi_request;
186     MC_process_read_simple(&mc_model_checker->process,
187       &mpi_request, (MPI_Request) simcall_comm_isend__get__data(request),
188       sizeof(mpi_request));
189     pattern->tag = mpi_request.tag;
190
191     if(synchro.comm.src_buff != NULL){
192       pattern->data_size = synchro.comm.src_buff_size;
193       pattern->data = xbt_malloc0(pattern->data_size);
194       MC_process_read_simple(&mc_model_checker->process,
195         pattern->data, synchro.comm.src_buff, pattern->data_size);
196     }
197     if(mpi_request.detached){
198       if (!initial_global_state->initial_communications_pattern_done) {
199         /* Store comm pattern */
200         xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->list, &pattern);
201       } else {
202         /* Evaluate comm determinism */
203         deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
204         ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->index_comm++;
205       }
206       return;
207     }
208   } else if (call_type == MC_CALL_TYPE_RECV) {                      
209     pattern->type = SIMIX_COMM_RECEIVE;
210     pattern->comm = simcall_comm_irecv__get__result(request);
211
212     struct s_smpi_mpi_request mpi_request;
213     MC_process_read_simple(&mc_model_checker->process,
214       &mpi_request, (MPI_Request) simcall_comm_irecv__get__data(request),
215       sizeof(mpi_request));
216     pattern->tag = mpi_request.tag;
217
218     s_smx_synchro_t synchro;
219     MC_process_read_simple(&mc_model_checker->process,
220       &synchro, pattern->comm, sizeof(synchro));
221
222     pattern->rdv = (synchro.comm.rdv != NULL) ? strdup(synchro.comm.rdv->name) : strdup(synchro.comm.rdv_cpy->name);
223     pattern->dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc)->pid;
224     // FIXME, remote process access
225     pattern->dst_host = MC_smx_process_get_host_name(issuer);
226   } else {
227     xbt_die("Unexpected call_type %i", (int) call_type);
228   }
229
230   xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t), &pattern);
231
232   XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, issuer->pid);
233 }
234
235 void MC_complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, unsigned int issuer, int backtracking) {
236   mc_comm_pattern_t current_comm_pattern;
237   unsigned int cursor = 0;
238   mc_comm_pattern_t comm_pattern;
239   int completed = 0;
240
241   /* Complete comm pattern */
242   xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
243     if (current_comm_pattern-> comm == comm) {
244       update_comm_pattern(current_comm_pattern, comm);
245       completed = 1;
246       xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &comm_pattern);
247       XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
248       break;
249     }
250   }
251   if(!completed)
252     xbt_die("Corresponding communication not found!");
253
254   if (!initial_global_state->initial_communications_pattern_done) {
255     /* Store comm pattern */
256     xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->list, &comm_pattern);
257   } else {
258     /* Evaluate comm determinism */
259     deterministic_comm_pattern(issuer, comm_pattern, backtracking);
260     ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->index_comm++;
261   }
262 }
263
264
265 /************************ Main algorithm ************************/
266
267 void MC_pre_modelcheck_comm_determinism(void)
268 {
269   MC_SET_MC_HEAP;
270
271   mc_state_t initial_state = NULL;
272   smx_process_t process;
273   int i;
274
275   if (_sg_mc_visited > 0)
276     visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
277  
278   initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), MC_list_comm_pattern_free_voidp);
279   for (i=0; i < MC_smx_get_maxpid(); i++){
280     mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
281     process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
282     process_list_pattern->index_comm = 0;
283     xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
284   }
285   incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
286   for (i=0; i < MC_smx_get_maxpid(); i++){
287     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
288     xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
289   }
290
291   initial_state = MC_state_new();
292   MC_SET_STD_HEAP;
293   
294   XBT_DEBUG("********* Start communication determinism verification *********");
295
296   /* Wait for requests (schedules processes) */
297   MC_wait_for_requests();
298
299   MC_SET_MC_HEAP;
300
301   /* Get an enabled process and insert it in the interleave set of the initial state */
302   MC_EACH_SIMIX_PROCESS(process,
303     if (MC_process_is_enabled(process)) {
304       MC_state_interleave_process(initial_state, process);
305     }
306   );
307
308   xbt_fifo_unshift(mc_stack, initial_state);
309
310   MC_SET_STD_HEAP;
311
312 }
313
314 void MC_modelcheck_comm_determinism(void)
315 {
316
317   char *req_str = NULL;
318   int value;
319   mc_visited_state_t visited_state = NULL;
320   smx_simcall_t req = NULL;
321   smx_process_t process = NULL;
322   mc_state_t state = NULL, next_state = NULL;
323
324   while (xbt_fifo_size(mc_stack) > 0) {
325
326     /* Get current state */
327     state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
328
329     XBT_DEBUG("**************************************************");
330     XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
331               xbt_fifo_size(mc_stack), state->num,
332               MC_state_interleave_size(state));
333
334     /* Update statistics */
335     mc_stats->visited_states++;
336
337     if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
338         && (req = MC_state_get_request(state, &value))
339         && (visited_state == NULL)) {
340
341       req_str = MC_request_to_string(req, value);  
342       XBT_DEBUG("Execute: %s", req_str);                 
343       xbt_free(req_str);
344       
345       if (dot_output != NULL) {
346         MC_SET_MC_HEAP;
347         req_str = MC_request_get_dot_output(req, value);
348         MC_SET_STD_HEAP;
349       }
350
351       MC_state_set_executed_request(state, req, value);
352       mc_stats->executed_transitions++;
353
354       /* TODO : handle test and testany simcalls */
355       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
356       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
357         call = MC_get_call_type(req);
358       }
359
360       /* Answer the request */
361       MC_simcall_handle(req, value);    /* After this call req is no longer useful */
362
363       MC_SET_MC_HEAP;
364       if(!initial_global_state->initial_communications_pattern_done)
365         MC_handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
366       else
367         MC_handle_comm_pattern(call, req, value, NULL, 0);
368       MC_SET_STD_HEAP;
369
370       /* Wait for requests (schedules processes) */
371       MC_wait_for_requests();
372
373       /* Create the new expanded state */
374       MC_SET_MC_HEAP;
375
376       next_state = MC_state_new();
377
378       if ((visited_state = is_visited_state(next_state)) == NULL) {
379
380         /* Get enabled processes and insert them in the interleave set of the next state */
381         MC_EACH_SIMIX_PROCESS(process,
382           if (MC_process_is_enabled(process)) {
383             MC_state_interleave_process(next_state, process);
384           }
385         );
386
387         if (dot_output != NULL)
388           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,  next_state->num, req_str);
389
390       } else {
391
392         if (dot_output != NULL)
393           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
394
395       }
396
397       xbt_fifo_unshift(mc_stack, next_state);
398
399       if (dot_output != NULL)
400         xbt_free(req_str);
401
402       MC_SET_STD_HEAP;
403
404     } else {
405
406       if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
407         XBT_WARN("/!\\ Max depth reached ! /!\\ ");
408       } else if (visited_state != NULL) {
409         XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.", visited_state->other_num == -1 ? visited_state->num : visited_state->other_num);
410       } else {
411         XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
412       }
413
414       MC_SET_MC_HEAP;
415
416       if (!initial_global_state->initial_communications_pattern_done) 
417         initial_global_state->initial_communications_pattern_done = 1;
418
419       /* Trash the current state, no longer needed */
420       xbt_fifo_shift(mc_stack);
421       MC_state_delete(state, !state->in_visited_states ? 1 : 0);
422       XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
423
424       MC_SET_STD_HEAP;
425
426       visited_state = NULL;
427
428       /* Check for deadlocks */
429       if (MC_deadlock_check()) {
430         MC_show_deadlock(NULL);
431         return;
432       }
433
434       MC_SET_MC_HEAP;
435
436       while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
437         if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
438           /* We found a back-tracking point, let's loop */
439           XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
440           xbt_fifo_unshift(mc_stack, state);
441           MC_SET_STD_HEAP;
442
443           MC_replay(mc_stack);
444
445           XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
446
447           break;
448         } else {
449           XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
450           MC_state_delete(state, !state->in_visited_states ? 1 : 0);
451         }
452       }
453
454       MC_SET_STD_HEAP;
455     }
456   }
457
458   MC_print_statistics(mc_stats);
459   MC_SET_STD_HEAP;
460
461   return;
462 }