Logo AND Algorithmique Numérique Distribuée

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