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);
102 static void update_comm_pattern(mc_comm_pattern_t comm_pattern, smx_synchro_t comm)
105 comm_pattern->src_proc = comm->comm.src_proc->pid;
106 comm_pattern->dst_proc = comm->comm.dst_proc->pid;
107 comm_pattern->src_host = simcall_host_get_name(comm->comm.src_proc->smx_host);
108 comm_pattern->dst_host = simcall_host_get_name(comm->comm.dst_proc->smx_host);
109 if (comm_pattern->data_size == -1 && comm->comm.src_buff != NULL) {
110 comm_pattern->data_size = *(comm->comm.dst_buff_size);
111 comm_pattern->data = xbt_malloc0(comm_pattern->data_size);
112 addr_pointed = *(void **) comm->comm.src_buff;
113 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
114 memcpy(comm_pattern->data, addr_pointed, comm_pattern->data_size);
116 memcpy(comm_pattern->data, comm->comm.src_buff, comm_pattern->data_size);
120 static void deterministic_comm_pattern(int process, mc_comm_pattern_t comm, int backtracking) {
122 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);
125 mc_comm_pattern_t initial_comm = xbt_dynar_get_as(list_comm_pattern->list, list_comm_pattern->index_comm, mc_comm_pattern_t);
126 e_mc_comm_pattern_difference_t diff;
128 if((diff = compare_comm_pattern(initial_comm, comm)) != NONE_DIFF){
129 if (comm->type == SIMIX_COMM_SEND){
130 initial_global_state->send_deterministic = 0;
131 if(initial_global_state->send_diff != NULL)
132 xbt_free(initial_global_state->send_diff);
133 initial_global_state->send_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
135 initial_global_state->recv_deterministic = 0;
136 if(initial_global_state->recv_diff != NULL)
137 xbt_free(initial_global_state->recv_diff);
138 initial_global_state->recv_diff = print_determinism_result(diff, process, comm, list_comm_pattern->index_comm + 1);
140 if(_sg_mc_send_determinism && !initial_global_state->send_deterministic){
141 XBT_INFO("*********************************************************");
142 XBT_INFO("***** Non-send-deterministic communications pattern *****");
143 XBT_INFO("*********************************************************");
144 XBT_INFO("%s", initial_global_state->send_diff);
145 xbt_free(initial_global_state->send_diff);
146 initial_global_state->send_diff = NULL;
147 MC_print_statistics(mc_stats);
149 }else if(_sg_mc_comms_determinism && (!initial_global_state->send_deterministic && !initial_global_state->recv_deterministic)) {
150 XBT_INFO("****************************************************");
151 XBT_INFO("***** Non-deterministic communications pattern *****");
152 XBT_INFO("****************************************************");
153 XBT_INFO("%s", initial_global_state->send_diff);
154 XBT_INFO("%s", initial_global_state->recv_diff);
155 xbt_free(initial_global_state->send_diff);
156 initial_global_state->send_diff = NULL;
157 xbt_free(initial_global_state->recv_diff);
158 initial_global_state->recv_diff = NULL;
159 MC_print_statistics(mc_stats);
165 comm_pattern_free(comm);
169 /********** Non Static functions ***********/
171 void comm_pattern_free_voidp(void *p) {
172 comm_pattern_free((mc_comm_pattern_t) * (void **) p);
175 void list_comm_pattern_free_voidp(void *p) {
176 list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
179 void get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type_t call_type, int backtracking)
182 mc_comm_pattern_t pattern = NULL;
183 pattern = xbt_new0(s_mc_comm_pattern_t, 1);
184 pattern->data_size = -1;
185 pattern->data = NULL;
189 if (call_type == MC_CALL_TYPE_SEND) {
190 /* Create comm pattern */
191 pattern->type = SIMIX_COMM_SEND;
192 pattern->comm = simcall_comm_isend__get__result(request);
193 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
194 pattern->src_proc = pattern->comm->comm.src_proc->pid;
195 pattern->src_host = simcall_host_get_name(request->issuer->smx_host);
196 pattern->tag = ((MPI_Request)simcall_comm_isend__get__data(request))->tag;
197 if(pattern->comm->comm.src_buff != NULL){
198 pattern->data_size = pattern->comm->comm.src_buff_size;
199 pattern->data = xbt_malloc0(pattern->data_size);
200 addr_pointed = *(void **) pattern->comm->comm.src_buff;
201 if (addr_pointed > (void*) std_heap && addr_pointed < std_heap->breakval)
202 memcpy(pattern->data, addr_pointed, pattern->data_size);
204 memcpy(pattern->data, pattern->comm->comm.src_buff, pattern->data_size);
206 if(((MPI_Request)simcall_comm_isend__get__data(request))->detached){
207 if (!initial_global_state->initial_communications_pattern_done) {
208 /* Store comm pattern */
209 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);
211 /* Evaluate comm determinism */
212 deterministic_comm_pattern(pattern->src_proc, pattern, backtracking);
213 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, pattern->src_proc, mc_list_comm_pattern_t))->index_comm++;
217 } else if (call_type == MC_CALL_TYPE_RECV) {
218 pattern->type = SIMIX_COMM_RECEIVE;
219 pattern->comm = simcall_comm_irecv__get__result(request);
220 pattern->tag = ((MPI_Request)simcall_comm_irecv__get__data(request))->tag;
221 pattern->rdv = (pattern->comm->comm.rdv != NULL) ? strdup(pattern->comm->comm.rdv->name) : strdup(pattern->comm->comm.rdv_cpy->name);
222 pattern->dst_proc = pattern->comm->comm.dst_proc->pid;
223 pattern->dst_host = simcall_host_get_name(request->issuer->smx_host);
225 xbt_die("Unexpected call_type %i", (int) call_type);
228 xbt_dynar_push((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, request->issuer->pid, xbt_dynar_t), &pattern);
230 XBT_DEBUG("Insert incomplete comm pattern %p for process %lu", pattern, request->issuer->pid);
233 void complete_comm_pattern(xbt_dynar_t list, smx_synchro_t comm, unsigned int issuer, int backtracking) {
235 mc_comm_pattern_t current_comm_pattern;
236 unsigned int cursor = 0;
237 mc_comm_pattern_t comm_pattern;
240 /* Complete comm pattern */
241 xbt_dynar_foreach((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, current_comm_pattern) {
242 if (current_comm_pattern-> comm == comm) {
243 update_comm_pattern(current_comm_pattern, comm);
245 xbt_dynar_remove_at((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, issuer, xbt_dynar_t), cursor, &comm_pattern);
246 XBT_DEBUG("Remove incomplete comm pattern for process %u at cursor %u", issuer, cursor);
251 xbt_die("Corresponding communication not found!");
253 if (!initial_global_state->initial_communications_pattern_done) {
254 /* Store comm pattern */
255 xbt_dynar_push(((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->list, &comm_pattern);
257 /* Evaluate comm determinism */
258 deterministic_comm_pattern(issuer, comm_pattern, backtracking);
259 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, issuer, mc_list_comm_pattern_t))->index_comm++;
264 /************************ Main algorithm ************************/
266 void MC_pre_modelcheck_comm_determinism(void)
269 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
271 mc_state_t initial_state = NULL;
272 smx_process_t process;
278 if (_sg_mc_visited > 0)
279 visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
281 initial_communications_pattern = xbt_dynar_new(sizeof(mc_list_comm_pattern_t), list_comm_pattern_free_voidp);
282 for (i=0; i<simix_process_maxpid; i++){
283 mc_list_comm_pattern_t process_list_pattern = xbt_new0(s_mc_list_comm_pattern_t, 1);
284 process_list_pattern->list = xbt_dynar_new(sizeof(mc_comm_pattern_t), comm_pattern_free_voidp);
285 process_list_pattern->index_comm = 0;
286 xbt_dynar_insert_at(initial_communications_pattern, i, &process_list_pattern);
288 incomplete_communications_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
289 for (i=0; i<simix_process_maxpid; i++){
290 xbt_dynar_t process_pattern = xbt_dynar_new(sizeof(mc_comm_pattern_t), NULL);
291 xbt_dynar_insert_at(incomplete_communications_pattern, i, &process_pattern);
294 initial_state = MC_state_new();
297 XBT_DEBUG("********* Start communication determinism verification *********");
299 /* Wait for requests (schedules processes) */
300 MC_wait_for_requests();
304 /* Get an enabled process and insert it in the interleave set of the initial state */
305 xbt_swag_foreach(process, simix_global->process_list) {
306 if (MC_process_is_enabled(process)) {
307 MC_state_interleave_process(initial_state, process);
311 xbt_fifo_unshift(mc_stack, initial_state);
317 void MC_modelcheck_comm_determinism(void)
320 char *req_str = NULL;
322 mc_visited_state_t visited_state = NULL;
323 smx_simcall_t req = NULL;
324 smx_process_t process = NULL;
325 mc_state_t state = NULL, next_state = NULL;
327 while (xbt_fifo_size(mc_stack) > 0) {
329 /* Get current state */
330 state = (mc_state_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
332 XBT_DEBUG("**************************************************");
333 XBT_DEBUG("Exploration depth = %d (state = %d, interleaved processes = %d)",
334 xbt_fifo_size(mc_stack), state->num,
335 MC_state_interleave_size(state));
337 /* Update statistics */
338 mc_stats->visited_states++;
340 if ((xbt_fifo_size(mc_stack) <= _sg_mc_max_depth)
341 && (req = MC_state_get_request(state, &value))
342 && (visited_state == NULL)) {
344 req_str = MC_request_to_string(req, value);
345 XBT_DEBUG("Execute: %s", req_str);
348 if (dot_output != NULL) {
350 req_str = MC_request_get_dot_output(req, value);
354 MC_state_set_executed_request(state, req, value);
355 mc_stats->executed_transitions++;
357 /* TODO : handle test and testany simcalls */
358 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
359 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
360 call = mc_get_call_type(req);
363 /* Answer the request */
364 SIMIX_simcall_handle(req, value); /* After this call req is no longer useful */
367 if(!initial_global_state->initial_communications_pattern_done)
368 handle_comm_pattern(call, req, value, initial_communications_pattern, 0);
370 handle_comm_pattern(call, req, value, NULL, 0);
373 /* Wait for requests (schedules processes) */
374 MC_wait_for_requests();
376 /* Create the new expanded state */
379 next_state = MC_state_new();
381 if ((visited_state = is_visited_state(next_state)) == NULL) {
383 /* Get enabled processes and insert them in the interleave set of the next state */
384 xbt_swag_foreach(process, simix_global->process_list) {
385 if (MC_process_is_enabled(process)) {
386 MC_state_interleave_process(next_state, process);
390 if (dot_output != NULL)
391 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
395 if (dot_output != NULL)
396 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
400 xbt_fifo_unshift(mc_stack, next_state);
402 if (dot_output != NULL)
409 if (xbt_fifo_size(mc_stack) > _sg_mc_max_depth) {
410 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
411 } else if (visited_state != NULL) {
412 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);
414 XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack));
419 if (!initial_global_state->initial_communications_pattern_done)
420 initial_global_state->initial_communications_pattern_done = 1;
422 /* Trash the current state, no longer needed */
423 xbt_fifo_shift(mc_stack);
424 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
425 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
429 visited_state = NULL;
431 /* Check for deadlocks */
432 if (MC_deadlock_check()) {
433 MC_show_deadlock(NULL);
439 while ((state = xbt_fifo_shift(mc_stack)) != NULL) {
440 if (MC_state_interleave_size(state) && xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
441 /* We found a back-tracking point, let's loop */
442 XBT_DEBUG("Back-tracking to state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
443 xbt_fifo_unshift(mc_stack, state);
448 XBT_DEBUG("Back-tracking to state %d at depth %d done", state->num, xbt_fifo_size(mc_stack));
452 XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
453 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
461 MC_print_statistics(mc_stats);