1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
14 #include "mc_client.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
19 "Logging specific to MC communication determinism detection");
21 /********** Global variables **********/
23 xbt_dynar_t initial_communications_pattern;
24 xbt_dynar_t incomplete_communications_pattern;
26 /********** Static functions ***********/
28 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
29 if(comm1->type != comm2->type)
31 if (strcmp(comm1->rdv, comm2->rdv) != 0)
33 if (comm1->src_proc != comm2->src_proc)
35 if (comm1->dst_proc != comm2->dst_proc)
37 if (comm1->tag != comm2->tag)
39 if (comm1->data_size != comm2->data_size)
40 return DATA_SIZE_DIFF;
41 if(comm1->data == NULL && comm2->data == NULL)
43 if(comm1->data != NULL && comm2->data !=NULL) {
44 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
53 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
56 if(comm->type == SIMIX_COMM_SEND)
57 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
59 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
63 res = bprintf("%s Different type for communication #%d", type, cursor);
66 res = bprintf("%s Different rdv for communication #%d", type, cursor);
69 res = bprintf("%s Different tag for communication #%d", type, cursor);
72 res = bprintf("%s Different source for communication #%d", type, cursor);
75 res = bprintf("%s Different destination for communication #%d", type, cursor);
78 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
81 res = bprintf("%s\n Different data for communication #%d", type, cursor);
91 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm_addr)
94 MC_process_read_simple(&mc_model_checker->process,
95 &comm, comm_addr, sizeof(comm));
97 smx_process_t src_proc = MC_smx_resolve_process(comm.comm.src_proc);
98 smx_process_t dst_proc = MC_smx_resolve_process(comm.comm.dst_proc);
99 comm_pattern->src_proc = src_proc->pid;
100 comm_pattern->dst_proc = dst_proc->pid;
101 comm_pattern->src_host = MC_smx_process_get_host_name(src_proc);
102 comm_pattern->dst_host = MC_smx_process_get_host_name(dst_proc);
103 if (comm_pattern->data_size == -1 && comm.comm.src_buff != NULL) {
105 MC_process_read_simple(&mc_model_checker->process,
106 &buff_size, comm.comm.dst_buff_size, sizeof(buff_size));
107 comm_pattern->data_size = buff_size;
108 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
109 MC_process_read_simple(&mc_model_checker->process,
110 comm_pattern->data, comm.comm.src_buff, comm_pattern->data_size);
114 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
116 mc_list_comm_pattern_t list =
117 xbt_dynar_get_as(initial_communications_pattern, process, mc_list_comm_pattern_t);
120 mc_comm_pattern_t initial_comm =
121 xbt_dynar_get_as(list->list, list->index_comm, mc_comm_pattern_t);
122 e_mc_comm_pattern_difference_t diff =
123 compare_comm_pattern(initial_comm, comm);
125 if (diff != NONE_DIFF) {
126 if (comm->type == SIMIX_COMM_SEND){
127 initial_global_state->send_deterministic = 0;
128 if(initial_global_state->send_diff != NULL)
129 xbt_free(initial_global_state->send_diff);
130 initial_global_state->send_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
132 initial_global_state->recv_deterministic = 0;
133 if(initial_global_state->recv_diff != NULL)
134 xbt_free(initial_global_state->recv_diff);
135 initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
137 if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
138 XBT_INFO("*********************************************************");
139 XBT_INFO("***** Non-send-deterministic communications pattern *****");
140 XBT_INFO("*********************************************************");
141 XBT_INFO("%s", initial_global_state->send_diff);
142 xbt_free(initial_global_state->send_diff);
143 initial_global_state->send_diff = NULL;
144 MC_print_statistics(mc_stats);
146 }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
147 XBT_INFO("****************************************************");
148 XBT_INFO("***** Non-deterministic communications pattern *****");
149 XBT_INFO("****************************************************");
150 XBT_INFO("%s", initial_global_state->send_diff);
151 XBT_INFO("%s", initial_global_state->recv_diff);
152 xbt_free(initial_global_state->send_diff);
153 initial_global_state->send_diff = NULL;
154 xbt_free(initial_global_state->recv_diff);
155 initial_global_state->recv_diff = NULL;
156 MC_print_statistics(mc_stats);
162 MC_comm_pattern_free(comm);
166 /********** Non Static functions ***********/
168 void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
170 const smx_process_t issuer = MC_smx_simcall_get_issuer(request);
171 mc_list_comm_pattern_t initial_pattern = xbt_dynar_get_as(
172 initial_communications_pattern, issuer->pid, mc_list_comm_pattern_t);
173 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(
174 incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
176 mc_comm_pattern_t pattern = xbt_new0(s_mc_comm_pattern_t, 1);
177 pattern->data_size = -1;
178 pattern->data = NULL;
180 initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
182 if (call_type == MC_CALL_TYPE_SEND) {
183 /* Create comm pattern */
184 pattern->type = SIMIX_COMM_SEND;
185 pattern->comm_addr = simcall_comm_isend__get__result(request);
187 s_smx_synchro_t synchro;
188 MC_process_read_simple(&mc_model_checker->process,
189 &synchro, pattern->comm_addr, sizeof(synchro));
192 MC_process_read_simple(&mc_model_checker->process, &remote_name,
193 synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name,
194 sizeof(remote_name));
196 MC_process_read_string(&mc_model_checker->process, remote_name);
197 pattern->src_proc = MC_smx_resolve_process(synchro.comm.src_proc)->pid;
198 pattern->src_host = MC_smx_process_get_host_name(issuer);
200 struct s_smpi_mpi_request mpi_request;
201 MC_process_read_simple(&mc_model_checker->process,
202 &mpi_request, (MPI_Request) simcall_comm_isend__get__data(request),
203 sizeof(mpi_request));
204 pattern->tag = mpi_request.tag;
206 if(synchro.comm.src_buff != NULL){
207 pattern->data_size = synchro.comm.src_buff_size;
208 pattern->data = xbt_malloc0(pattern->data_size);
209 MC_process_read_simple(&mc_model_checker->process,
210 pattern->data, synchro.comm.src_buff, pattern->data_size);
212 if(mpi_request.detached){
213 if (!initial_global_state->initial_communications_pattern_done) {
214 /* Store comm pattern */
217 initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t
221 /* Evaluate comm determinism */
222 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
224 initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t
229 } else if (call_type == MC_CALL_TYPE_RECV) {
230 pattern->type = SIMIX_COMM_RECEIVE;
231 pattern->comm_addr = simcall_comm_irecv__get__result(request);
233 struct s_smpi_mpi_request mpi_request;
234 MC_process_read_simple(&mc_model_checker->process,
235 &mpi_request, (MPI_Request) simcall_comm_irecv__get__data(request),
236 sizeof(mpi_request));
237 pattern->tag = mpi_request.tag;
239 s_smx_synchro_t synchro;
240 MC_process_read_simple(&mc_model_checker->process,
241 &synchro, pattern->comm_addr, sizeof(synchro));
244 MC_process_read_simple(&mc_model_checker->process, &remote_name,
245 synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name,
246 sizeof(remote_name));
248 MC_process_read_string(&mc_model_checker->process, remote_name);
249 pattern->dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc)->pid;
250 pattern->dst_host = MC_smx_process_get_host_name(issuer);
252 xbt_die("Unexpected call_type %i", (int) call_type);
256 xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t),
259 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, issuer->pid);
262 void MC_complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm_addr, unsigned int issuer, int backtracking) {
263 mc_comm_pattern_t current_comm_pattern;
264 unsigned int cursor = 0;
265 mc_comm_pattern_t comm_pattern;
268 /* Complete comm pattern */
269 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
270 if (current_comm_pattern->comm_addr == comm_addr) {
271 update_comm_pattern(current_comm_pattern, comm_addr);
274 xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t),
275 cursor, &comm_pattern);
276 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
281 xbt_die("Corresponding communication not found!");
283 mc_list_comm_pattern_t pattern = xbt_dynar_get_as(
284 initial_communications_pattern, issuer, mc_list_comm_pattern_t);
286 if (!initial_global_state->initial_communications_pattern_done) {
287 /* Store comm pattern */
288 xbt_dynar_push(pattern->list, &comm_pattern);
290 /* Evaluate comm determinism */
291 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
292 pattern->index_comm++;
297 /************************ Main algorithm ************************/
299 static void MC_modelcheck_comm_determinism_main(void);
301 static void MC_pre_modelcheck_comm_determinism(void)
305 mc_state_t initial_state = NULL;
306 smx_process_t process;
308 const int maxpid = MC_smx_get_maxpid();
310 if (_sg_mc_visited > 0)
311 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
313 // Create initial_communications_pattern elements:
314 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), MC_list_comm_pattern_free_voidp);
315 for (i=0; i < maxpid; i++){
316 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
317 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
318 process_list_pattern->index_comm = 0;
319 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
322 // Create incomplete_communications_pattern elements:
323 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
324 for (i=0; i < maxpid; i++){
325 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
326 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
329 initial_state = MC_state_new();
332 XBT_DEBUG("********* Start communication determinism verification *********");
334 /* Wait for requests (schedules processes) */
335 MC_wait_for_requests();
339 /* Get an enabled process and insert it in the interleave set of the initial state */
340 MC_EACH_SIMIX_PROCESS(process,
341 if (MC_process_is_enabled(process)) {
342 MC_state_interleave_process(initial_state, process);
346 xbt_fifo_unshift(mc_stack, initial_state);
352 static void MC_modelcheck_comm_determinism_main(void)
355 char *req_str = NULL;
357 mc_visited_state_t visited_state = NULL;
358 smx_simcall_t req = NULL;
359 smx_process_t process = NULL;
360 mc_state_t state = NULL, next_state = NULL;
362 while (xbt_fifo_size(mc_stack) > 0) {
364 /* Get current state */
365 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
367 XBT_DEBUG("**************************************************");
368 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
369 xbt_fifo_size(mc_stack), state->num,
370 MC_state_interleave_size(state));
372 /* Update statistics */
373 mc_stats->visited_states++;
375 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
376 && (req = MC_state_get_request(state, &value))
377 && (visited_state == NULL)) {
379 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
380 XBT_DEBUG("Execute: %s", req_str);
383 if (dot_output != NULL) {
385 req_str = MC_request_get_dot_output(req, value);
389 MC_state_set_executed_request(state, req, value);
390 mc_stats->executed_transitions++;
392 /* TODO : handle test and testany simcalls */
393 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
394 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
395 call = MC_get_call_type(req);
398 /* Answer the request */
399 MC_simcall_handle(req, value); /* After this call req is no longer useful */
402 if(!initial_global_state->initial_communications_pattern_done)
403 MC_handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
405 MC_handle_comm_pattern(call, req, value, NULL, 0);
408 /* Wait for requests (schedules processes) */
409 MC_wait_for_requests();
411 /* Create the new expanded state */
414 next_state = MC_state_new();
416 if ((visited_state = is_visited_state(next_state)) == NULL) {
418 /* Get enabled processes and insert them in the interleave set of the next state */
419 MC_EACH_SIMIX_PROCESS(process,
420 if (MC_process_is_enabled(process)) {
421 MC_state_interleave_process(next_state, process);
425 if (dot_output != NULL)
426 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
430 if (dot_output != NULL)
431 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
435 xbt_fifo_unshift(mc_stack, next_state);
437 if (dot_output != NULL)
444 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
445 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
446 } else if (visited_state != NULL) {
447 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);
449 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
454 if (!initial_global_state->initial_communications_pattern_done)
455 initial_global_state->initial_communications_pattern_done = 1;
457 /* Trash the current state, no longer needed */
458 xbt_fifo_shift(mc_stack);
459 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
460 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
464 visited_state = NULL;
466 /* Check for deadlocks */
467 if (MC_deadlock_check()) {
468 MC_show_deadlock(NULL);
474 while ((state = (mc_state_t) xbt_fifo_shift(mc_stack)) != NULL) {
475 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
476 /* We found a back-tracking point, let's loop */
477 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
478 xbt_fifo_unshift(mc_stack, state);
483 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
487 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
488 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
496 MC_print_statistics(mc_stats);
502 void MC_modelcheck_comm_determinism(void)
504 XBT_INFO("Check communication determinism");
505 mc_reduce_kind = e_mc_reduce_none;
506 MC_wait_for_requests();
508 if (mc_mode == MC_MODE_CLIENT) {
509 // This will move somehwere else:
510 MC_client_handle_messages();
513 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
515 /* Create exploration stack */
516 mc_stack = xbt_fifo_new();
520 MC_pre_modelcheck_comm_determinism();
523 initial_global_state = xbt_new0(s_mc_global_t, 1);
524 initial_global_state->snapshot = MC_take_snapshot(0);
525 initial_global_state->initial_communications_pattern_done = 0;
526 initial_global_state->recv_deterministic = 1;
527 initial_global_state->send_deterministic = 1;
528 initial_global_state->recv_diff = NULL;
529 initial_global_state->send_diff = NULL;
533 MC_modelcheck_comm_determinism_main();
535 mmalloc_set_current_heap(heap);