Logo AND Algorithmique Numérique Distribuée

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