Logo AND Algorithmique Numérique Distribuée

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