Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
31ea363faddb44502d68a38ed980496f5cebf35c
[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_private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
10                                 "Logging specific to MC communication determinism detection");
11
12 /********** Global variables **********/
13
14 xbt_dynar_t initial_communications_pattern;
15 xbt_dynar_t incomplete_communications_pattern;
16 xbt_dynar_t communications_pattern;
17 int nb_comm_pattern;
18
19 /********** Static functions ***********/
20
21 static void comm_pattern_free(mc_comm_pattern_t p)
22 {
23   xbt_free(p->rdv);
24   xbt_free(p->data);
25   xbt_free(p);
26   p = NULL;
27 }
28
29 static void comm_pattern_free_voidp(void *p)
30 {
31   comm_pattern_free((mc_comm_pattern_t) * (void **) p);
32 }
33
34 static mc_comm_pattern_t get_comm_pattern_from_idx(xbt_dynar_t pattern,
35                                                    unsigned int *idx,
36                                                    e_smx_comm_type_t type,
37                                                    unsigned long proc)
38 {
39   mc_comm_pattern_t current_comm;
40   while (*idx < xbt_dynar_length(pattern)) {
41     current_comm =
42         (mc_comm_pattern_t) xbt_dynar_get_as(pattern, *idx, mc_comm_pattern_t);
43     if (current_comm->type == type && type == SIMIX_COMM_SEND) {
44       if (current_comm->src_proc == proc)
45         return current_comm;
46     } else if (current_comm->type == type && type == SIMIX_COMM_RECEIVE) {
47       if (current_comm->dst_proc == proc)
48         return current_comm;
49     }
50     (*idx)++;
51   }
52   return NULL;
53 }
54
55 static int compare_comm_pattern(mc_comm_pattern_t comm1,
56                                 mc_comm_pattern_t comm2)
57 {
58   if (strcmp(comm1->rdv, comm2->rdv) != 0)
59     return 1;
60   if (comm1->src_proc != comm2->src_proc)
61     return 1;
62   if (comm1->dst_proc != comm2->dst_proc)
63     return 1;
64   if (comm1->data_size != comm2->data_size)
65     return 1;
66   if (memcmp(comm1->data, comm2->data, comm1->data_size) != 0)
67     return 1;
68   return 0;
69 }
70
71 static void deterministic_pattern(xbt_dynar_t pattern, int partial)
72 {
73
74   unsigned int cursor = 0, send_index = 0, recv_index = 0;
75   mc_comm_pattern_t comm1, comm2;
76   unsigned int current_process = 1; /* Process 0 corresponds to maestro */
77   unsigned int nb_comms1, nb_comms2;
78   xbt_dynar_t process_comms_pattern1, process_comms_pattern2; 
79   
80   while (current_process < simix_process_maxpid) {
81     process_comms_pattern1 = (xbt_dynar_t)xbt_dynar_get_as(initial_communications_pattern, current_process, xbt_dynar_t);
82     process_comms_pattern2 = (xbt_dynar_t)xbt_dynar_get_as(pattern, current_process, xbt_dynar_t);
83     nb_comms1 = xbt_dynar_length(process_comms_pattern1);
84     nb_comms2 = xbt_dynar_length(process_comms_pattern2);
85     if(!xbt_dynar_is_empty((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, current_process, xbt_dynar_t)))
86       xbt_die("Damn ! Some communications from the process %u are incomplete (%lu)! That means one or several simcalls are not handle.", current_process, xbt_dynar_length((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, current_process, xbt_dynar_t)));
87     if (!partial && (nb_comms1 != nb_comms2)) {
88       XBT_INFO("The total number of communications is different between the compared patterns for the process %u.\n Communication determinism verification for this process cannot be performed.", current_process);
89       initial_global_state->send_deterministic = -1;
90       initial_global_state->comm_deterministic = -1;
91     } else {
92       while (cursor < nb_comms2) {
93         comm1 = (mc_comm_pattern_t)xbt_dynar_get_as(process_comms_pattern1, cursor, mc_comm_pattern_t);
94         if (comm1->type == SIMIX_COMM_SEND) {
95           comm2 = get_comm_pattern_from_idx(process_comms_pattern2, &send_index, comm1->type, current_process);
96           if (compare_comm_pattern(comm1, comm2)) {
97             XBT_INFO("The communications pattern of the process %u is different! (Different communication : %u)", current_process, cursor);
98             initial_global_state->send_deterministic = 0;
99             initial_global_state->comm_deterministic = 0;
100             return;
101           }
102           send_index++;
103         } else if (comm1->type == SIMIX_COMM_RECEIVE) {
104           comm2 = get_comm_pattern_from_idx(process_comms_pattern2, &recv_index, comm1->type, current_process);
105           if (compare_comm_pattern(comm1, comm2)) {
106             initial_global_state->comm_deterministic = 0;
107             if (!_sg_mc_send_determinism){
108               XBT_INFO("The communications pattern of the process %u is different! (Different communication : %u)", current_process, cursor);
109               return;
110             }
111           }
112           recv_index++;
113         }
114         cursor++;
115       }
116     }
117     current_process++;
118     cursor = 0;
119     send_index = 0;
120     recv_index = 0;
121   }
122 }
123
124 static void print_communications_pattern(xbt_dynar_t comms_pattern)
125 {
126   unsigned int cursor = 0;
127   mc_comm_pattern_t current_comm;
128   unsigned int current_process = 1;
129   xbt_dynar_t current_pattern;
130   while (current_process < simix_process_maxpid) {
131     current_pattern = (xbt_dynar_t)xbt_dynar_get_as(comms_pattern, current_process, xbt_dynar_t);
132     XBT_INFO("Communications from the process %u:", current_process);
133     xbt_dynar_foreach(current_pattern, cursor, current_comm) {
134       if (current_comm->type == SIMIX_COMM_SEND) {
135         XBT_INFO("[(%lu) %s -> (%lu) %s] %s ", current_comm->src_proc,
136                  current_comm->src_host, current_comm->dst_proc,
137                  current_comm->dst_host, "iSend");
138       } else {
139         XBT_INFO("[(%lu) %s <- (%lu) %s] %s ", current_comm->dst_proc,
140                  current_comm->dst_host, current_comm->src_proc,
141                  current_comm->src_host, "iRecv");
142       }
143     }
144     current_process++;
145     cursor = 0;
146   }
147 }
148
149 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_action_t comm)
150 {
151   void *addr_pointed;
152   comm_pattern->src_proc = comm->comm.src_proc->pid;
153   comm_pattern->dst_proc = comm->comm.dst_proc->pid;
154   comm_pattern->src_host =
155     simcall_host_get_name(comm->comm.src_proc->smx_host);
156   comm_pattern->dst_host =
157     simcall_host_get_name(comm->comm.dst_proc->smx_host);
158   if (comm_pattern->data_size == -1) {
159     comm_pattern->data_size = *(comm->comm.dst_buff_size);
160     comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
161     addr_pointed = *(void **) comm->comm.src_buff;
162     if (addr_pointed > std_heap && addr_pointed < ((xbt_mheap_t) std_heap)->breakval)
163       memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
164     else
165       memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
166   }
167 }
168
169 /********** Non Static functions ***********/
170
171 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, int call)
172 {
173   mc_comm_pattern_t pattern = NULL;
174   pattern = xbt_new0(s_mc_comm_pattern_t, 1);
175   pattern->num = ++nb_comm_pattern;
176   pattern->data_size = -1;
177   void *addr_pointed;
178   if (call == 1) {              // ISEND
179     pattern->type = SIMIX_COMM_SEND;
180     pattern->comm = simcall_comm_isend__get__result(request);
181     pattern->src_proc = pattern->comm->comm.src_proc->pid;
182     pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
183     pattern->data_size = pattern->comm->comm.src_buff_size;
184     pattern->data = xbt_malloc0(pattern->data_size);
185     addr_pointed = *(void **) pattern->comm->comm.src_buff;
186     if (addr_pointed > std_heap && addr_pointed < ((xbt_mheap_t) std_heap)->breakval)
187       memcpy(pattern->data, addr_pointed, pattern->data_size);
188     else
189       memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
190   } else {                      // IRECV
191     pattern->type = SIMIX_COMM_RECEIVE;
192     pattern->comm = simcall_comm_irecv__get__result(request);
193     pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
194     pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
195   }
196
197   if (pattern->comm->comm.rdv != NULL)
198     pattern->rdv = strdup(pattern->comm->comm.rdv->name);
199   else
200     pattern->rdv = strdup(pattern->comm->comm.rdv_cpy->name);
201
202   xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(list, request->issuer->pid, xbt_dynar_t), &pattern);
203
204   xbt_dynar_push_as((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, request->issuer->pid, xbt_dynar_t), int, xbt_dynar_length((xbt_dynar_t)xbt_dynar_get_as(list, request->issuer->pid, xbt_dynar_t)) - 1);
205
206 }
207
208 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm)
209 {
210   mc_comm_pattern_t current_comm_pattern;
211   unsigned int cursor = 0;
212   int index;
213   unsigned int src = comm->comm.src_proc->pid;
214   unsigned int dst = comm->comm.dst_proc->pid;
215   int src_completed = 0, dst_completed = 0;
216
217   /* Looking for the corresponding communication in the comm pattern list of the src process */
218   xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, index){
219     current_comm_pattern = (mc_comm_pattern_t) xbt_dynar_get_as((xbt_dynar_t)xbt_dynar_get_as(list, src, xbt_dynar_t), index, mc_comm_pattern_t);
220     if(current_comm_pattern->comm == comm){
221       update_comm_pattern(current_comm_pattern, comm);
222       xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, NULL);
223       src_completed = 1;
224       break;
225     }
226   }
227
228   if(!src_completed)
229     xbt_die("Corresponding communication for the source process not found!");
230
231   cursor = 0;
232
233   /* Looking for the corresponding communication in the comm pattern list of the dst process */
234   xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, index){
235     current_comm_pattern = (mc_comm_pattern_t) xbt_dynar_get_as((xbt_dynar_t)xbt_dynar_get_as(list, dst, xbt_dynar_t), index, mc_comm_pattern_t);
236     if(current_comm_pattern->comm == comm){
237       update_comm_pattern(current_comm_pattern, comm);
238       xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, NULL);
239       dst_completed = 1;
240       break;
241     }
242   }
243
244   if(!dst_completed)
245     xbt_die("Corresponding communication for the dest process not found!");
246
247
248 }
249
250 /************************ Main algorithm ************************/
251
252 void MC_pre_modelcheck_comm_determinism(void)
253 {
254
255   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
256
257   mc_state_t initial_state = NULL;
258   smx_process_t process;
259   int i;
260
261   if (!mc_mem_set)
262     MC_SET_MC_HEAP;
263
264   if (_sg_mc_visited > 0)
265     visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
266  
267   initial_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
268   for (i=0; i<simix_process_maxpid; i++){
269     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
270     xbt_dynar_insert_at(initial_communications_pattern, i, &process_pattern);
271   }
272   communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
273   for (i=0; i<simix_process_maxpid; i++){
274     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
275     xbt_dynar_insert_at(communications_pattern, i, &process_pattern);
276   }
277   incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
278   for (i=0; i<simix_process_maxpid; i++){
279     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(int), NULL);
280     xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
281   }
282
283   nb_comm_pattern = 0;
284
285   initial_state = MC_state_new();
286
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   xbt_swag_foreach(process, simix_global->process_list) {
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, call = 0;
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   smx_action_t current_comm;
319   xbt_dynar_t current_pattern;
320
321   while (xbt_fifo_size(mc_stack) > 0) {
322
323     /* Get current state */
324     state =
325         (mc_state_t)
326         xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
327
328     XBT_DEBUG("**************************************************");
329     XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
330               xbt_fifo_size(mc_stack), state->num,
331               MC_state_interleave_size(state));
332
333     /* Update statistics */
334     mc_stats->visited_states++;
335
336     if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
337         && (req = MC_state_get_request(state, &value))
338         && (visited_state == NULL)) {
339
340       /* Debug information */
341       if (XBT_LOG_ISENABLED(mc_comm_determinism, xbt_log_priority_debug)) {
342         req_str = MC_request_to_string(req, value);
343         XBT_DEBUG("Execute: %s", req_str);
344         xbt_free(req_str);
345       }
346
347       MC_SET_MC_HEAP;
348       if (dot_output != NULL)
349         req_str = MC_request_get_dot_output(req, value);
350       MC_SET_STD_HEAP;
351
352       MC_state_set_executed_request(state, req, value);
353       mc_stats->executed_transitions++;
354
355       /* TODO : handle test and testany simcalls */
356       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
357         if (req->call == SIMCALL_COMM_ISEND)
358           call = 1;
359         else if (req->call == SIMCALL_COMM_IRECV)
360           call = 2;
361         else if (req->call == SIMCALL_COMM_WAIT)
362           call = 3;
363         else if (req->call == SIMCALL_COMM_WAITANY)
364           call = 4;
365       }
366
367       /* Answer the request */
368       SIMIX_simcall_pre(req, value);    /* After this call req is no longer usefull */
369
370       MC_SET_MC_HEAP;
371       current_pattern = !initial_global_state->initial_communications_pattern_done ? initial_communications_pattern : communications_pattern; 
372       if (call == 1) { /* Send */
373         get_comm_pattern(current_pattern, req, call);
374       } else if (call == 2) { /* Recv */
375         get_comm_pattern(current_pattern, req, call);
376       } else if (call == 3) { /* Wait */
377         current_comm = simcall_comm_wait__get__comm(req);
378         if (current_comm->comm.refcount == 1)  /* First wait only must be considered */
379           complete_comm_pattern(current_pattern, current_comm);
380       } else if (call == 4) { /* WaitAny */
381         current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_action_t);
382         if (current_comm->comm.refcount == 1) /* First wait only must be considered */
383           complete_comm_pattern(current_pattern, current_comm);
384       }
385       MC_SET_STD_HEAP;
386
387       call = 0;
388
389       /* Wait for requests (schedules processes) */
390       MC_wait_for_requests();
391
392       /* Create the new expanded state */
393       MC_SET_MC_HEAP;
394
395       next_state = MC_state_new();
396
397       if ((visited_state = is_visited_state()) == NULL) {
398
399         /* Get enabled processes and insert them in the interleave set of the next state */
400         xbt_swag_foreach(process, simix_global->process_list) {
401           if (MC_process_is_enabled(process)) {
402             MC_state_interleave_process(next_state, process);
403           }
404         }
405
406         if (dot_output != NULL)
407           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
408                   next_state->num, req_str);
409
410       } else {
411
412         if (dot_output != NULL)
413           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
414                   visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
415
416       }
417
418       xbt_fifo_unshift(mc_stack, next_state);
419
420       if (dot_output != NULL)
421         xbt_free(req_str);
422
423       MC_SET_STD_HEAP;
424
425     } else {
426
427       if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
428         XBT_WARN("/!\\ Max depth reached ! /!\\ ");
429       } else if (visited_state != NULL) {
430         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);
431       } else {
432         XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
433       }
434
435       MC_SET_MC_HEAP;
436
437       if (initial_global_state->initial_communications_pattern_done) {
438         if (!visited_state) {
439           deterministic_pattern(communications_pattern, 0);
440         } else {
441           deterministic_pattern(communications_pattern, 1);
442         }
443
444         if (_sg_mc_comms_determinism && !initial_global_state->comm_deterministic) {
445             XBT_INFO("****************************************************");
446             XBT_INFO("***** Non-deterministic communications pattern *****");
447             XBT_INFO("****************************************************");
448             XBT_INFO("** Initial communications pattern (per process): **");
449             print_communications_pattern(initial_communications_pattern);
450             XBT_INFO("** Communications pattern counter-example (per process): **");
451             print_communications_pattern(communications_pattern);
452             MC_print_statistics(mc_stats);
453             MC_SET_STD_HEAP;
454             return;
455           } else if (_sg_mc_send_determinism && !initial_global_state->send_deterministic) {
456             XBT_INFO
457                 ("*********************************************************");
458             XBT_INFO
459                 ("***** Non-send-deterministic communications pattern *****");
460             XBT_INFO
461                 ("*********************************************************");
462             XBT_INFO("** Initial communications pattern: **");
463             print_communications_pattern(initial_communications_pattern);
464             XBT_INFO("** Communications pattern counter-example: **");
465             print_communications_pattern(communications_pattern);
466             MC_print_statistics(mc_stats);
467             MC_SET_STD_HEAP;
468             return;
469         }
470
471       } else {
472         initial_global_state->initial_communications_pattern_done = 1;
473       }
474
475       /* Trash the current state, no longer needed */
476       xbt_fifo_shift(mc_stack);
477       MC_state_delete(state);
478       XBT_DEBUG("Delete state %d at depth %d", state->num,
479                 xbt_fifo_size(mc_stack) + 1);
480
481       MC_SET_STD_HEAP;
482
483       visited_state = NULL;
484
485       /* Check for deadlocks */
486       if (MC_deadlock_check()) {
487         MC_show_deadlock(NULL);
488         return;
489       }
490
491       MC_SET_MC_HEAP;
492
493       while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
494         if (MC_state_interleave_size(state)
495             && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
496           /* We found a back-tracking point, let's loop */
497           XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
498                     xbt_fifo_size(mc_stack) + 1);
499           xbt_fifo_unshift(mc_stack, state);
500           MC_SET_STD_HEAP;
501
502           MC_replay(mc_stack, -1);
503
504           XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
505                     xbt_fifo_size(mc_stack));
506           break;
507         } else {
508           XBT_DEBUG("Delete state %d at depth %d", state->num,
509                     xbt_fifo_size(mc_stack) + 1);
510           MC_state_delete(state);
511         }
512       }
513
514       MC_SET_STD_HEAP;
515     }
516   }
517
518   MC_print_statistics(mc_stats);
519   MC_SET_STD_HEAP;
520
521   return;
522 }