Logo AND Algorithmique Numérique Distribuée

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