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. */
7 #include "mc_private.h"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
10 "Logging specific to MC communication determinism detection");
12 /********** Global variables **********/
14 xbt_dynar_t initial_communications_pattern;
15 xbt_dynar_t incomplete_communications_pattern;
16 xbt_dynar_t communications_pattern;
19 /********** Static functions ***********/
21 static void comm_pattern_free(mc_comm_pattern_t p)
29 static void comm_pattern_free_voidp(void *p)
31 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
34 static mc_comm_pattern_t get_comm_pattern_from_idx(xbt_dynar_t pattern,
36 e_smx_comm_type_t type,
39 mc_comm_pattern_t current_comm;
40 while (*idx < xbt_dynar_length(pattern)) {
42 (mc_comm_pattern_t) xbt_dynar_get_as(pattern, *idx, mc_comm_pattern_t);
43 if (current_comm->type == type && type == SIMIX_COMM_SEND) {
44 if (current_comm->src_proc == proc)
46 } else if (current_comm->type == type && type == SIMIX_COMM_RECEIVE) {
47 if (current_comm->dst_proc == proc)
55 static int compare_comm_pattern(mc_comm_pattern_t comm1,
56 mc_comm_pattern_t comm2)
58 if (strcmp(comm1->rdv, comm2->rdv) != 0)
60 if (comm1->src_proc != comm2->src_proc)
62 if (comm1->dst_proc != comm2->dst_proc)
64 if (comm1->data_size != comm2->data_size)
66 if (memcmp(comm1->data, comm2->data, comm1->data_size) != 0)
71 static void deterministic_pattern(xbt_dynar_t initial_pattern,
75 if (!xbt_dynar_is_empty(incomplete_communications_pattern))
77 ("Damn ! Some communications are incomplete that means one or several simcalls are not handle ... ");
79 unsigned int cursor = 0, send_index = 0, recv_index = 0;
80 mc_comm_pattern_t comm1, comm2;
81 int comm_comparison = 0;
82 int current_process = 0;
83 while (current_process < simix_process_maxpid) {
84 while (cursor < xbt_dynar_length(initial_pattern)) {
86 (mc_comm_pattern_t) xbt_dynar_get_as(initial_pattern, cursor,
88 if (comm1->type == SIMIX_COMM_SEND && comm1->src_proc == current_process) {
90 get_comm_pattern_from_idx(pattern, &send_index, comm1->type,
92 comm_comparison = compare_comm_pattern(comm1, comm2);
93 if (comm_comparison == 1) {
94 initial_global_state->send_deterministic = 0;
95 initial_global_state->comm_deterministic = 0;
99 } else if (comm1->type == SIMIX_COMM_RECEIVE
100 && comm1->dst_proc == current_process) {
102 get_comm_pattern_from_idx(pattern, &recv_index, comm1->type,
104 comm_comparison = compare_comm_pattern(comm1, comm2);
105 if (comm_comparison == 1) {
106 initial_global_state->comm_deterministic = 0;
107 if (!_sg_mc_send_determinism)
121 static void print_communications_pattern(xbt_dynar_t comms_pattern)
123 unsigned int cursor = 0;
124 mc_comm_pattern_t current_comm;
125 xbt_dynar_foreach(comms_pattern, cursor, current_comm) {
126 if (current_comm->type == SIMIX_COMM_SEND)
127 XBT_INFO("[(%lu) %s -> (%lu) %s] %s ", current_comm->src_proc,
128 current_comm->src_host, current_comm->dst_proc,
129 current_comm->dst_host, "iSend");
131 XBT_INFO("[(%lu) %s <- (%lu) %s] %s ", current_comm->dst_proc,
132 current_comm->dst_host, current_comm->src_proc,
133 current_comm->src_host, "iRecv");
138 /********** Non Static functions ***********/
140 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, int call)
142 mc_comm_pattern_t pattern = NULL;
143 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
144 pattern->num = ++nb_comm_pattern;
145 pattern->data_size = -1;
147 if (call == 1) { // ISEND
148 pattern->comm = simcall_comm_isend__get__result(request);
149 pattern->type = SIMIX_COMM_SEND;
150 pattern->src_proc = pattern->comm->comm.src_proc->pid;
151 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
152 pattern->data_size = pattern->comm->comm.src_buff_size;
153 pattern->data = xbt_malloc0(pattern->data_size);
154 addr_pointed = *(void **) pattern->comm->comm.src_buff;
155 if (addr_pointed > std_heap
156 && addr_pointed < ((xbt_mheap_t) std_heap)->breakval)
157 memcpy(pattern->data, addr_pointed, pattern->data_size);
159 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
161 pattern->comm = simcall_comm_irecv__get__result(request);
162 pattern->type = SIMIX_COMM_RECEIVE;
163 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
165 simcall_host_get_name(pattern->comm->comm.dst_proc->smx_host);
168 if (pattern->comm->comm.rdv != NULL)
169 pattern->rdv = strdup(pattern->comm->comm.rdv->name);
171 pattern->rdv = strdup(pattern->comm->comm.rdv_cpy->name);
173 xbt_dynar_push(list, &pattern);
175 xbt_dynar_push_as(incomplete_communications_pattern, int,
176 xbt_dynar_length(list) - 1);
180 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm)
182 mc_comm_pattern_t current_pattern;
183 unsigned int cursor = 0;
187 xbt_dynar_foreach(incomplete_communications_pattern, cursor, index) {
189 (mc_comm_pattern_t) xbt_dynar_get_as(list, index, mc_comm_pattern_t);
190 if (current_pattern->comm == comm) {
191 current_pattern->src_proc = comm->comm.src_proc->pid;
192 current_pattern->dst_proc = comm->comm.dst_proc->pid;
193 current_pattern->src_host =
194 simcall_host_get_name(comm->comm.src_proc->smx_host);
195 current_pattern->dst_host =
196 simcall_host_get_name(comm->comm.dst_proc->smx_host);
197 if (current_pattern->data_size == -1) {
198 current_pattern->data_size = *(comm->comm.dst_buff_size);
199 current_pattern->data = xbt_malloc0(current_pattern->data_size);
200 addr_pointed = *(void **) comm->comm.src_buff;
201 if (addr_pointed > std_heap
202 && addr_pointed < ((xbt_mheap_t) std_heap)->breakval)
203 memcpy(current_pattern->data, addr_pointed,
204 current_pattern->data_size);
206 memcpy(current_pattern->data, comm->comm.src_buff,
207 current_pattern->data_size);
209 xbt_dynar_remove_at(incomplete_communications_pattern, cursor, NULL);
218 /************************ Main algorithm ************************/
220 static void MC_modelcheck_comm_determinism(void)
223 char *req_str = NULL;
225 smx_simcall_t req = NULL;
226 smx_process_t process = NULL;
227 mc_state_t state = NULL, next_state = NULL;
228 int comm_pattern = 0, visited_state = -1;
229 smx_action_t current_comm;
231 while (xbt_fifo_size(mc_stack) > 0) {
233 /* Get current state */
236 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
238 XBT_DEBUG("**************************************************");
239 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
240 xbt_fifo_size(mc_stack), state->num,
241 MC_state_interleave_size(state));
243 /* Update statistics */
244 mc_stats->visited_states++;
246 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
247 && (req = MC_state_get_request(state, &value))
248 && (visited_state == -1)) {
250 /* Debug information */
251 if (XBT_LOG_ISENABLED(mc_comm_determinism, xbt_log_priority_debug)) {
252 req_str = MC_request_to_string(req, value);
253 XBT_DEBUG("Execute: %s", req_str);
258 if (dot_output != NULL)
259 req_str = MC_request_get_dot_output(req, value);
262 MC_state_set_executed_request(state, req, value);
263 mc_stats->executed_transitions++;
265 /* TODO : handle test and testany simcalls */
266 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
267 if (req->call == SIMCALL_COMM_ISEND)
269 else if (req->call == SIMCALL_COMM_IRECV)
271 else if (req->call == SIMCALL_COMM_WAIT)
273 else if (req->call == SIMCALL_COMM_WAITANY)
277 /* Answer the request */
278 SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
281 if (comm_pattern == 1 || comm_pattern == 2) {
282 if (!initial_global_state->initial_communications_pattern_done)
283 get_comm_pattern(initial_communications_pattern, req, comm_pattern);
285 get_comm_pattern(communications_pattern, req, comm_pattern);
286 } else if (comm_pattern == 3) {
287 current_comm = simcall_comm_wait__get__comm(req);
288 if (current_comm->comm.refcount == 1) { /* First wait only must be considered */
289 if (!initial_global_state->initial_communications_pattern_done)
290 complete_comm_pattern(initial_communications_pattern, current_comm);
292 complete_comm_pattern(communications_pattern, current_comm);
294 } else if (comm_pattern == 4) {
296 xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
298 if (current_comm->comm.refcount == 1) { /* First wait only must be considered */
299 if (!initial_global_state->initial_communications_pattern_done)
300 complete_comm_pattern(initial_communications_pattern, current_comm);
302 complete_comm_pattern(communications_pattern, current_comm);
309 /* Wait for requests (schedules processes) */
310 MC_wait_for_requests();
312 /* Create the new expanded state */
315 next_state = MC_state_new();
317 if ((visited_state = is_visited_state()) == -1) {
319 /* Get an enabled process and insert it in the interleave set of the next state */
320 xbt_swag_foreach(process, simix_global->process_list) {
321 if (MC_process_is_enabled(process)) {
322 MC_state_interleave_process(next_state, process);
326 if (dot_output != NULL)
327 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
328 next_state->num, req_str);
332 if (dot_output != NULL)
333 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
334 visited_state, req_str);
338 xbt_fifo_unshift(mc_stack, next_state);
340 if (dot_output != NULL)
347 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
348 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
349 } else if (visited_state != -1) {
350 XBT_DEBUG("State already visited, stop the exploration");
352 XBT_DEBUG("There are no more processes to interleave. (depth %d)",
353 xbt_fifo_size(mc_stack) + 1);
358 if (initial_global_state->initial_communications_pattern_done) {
359 if (visited_state == -1) {
360 deterministic_pattern(initial_communications_pattern,
361 communications_pattern);
362 if (initial_global_state->comm_deterministic == 0
363 && _sg_mc_comms_determinism) {
364 XBT_INFO("****************************************************");
365 XBT_INFO("***** Non-deterministic communications pattern *****");
366 XBT_INFO("****************************************************");
367 XBT_INFO("Initial communications pattern:");
368 print_communications_pattern(initial_communications_pattern);
369 XBT_INFO("Communications pattern counter-example:");
370 print_communications_pattern(communications_pattern);
371 MC_print_statistics(mc_stats);
373 } else if (initial_global_state->send_deterministic == 0
374 && _sg_mc_send_determinism) {
376 ("*********************************************************");
378 ("***** Non-send-deterministic communications pattern *****");
380 ("*********************************************************");
381 XBT_INFO("Initial communications pattern:");
382 print_communications_pattern(initial_communications_pattern);
383 XBT_INFO("Communications pattern counter-example:");
384 print_communications_pattern(communications_pattern);
385 MC_print_statistics(mc_stats);
392 initial_global_state->initial_communications_pattern_done = 1;
395 /* Trash the current state, no longer needed */
396 xbt_fifo_shift(mc_stack);
397 MC_state_delete(state);
398 XBT_DEBUG("Delete state %d at depth %d", state->num,
399 xbt_fifo_size(mc_stack) + 1);
405 /* Check for deadlocks */
406 if (MC_deadlock_check()) {
407 MC_show_deadlock(NULL);
411 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
412 if (MC_state_interleave_size(state)
413 && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
414 /* We found a back-tracking point, let's loop */
415 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
416 xbt_fifo_size(mc_stack) + 1);
417 xbt_fifo_unshift(mc_stack, state);
420 MC_replay(mc_stack, -1);
422 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
423 xbt_fifo_size(mc_stack));
426 XBT_DEBUG("Delete state %d at depth %d", state->num,
427 xbt_fifo_size(mc_stack) + 1);
428 MC_state_delete(state);
436 MC_print_statistics(mc_stats);
442 void MC_pre_modelcheck_comm_determinism(void)
445 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
447 mc_state_t initial_state = NULL;
448 smx_process_t process;
453 if (_sg_mc_visited > 0)
455 xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
457 initial_communications_pattern =
458 xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
459 communications_pattern =
460 xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
461 incomplete_communications_pattern = xbt_dynar_new(sizeof(int), NULL);
464 initial_state = MC_state_new();
468 XBT_DEBUG("********* Start communication determinism verification *********");
470 /* Wait for requests (schedules processes) */
471 MC_wait_for_requests();
473 if (_sg_mc_visited > 0) {
474 MC_ignore_heap(simix_global->process_to_run->data, 0);
475 MC_ignore_heap(simix_global->process_that_ran->data, 0);
480 /* Get an enabled process and insert it in the interleave set of the initial state */
481 xbt_swag_foreach(process, simix_global->process_list) {
482 if (MC_process_is_enabled(process)) {
483 MC_state_interleave_process(initial_state, process);
487 xbt_fifo_unshift(mc_stack, initial_state);
491 MC_modelcheck_comm_determinism();