Logo AND Algorithmique Numérique Distribuée

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