Logo AND Algorithmique Numérique Distribuée

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