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)
157 comm_pattern->src_proc = comm->comm.src_proc->pid;
158 comm_pattern->dst_proc = comm->comm.dst_proc->pid;
159 comm_pattern->src_host =
160 simcall_host_get_name(comm->comm.src_proc->smx_host);
161 comm_pattern->dst_host =
162 simcall_host_get_name(comm->comm.dst_proc->smx_host);
163 if (comm_pattern->data_size == -1) {
164 comm_pattern->data_size = *(comm->comm.dst_buff_size);
165 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
166 addr_pointed = *(void **) comm->comm.src_buff;
167 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
168 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
170 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
174 /********** Non Static functions ***********/
176 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, mc_call_type call_type)
178 mc_comm_pattern_t pattern = NULL;
179 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
180 pattern->num = ++nb_comm_pattern;
181 pattern->data_size = -1;
183 if (call_type == MC_CALL_TYPE_SEND) { // ISEND
184 pattern->type = SIMIX_COMM_SEND;
185 pattern->comm = simcall_comm_isend__get__result(request);
186 pattern->src_proc = pattern->comm->comm.src_proc->pid;
187 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
188 pattern->data_size = pattern->comm->comm.src_buff_size;
189 pattern->data = xbt_malloc0(pattern->data_size);
190 addr_pointed = *(void **) pattern->comm->comm.src_buff;
191 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
192 memcpy(pattern->data, addr_pointed, pattern->data_size);
194 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
195 } else if (call_type == MC_CALL_TYPE_RECV) { // IRECV
196 pattern->type = SIMIX_COMM_RECEIVE;
197 pattern->comm = simcall_comm_irecv__get__result(request);
198 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
199 pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
201 xbt_die("Unexpected call_type %i", (int) call_type);
204 if (pattern->comm->comm.rdv != NULL)
205 pattern->rdv = strdup(pattern->comm->comm.rdv->name);
207 pattern->rdv = strdup(pattern->comm->comm.rdv_cpy->name);
209 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(list, request->issuer->pid, xbt_dynar_t), &pattern);
211 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);
215 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm)
217 mc_comm_pattern_t current_comm_pattern;
218 unsigned int cursor = 0;
220 unsigned int src = comm->comm.src_proc->pid;
221 unsigned int dst = comm->comm.dst_proc->pid;
222 int src_completed = 0, dst_completed = 0;
224 /* Looking for the corresponding communication in the comm pattern list of the src process */
225 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, index){
226 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);
227 if(current_comm_pattern->comm == comm){
228 update_comm_pattern(current_comm_pattern, comm);
229 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, src, xbt_dynar_t), cursor, NULL);
236 xbt_die("Corresponding communication for the source process not found!");
240 /* Looking for the corresponding communication in the comm pattern list of the dst process */
241 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, index){
242 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);
243 if(current_comm_pattern->comm == comm){
244 update_comm_pattern(current_comm_pattern, comm);
245 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, dst, xbt_dynar_t), cursor, NULL);
252 xbt_die("Corresponding communication for the dest process not found!");
257 /************************ Main algorithm ************************/
259 void MC_pre_modelcheck_comm_determinism(void)
262 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
264 mc_state_t initial_state = NULL;
265 smx_process_t process;
271 if (_sg_mc_visited > 0)
272 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
274 initial_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
275 for (i=0; i<simix_process_maxpid; i++){
276 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
277 xbt_dynar_insert_at(initial_communications_pattern, i, &process_pattern);
279 communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
280 for (i=0; i<simix_process_maxpid; i++){
281 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
282 xbt_dynar_insert_at(communications_pattern, i, &process_pattern);
284 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
285 for (i=0; i<simix_process_maxpid; i++){
286 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(int), NULL);
287 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
292 initial_state = MC_state_new();
296 XBT_DEBUG("********* Start communication determinism verification *********");
298 /* Wait for requests (schedules processes) */
299 MC_wait_for_requests();
303 /* Get an enabled process and insert it in the interleave set of the initial state */
304 xbt_swag_foreach(process, simix_global->process_list) {
305 if (MC_process_is_enabled(process)) {
306 MC_state_interleave_process(initial_state, process);
310 xbt_fifo_unshift(mc_stack, initial_state);
316 void MC_modelcheck_comm_determinism(void)
319 char *req_str = NULL;
321 mc_visited_state_t visited_state = NULL;
322 smx_simcall_t req = NULL;
323 smx_process_t process = NULL;
324 mc_state_t state = NULL, next_state = NULL;
325 xbt_dynar_t current_pattern;
327 while (xbt_fifo_size(mc_stack) > 0) {
329 /* Get current state */
332 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
334 XBT_DEBUG("**************************************************");
335 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
336 xbt_fifo_size(mc_stack), state->num,
337 MC_state_interleave_size(state));
339 /* Update statistics */
340 mc_stats->visited_states++;
342 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
343 && (req = MC_state_get_request(state, &value))
344 && (visited_state == NULL)) {
346 MC_LOG_REQUEST(mc_comm_determinism, req, value);
348 if (dot_output != NULL) {
350 req_str = MC_request_get_dot_output(req, value);
354 MC_state_set_executed_request(state, req, value);
355 mc_stats->executed_transitions++;
357 /* TODO : handle test and testany simcalls */
358 mc_call_type call = MC_CALL_TYPE_NONE;
359 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
360 call = mc_get_call_type(req);
363 /* Answer the request */
364 SIMIX_simcall_handle(req, value); /* After this call req is no longer useful */
367 current_pattern = !initial_global_state->initial_communications_pattern_done ? initial_communications_pattern : communications_pattern;
368 mc_update_comm_pattern(call, req, value, current_pattern);
371 /* Wait for requests (schedules processes) */
372 MC_wait_for_requests();
374 /* Create the new expanded state */
377 next_state = MC_state_new();
379 if ((visited_state = is_visited_state()) == NULL) {
381 /* Get enabled processes and insert them in the interleave set of the next state */
382 xbt_swag_foreach(process, simix_global->process_list) {
383 if (MC_process_is_enabled(process)) {
384 MC_state_interleave_process(next_state, process);
388 if (dot_output != NULL)
389 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
390 next_state->num, req_str);
394 if (dot_output != NULL)
395 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
396 visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
400 xbt_fifo_unshift(mc_stack, next_state);
402 if (dot_output != NULL)
409 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
410 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
411 } else if (visited_state != NULL) {
412 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);
414 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
419 if (initial_global_state->initial_communications_pattern_done) {
420 if (!visited_state) {
421 deterministic_pattern(communications_pattern, 0);
423 deterministic_pattern(communications_pattern, 1);
426 if (_sg_mc_comms_determinism && !initial_global_state->comm_deterministic) {
427 XBT_INFO("****************************************************");
428 XBT_INFO("***** Non-deterministic communications pattern *****");
429 XBT_INFO("****************************************************");
430 XBT_INFO("** Initial communications pattern (per process): **");
431 print_communications_pattern(initial_communications_pattern);
432 XBT_INFO("** Communications pattern counter-example (per process): **");
433 print_communications_pattern(communications_pattern);
434 MC_print_statistics(mc_stats);
437 } else if (_sg_mc_send_determinism && !initial_global_state->send_deterministic) {
439 ("*********************************************************");
441 ("***** Non-send-deterministic communications pattern *****");
443 ("*********************************************************");
444 XBT_INFO("** Initial communications pattern: **");
445 print_communications_pattern(initial_communications_pattern);
446 XBT_INFO("** Communications pattern counter-example: **");
447 print_communications_pattern(communications_pattern);
448 MC_print_statistics(mc_stats);
454 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);
460 XBT_DEBUG("Delete state %d at depth %d", state->num,
461 xbt_fifo_size(mc_stack) + 1);
465 visited_state = NULL;
467 /* Check for deadlocks */
468 if (MC_deadlock_check()) {
469 MC_show_deadlock(NULL);
475 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
476 if (MC_state_interleave_size(state)
477 && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
478 /* We found a back-tracking point, let's loop */
479 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
480 xbt_fifo_size(mc_stack) + 1);
481 xbt_fifo_unshift(mc_stack, state);
484 MC_replay(mc_stack, -1);
486 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
487 xbt_fifo_size(mc_stack));
490 XBT_DEBUG("Delete state %d at depth %d", state->num,
491 xbt_fifo_size(mc_stack) + 1);
492 MC_state_delete(state);
500 MC_print_statistics(mc_stats);