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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
15 "Logging specific to MC communication determinism detection");
17 /********** Global variables **********/
19 xbt_dynar_t initial_communications_pattern;
20 xbt_dynar_t incomplete_communications_pattern;
22 /********** Static functions ***********/
24 static void comm_pattern_free(mc_comm_pattern_t p)
32 static void list_comm_pattern_free(mc_list_comm_pattern_t l)
34 xbt_dynar_free(&(l->list));
39 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
40 if(comm1->type != comm2->type)
42 if (strcmp(comm1->rdv, comm2->rdv) != 0)
44 if (comm1->src_proc != comm2->src_proc)
46 if (comm1->dst_proc != comm2->dst_proc)
48 if (comm1->data_size != comm2->data_size)
49 return DATA_SIZE_DIFF;
50 if(comm1->data == NULL && comm2->data == NULL)
52 /*if(comm1->data != NULL && comm2->data !=NULL) {
53 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
62 static void print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
63 if (_sg_mc_comms_determinism && !initial_global_state->comm_deterministic) {
64 XBT_INFO("****************************************************");
65 XBT_INFO("***** Non-deterministic communications pattern *****");
66 XBT_INFO("****************************************************");
67 XBT_INFO("The communications pattern of the process %d is different!", process);
70 XBT_INFO("Different communication type for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
73 XBT_INFO("Different communication rdv for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
76 XBT_INFO("Different communication source process for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
79 XBT_INFO("Different communication destination process for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
82 XBT_INFO("Different communication data size for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
85 XBT_INFO("Different communication data for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
90 MC_print_statistics(mc_stats);
92 } else if (_sg_mc_send_determinism && !initial_global_state->send_deterministic) {
93 XBT_INFO("*********************************************************");
94 XBT_INFO("***** Non-send-deterministic communications pattern *****");
95 XBT_INFO("*********************************************************");
96 XBT_INFO("The communications pattern of the process %d is different!", process);
99 XBT_INFO("Different communication type for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
102 XBT_INFO("Different communication rdv for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
105 XBT_INFO("Different communication source process for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
108 XBT_INFO("Different communication destination process for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
111 XBT_INFO("Different communication data size for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
114 XBT_INFO("Different communication data for communication %s at index %d", comm->type == SIMIX_COMM_SEND ? "Send" : "Recv", cursor);
119 MC_print_statistics(mc_stats);
124 static void print_communications_pattern()
126 unsigned int cursor = 0, cursor2 = 0;
127 mc_comm_pattern_t current_comm;
128 mc_list_comm_pattern_t current_list;
129 unsigned int current_process = 1;
130 while (current_process < simix_process_maxpid) {
131 current_list = (mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, current_process, mc_list_comm_pattern_t);
132 XBT_INFO("Communications from the process %u:", current_process);
133 while(cursor2 < current_list->index_comm){
134 current_comm = (mc_comm_pattern_t)xbt_dynar_get_as(current_list->list, cursor2, mc_comm_pattern_t);
135 if (current_comm->type == SIMIX_COMM_SEND) {
136 XBT_INFO("(%u) [(%lu) %s -> (%lu) %s] %s ", cursor,current_comm->src_proc,
137 current_comm->src_host, current_comm->dst_proc,
138 current_comm->dst_host, "iSend");
140 XBT_INFO("(%u) [(%lu) %s <- (%lu) %s] %s ", cursor, current_comm->dst_proc,
141 current_comm->dst_host, current_comm->src_proc,
142 current_comm->src_host, "iRecv");
152 static void print_incomplete_communications_pattern(){
153 unsigned int cursor = 0;
154 unsigned int current_process = 1;
155 xbt_dynar_t current_pattern;
156 mc_comm_pattern_t comm;
157 while (current_process < simix_process_maxpid) {
158 current_pattern = (xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, current_process, xbt_dynar_t);
159 XBT_INFO("Incomplete communications from the process %u:", current_process);
160 xbt_dynar_foreach(current_pattern, cursor, comm) {
161 XBT_DEBUG("(%u) Communication %p", cursor, comm);
168 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
170 mc_process_t process = &mc_model_checker->process;
172 comm_pattern->src_proc = comm->comm.src_proc->pid;
173 comm_pattern->dst_proc = comm->comm.dst_proc->pid;
174 comm_pattern->src_host = simcall_host_get_name(comm->comm.src_proc->smx_host);
175 comm_pattern->dst_host = simcall_host_get_name(comm->comm.dst_proc->smx_host);
176 if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
177 comm_pattern->data_size = *(comm->comm.dst_buff_size);
178 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
179 addr_pointed = *(void **) comm->comm.src_buff;
180 if (addr_pointed > (void*) process->heap_address
181 && addr_pointed < MC_process_get_heap(process)->breakval)
182 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
184 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
188 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
190 mc_list_comm_pattern_t list_comm_pattern = (mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, process, mc_list_comm_pattern_t);
193 mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, comm->index, mc_comm_pattern_t);
194 e_mc_comm_pattern_difference_t diff;
196 if((diff = compare_comm_pattern(initial_comm, comm)) != NONE_DIFF){
197 if (comm->type == SIMIX_COMM_SEND)
198 initial_global_state->send_deterministic = 0;
199 initial_global_state->comm_deterministic = 0;
200 print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
204 list_comm_pattern->index_comm++;
205 comm_pattern_free(comm);
209 /********** Non Static functions ***********/
211 void comm_pattern_free_voidp(void *p) {
212 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
215 void list_comm_pattern_free_voidp(void *p) {
216 list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
219 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type)
221 mc_process_t process = &mc_model_checker->process;
222 mc_comm_pattern_t pattern = NULL;
223 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
224 pattern->data_size = -1;
225 pattern->data = NULL;
227 pattern->index = ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, request->issuer->pid, mc_list_comm_pattern_t))->index_comm + xbt_dynar_length((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, request->issuer->pid, xbt_dynar_t));
231 if (call_type == MC_CALL_TYPE_SEND) {
232 /* Create comm pattern */
233 pattern->type = SIMIX_COMM_SEND;
234 pattern->comm = simcall_comm_isend__get__result(request);
235 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
236 pattern->src_proc = pattern->comm->comm.src_proc->pid;
237 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
238 if(pattern->comm->comm.src_buff != NULL){
239 pattern->data_size = pattern->comm->comm.src_buff_size;
240 pattern->data = xbt_malloc0(pattern->data_size);
241 addr_pointed = *(void **) pattern->comm->comm.src_buff;
242 if (addr_pointed > (void*) process->heap_address
243 && addr_pointed < MC_process_get_heap(process)->breakval)
244 memcpy(pattern->data, addr_pointed, pattern->data_size);
246 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
248 } else if (call_type == MC_CALL_TYPE_RECV) {
249 pattern->type = SIMIX_COMM_RECEIVE;
250 pattern->comm = simcall_comm_irecv__get__result(request);
251 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
252 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
253 pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
255 xbt_die("Unexpected call_type %i", (int) call_type);
258 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, request->issuer->pid, xbt_dynar_t), &pattern);
260 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, request->issuer->pid);
263 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, int backtracking) {
265 mc_comm_pattern_t current_comm_pattern;
266 unsigned int cursor = 0;
267 unsigned int src = comm->comm.src_proc->pid;
268 unsigned int dst = comm->comm.dst_proc->pid;
269 mc_comm_pattern_t src_comm_pattern;
270 mc_comm_pattern_t dst_comm_pattern;
271 int src_completed = 0, dst_completed = 0;
273 /* Complete comm pattern */
274 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, current_comm_pattern) {
275 if (current_comm_pattern-> comm == comm) {
276 update_comm_pattern(current_comm_pattern, comm);
278 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, &src_comm_pattern);
279 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", src, cursor);
284 xbt_die("Corresponding communication for the source process not found!");
288 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, current_comm_pattern) {
289 if (current_comm_pattern-> comm == comm) {
290 update_comm_pattern(current_comm_pattern, comm);
292 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, &dst_comm_pattern);
293 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", dst, cursor);
298 xbt_die("Corresponding communication for the destination process not found!");
300 if (!initial_global_state->initial_communications_pattern_done) {
301 /* Store comm pattern */
302 if(src_comm_pattern->index < xbt_dynar_length(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, src, mc_list_comm_pattern_t))->list)){
303 xbt_dynar_set(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, src, mc_list_comm_pattern_t))->list, src_comm_pattern->index, &src_comm_pattern);
304 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, src, mc_list_comm_pattern_t))->list->used++;
306 xbt_dynar_insert_at(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, src, mc_list_comm_pattern_t))->list, src_comm_pattern->index, &src_comm_pattern);
309 if(dst_comm_pattern->index < xbt_dynar_length(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, dst, mc_list_comm_pattern_t))->list)) {
310 xbt_dynar_set(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, dst, mc_list_comm_pattern_t))->list, dst_comm_pattern->index, &dst_comm_pattern);
311 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, dst, mc_list_comm_pattern_t))->list->used++;
313 xbt_dynar_insert_at(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, dst, mc_list_comm_pattern_t))->list, dst_comm_pattern->index, &dst_comm_pattern);
315 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, src, mc_list_comm_pattern_t))->index_comm++;
316 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, dst, mc_list_comm_pattern_t))->index_comm++;
318 /* Evaluate comm determinism */
319 deterministic_comm_pattern(src, src_comm_pattern, backtracking);
320 deterministic_comm_pattern(dst, dst_comm_pattern, backtracking);
324 /************************ Main algorithm ************************/
326 void MC_pre_modelcheck_comm_determinism(void)
330 mc_state_t initial_state = NULL;
331 smx_process_t process;
334 if (_sg_mc_visited > 0)
335 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
337 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), list_comm_pattern_free_voidp);
338 for (i=0; i<simix_process_maxpid; i++){
339 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
340 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
341 process_list_pattern->index_comm = 0;
342 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
344 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
345 for (i=0; i<simix_process_maxpid; i++){
346 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
347 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
350 initial_state = MC_state_new();
353 XBT_DEBUG("********* Start communication determinism verification *********");
355 /* Wait for requests (schedules processes) */
356 MC_wait_for_requests();
360 /* Get an enabled process and insert it in the interleave set of the initial state */
361 xbt_swag_foreach(process, simix_global->process_list) {
362 if (MC_process_is_enabled(process)) {
363 MC_state_interleave_process(initial_state, process);
367 xbt_fifo_unshift(mc_stack, initial_state);
373 void MC_modelcheck_comm_determinism(void)
376 char *req_str = NULL;
378 mc_visited_state_t visited_state = NULL;
379 smx_simcall_t req = NULL;
380 smx_process_t process = NULL;
381 mc_state_t state = NULL, next_state = NULL;
383 while (xbt_fifo_size(mc_stack) > 0) {
385 /* Get current state */
386 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
388 XBT_DEBUG("**************************************************");
389 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
390 xbt_fifo_size(mc_stack), state->num,
391 MC_state_interleave_size(state));
393 /* Update statistics */
394 mc_stats->visited_states++;
396 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
397 && (req = MC_state_get_request(state, &value))
398 && (visited_state == NULL)) {
400 req_str = MC_request_to_string(req, value);
401 XBT_DEBUG("Execute: %s", req_str);
404 if (dot_output != NULL) {
406 req_str = MC_request_get_dot_output(req, value);
410 MC_state_set_executed_request(state, req, value);
411 mc_stats->executed_transitions++;
413 /* TODO : handle test and testany simcalls */
414 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
415 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
416 call = mc_get_call_type(req);
419 /* Answer the request */
420 SIMIX_simcall_handle(req, value); /* After this call req is no longer useful */
423 if(!initial_global_state->initial_communications_pattern_done)
424 handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
426 handle_comm_pattern(call, req, value, NULL, 0);
429 /* Wait for requests (schedules processes) */
430 MC_wait_for_requests();
432 /* Create the new expanded state */
435 next_state = MC_state_new();
437 if ((visited_state = is_visited_state(next_state)) == NULL) {
439 /* Get enabled processes and insert them in the interleave set of the next state */
440 xbt_swag_foreach(process, simix_global->process_list) {
441 if (MC_process_is_enabled(process)) {
442 MC_state_interleave_process(next_state, process);
446 if (dot_output != NULL)
447 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
451 if (dot_output != NULL)
452 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
456 xbt_fifo_unshift(mc_stack, next_state);
458 if (dot_output != NULL)
465 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
466 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
467 } else if (visited_state != NULL) {
468 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);
470 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
475 if (!initial_global_state->initial_communications_pattern_done)
476 initial_global_state->initial_communications_pattern_done = 1;
478 /* Trash the current state, no longer needed */
479 xbt_fifo_shift(mc_stack);
480 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
481 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
485 visited_state = NULL;
487 /* Check for deadlocks */
488 if (MC_deadlock_check()) {
489 MC_show_deadlock(NULL);
495 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
496 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
497 /* We found a back-tracking point, let's loop */
498 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
499 xbt_fifo_unshift(mc_stack, state);
504 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
508 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
509 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
517 MC_print_statistics(mc_stats);