Logo AND Algorithmique Numérique Distribuée

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