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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_determinism, mc,
15 "Logging specific to MC communication determinism detection");
17 /********** Global variables **********/
19 xbt_dynar_t initial_communications_pattern;
20 xbt_dynar_t incomplete_communications_pattern;
22 /********** Static functions ***********/
24 static void comm_pattern_free(mc_comm_pattern_t p)
32 static void list_comm_pattern_free(mc_list_comm_pattern_t l)
34 xbt_dynar_free(&(l->list));
39 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
40 if(comm1->type != comm2->type)
42 if (strcmp(comm1->rdv, comm2->rdv) != 0)
44 if (comm1->src_proc != comm2->src_proc)
46 if (comm1->dst_proc != comm2->dst_proc)
48 if (comm1->tag != comm2->tag)
50 if (comm1->data_size != comm2->data_size)
51 return DATA_SIZE_DIFF;
52 if(comm1->data == NULL && comm2->data == NULL)
54 if(comm1->data != NULL && comm2->data !=NULL) {
55 if (!memcmp(comm1->data, comm2->data, comm1->data_size))
64 static char* print_determinism_result(e_mc_comm_pattern_difference_t diff, int process, mc_comm_pattern_t comm, unsigned int cursor) {
67 if(comm->type == SIMIX_COMM_SEND)
68 type = bprintf("The send communications pattern of the process %d is different!", process - 1);
70 type = bprintf("The recv communications pattern of the process %d is different!", process - 1);
74 res = bprintf("%s Different type for communication #%d", type, cursor);
77 res = bprintf("%s Different rdv for communication #%d", type, cursor);
80 res = bprintf("%s Different tag for communication #%d", type, cursor);
83 res = bprintf("%s Different source for communication #%d", type, cursor);
86 res = bprintf("%s Different destination for communication #%d", type, cursor);
89 res = bprintf("%s\n Different data size for communication #%d", type, cursor);
92 res = bprintf("%s\n Different data for communication #%d", type, cursor);
101 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
104 comm_pattern->src_proc = comm->comm.src_proc->pid;
105 comm_pattern->dst_proc = comm->comm.dst_proc->pid;
106 comm_pattern->src_host = simcall_host_get_name(comm->comm.src_proc->smx_host);
107 comm_pattern->dst_host = simcall_host_get_name(comm->comm.dst_proc->smx_host);
108 if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
109 comm_pattern->data_size = *(comm->comm.dst_buff_size);
110 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
111 addr_pointed = *(void **) comm->comm.src_buff;
112 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
113 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
115 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
119 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
121 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);
124 mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, list_comm_pattern->index_comm, mc_comm_pattern_t);
125 e_mc_comm_pattern_difference_t diff;
127 if((diff = compare_comm_pattern(initial_comm, comm)) != 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_comm_pattern->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_comm_pattern->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 comm_pattern_free(comm);
168 /********** Non Static functions ***********/
170 void comm_pattern_free_voidp(void *p) {
171 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
174 void list_comm_pattern_free_voidp(void *p) {
175 list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
178 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
181 mc_comm_pattern_t pattern = NULL;
182 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
183 pattern->data_size = -1;
184 pattern->data = NULL;
188 if (call_type == MC_CALL_TYPE_SEND) {
189 /* Create comm pattern */
190 pattern->type = SIMIX_COMM_SEND;
191 pattern->comm = simcall_comm_isend__get__result(request);
192 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
193 pattern->src_proc = pattern->comm->comm.src_proc->pid;
194 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
195 pattern->tag = ((MPI_Request)simcall_comm_isend__get__data(request))->tag;
196 if(pattern->comm->comm.src_buff != NULL){
197 pattern->data_size = pattern->comm->comm.src_buff_size;
198 pattern->data = xbt_malloc0(pattern->data_size);
199 addr_pointed = *(void **) pattern->comm->comm.src_buff;
200 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
201 memcpy(pattern->data, addr_pointed, pattern->data_size);
203 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
205 if(((MPI_Request)simcall_comm_isend__get__data(request))->detached){
206 if (!initial_global_state->initial_communications_pattern_done) {
207 /* Store comm pattern */
208 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);
210 /* Evaluate comm determinism */
211 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
212 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->index_comm++;
216 } else if (call_type == MC_CALL_TYPE_RECV) {
217 pattern->type = SIMIX_COMM_RECEIVE;
218 pattern->comm = simcall_comm_irecv__get__result(request);
219 pattern->tag = ((MPI_Request)simcall_comm_irecv__get__data(request))->tag;
220 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
221 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
222 pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
224 xbt_die("Unexpected call_type %i", (int) call_type);
227 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, request->issuer->pid, xbt_dynar_t), &pattern);
229 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, request->issuer->pid);
232 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, unsigned int issuer, int backtracking) {
234 mc_comm_pattern_t current_comm_pattern;
235 unsigned int cursor = 0;
236 mc_comm_pattern_t comm_pattern;
239 /* Complete comm pattern */
240 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
241 if (current_comm_pattern-> comm == comm) {
242 update_comm_pattern(current_comm_pattern, comm);
244 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &comm_pattern);
245 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
250 xbt_die("Corresponding communication not found!");
252 if (!initial_global_state->initial_communications_pattern_done) {
253 /* Store comm pattern */
254 xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->list, &comm_pattern);
256 /* Evaluate comm determinism */
257 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
258 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->index_comm++;
263 /************************ Main algorithm ************************/
265 void MC_pre_modelcheck_comm_determinism(void)
268 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
270 mc_state_t initial_state = NULL;
271 smx_process_t process;
277 if (_sg_mc_visited > 0)
278 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
280 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), list_comm_pattern_free_voidp);
281 for (i=0; i<simix_process_maxpid; i++){
282 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
283 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
284 process_list_pattern->index_comm = 0;
285 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
287 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
288 for (i=0; i<simix_process_maxpid; i++){
289 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
290 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
293 initial_state = MC_state_new();
296 XBT_DEBUG("********* Start communication determinism verification *********");
298 /* Wait for requests (schedules processes) */
299 MC_wait_for_requests();
303 /* Get an enabled process and insert it in the interleave set of the initial state */
304 xbt_swag_foreach(process, simix_global->process_list) {
305 if (MC_process_is_enabled(process)) {
306 MC_state_interleave_process(initial_state, process);
310 xbt_fifo_unshift(mc_stack, initial_state);
316 void MC_modelcheck_comm_determinism(void)
319 char *req_str = NULL;
321 mc_visited_state_t visited_state = NULL;
322 smx_simcall_t req = NULL;
323 smx_process_t process = NULL;
324 mc_state_t state = NULL, next_state = NULL;
326 while (xbt_fifo_size(mc_stack) > 0) {
328 /* Get current state */
329 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
331 XBT_DEBUG("**************************************************");
332 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
333 xbt_fifo_size(mc_stack), state->num,
334 MC_state_interleave_size(state));
336 /* Update statistics */
337 mc_stats->visited_states++;
339 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
340 && (req = MC_state_get_request(state, &value))
341 && (visited_state == NULL)) {
343 req_str = MC_request_to_string(req, value);
344 XBT_DEBUG("Execute: %s", req_str);
347 if (dot_output != NULL) {
349 req_str = MC_request_get_dot_output(req, value);
353 MC_state_set_executed_request(state, req, value);
354 mc_stats->executed_transitions++;
356 /* TODO : handle test and testany simcalls */
357 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
358 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
359 call = mc_get_call_type(req);
362 /* Answer the request */
363 SIMIX_simcall_handle(req, value); /* After this call req is no longer useful */
366 if(!initial_global_state->initial_communications_pattern_done)
367 handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
369 handle_comm_pattern(call, req, value, NULL, 0);
372 /* Wait for requests (schedules processes) */
373 MC_wait_for_requests();
375 /* Create the new expanded state */
378 next_state = MC_state_new();
380 if ((visited_state = is_visited_state(next_state)) == NULL) {
382 /* Get enabled processes and insert them in the interleave set of the next state */
383 xbt_swag_foreach(process, simix_global->process_list) {
384 if (MC_process_is_enabled(process)) {
385 MC_state_interleave_process(next_state, process);
389 if (dot_output != NULL)
390 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
394 if (dot_output != NULL)
395 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
399 xbt_fifo_unshift(mc_stack, next_state);
401 if (dot_output != NULL)
408 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
409 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
410 } else if (visited_state != NULL) {
411 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);
413 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
418 if (!initial_global_state->initial_communications_pattern_done)
419 initial_global_state->initial_communications_pattern_done = 1;
421 /* Trash the current state, no longer needed */
422 xbt_fifo_shift(mc_stack);
423 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
424 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
428 visited_state = NULL;
430 /* Check for deadlocks */
431 if (MC_deadlock_check()) {
432 MC_show_deadlock(NULL);
438 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
439 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
440 /* We found a back-tracking point, let's loop */
441 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
442 xbt_fifo_unshift(mc_stack, state);
447 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
451 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
452 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
460 MC_print_statistics(mc_stats);