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"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
16 "Logging specific to MC communication determinism detection");
18 /********** Global variables **********/
20 xbt_dynar_t initial_communications_pattern;
21 xbt_dynar_t incomplete_communications_pattern;
23 /********** Static functions ***********/
25 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
26 if(comm1->type != comm2->type)
28 if (strcmp(comm1->rdv, comm2->rdv) != 0)
30 if (comm1->src_proc != comm2->src_proc)
32 if (comm1->dst_proc != comm2->dst_proc)
34 if (comm1->tag != comm2->tag)
36 if (comm1->data_size != comm2->data_size)
37 return DATA_SIZE_DIFF;
38 if(comm1->data == NULL && comm2->data == NULL)
40 if(comm1->data != NULL && comm2->data !=NULL) {
41 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
50 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
53 if(comm->type == SIMIX_COMM_SEND)
54 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
56 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
60 res = bprintf("%s Different type for communication #%d", type, cursor);
63 res = bprintf("%s Different rdv for communication #%d", type, cursor);
66 res = bprintf("%s Different tag for communication #%d", type, cursor);
69 res = bprintf("%s Different source for communication #%d", type, cursor);
72 res = bprintf("%s Different destination for communication #%d", type, cursor);
75 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
78 res = bprintf("%s\n Different data for communication #%d", type, cursor);
88 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
90 smx_process_t src_proc = MC_smx_resolve_process(comm->comm.src_proc);
91 smx_process_t dst_proc = MC_smx_resolve_process(comm->comm.dst_proc);
92 comm_pattern->src_proc = src_proc->pid;
93 comm_pattern->dst_proc = dst_proc->pid;
94 comm_pattern->src_host = MC_smx_process_get_host_name(src_proc);
95 comm_pattern->dst_host = MC_smx_process_get_host_name(dst_proc);
96 if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
97 comm_pattern->data_size = *(comm->comm.dst_buff_size);
98 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
99 MC_process_read_simple(&mc_model_checker->process,
100 comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
104 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
106 mc_list_comm_pattern_t list_comm_pattern = (mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, process, mc_list_comm_pattern_t);
109 mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, list_comm_pattern->index_comm, mc_comm_pattern_t);
110 e_mc_comm_pattern_difference_t diff;
112 if((diff = compare_comm_pattern(initial_comm, comm)) != NONE_DIFF){
113 if (comm->type == SIMIX_COMM_SEND){
114 initial_global_state->send_deterministic = 0;
115 if(initial_global_state->send_diff != NULL)
116 xbt_free(initial_global_state->send_diff);
117 initial_global_state->send_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
119 initial_global_state->recv_deterministic = 0;
120 if(initial_global_state->recv_diff != NULL)
121 xbt_free(initial_global_state->recv_diff);
122 initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
124 if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
125 XBT_INFO("*********************************************************");
126 XBT_INFO("***** Non-send-deterministic communications pattern *****");
127 XBT_INFO("*********************************************************");
128 XBT_INFO("%s", initial_global_state->send_diff);
129 xbt_free(initial_global_state->send_diff);
130 initial_global_state->send_diff = NULL;
131 MC_print_statistics(mc_stats);
133 }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
134 XBT_INFO("****************************************************");
135 XBT_INFO("***** Non-deterministic communications pattern *****");
136 XBT_INFO("****************************************************");
137 XBT_INFO("%s", initial_global_state->send_diff);
138 XBT_INFO("%s", initial_global_state->recv_diff);
139 xbt_free(initial_global_state->send_diff);
140 initial_global_state->send_diff = NULL;
141 xbt_free(initial_global_state->recv_diff);
142 initial_global_state->recv_diff = NULL;
143 MC_print_statistics(mc_stats);
149 MC_comm_pattern_free(comm);
153 /********** Non Static functions ***********/
155 void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
157 mc_comm_pattern_t pattern = xbt_new0(s_mc_comm_pattern_t, 1);
158 pattern->data_size = -1;
159 pattern->data = NULL;
161 // Fill initial_pattern->index_comm:
162 const smx_process_t issuer = MC_smx_simcall_get_issuer(request);
163 mc_list_comm_pattern_t initial_pattern =
164 (mc_list_comm_pattern_t) xbt_dynar_get_as(initial_communications_pattern, issuer->pid, mc_list_comm_pattern_t);
165 xbt_dynar_t incomplete_pattern =
166 (xbt_dynar_t) xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
168 initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
171 if (call_type == MC_CALL_TYPE_SEND) {
172 /* Create comm pattern */
173 pattern->type = SIMIX_COMM_SEND;
174 pattern->comm = simcall_comm_isend__get__result(request);
175 // TODO, resolve comm
176 // FIXME, remote access to rdv->name
177 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
178 pattern->src_proc = MC_smx_resolve_process(pattern->comm->comm.src_proc)->pid;
179 pattern->src_host = MC_smx_process_get_host_name(issuer);
180 pattern->tag = ((MPI_Request)simcall_comm_isend__get__data(request))->tag;
181 if(pattern->comm->comm.src_buff != NULL){
182 pattern->data_size = pattern->comm->comm.src_buff_size;
183 pattern->data = xbt_malloc0(pattern->data_size);
184 MC_process_read_simple(&mc_model_checker->process,
185 pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
187 if(((MPI_Request)simcall_comm_isend__get__data(request))->detached){
188 if (!initial_global_state->initial_communications_pattern_done) {
189 /* Store comm pattern */
190 xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->list, &pattern);
192 /* Evaluate comm determinism */
193 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
194 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->index_comm++;
198 } else if (call_type == MC_CALL_TYPE_RECV) {
199 pattern->type = SIMIX_COMM_RECEIVE;
200 pattern->comm = simcall_comm_irecv__get__result(request);
201 // TODO, remote access
202 pattern->tag = ((MPI_Request)simcall_comm_irecv__get__data(request))->tag;
203 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
204 pattern->dst_proc = MC_smx_resolve_process(pattern->comm->comm.dst_proc)->pid;
205 // FIXME, remote process access
206 pattern->dst_host = MC_smx_process_get_host_name(issuer);
208 xbt_die("Unexpected call_type %i", (int) call_type);
211 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t), &pattern);
213 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, issuer->pid);
216 void MC_complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, unsigned int issuer, int backtracking) {
217 mc_comm_pattern_t current_comm_pattern;
218 unsigned int cursor = 0;
219 mc_comm_pattern_t comm_pattern;
222 /* Complete comm pattern */
223 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
224 if (current_comm_pattern-> comm == comm) {
225 update_comm_pattern(current_comm_pattern, comm);
227 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &comm_pattern);
228 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
233 xbt_die("Corresponding communication not found!");
235 if (!initial_global_state->initial_communications_pattern_done) {
236 /* Store comm pattern */
237 xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->list, &comm_pattern);
239 /* Evaluate comm determinism */
240 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
241 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->index_comm++;
246 /************************ Main algorithm ************************/
248 void MC_pre_modelcheck_comm_determinism(void)
252 mc_state_t initial_state = NULL;
253 smx_process_t process;
256 if (_sg_mc_visited > 0)
257 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
259 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), MC_list_comm_pattern_free_voidp);
260 for (i=0; i < MC_smx_get_maxpid(); i++){
261 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
262 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
263 process_list_pattern->index_comm = 0;
264 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
266 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
267 for (i=0; i < MC_smx_get_maxpid(); i++){
268 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
269 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
272 initial_state = MC_state_new();
275 XBT_DEBUG("********* Start communication determinism verification *********");
277 /* Wait for requests (schedules processes) */
278 MC_wait_for_requests();
282 /* Get an enabled process and insert it in the interleave set of the initial state */
283 MC_EACH_SIMIX_PROCESS(process,
284 if (MC_process_is_enabled(process)) {
285 MC_state_interleave_process(initial_state, process);
289 xbt_fifo_unshift(mc_stack, initial_state);
295 void MC_modelcheck_comm_determinism(void)
298 char *req_str = NULL;
300 mc_visited_state_t visited_state = NULL;
301 smx_simcall_t req = NULL;
302 smx_process_t process = NULL;
303 mc_state_t state = NULL, next_state = NULL;
305 while (xbt_fifo_size(mc_stack) > 0) {
307 /* Get current state */
308 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
310 XBT_DEBUG("**************************************************");
311 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
312 xbt_fifo_size(mc_stack), state->num,
313 MC_state_interleave_size(state));
315 /* Update statistics */
316 mc_stats->visited_states++;
318 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
319 && (req = MC_state_get_request(state, &value))
320 && (visited_state == NULL)) {
322 req_str = MC_request_to_string(req, value);
323 XBT_DEBUG("Execute: %s", req_str);
326 if (dot_output != NULL) {
328 req_str = MC_request_get_dot_output(req, value);
332 MC_state_set_executed_request(state, req, value);
333 mc_stats->executed_transitions++;
335 /* TODO : handle test and testany simcalls */
336 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
337 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
338 call = MC_get_call_type(req);
341 /* Answer the request */
342 MC_simcall_handle(req, value); /* After this call req is no longer useful */
345 if(!initial_global_state->initial_communications_pattern_done)
346 MC_handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
348 MC_handle_comm_pattern(call, req, value, NULL, 0);
351 /* Wait for requests (schedules processes) */
352 MC_wait_for_requests();
354 /* Create the new expanded state */
357 next_state = MC_state_new();
359 if ((visited_state = is_visited_state(next_state)) == NULL) {
361 /* Get enabled processes and insert them in the interleave set of the next state */
362 MC_EACH_SIMIX_PROCESS(process,
363 if (MC_process_is_enabled(process)) {
364 MC_state_interleave_process(next_state, process);
368 if (dot_output != NULL)
369 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
373 if (dot_output != NULL)
374 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
378 xbt_fifo_unshift(mc_stack, next_state);
380 if (dot_output != NULL)
387 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
388 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
389 } else if (visited_state != NULL) {
390 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);
392 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
397 if (!initial_global_state->initial_communications_pattern_done)
398 initial_global_state->initial_communications_pattern_done = 1;
400 /* Trash the current state, no longer needed */
401 xbt_fifo_shift(mc_stack);
402 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
403 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
407 visited_state = NULL;
409 /* Check for deadlocks */
410 if (MC_deadlock_check()) {
411 MC_show_deadlock(NULL);
417 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
418 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
419 /* We found a back-tracking point, let's loop */
420 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
421 xbt_fifo_unshift(mc_stack, state);
426 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
430 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
431 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
439 MC_print_statistics(mc_stats);