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 void comm_pattern_free(mc_comm_pattern_t p)
33 static void list_comm_pattern_free(mc_list_comm_pattern_t l)
35 xbt_dynar_free(&(l->list));
40 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
41 if(comm1->type != comm2->type)
43 if (strcmp(comm1->rdv, comm2->rdv) != 0)
45 if (comm1->src_proc != comm2->src_proc)
47 if (comm1->dst_proc != comm2->dst_proc)
49 if (comm1->tag != comm2->tag)
51 if (comm1->data_size != comm2->data_size)
52 return DATA_SIZE_DIFF;
53 if(comm1->data == NULL && comm2->data == NULL)
55 if(comm1->data != NULL && comm2->data !=NULL) {
56 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
65 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
68 if(comm->type == SIMIX_COMM_SEND)
69 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
71 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
75 res = bprintf("%s Different type for communication #%d", type, cursor);
78 res = bprintf("%s Different rdv for communication #%d", type, cursor);
81 res = bprintf("%s Different tag for communication #%d", type, cursor);
84 res = bprintf("%s Different source for communication #%d", type, cursor);
87 res = bprintf("%s Different destination for communication #%d", type, cursor);
90 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
93 res = bprintf("%s\n Different data for communication #%d", type, cursor);
103 // FIXME, remote comm
104 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
106 mc_process_t process = &mc_model_checker->process;
108 smx_process_t src_proc = MC_smx_resolve_process(comm->comm.src_proc);
109 smx_process_t dst_proc = MC_smx_resolve_process(comm->comm.dst_proc);
110 comm_pattern->src_proc = src_proc->pid;
111 comm_pattern->dst_proc = dst_proc->pid;
112 // TODO, resolve host name
113 comm_pattern->src_host = MC_smx_process_get_host_name(src_proc);
114 comm_pattern->dst_host = MC_smx_process_get_host_name(dst_proc);
115 if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
116 comm_pattern->data_size = *(comm->comm.dst_buff_size);
117 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
118 addr_pointed = *(void **) comm->comm.src_buff;
119 if (addr_pointed > (void*) process->heap_address
120 && addr_pointed < MC_process_get_heap(process)->breakval)
121 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
123 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
127 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
129 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);
132 mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, list_comm_pattern->index_comm, mc_comm_pattern_t);
133 e_mc_comm_pattern_difference_t diff;
135 if((diff = compare_comm_pattern(initial_comm, comm)) != NONE_DIFF){
136 if (comm->type == SIMIX_COMM_SEND){
137 initial_global_state->send_deterministic = 0;
138 if(initial_global_state->send_diff != NULL)
139 xbt_free(initial_global_state->send_diff);
140 initial_global_state->send_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
142 initial_global_state->recv_deterministic = 0;
143 if(initial_global_state->recv_diff != NULL)
144 xbt_free(initial_global_state->recv_diff);
145 initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
147 if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
148 XBT_INFO("*********************************************************");
149 XBT_INFO("***** Non-send-deterministic communications pattern *****");
150 XBT_INFO("*********************************************************");
151 XBT_INFO("%s", initial_global_state->send_diff);
152 xbt_free(initial_global_state->send_diff);
153 initial_global_state->send_diff = NULL;
154 MC_print_statistics(mc_stats);
156 }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
157 XBT_INFO("****************************************************");
158 XBT_INFO("***** Non-deterministic communications pattern *****");
159 XBT_INFO("****************************************************");
160 XBT_INFO("%s", initial_global_state->send_diff);
161 XBT_INFO("%s", initial_global_state->recv_diff);
162 xbt_free(initial_global_state->send_diff);
163 initial_global_state->send_diff = NULL;
164 xbt_free(initial_global_state->recv_diff);
165 initial_global_state->recv_diff = NULL;
166 MC_print_statistics(mc_stats);
172 comm_pattern_free(comm);
176 /********** Non Static functions ***********/
178 void comm_pattern_free_voidp(void *p) {
179 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
182 void list_comm_pattern_free_voidp(void *p) {
183 list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
186 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
188 mc_process_t process = &mc_model_checker->process;
189 mc_comm_pattern_t pattern = NULL;
190 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
191 pattern->data_size = -1;
192 pattern->data = NULL;
194 const smx_process_t issuer = MC_smx_simcall_get_issuer(request);
195 mc_list_comm_pattern_t initial_pattern =
196 (mc_list_comm_pattern_t) xbt_dynar_get_as(initial_communications_pattern, issuer->pid, mc_list_comm_pattern_t);
197 xbt_dynar_t incomplete_pattern =
198 (xbt_dynar_t) xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t);
200 initial_pattern->index_comm + xbt_dynar_length(incomplete_pattern);
204 if (call_type == MC_CALL_TYPE_SEND) {
205 /* Create comm pattern */
206 pattern->type = SIMIX_COMM_SEND;
207 pattern->comm = simcall_comm_isend__get__result(request);
208 // FIXME, remote access to rdv->name
209 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
210 pattern->src_proc = MC_smx_resolve_process(pattern->comm->comm.src_proc)->pid;
211 pattern->src_host = MC_smx_process_get_host_name(issuer);
212 pattern->tag = ((MPI_Request)simcall_comm_isend__get__data(request))->tag;
213 if(pattern->comm->comm.src_buff != NULL){
214 pattern->data_size = pattern->comm->comm.src_buff_size;
215 pattern->data = xbt_malloc0(pattern->data_size);
216 addr_pointed = *(void **) pattern->comm->comm.src_buff;
217 if (addr_pointed > (void*) process->heap_address
218 && addr_pointed < MC_process_get_heap(process)->breakval)
219 memcpy(pattern->data, addr_pointed, pattern->data_size);
221 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
223 if(((MPI_Request)simcall_comm_isend__get__data(request))->detached){
224 if (!initial_global_state->initial_communications_pattern_done) {
225 /* Store comm pattern */
226 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);
228 /* Evaluate comm determinism */
229 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
230 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->index_comm++;
234 } else if (call_type == MC_CALL_TYPE_RECV) {
235 pattern->type = SIMIX_COMM_RECEIVE;
236 pattern->comm = simcall_comm_irecv__get__result(request);
237 // TODO, remote access
238 pattern->tag = ((MPI_Request)simcall_comm_irecv__get__data(request))->tag;
239 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
240 pattern->dst_proc = MC_smx_resolve_process(pattern->comm->comm.dst_proc)->pid;
241 // FIXME, remote process access
242 pattern->dst_host = MC_smx_process_get_host_name(issuer);
244 xbt_die("Unexpected call_type %i", (int) call_type);
247 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer->pid, xbt_dynar_t), &pattern);
249 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, issuer->pid);
252 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, unsigned int issuer, int backtracking) {
253 mc_comm_pattern_t current_comm_pattern;
254 unsigned int cursor = 0;
255 mc_comm_pattern_t comm_pattern;
258 /* Complete comm pattern */
259 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
260 if (current_comm_pattern-> comm == comm) {
261 update_comm_pattern(current_comm_pattern, comm);
263 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &comm_pattern);
264 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
269 xbt_die("Corresponding communication not found!");
271 if (!initial_global_state->initial_communications_pattern_done) {
272 /* Store comm pattern */
273 xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->list, &comm_pattern);
275 /* Evaluate comm determinism */
276 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
277 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->index_comm++;
282 /************************ Main algorithm ************************/
284 void MC_pre_modelcheck_comm_determinism(void)
288 mc_state_t initial_state = NULL;
289 smx_process_t process;
292 if (_sg_mc_visited > 0)
293 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
295 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), list_comm_pattern_free_voidp);
296 for (i=0; i < MC_smx_get_maxpid(); i++){
297 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
298 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
299 process_list_pattern->index_comm = 0;
300 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
302 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
303 for (i=0; i < MC_smx_get_maxpid(); i++){
304 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
305 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
308 initial_state = MC_state_new();
311 XBT_DEBUG("********* Start communication determinism verification *********");
313 /* Wait for requests (schedules processes) */
314 MC_wait_for_requests();
318 /* Get an enabled process and insert it in the interleave set of the initial state */
319 MC_EACH_SIMIX_PROCESS(process,
320 if (MC_process_is_enabled(process)) {
321 MC_state_interleave_process(initial_state, process);
325 xbt_fifo_unshift(mc_stack, initial_state);
331 void MC_modelcheck_comm_determinism(void)
334 char *req_str = NULL;
336 mc_visited_state_t visited_state = NULL;
337 smx_simcall_t req = NULL;
338 smx_process_t process = NULL;
339 mc_state_t state = NULL, next_state = NULL;
341 while (xbt_fifo_size(mc_stack) > 0) {
343 /* Get current state */
344 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
346 XBT_DEBUG("**************************************************");
347 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
348 xbt_fifo_size(mc_stack), state->num,
349 MC_state_interleave_size(state));
351 /* Update statistics */
352 mc_stats->visited_states++;
354 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
355 && (req = MC_state_get_request(state, &value))
356 && (visited_state == NULL)) {
358 req_str = MC_request_to_string(req, value);
359 XBT_DEBUG("Execute: %s", req_str);
362 if (dot_output != NULL) {
364 req_str = MC_request_get_dot_output(req, value);
368 MC_state_set_executed_request(state, req, value);
369 mc_stats->executed_transitions++;
371 /* TODO : handle test and testany simcalls */
372 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
373 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
374 call = mc_get_call_type(req);
377 /* Answer the request */
378 MC_simcall_handle(req, value); /* After this call req is no longer useful */
381 if(!initial_global_state->initial_communications_pattern_done)
382 handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
384 handle_comm_pattern(call, req, value, NULL, 0);
387 /* Wait for requests (schedules processes) */
388 MC_wait_for_requests();
390 /* 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)
423 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
424 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
425 } else if (visited_state != NULL) {
426 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);
428 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
433 if (!initial_global_state->initial_communications_pattern_done)
434 initial_global_state->initial_communications_pattern_done = 1;
436 /* Trash the current state, no longer needed */
437 xbt_fifo_shift(mc_stack);
438 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
439 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
443 visited_state = NULL;
445 /* Check for deadlocks */
446 if (MC_deadlock_check()) {
447 MC_show_deadlock(NULL);
453 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
454 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
455 /* We found a back-tracking point, let's loop */
456 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
457 xbt_fifo_unshift(mc_stack, state);
462 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
466 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
467 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
475 MC_print_statistics(mc_stats);