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 #include "mc_client.h"
16 using simgrid::mc::remote;
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
21 "Logging specific to MC communication determinism detection");
23 /********** Global variables **********/
25 xbt_dynar_t initial_communications_pattern;
26 xbt_dynar_t incomplete_communications_pattern;
28 /********** Static functions ***********/
30 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
31 if(comm1->type != comm2->type)
33 if (strcmp(comm1->rdv, comm2->rdv) != 0)
35 if (comm1->src_proc != comm2->src_proc)
37 if (comm1->dst_proc != comm2->dst_proc)
39 if (comm1->tag != comm2->tag)
41 if (comm1->data_size != comm2->data_size)
42 return DATA_SIZE_DIFF;
43 if(comm1->data == NULL && comm2->data == NULL)
45 if(comm1->data != NULL && comm2->data !=NULL) {
46 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
55 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
58 if(comm->type == SIMIX_COMM_SEND)
59 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
61 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
65 res = bprintf("%s Different type for communication #%d", type, cursor);
68 res = bprintf("%s Different rdv for communication #%d", type, cursor);
71 res = bprintf("%s Different tag for communication #%d", type, cursor);
74 res = bprintf("%s Different source for communication #%d", type, cursor);
77 res = bprintf("%s Different destination for communication #%d", type, cursor);
80 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
83 res = bprintf("%s\n Different data for communication #%d", type, cursor);
93 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm_addr)
96 mc_model_checker->process().read(&comm, remote(comm_addr));
98 smx_process_t src_proc = MC_smx_resolve_process(comm.comm.src_proc);
99 smx_process_t dst_proc = MC_smx_resolve_process(comm.comm.dst_proc);
100 comm_pattern->src_proc = src_proc->pid;
101 comm_pattern->dst_proc = dst_proc->pid;
102 comm_pattern->src_host = MC_smx_process_get_host_name(src_proc);
103 comm_pattern->dst_host = MC_smx_process_get_host_name(dst_proc);
104 if (comm_pattern->data_size == -1 && comm.comm.src_buff != NULL) {
106 mc_model_checker->process().read(
107 &buff_size, remote(comm.comm.dst_buff_size));
108 comm_pattern->data_size = buff_size;
109 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
110 mc_model_checker->process().read_bytes(
111 comm_pattern->data, comm_pattern->data_size,
112 remote(comm.comm.src_buff));
116 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
118 mc_list_comm_pattern_t list =
119 xbt_dynar_get_as(initial_communications_pattern, process, mc_list_comm_pattern_t);
122 mc_comm_pattern_t initial_comm =
123 xbt_dynar_get_as(list->list, list->index_comm, mc_comm_pattern_t);
124 e_mc_comm_pattern_difference_t diff =
125 compare_comm_pattern(initial_comm, comm);
127 if (diff != NONE_DIFF) {
128 if (comm->type == SIMIX_COMM_SEND){
129 initial_global_state->send_deterministic = 0;
130 if(initial_global_state->send_diff != NULL)
131 xbt_free(initial_global_state->send_diff);
132 initial_global_state->send_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
134 initial_global_state->recv_deterministic = 0;
135 if(initial_global_state->recv_diff != NULL)
136 xbt_free(initial_global_state->recv_diff);
137 initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list->index_comm + 1);
139 if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
140 XBT_INFO("*********************************************************");
141 XBT_INFO("***** Non-send-deterministic communications pattern *****");
142 XBT_INFO("*********************************************************");
143 XBT_INFO("%s", initial_global_state->send_diff);
144 xbt_free(initial_global_state->send_diff);
145 initial_global_state->send_diff = NULL;
146 MC_print_statistics(mc_stats);
148 }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
149 XBT_INFO("****************************************************");
150 XBT_INFO("***** Non-deterministic communications pattern *****");
151 XBT_INFO("****************************************************");
152 XBT_INFO("%s", initial_global_state->send_diff);
153 XBT_INFO("%s", initial_global_state->recv_diff);
154 xbt_free(initial_global_state->send_diff);
155 initial_global_state->send_diff = NULL;
156 xbt_free(initial_global_state->recv_diff);
157 initial_global_state->recv_diff = NULL;
158 MC_print_statistics(mc_stats);
164 MC_comm_pattern_free(comm);
168 /********** Non Static functions ***********/
170 void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
172 const smx_process_t issuer = MC_smx_simcall_get_issuer(request);
173 mc_list_comm_pattern_t initial_pattern = xbt_dynar_get_as(
174 initial_communications_pattern, issuer->pid, mc_list_comm_pattern_t);
175 xbt_dynar_t incomplete_pattern = xbt_dynar_get_as(
176 incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
178 mc_comm_pattern_t pattern = xbt_new0(s_mc_comm_pattern_t, 1);
179 pattern->data_size = -1;
180 pattern->data = NULL;
182 initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
184 if (call_type == MC_CALL_TYPE_SEND) {
185 /* Create comm pattern */
186 pattern->type = SIMIX_COMM_SEND;
187 pattern->comm_addr = simcall_comm_isend__get__result(request);
189 s_smx_synchro_t synchro = mc_model_checker->process().read<s_smx_synchro_t>(
190 (std::uint64_t) pattern->comm_addr);
192 char* remote_name = mc_model_checker->process().read<char*>(
193 (std::uint64_t)(synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name));
194 pattern->rdv = mc_model_checker->process().read_string(remote_name);
195 pattern->src_proc = MC_smx_resolve_process(synchro.comm.src_proc)->pid;
196 pattern->src_host = MC_smx_process_get_host_name(issuer);
198 struct s_smpi_mpi_request mpi_request =
199 mc_model_checker->process().read<s_smpi_mpi_request>(
200 (std::uint64_t) simcall_comm_isend__get__data(request));
201 pattern->tag = mpi_request.tag;
203 if(synchro.comm.src_buff != NULL){
204 pattern->data_size = synchro.comm.src_buff_size;
205 pattern->data = xbt_malloc0(pattern->data_size);
206 mc_model_checker->process().read_bytes(
207 pattern->data, pattern->data_size, remote(synchro.comm.src_buff));
209 if(mpi_request.detached){
210 if (!initial_global_state->initial_communications_pattern_done) {
211 /* Store comm pattern */
214 initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t
218 /* Evaluate comm determinism */
219 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
221 initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t
226 } else if (call_type == MC_CALL_TYPE_RECV) {
227 pattern->type = SIMIX_COMM_RECEIVE;
228 pattern->comm_addr = simcall_comm_irecv__get__result(request);
230 struct s_smpi_mpi_request mpi_request;
231 mc_model_checker->process().read(
232 &mpi_request, remote((struct s_smpi_mpi_request*)simcall_comm_irecv__get__data(request)));
233 pattern->tag = mpi_request.tag;
235 s_smx_synchro_t synchro;
236 mc_model_checker->process().read(&synchro, remote(pattern->comm_addr));
239 mc_model_checker->process().read(&remote_name,
240 remote(synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name));
241 pattern->rdv = mc_model_checker->process().read_string(remote_name);
242 pattern->dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc)->pid;
243 pattern->dst_host = MC_smx_process_get_host_name(issuer);
245 xbt_die("Unexpected call_type %i", (int) call_type);
249 xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t),
252 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, issuer->pid);
255 void MC_complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm_addr, unsigned int issuer, int backtracking) {
256 mc_comm_pattern_t current_comm_pattern;
257 unsigned int cursor = 0;
258 mc_comm_pattern_t comm_pattern;
261 /* Complete comm pattern */
262 xbt_dynar_foreach(xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
263 if (current_comm_pattern->comm_addr == comm_addr) {
264 update_comm_pattern(current_comm_pattern, comm_addr);
267 xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t),
268 cursor, &comm_pattern);
269 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
274 xbt_die("Corresponding communication not found!");
276 mc_list_comm_pattern_t pattern = xbt_dynar_get_as(
277 initial_communications_pattern, issuer, mc_list_comm_pattern_t);
279 if (!initial_global_state->initial_communications_pattern_done) {
280 /* Store comm pattern */
281 xbt_dynar_push(pattern->list, &comm_pattern);
283 /* Evaluate comm determinism */
284 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
285 pattern->index_comm++;
290 /************************ Main algorithm ************************/
292 static void MC_modelcheck_comm_determinism_main(void);
294 static void MC_pre_modelcheck_comm_determinism(void)
296 mc_state_t initial_state = NULL;
297 smx_process_t process;
299 const int maxpid = MC_smx_get_maxpid();
301 if (_sg_mc_visited > 0)
302 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
304 // Create initial_communications_pattern elements:
305 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), MC_list_comm_pattern_free_voidp);
306 for (i=0; i < maxpid; i++){
307 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
308 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
309 process_list_pattern->index_comm = 0;
310 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
313 // Create incomplete_communications_pattern elements:
314 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
315 for (i=0; i < maxpid; i++){
316 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
317 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
320 initial_state = MC_state_new();
322 XBT_DEBUG("********* Start communication determinism verification *********");
324 /* Wait for requests (schedules processes) */
325 MC_wait_for_requests();
327 /* Get an enabled process and insert it in the interleave set of the initial state */
328 MC_EACH_SIMIX_PROCESS(process,
329 if (MC_process_is_enabled(process)) {
330 MC_state_interleave_process(initial_state, process);
334 xbt_fifo_unshift(mc_stack, initial_state);
337 static void MC_modelcheck_comm_determinism_main(void)
340 char *req_str = NULL;
342 mc_visited_state_t visited_state = NULL;
343 smx_simcall_t req = NULL;
344 smx_process_t process = NULL;
345 mc_state_t state = NULL, next_state = NULL;
347 while (xbt_fifo_size(mc_stack) > 0) {
349 /* Get current state */
350 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
352 XBT_DEBUG("**************************************************");
353 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
354 xbt_fifo_size(mc_stack), state->num,
355 MC_state_interleave_size(state));
357 /* Update statistics */
358 mc_stats->visited_states++;
360 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
361 && (req = MC_state_get_request(state, &value))
362 && (visited_state == NULL)) {
364 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
365 XBT_DEBUG("Execute: %s", req_str);
368 if (dot_output != NULL) {
369 req_str = MC_request_get_dot_output(req, value);
372 MC_state_set_executed_request(state, req, value);
373 mc_stats->executed_transitions++;
375 /* TODO : handle test and testany simcalls */
376 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
377 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
378 call = MC_get_call_type(req);
381 /* Answer the request */
382 MC_simcall_handle(req, value); /* After this call req is no longer useful */
384 if(!initial_global_state->initial_communications_pattern_done)
385 MC_handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
387 MC_handle_comm_pattern(call, req, value, NULL, 0);
389 /* Wait for requests (schedules processes) */
390 MC_wait_for_requests();
392 /* Create the new expanded state */
393 next_state = MC_state_new();
395 if ((visited_state = is_visited_state(next_state)) == NULL) {
397 /* Get enabled processes and insert them in the interleave set of the next state */
398 MC_EACH_SIMIX_PROCESS(process,
399 if (MC_process_is_enabled(process)) {
400 MC_state_interleave_process(next_state, process);
404 if (dot_output != NULL)
405 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
409 if (dot_output != NULL)
410 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
414 xbt_fifo_unshift(mc_stack, next_state);
416 if (dot_output != NULL)
421 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
422 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
423 } else if (visited_state != NULL) {
424 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);
426 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
429 if (!initial_global_state->initial_communications_pattern_done)
430 initial_global_state->initial_communications_pattern_done = 1;
432 /* Trash the current state, no longer needed */
433 xbt_fifo_shift(mc_stack);
434 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
435 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
437 visited_state = NULL;
439 /* Check for deadlocks */
440 if (MC_deadlock_check()) {
441 MC_show_deadlock(NULL);
445 while ((state = (mc_state_t) xbt_fifo_shift(mc_stack)) != NULL) {
446 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
447 /* We found a back-tracking point, let's loop */
448 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
449 xbt_fifo_unshift(mc_stack, state);
453 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
457 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
458 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
464 MC_print_statistics(mc_stats);
468 void MC_modelcheck_comm_determinism(void)
470 XBT_INFO("Check communication determinism");
471 mc_reduce_kind = e_mc_reduce_none;
472 MC_wait_for_requests();
474 if (mc_mode == MC_MODE_CLIENT) {
475 // This will move somehwere else:
476 MC_client_handle_messages();
479 /* Create exploration stack */
480 mc_stack = xbt_fifo_new();
482 MC_pre_modelcheck_comm_determinism();
484 initial_global_state = xbt_new0(s_mc_global_t, 1);
485 initial_global_state->snapshot = MC_take_snapshot(0);
486 initial_global_state->initial_communications_pattern_done = 0;
487 initial_global_state->recv_deterministic = 1;
488 initial_global_state->send_deterministic = 1;
489 initial_global_state->recv_diff = NULL;
490 initial_global_state->send_diff = NULL;
492 MC_modelcheck_comm_determinism_main();