Logo AND Algorithmique Numérique Distribuée

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