Logo AND Algorithmique Numérique Distribuée

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