Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make 'colors' variable static
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cinttypes>
8 #include <cassert>
9 #include <cstddef>
10 #include <cstdint>
11
12 #include <vector>
13
14 #include "mc_base.h"
15
16 #include "mc/mc.h"
17
18 #ifndef _WIN32
19 #include <unistd.h>
20 #include <sys/wait.h>
21 #include <sys/time.h>
22 #endif
23
24 #include <xbt/fifo.h>
25 #include <xbt/automaton.h>
26
27 #include "src/simix/smx_process_private.h"
28
29 #if HAVE_MC
30 #include <libunwind.h>
31 #include "src/mc/mc_comm_pattern.h"
32 #include "src/mc/mc_request.h"
33 #include "src/mc/mc_safety.h"
34 #include "src/mc/mc_snapshot.h"
35 #include "src/mc/mc_liveness.h"
36 #include "src/mc/mc_private.h"
37 #include "src/mc/mc_unw.h"
38 #include "src/mc/mc_smx.h"
39 #endif
40
41 #include "src/mc/mc_record.h"
42 #include "src/mc/mc_protocol.h"
43 #include "src/mc/Client.hpp"
44
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
46
47 e_mc_mode_t mc_mode;
48
49 namespace simgrid {
50 namespace mc {
51
52 std::vector<double> processes_time;
53
54 }
55 }
56
57 #if HAVE_MC
58 int user_max_depth_reached = 0;
59
60 /* MC global data structures */
61 mc_state_t mc_current_state = nullptr;
62 char mc_replay_mode = false;
63
64 mc_stats_t mc_stats = nullptr;
65 mc_global_t initial_global_state = nullptr;
66 xbt_fifo_t mc_stack = nullptr;
67
68 /* Liveness */
69
70 namespace simgrid {
71 namespace mc {
72
73 xbt_automaton_t property_automaton = nullptr;
74
75 }
76 }
77
78 /* Dot output */
79 FILE *dot_output = nullptr;
80
81
82 /*******************************  Initialisation of MC *******************************/
83 /*********************************************************************************/
84
85 void MC_init_dot_output()
86 {
87   dot_output = fopen(_sg_mc_dot_output_file, "w");
88
89   if (dot_output == nullptr) {
90     perror("Error open dot output file");
91     xbt_abort();
92   }
93
94   fprintf(dot_output,
95           "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
96
97 }
98
99 #if HAVE_MC
100 void MC_init()
101 {
102   simgrid::mc::processes_time.resize(simix_process_maxpid);
103
104   if (_sg_mc_visited > 0 || _sg_mc_liveness  || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
105     /* Those requests are handled on the client side and propagated by message
106      * to the server: */
107
108     MC_ignore_heap(simgrid::mc::processes_time.data(),
109       simix_process_maxpid * sizeof(double));
110
111     smx_process_t process;
112     xbt_swag_foreach(process, simix_global->process_list)
113       MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
114   }
115 }
116
117 #endif
118
119 /*******************************  Core of MC *******************************/
120 /**************************************************************************/
121
122 void MC_run()
123 {
124   mc_mode = MC_MODE_CLIENT;
125   MC_init();
126   simgrid::mc::Client::get()->mainLoop();
127 }
128
129 void MC_exit(void)
130 {
131   simgrid::mc::processes_time.clear();
132   MC_memory_exit();
133   //xbt_abort();
134 }
135
136 #if HAVE_MC
137 int MC_deadlock_check()
138 {
139   if (mc_mode == MC_MODE_SERVER) {
140     int res;
141     if ((res = mc_model_checker->process().getChannel().send(MC_MESSAGE_DEADLOCK_CHECK)))
142       xbt_die("Could not check deadlock state");
143     s_mc_int_message_t message;
144     ssize_t s = mc_model_checker->process().getChannel().receive(message);
145     if (s == -1)
146       xbt_die("Could not receive message");
147     if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
148       xbt_die("%s received unexpected message %s (%i, size=%i) "
149         "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
150         MC_mode_name(mc_mode),
151         MC_message_type_name(message.type), (int) message.type, (int) s,
152         (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
153         );
154     return message.value;
155   }
156
157   bool deadlock = false;
158   smx_process_t process;
159   if (xbt_swag_size(simix_global->process_list)) {
160     deadlock = true;
161     xbt_swag_foreach(process, simix_global->process_list)
162       if (simgrid::mc::process_is_enabled(process)) {
163         deadlock = false;
164         break;
165       }
166   }
167   return deadlock;
168 }
169 #endif
170
171 /**
172  * \brief Re-executes from the state at position start all the transitions indicated by
173  *        a given model-checker stack.
174  * \param stack The stack with the transitions to execute.
175  * \param start Start index to begin the re-execution.
176  */
177 void MC_replay(xbt_fifo_t stack)
178 {
179   int value, count = 1;
180   char *req_str;
181   smx_simcall_t req = nullptr, saved_req = NULL;
182   xbt_fifo_item_t item, start_item;
183   mc_state_t state;
184   
185   XBT_DEBUG("**** Begin Replay ****");
186
187   /* Intermediate backtracking */
188   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
189     start_item = xbt_fifo_get_first_item(stack);
190     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
191     if(state->system_state){
192       simgrid::mc::restore_snapshot(state->system_state);
193       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
194         MC_restore_communications_pattern(state);
195       return;
196     }
197   }
198
199
200   /* Restore the initial state */
201   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
202   /* At the moment of taking the snapshot the raw heap was set, so restoring
203    * it will set it back again, we have to unset it to continue  */
204
205   start_item = xbt_fifo_get_last_item(stack);
206
207   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
208     // int n = xbt_dynar_length(incomplete_communications_pattern);
209     unsigned n = MC_smx_get_maxpid();
210     assert(n == xbt_dynar_length(incomplete_communications_pattern));
211     assert(n == xbt_dynar_length(initial_communications_pattern));
212     for (unsigned j=0; j < n ; j++) {
213       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
214       xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
215     }
216   }
217
218   /* Traverse the stack from the state at position start and re-execute the transitions */
219   for (item = start_item;
220        item != xbt_fifo_get_first_item(stack);
221        item = xbt_fifo_get_prev_item(item)) {
222
223     state = (mc_state_t) xbt_fifo_get_item_content(item);
224     saved_req = MC_state_get_executed_request(state, &value);
225     
226     if (saved_req) {
227       /* because we got a copy of the executed request, we have to fetch the  
228          real one, pointed by the request field of the issuer process */
229
230       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
231       req = &issuer->simcall;
232
233       /* Debug information */
234       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
235         req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
236         XBT_DEBUG("Replay: %s (%p)", req_str, state);
237         xbt_free(req_str);
238       }
239
240       /* TODO : handle test and testany simcalls */
241       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
242       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
243         call = MC_get_call_type(req);
244
245       simgrid::mc::handle_simcall(req, value);
246
247       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
248         MC_handle_comm_pattern(call, req, value, nullptr, 1);
249
250       mc_model_checker->wait_for_requests();
251
252       count++;
253     }
254
255     /* Update statistics */
256     mc_stats->visited_states++;
257     mc_stats->executed_transitions++;
258
259   }
260
261   XBT_DEBUG("**** End Replay ****");
262 }
263
264 void MC_replay_liveness(xbt_fifo_t stack)
265 {
266   xbt_fifo_item_t item;
267   simgrid::mc::Pair* pair = nullptr;
268   mc_state_t state = nullptr;
269   smx_simcall_t req = nullptr, saved_req = NULL;
270   int value, depth = 1;
271   char *req_str;
272
273   XBT_DEBUG("**** Begin Replay ****");
274
275   /* Intermediate backtracking */
276   if(_sg_mc_checkpoint > 0) {
277     item = xbt_fifo_get_first_item(stack);
278     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
279     if(pair->graph_state->system_state){
280       simgrid::mc::restore_snapshot(pair->graph_state->system_state);
281       return;
282     }
283   }
284
285   /* Restore the initial state */
286   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
287
288     /* Traverse the stack from the initial state and re-execute the transitions */
289     for (item = xbt_fifo_get_last_item(stack);
290          item != xbt_fifo_get_first_item(stack);
291          item = xbt_fifo_get_prev_item(item)) {
292
293       pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
294
295       state = (mc_state_t) pair->graph_state;
296
297       if (pair->exploration_started) {
298
299         saved_req = MC_state_get_executed_request(state, &value);
300
301         if (saved_req != nullptr) {
302           /* because we got a copy of the executed request, we have to fetch the
303              real one, pointed by the request field of the issuer process */
304           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
305           req = &issuer->simcall;
306
307           /* Debug information */
308           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
309             req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
310             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
311             xbt_free(req_str);
312           }
313
314         }
315
316         simgrid::mc::handle_simcall(req, value);
317         mc_model_checker->wait_for_requests();
318       }
319
320       /* Update statistics */
321       mc_stats->visited_pairs++;
322       mc_stats->executed_transitions++;
323
324       depth++;
325       
326     }
327
328   XBT_DEBUG("**** End Replay ****");
329 }
330
331 /**
332  * \brief Dumps the contents of a model-checker's stack and shows the actual
333  *        execution trace
334  * \param stack The stack to dump
335  */
336 void MC_dump_stack_safety(xbt_fifo_t stack)
337 {
338   MC_show_stack_safety(stack);
339   
340   mc_state_t state;
341
342   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
343     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
344 }
345
346
347 void MC_show_stack_safety(xbt_fifo_t stack)
348 {
349   int value;
350   mc_state_t state;
351   xbt_fifo_item_t item;
352   smx_simcall_t req;
353   char *req_str = nullptr;
354
355   for (item = xbt_fifo_get_last_item(stack);
356        item; item = xbt_fifo_get_prev_item(item)) {
357     state = (mc_state_t)xbt_fifo_get_item_content(item);
358     req = MC_state_get_executed_request(state, &value);
359     if (req) {
360       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
361       XBT_INFO("%s", req_str);
362       xbt_free(req_str);
363     }
364   }
365 }
366
367 void MC_show_deadlock(smx_simcall_t req)
368 {
369   /*char *req_str = nullptr; */
370   XBT_INFO("**************************");
371   XBT_INFO("*** DEAD-LOCK DETECTED ***");
372   XBT_INFO("**************************");
373   XBT_INFO("Locked request:");
374   /*req_str = simgrid::mc::request_to_string(req);
375      XBT_INFO("%s", req_str);
376      xbt_free(req_str); */
377   XBT_INFO("Counter-example execution trace:");
378   MC_dump_stack_safety(mc_stack);
379   MC_print_statistics(mc_stats);
380 }
381
382 void MC_show_non_termination(void){
383   XBT_INFO("******************************************");
384   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
385   XBT_INFO("******************************************");
386   XBT_INFO("Counter-example execution trace:");
387   MC_dump_stack_safety(mc_stack);
388   MC_print_statistics(mc_stats);
389 }
390
391 namespace simgrid {
392 namespace mc {
393
394 void show_stack_liveness(xbt_fifo_t stack)
395 {
396   int value;
397   simgrid::mc::Pair* pair;
398   xbt_fifo_item_t item;
399   smx_simcall_t req;
400   char *req_str = nullptr;
401
402   for (item = xbt_fifo_get_last_item(stack);
403        item; item = xbt_fifo_get_prev_item(item)) {
404     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
405     req = MC_state_get_executed_request(pair->graph_state, &value);
406     if (req && req->call != SIMCALL_NONE) {
407       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
408       XBT_INFO("%s", req_str);
409       xbt_free(req_str);
410     }
411   }
412 }
413
414 void dump_stack_liveness(xbt_fifo_t stack)
415 {
416   simgrid::mc::Pair* pair;
417   while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
418     delete pair;
419 }
420
421 }
422 }
423
424 void MC_print_statistics(mc_stats_t stats)
425 {
426   if(_sg_mc_comms_determinism) {
427     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
428       XBT_INFO("******************************************************");
429       XBT_INFO("**** Only-send-deterministic communication pattern ****");
430       XBT_INFO("******************************************************");
431       XBT_INFO("%s", initial_global_state->recv_diff);
432     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
433       XBT_INFO("******************************************************");
434       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
435       XBT_INFO("******************************************************");
436       XBT_INFO("%s", initial_global_state->send_diff);
437     }
438   }
439
440   if (stats->expanded_pairs == 0) {
441     XBT_INFO("Expanded states = %lu", stats->expanded_states);
442     XBT_INFO("Visited states = %lu", stats->visited_states);
443   } else {
444     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
445     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
446   }
447   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
448   if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
449     fprintf(dot_output, "}\n");
450     fclose(dot_output);
451   }
452   if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
453     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
454     if (_sg_mc_comms_determinism)
455       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
456   }
457   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
458     int ret=system("free");
459     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
460   }
461 }
462
463 void MC_automaton_load(const char *file)
464 {
465   if (simgrid::mc::property_automaton == nullptr)
466     simgrid::mc::property_automaton = xbt_automaton_new();
467
468   xbt_automaton_load(simgrid::mc::property_automaton, file);
469 }
470
471 // TODO, fix cross-process access (this function is not used)
472 static void MC_dump_stacks(FILE* file)
473 {
474   int nstack = 0;
475   for (auto const& stack : mc_model_checker->process().stack_areas()) {
476
477     xbt_die("Fix cross-process access to the context");
478     unw_context_t * context = (unw_context_t *)stack.context;
479     fprintf(file, "Stack %i:\n", nstack);
480
481     int nframe = 0;
482     char buffer[100];
483     unw_cursor_t c;
484     unw_init_local (&c, context);
485     unw_word_t off;
486     do {
487       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
488 #if defined(__x86_64__)
489       unw_word_t rip = 0;
490       unw_word_t rsp = 0;
491       unw_get_reg(&c, UNW_X86_64_RIP, &rip);
492       unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
493       fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
494         nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
495 #else
496       fprintf(file, "  %i: %s\n", nframe, name);
497 #endif
498       ++nframe;
499     } while(unw_step(&c));
500
501     ++nstack;
502   }
503 }
504 #endif
505
506 double MC_process_clock_get(smx_process_t process)
507 {
508   if (simgrid::mc::processes_time.empty())
509     return 0;
510   if (process != nullptr)
511     return simgrid::mc::processes_time[process->pid];
512   return -1;
513 }
514
515 void MC_process_clock_add(smx_process_t process, double amount)
516 {
517   simgrid::mc::processes_time[process->pid] += amount;
518 }
519
520 #if HAVE_MC
521 void MC_report_assertion_error(void)
522 {
523   XBT_INFO("**************************");
524   XBT_INFO("*** PROPERTY NOT VALID ***");
525   XBT_INFO("**************************");
526   XBT_INFO("Counter-example execution trace:");
527   MC_record_dump_path(mc_stack);
528   MC_dump_stack_safety(mc_stack);
529   MC_print_statistics(mc_stats);
530 }
531
532 void MC_report_crash(int status)
533 {
534   XBT_INFO("**************************");
535   XBT_INFO("** CRASH IN THE PROGRAM **");
536   XBT_INFO("**************************");
537   if (WIFSIGNALED(status))
538     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
539   else if (WIFEXITED(status))
540     XBT_INFO("From exit: %i", WEXITSTATUS(status));
541   if (WCOREDUMP(status))
542     XBT_INFO("A core dump was generated by the system.");
543   else
544     XBT_INFO("No core dump was generated by the system.");
545   XBT_INFO("Counter-example execution trace:");
546   MC_record_dump_path(mc_stack);
547   MC_dump_stack_safety(mc_stack);
548   MC_print_statistics(mc_stats);
549 }
550
551 #endif