Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix dot output with file descriptor checkpoint/restore
[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   void *addr_pointed;
157   comm_pattern->src_proc = comm->comm.src_proc->pid;
158   comm_pattern->dst_proc = comm->comm.dst_proc->pid;
159   comm_pattern->src_host =
160     simcall_host_get_name(comm->comm.src_proc->smx_host);
161   comm_pattern->dst_host =
162     simcall_host_get_name(comm->comm.dst_proc->smx_host);
163   if (comm_pattern->data_size == -1) {
164     comm_pattern->data_size = *(comm->comm.dst_buff_size);
165     comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
166     addr_pointed = *(void **) comm->comm.src_buff;
167     if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
168       memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
169     else
170       memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
171   }
172 }
173
174 /********** Non Static functions ***********/
175
176 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, mc_call_type call_type)
177 {
178   mc_comm_pattern_t pattern = NULL;
179   pattern = xbt_new0(s_mc_comm_pattern_t, 1);
180   pattern->num = ++nb_comm_pattern;
181   pattern->data_size = -1;
182   void *addr_pointed;
183   if (call_type == MC_CALL_TYPE_SEND) {              // ISEND
184     pattern->type = SIMIX_COMM_SEND;
185     pattern->comm = simcall_comm_isend__get__result(request);
186     pattern->src_proc = pattern->comm->comm.src_proc->pid;
187     pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
188     pattern->data_size = pattern->comm->comm.src_buff_size;
189     pattern->data = xbt_malloc0(pattern->data_size);
190     addr_pointed = *(void **) pattern->comm->comm.src_buff;
191     if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
192       memcpy(pattern->data, addr_pointed, pattern->data_size);
193     else
194       memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
195   } else if (call_type == MC_CALL_TYPE_RECV) {                      // IRECV
196     pattern->type = SIMIX_COMM_RECEIVE;
197     pattern->comm = simcall_comm_irecv__get__result(request);
198     pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
199     pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
200   } else {
201     xbt_die("Unexpected call_type %i", (int) call_type);
202   }
203
204   if (pattern->comm->comm.rdv != NULL)
205     pattern->rdv = strdup(pattern->comm->comm.rdv->name);
206   else
207     pattern->rdv = strdup(pattern->comm->comm.rdv_cpy->name);
208
209   xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(list, request->issuer->pid, xbt_dynar_t), &pattern);
210
211   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);
212
213 }
214
215 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm)
216 {
217   mc_comm_pattern_t current_comm_pattern;
218   unsigned int cursor = 0;
219   int index;
220   unsigned int src = comm->comm.src_proc->pid;
221   unsigned int dst = comm->comm.dst_proc->pid;
222   int src_completed = 0, dst_completed = 0;
223
224   /* Looking for the corresponding communication in the comm pattern list of the src process */
225   xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, index){
226     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);
227     if(current_comm_pattern->comm == comm){
228       update_comm_pattern(current_comm_pattern, comm);
229       xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, NULL);
230       src_completed = 1;
231       break;
232     }
233   }
234
235   if(!src_completed)
236     xbt_die("Corresponding communication for the source process not found!");
237
238   cursor = 0;
239
240   /* Looking for the corresponding communication in the comm pattern list of the dst process */
241   xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, index){
242     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);
243     if(current_comm_pattern->comm == comm){
244       update_comm_pattern(current_comm_pattern, comm);
245       xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, NULL);
246       dst_completed = 1;
247       break;
248     }
249   }
250
251   if(!dst_completed)
252     xbt_die("Corresponding communication for the dest process not found!");
253
254
255 }
256
257 /************************ Main algorithm ************************/
258
259 void MC_pre_modelcheck_comm_determinism(void)
260 {
261
262   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
263
264   mc_state_t initial_state = NULL;
265   smx_process_t process;
266   int i;
267
268   if (!mc_mem_set)
269     MC_SET_MC_HEAP;
270
271   if (_sg_mc_visited > 0)
272     visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
273  
274   initial_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
275   for (i=0; i<simix_process_maxpid; i++){
276     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
277     xbt_dynar_insert_at(initial_communications_pattern, i, &process_pattern);
278   }
279   communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
280   for (i=0; i<simix_process_maxpid; i++){
281     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
282     xbt_dynar_insert_at(communications_pattern, i, &process_pattern);
283   }
284   incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
285   for (i=0; i<simix_process_maxpid; i++){
286     xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(int), NULL);
287     xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
288   }
289
290   nb_comm_pattern = 0;
291
292   initial_state = MC_state_new();
293
294   MC_SET_STD_HEAP;
295
296   XBT_DEBUG("********* Start communication determinism verification *********");
297
298   /* Wait for requests (schedules processes) */
299   MC_wait_for_requests();
300
301   MC_SET_MC_HEAP;
302
303   /* Get an enabled process and insert it in the interleave set of the initial state */
304   xbt_swag_foreach(process, simix_global->process_list) {
305     if (MC_process_is_enabled(process)) {
306       MC_state_interleave_process(initial_state, process);
307     }
308   }
309
310   xbt_fifo_unshift(mc_stack, initial_state);
311
312   MC_SET_STD_HEAP;
313
314 }
315
316 void MC_modelcheck_comm_determinism(void)
317 {
318
319   char *req_str = NULL;
320   int value;
321   mc_visited_state_t visited_state = NULL;
322   smx_simcall_t req = NULL;
323   smx_process_t process = NULL;
324   mc_state_t state = NULL, next_state = NULL;
325   xbt_dynar_t current_pattern;
326
327   while (xbt_fifo_size(mc_stack) > 0) {
328
329     /* Get current state */
330     state =
331         (mc_state_t)
332         xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
333
334     XBT_DEBUG("**************************************************");
335     XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
336               xbt_fifo_size(mc_stack), state->num,
337               MC_state_interleave_size(state));
338
339     /* Update statistics */
340     mc_stats->visited_states++;
341
342     if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
343         && (req = MC_state_get_request(state, &value))
344         && (visited_state == NULL)) {
345
346       MC_LOG_REQUEST(mc_comm_determinism, req, value);
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       mc_call_type 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       current_pattern = !initial_global_state->initial_communications_pattern_done ? initial_communications_pattern : communications_pattern; 
368       mc_update_comm_pattern(call, req, value, current_pattern);
369       MC_SET_STD_HEAP;
370
371       /* Wait for requests (schedules processes) */
372       MC_wait_for_requests();
373
374       /* Create the new expanded state */
375       MC_SET_MC_HEAP;
376
377       next_state = MC_state_new();
378
379       if ((visited_state = is_visited_state()) == NULL) {
380
381         /* Get enabled processes and insert them in the interleave set of the next state */
382         xbt_swag_foreach(process, simix_global->process_list) {
383           if (MC_process_is_enabled(process)) {
384             MC_state_interleave_process(next_state, process);
385           }
386         }
387
388         if (dot_output != NULL)
389           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
390                   next_state->num, req_str);
391
392       } else {
393
394         if (dot_output != NULL)
395           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
396                   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         if (!visited_state) {
421           deterministic_pattern(communications_pattern, 0);
422         } else {
423           deterministic_pattern(communications_pattern, 1);
424         }
425
426         if (_sg_mc_comms_determinism && !initial_global_state->comm_deterministic) {
427             XBT_INFO("****************************************************");
428             XBT_INFO("***** Non-deterministic communications pattern *****");
429             XBT_INFO("****************************************************");
430             XBT_INFO("** Initial communications pattern (per process): **");
431             print_communications_pattern(initial_communications_pattern);
432             XBT_INFO("** Communications pattern counter-example (per process): **");
433             print_communications_pattern(communications_pattern);
434             MC_print_statistics(mc_stats);
435             MC_SET_STD_HEAP;
436             return;
437           } else if (_sg_mc_send_determinism && !initial_global_state->send_deterministic) {
438             XBT_INFO
439                 ("*********************************************************");
440             XBT_INFO
441                 ("***** Non-send-deterministic communications pattern *****");
442             XBT_INFO
443                 ("*********************************************************");
444             XBT_INFO("** Initial communications pattern: **");
445             print_communications_pattern(initial_communications_pattern);
446             XBT_INFO("** Communications pattern counter-example: **");
447             print_communications_pattern(communications_pattern);
448             MC_print_statistics(mc_stats);
449             MC_SET_STD_HEAP;
450             return;
451         }
452
453       } else {
454         initial_global_state->initial_communications_pattern_done = 1;
455       }
456
457       /* Trash the current state, no longer needed */
458       xbt_fifo_shift(mc_stack);
459       MC_state_delete(state);
460       XBT_DEBUG("Delete state %d at depth %d", state->num,
461                 xbt_fifo_size(mc_stack) + 1);
462
463       MC_SET_STD_HEAP;
464
465       visited_state = NULL;
466
467       /* Check for deadlocks */
468       if (MC_deadlock_check()) {
469         MC_show_deadlock(NULL);
470         return;
471       }
472
473       MC_SET_MC_HEAP;
474
475       while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
476         if (MC_state_interleave_size(state)
477             && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
478           /* We found a back-tracking point, let's loop */
479           XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
480                     xbt_fifo_size(mc_stack) + 1);
481           xbt_fifo_unshift(mc_stack, state);
482           MC_SET_STD_HEAP;
483
484           MC_replay(mc_stack, -1);
485
486           XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
487                     xbt_fifo_size(mc_stack));
488           break;
489         } else {
490           XBT_DEBUG("Delete state %d at depth %d", state->num,
491                     xbt_fifo_size(mc_stack) + 1);
492           MC_state_delete(state);
493         }
494       }
495
496       MC_SET_STD_HEAP;
497     }
498   }
499
500   MC_print_statistics(mc_stats);
501   MC_SET_STD_HEAP;
502
503   return;
504 }