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;
21 xbt_dynar_t communications_pattern;
24 /********** Static functions ***********/
26 static void comm_pattern_free(mc_comm_pattern_t p)
34 static void comm_pattern_free_voidp(void *p)
36 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
39 static mc_comm_pattern_t get_comm_pattern_from_idx(xbt_dynar_t pattern,
41 e_smx_comm_type_t type,
44 mc_comm_pattern_t current_comm;
45 while (*idx < xbt_dynar_length(pattern)) {
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)
51 } else if (current_comm->type == type && type == SIMIX_COMM_RECEIVE) {
52 if (current_comm->dst_proc == proc)
60 static int compare_comm_pattern(mc_comm_pattern_t comm1,
61 mc_comm_pattern_t comm2)
63 if (strcmp(comm1->rdv, comm2->rdv) != 0)
65 if (comm1->src_proc != comm2->src_proc)
67 if (comm1->dst_proc != comm2->dst_proc)
69 if (comm1->data_size != comm2->data_size)
71 if (memcmp(comm1->data, comm2->data, comm1->data_size) != 0)
76 static void deterministic_pattern(xbt_dynar_t pattern, int partial)
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;
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;
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;
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);
129 static void print_communications_pattern(xbt_dynar_t comms_pattern)
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");
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");
154 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
156 mc_process_t process = &mc_model_checker->process;
158 comm_pattern->src_proc = comm->comm.src_proc->pid;
159 comm_pattern->dst_proc = comm->comm.dst_proc->pid;
160 comm_pattern->src_host =
161 simcall_host_get_name(comm->comm.src_proc->smx_host);
162 comm_pattern->dst_host =
163 simcall_host_get_name(comm->comm.dst_proc->smx_host);
164 if (comm_pattern->data_size == -1) {
165 comm_pattern->data_size = *(comm->comm.dst_buff_size);
166 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
167 addr_pointed = *(void **) comm->comm.src_buff;
168 if (addr_pointed > (void*) process->heap_address
169 && addr_pointed < MC_process_get_heap(process)->breakval)
170 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
172 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
176 /********** Non Static functions ***********/
178 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, mc_call_type call_type)
180 mc_process_t process = &mc_model_checker->process;
181 mc_comm_pattern_t pattern = NULL;
182 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
183 pattern->num = ++nb_comm_pattern;
184 pattern->data_size = -1;
186 if (call_type == MC_CALL_TYPE_SEND) { // ISEND
187 pattern->type = SIMIX_COMM_SEND;
188 pattern->comm = simcall_comm_isend__get__result(request);
189 pattern->src_proc = pattern->comm->comm.src_proc->pid;
190 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
191 pattern->data_size = pattern->comm->comm.src_buff_size;
192 pattern->data = xbt_malloc0(pattern->data_size);
193 addr_pointed = *(void **) pattern->comm->comm.src_buff;
194 if (addr_pointed > (void*) process->heap_address
195 && addr_pointed < MC_process_get_heap(process)->breakval)
196 memcpy(pattern->data, addr_pointed, pattern->data_size);
198 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
199 } else if (call_type == MC_CALL_TYPE_RECV) { // IRECV
200 pattern->type = SIMIX_COMM_RECEIVE;
201 pattern->comm = simcall_comm_irecv__get__result(request);
202 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
203 pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
205 xbt_die("Unexpected call_type %i", (int) call_type);
208 if (pattern->comm->comm.rdv != NULL)
209 pattern->rdv = strdup(pattern->comm->comm.rdv->name);
211 pattern->rdv = strdup(pattern->comm->comm.rdv_cpy->name);
213 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(list, request->issuer->pid, xbt_dynar_t), &pattern);
215 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);
219 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm)
221 mc_comm_pattern_t current_comm_pattern;
222 unsigned int cursor = 0;
224 unsigned int src = comm->comm.src_proc->pid;
225 unsigned int dst = comm->comm.dst_proc->pid;
226 int src_completed = 0, dst_completed = 0;
228 /* Looking for the corresponding communication in the comm pattern list of the src process */
229 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, index){
230 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);
231 if(current_comm_pattern->comm == comm){
232 update_comm_pattern(current_comm_pattern, comm);
233 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, NULL);
240 xbt_die("Corresponding communication for the source process not found!");
244 /* Looking for the corresponding communication in the comm pattern list of the dst process */
245 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, index){
246 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);
247 if(current_comm_pattern->comm == comm){
248 update_comm_pattern(current_comm_pattern, comm);
249 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, NULL);
256 xbt_die("Corresponding communication for the dest process not found!");
261 /************************ Main algorithm ************************/
263 void MC_pre_modelcheck_comm_determinism(void)
266 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
268 mc_state_t initial_state = NULL;
269 smx_process_t process;
275 if (_sg_mc_visited > 0)
276 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
278 initial_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
279 for (i=0; i<simix_process_maxpid; i++){
280 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
281 xbt_dynar_insert_at(initial_communications_pattern, i, &process_pattern);
283 communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
284 for (i=0; i<simix_process_maxpid; i++){
285 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
286 xbt_dynar_insert_at(communications_pattern, i, &process_pattern);
288 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
289 for (i=0; i<simix_process_maxpid; i++){
290 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(int), NULL);
291 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
296 initial_state = MC_state_new();
300 XBT_DEBUG("********* Start communication determinism verification *********");
302 /* Wait for requests (schedules processes) */
303 MC_wait_for_requests();
307 /* Get an enabled process and insert it in the interleave set of the initial state */
308 xbt_swag_foreach(process, simix_global->process_list) {
309 if (MC_process_is_enabled(process)) {
310 MC_state_interleave_process(initial_state, process);
314 xbt_fifo_unshift(mc_stack, initial_state);
320 void MC_modelcheck_comm_determinism(void)
323 char *req_str = NULL;
325 mc_visited_state_t visited_state = NULL;
326 smx_simcall_t req = NULL;
327 smx_process_t process = NULL;
328 mc_state_t state = NULL, next_state = NULL;
329 xbt_dynar_t current_pattern;
331 while (xbt_fifo_size(mc_stack) > 0) {
333 /* Get current state */
336 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
338 XBT_DEBUG("**************************************************");
339 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
340 xbt_fifo_size(mc_stack), state->num,
341 MC_state_interleave_size(state));
343 /* Update statistics */
344 mc_stats->visited_states++;
346 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
347 && (req = MC_state_get_request(state, &value))
348 && (visited_state == NULL)) {
350 MC_LOG_REQUEST(mc_comm_determinism, req, value);
352 if (dot_output != NULL) {
354 req_str = MC_request_get_dot_output(req, value);
358 MC_state_set_executed_request(state, req, value);
359 mc_stats->executed_transitions++;
361 /* TODO : handle test and testany simcalls */
362 mc_call_type call = MC_CALL_TYPE_NONE;
363 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
364 call = mc_get_call_type(req);
367 /* Answer the request */
368 SIMIX_simcall_handle(req, value); /* After this call req is no longer useful */
371 current_pattern = !initial_global_state->initial_communications_pattern_done ? initial_communications_pattern : communications_pattern;
372 mc_update_comm_pattern(call, req, value, current_pattern);
375 /* Wait for requests (schedules processes) */
376 MC_wait_for_requests();
378 /* Create the new expanded state */
381 next_state = MC_state_new();
383 if ((visited_state = is_visited_state()) == NULL) {
385 /* Get enabled processes and insert them in the interleave set of the next state */
386 xbt_swag_foreach(process, simix_global->process_list) {
387 if (MC_process_is_enabled(process)) {
388 MC_state_interleave_process(next_state, process);
392 if (dot_output != NULL)
393 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
394 next_state->num, req_str);
398 if (dot_output != NULL)
399 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
400 visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
404 xbt_fifo_unshift(mc_stack, next_state);
406 if (dot_output != NULL)
413 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
414 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
415 } else if (visited_state != NULL) {
416 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);
418 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
423 if (initial_global_state->initial_communications_pattern_done) {
424 if (!visited_state) {
425 deterministic_pattern(communications_pattern, 0);
427 deterministic_pattern(communications_pattern, 1);
430 if (_sg_mc_comms_determinism && !initial_global_state->comm_deterministic) {
431 XBT_INFO("****************************************************");
432 XBT_INFO("***** Non-deterministic communications pattern *****");
433 XBT_INFO("****************************************************");
434 XBT_INFO("** Initial communications pattern (per process): **");
435 print_communications_pattern(initial_communications_pattern);
436 XBT_INFO("** Communications pattern counter-example (per process): **");
437 print_communications_pattern(communications_pattern);
438 MC_print_statistics(mc_stats);
441 } else if (_sg_mc_send_determinism && !initial_global_state->send_deterministic) {
443 ("*********************************************************");
445 ("***** Non-send-deterministic communications pattern *****");
447 ("*********************************************************");
448 XBT_INFO("** Initial communications pattern: **");
449 print_communications_pattern(initial_communications_pattern);
450 XBT_INFO("** Communications pattern counter-example: **");
451 print_communications_pattern(communications_pattern);
452 MC_print_statistics(mc_stats);
458 initial_global_state->initial_communications_pattern_done = 1;
461 /* Trash the current state, no longer needed */
462 xbt_fifo_shift(mc_stack);
463 MC_state_delete(state);
464 XBT_DEBUG("Delete state %d at depth %d", state->num,
465 xbt_fifo_size(mc_stack) + 1);
469 visited_state = NULL;
471 /* Check for deadlocks */
472 if (MC_deadlock_check()) {
473 MC_show_deadlock(NULL);
479 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
480 if (MC_state_interleave_size(state)
481 && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
482 /* We found a back-tracking point, let's loop */
483 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
484 xbt_fifo_size(mc_stack) + 1);
485 xbt_fifo_unshift(mc_stack, state);
488 MC_replay(mc_stack, -1);
490 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
491 xbt_fifo_size(mc_stack));
494 XBT_DEBUG("Delete state %d at depth %d", state->num,
495 xbt_fifo_size(mc_stack) + 1);
496 MC_state_delete(state);
504 MC_print_statistics(mc_stats);