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 void MC_pre_modelcheck_comm_determinism(void)
223 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
225 mc_state_t initial_state = NULL;
226 smx_process_t process;
231 if (_sg_mc_visited > 0)
233 xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
235 initial_communications_pattern =
236 xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
237 communications_pattern =
238 xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
239 incomplete_communications_pattern = xbt_dynar_new(sizeof(int), NULL);
242 initial_state = MC_state_new();
246 XBT_DEBUG("********* Start communication determinism verification *********");
248 /* Wait for requests (schedules processes) */
249 MC_wait_for_requests();
253 /* Get an enabled process and insert it in the interleave set of the initial state */
254 xbt_swag_foreach(process, simix_global->process_list) {
255 if (MC_process_is_enabled(process)) {
256 MC_state_interleave_process(initial_state, process);
260 xbt_fifo_unshift(mc_stack, initial_state);
266 void MC_modelcheck_comm_determinism(void)
269 char *req_str = NULL;
271 smx_simcall_t req = NULL;
272 smx_process_t process = NULL;
273 mc_state_t state = NULL, next_state = NULL;
274 int comm_pattern = 0, visited_state = -1;
275 smx_action_t current_comm;
277 while (xbt_fifo_size(mc_stack) > 0) {
279 /* Get current state */
282 xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
284 XBT_DEBUG("**************************************************");
285 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
286 xbt_fifo_size(mc_stack), state->num,
287 MC_state_interleave_size(state));
289 /* Update statistics */
290 mc_stats->visited_states++;
292 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
293 && (req = MC_state_get_request(state, &value))
294 && (visited_state == -1)) {
296 /* Debug information */
297 if (XBT_LOG_ISENABLED(mc_comm_determinism, xbt_log_priority_debug)) {
298 req_str = MC_request_to_string(req, value);
299 XBT_DEBUG("Execute: %s", req_str);
304 if (dot_output != NULL)
305 req_str = MC_request_get_dot_output(req, value);
308 MC_state_set_executed_request(state, req, value);
309 mc_stats->executed_transitions++;
311 /* TODO : handle test and testany simcalls */
312 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
313 if (req->call == SIMCALL_COMM_ISEND)
315 else if (req->call == SIMCALL_COMM_IRECV)
317 else if (req->call == SIMCALL_COMM_WAIT)
319 else if (req->call == SIMCALL_COMM_WAITANY)
323 /* Answer the request */
324 SIMIX_simcall_pre(req, value); /* After this call req is no longer usefull */
327 if (comm_pattern == 1 || comm_pattern == 2) {
328 if (!initial_global_state->initial_communications_pattern_done)
329 get_comm_pattern(initial_communications_pattern, req, comm_pattern);
331 get_comm_pattern(communications_pattern, req, comm_pattern);
332 } else if (comm_pattern == 3) {
333 current_comm = simcall_comm_wait__get__comm(req);
334 if (current_comm->comm.refcount == 1) { /* First wait only must be considered */
335 if (!initial_global_state->initial_communications_pattern_done)
336 complete_comm_pattern(initial_communications_pattern, current_comm);
338 complete_comm_pattern(communications_pattern, current_comm);
340 } else if (comm_pattern == 4) {
342 xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
344 if (current_comm->comm.refcount == 1) { /* First wait only must be considered */
345 if (!initial_global_state->initial_communications_pattern_done)
346 complete_comm_pattern(initial_communications_pattern, current_comm);
348 complete_comm_pattern(communications_pattern, current_comm);
355 /* Wait for requests (schedules processes) */
356 MC_wait_for_requests();
358 /* Create the new expanded state */
361 next_state = MC_state_new();
363 if ((visited_state = is_visited_state()) == -1) {
365 /* Get an enabled process and insert it in the interleave set of the next state */
366 xbt_swag_foreach(process, simix_global->process_list) {
367 if (MC_process_is_enabled(process)) {
368 MC_state_interleave_process(next_state, process);
372 if (dot_output != NULL)
373 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
374 next_state->num, req_str);
378 if (dot_output != NULL)
379 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num,
380 visited_state, req_str);
384 xbt_fifo_unshift(mc_stack, next_state);
386 if (dot_output != NULL)
393 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
394 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
395 } else if (visited_state != -1) {
396 XBT_DEBUG("State already visited, exploration stopped on this path.");
398 XBT_DEBUG("There are no more processes to interleave. (depth %d)",
399 xbt_fifo_size(mc_stack) + 1);
404 if (initial_global_state->initial_communications_pattern_done) {
405 if (visited_state == -1) {
406 deterministic_pattern(initial_communications_pattern,
407 communications_pattern);
408 if (initial_global_state->comm_deterministic == 0
409 && _sg_mc_comms_determinism) {
410 XBT_INFO("****************************************************");
411 XBT_INFO("***** Non-deterministic communications pattern *****");
412 XBT_INFO("****************************************************");
413 XBT_INFO("Initial communications pattern:");
414 print_communications_pattern(initial_communications_pattern);
415 XBT_INFO("Communications pattern counter-example:");
416 print_communications_pattern(communications_pattern);
417 MC_print_statistics(mc_stats);
420 } else if (initial_global_state->send_deterministic == 0
421 && _sg_mc_send_determinism) {
423 ("*********************************************************");
425 ("***** Non-send-deterministic communications pattern *****");
427 ("*********************************************************");
428 XBT_INFO("Initial communications pattern:");
429 print_communications_pattern(initial_communications_pattern);
430 XBT_INFO("Communications pattern counter-example:");
431 print_communications_pattern(communications_pattern);
432 MC_print_statistics(mc_stats);
440 initial_global_state->initial_communications_pattern_done = 1;
443 /* Trash the current state, no longer needed */
444 xbt_fifo_shift(mc_stack);
445 MC_state_delete(state);
446 XBT_DEBUG("Delete state %d at depth %d", state->num,
447 xbt_fifo_size(mc_stack) + 1);
453 /* Check for deadlocks */
454 if (MC_deadlock_check()) {
455 MC_show_deadlock(NULL);
461 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
462 if (MC_state_interleave_size(state)
463 && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
464 /* We found a back-tracking point, let's loop */
465 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num,
466 xbt_fifo_size(mc_stack) + 1);
467 xbt_fifo_unshift(mc_stack, state);
470 MC_replay(mc_stack, -1);
472 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num,
473 xbt_fifo_size(mc_stack));
476 XBT_DEBUG("Delete state %d at depth %d", state->num,
477 xbt_fifo_size(mc_stack) + 1);
478 MC_state_delete(state);
486 MC_print_statistics(mc_stats);