Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
41e7877ac924244facae2f346a365acf640bb90a
[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 "src/simix/smx_process_private.h"
25
26 #if HAVE_MC
27 #include <libunwind.h>
28 #include "src/mc/mc_comm_pattern.h"
29 #include "src/mc/mc_request.h"
30 #include "src/mc/mc_safety.h"
31 #include "src/mc/mc_snapshot.h"
32 #include "src/mc/mc_liveness.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_unw.h"
35 #include "src/mc/mc_smx.h"
36 #endif
37
38 #include "src/mc/mc_record.h"
39 #include "src/mc/mc_protocol.h"
40 #include "src/mc/Client.hpp"
41
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
43
44 e_mc_mode_t mc_mode;
45
46 namespace simgrid {
47 namespace mc {
48
49 std::vector<double> processes_time;
50
51 }
52 }
53
54 #if HAVE_MC
55 int user_max_depth_reached = 0;
56
57 /* MC global data structures */
58 mc_state_t mc_current_state = nullptr;
59 char mc_replay_mode = false;
60
61 mc_stats_t mc_stats = nullptr;
62 mc_global_t initial_global_state = nullptr;
63 xbt_fifo_t mc_stack = nullptr;
64
65 /* Liveness */
66
67 namespace simgrid {
68 namespace mc {
69
70 xbt_automaton_t property_automaton = nullptr;
71
72 }
73 }
74
75 /* Dot output */
76 FILE *dot_output = nullptr;
77
78
79 /*******************************  Initialisation of MC *******************************/
80 /*********************************************************************************/
81
82 void MC_init_dot_output()
83 {
84   dot_output = fopen(_sg_mc_dot_output_file, "w");
85
86   if (dot_output == nullptr) {
87     perror("Error open dot output file");
88     xbt_abort();
89   }
90
91   fprintf(dot_output,
92           "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");
93
94 }
95
96 /*******************************  Core of MC *******************************/
97 /**************************************************************************/
98
99 void MC_run()
100 {
101   simgrid::mc::processes_time.resize(simix_process_maxpid);
102   MC_ignore_heap(simgrid::mc::processes_time.data(),
103     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
104   smx_process_t process;
105   xbt_swag_foreach(process, simix_global->process_list)
106     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
107   simgrid::mc::Client::get()->mainLoop();
108 }
109
110 void MC_exit(void)
111 {
112   simgrid::mc::processes_time.clear();
113   MC_memory_exit();
114   //xbt_abort();
115 }
116
117 /**
118  * \brief Re-executes from the state at position start all the transitions indicated by
119  *        a given model-checker stack.
120  * \param stack The stack with the transitions to execute.
121  * \param start Start index to begin the re-execution.
122  */
123 void MC_replay(xbt_fifo_t stack)
124 {
125   int value, count = 1;
126   char *req_str;
127   smx_simcall_t req = nullptr, saved_req = NULL;
128   xbt_fifo_item_t item, start_item;
129   mc_state_t state;
130   
131   XBT_DEBUG("**** Begin Replay ****");
132
133   /* Intermediate backtracking */
134   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
135     start_item = xbt_fifo_get_first_item(stack);
136     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
137     if(state->system_state){
138       simgrid::mc::restore_snapshot(state->system_state);
139       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
140         MC_restore_communications_pattern(state);
141       return;
142     }
143   }
144
145
146   /* Restore the initial state */
147   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
148   /* At the moment of taking the snapshot the raw heap was set, so restoring
149    * it will set it back again, we have to unset it to continue  */
150
151   start_item = xbt_fifo_get_last_item(stack);
152
153   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
154     // int n = xbt_dynar_length(incomplete_communications_pattern);
155     unsigned n = MC_smx_get_maxpid();
156     assert(n == xbt_dynar_length(incomplete_communications_pattern));
157     assert(n == xbt_dynar_length(initial_communications_pattern));
158     for (unsigned j=0; j < n ; j++) {
159       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
160       xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
161     }
162   }
163
164   /* Traverse the stack from the state at position start and re-execute the transitions */
165   for (item = start_item;
166        item != xbt_fifo_get_first_item(stack);
167        item = xbt_fifo_get_prev_item(item)) {
168
169     state = (mc_state_t) xbt_fifo_get_item_content(item);
170     saved_req = MC_state_get_executed_request(state, &value);
171     
172     if (saved_req) {
173       /* because we got a copy of the executed request, we have to fetch the  
174          real one, pointed by the request field of the issuer process */
175
176       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
177       req = &issuer->simcall;
178
179       /* Debug information */
180       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
181         req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
182         XBT_DEBUG("Replay: %s (%p)", req_str, state);
183         xbt_free(req_str);
184       }
185
186       /* TODO : handle test and testany simcalls */
187       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
188       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
189         call = MC_get_call_type(req);
190
191       simgrid::mc::handle_simcall(req, value);
192
193       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
194         MC_handle_comm_pattern(call, req, value, nullptr, 1);
195
196       mc_model_checker->wait_for_requests();
197
198       count++;
199     }
200
201     /* Update statistics */
202     mc_stats->visited_states++;
203     mc_stats->executed_transitions++;
204
205   }
206
207   XBT_DEBUG("**** End Replay ****");
208 }
209
210 /**
211  * \brief Dumps the contents of a model-checker's stack and shows the actual
212  *        execution trace
213  * \param stack The stack to dump
214  */
215 void MC_dump_stack_safety(xbt_fifo_t stack)
216 {
217   MC_show_stack_safety(stack);
218   
219   mc_state_t state;
220
221   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
222     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
223 }
224
225
226 void MC_show_stack_safety(xbt_fifo_t stack)
227 {
228   int value;
229   mc_state_t state;
230   xbt_fifo_item_t item;
231   smx_simcall_t req;
232   char *req_str = nullptr;
233
234   for (item = xbt_fifo_get_last_item(stack);
235        item; item = xbt_fifo_get_prev_item(item)) {
236     state = (mc_state_t)xbt_fifo_get_item_content(item);
237     req = MC_state_get_executed_request(state, &value);
238     if (req) {
239       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
240       XBT_INFO("%s", req_str);
241       xbt_free(req_str);
242     }
243   }
244 }
245
246 void MC_show_deadlock(smx_simcall_t req)
247 {
248   /*char *req_str = nullptr; */
249   XBT_INFO("**************************");
250   XBT_INFO("*** DEAD-LOCK DETECTED ***");
251   XBT_INFO("**************************");
252   XBT_INFO("Locked request:");
253   /*req_str = simgrid::mc::request_to_string(req);
254      XBT_INFO("%s", req_str);
255      xbt_free(req_str); */
256   XBT_INFO("Counter-example execution trace:");
257   MC_dump_stack_safety(mc_stack);
258   MC_print_statistics(mc_stats);
259 }
260
261 void MC_show_non_termination(void){
262   XBT_INFO("******************************************");
263   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
264   XBT_INFO("******************************************");
265   XBT_INFO("Counter-example execution trace:");
266   MC_dump_stack_safety(mc_stack);
267   MC_print_statistics(mc_stats);
268 }
269
270 void MC_print_statistics(mc_stats_t stats)
271 {
272   if(_sg_mc_comms_determinism) {
273     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
274       XBT_INFO("******************************************************");
275       XBT_INFO("**** Only-send-deterministic communication pattern ****");
276       XBT_INFO("******************************************************");
277       XBT_INFO("%s", initial_global_state->recv_diff);
278     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
279       XBT_INFO("******************************************************");
280       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
281       XBT_INFO("******************************************************");
282       XBT_INFO("%s", initial_global_state->send_diff);
283     }
284   }
285
286   if (stats->expanded_pairs == 0) {
287     XBT_INFO("Expanded states = %lu", stats->expanded_states);
288     XBT_INFO("Visited states = %lu", stats->visited_states);
289   } else {
290     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
291     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
292   }
293   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
294   if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
295     fprintf(dot_output, "}\n");
296     fclose(dot_output);
297   }
298   if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
299     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
300     if (_sg_mc_comms_determinism)
301       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
302   }
303   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
304     int ret=system("free");
305     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
306   }
307 }
308
309 void MC_automaton_load(const char *file)
310 {
311   if (simgrid::mc::property_automaton == nullptr)
312     simgrid::mc::property_automaton = xbt_automaton_new();
313
314   xbt_automaton_load(simgrid::mc::property_automaton, file);
315 }
316
317 // TODO, fix cross-process access (this function is not used)
318 static void MC_dump_stacks(FILE* file)
319 {
320   int nstack = 0;
321   for (auto const& stack : mc_model_checker->process().stack_areas()) {
322
323     xbt_die("Fix cross-process access to the context");
324     unw_context_t * context = (unw_context_t *)stack.context;
325     fprintf(file, "Stack %i:\n", nstack);
326
327     int nframe = 0;
328     char buffer[100];
329     unw_cursor_t c;
330     unw_init_local (&c, context);
331     unw_word_t off;
332     do {
333       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
334 #if defined(__x86_64__)
335       unw_word_t rip = 0;
336       unw_word_t rsp = 0;
337       unw_get_reg(&c, UNW_X86_64_RIP, &rip);
338       unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
339       fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
340         nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
341 #else
342       fprintf(file, "  %i: %s\n", nframe, name);
343 #endif
344       ++nframe;
345     } while(unw_step(&c));
346
347     ++nstack;
348   }
349 }
350 #endif
351
352 double MC_process_clock_get(smx_process_t process)
353 {
354   if (simgrid::mc::processes_time.empty())
355     return 0;
356   if (process != nullptr)
357     return simgrid::mc::processes_time[process->pid];
358   return -1;
359 }
360
361 void MC_process_clock_add(smx_process_t process, double amount)
362 {
363   simgrid::mc::processes_time[process->pid] += amount;
364 }
365
366 #if HAVE_MC
367 void MC_report_assertion_error(void)
368 {
369   XBT_INFO("**************************");
370   XBT_INFO("*** PROPERTY NOT VALID ***");
371   XBT_INFO("**************************");
372   XBT_INFO("Counter-example execution trace:");
373   MC_record_dump_path(mc_stack);
374   MC_dump_stack_safety(mc_stack);
375   MC_print_statistics(mc_stats);
376 }
377
378 void MC_report_crash(int status)
379 {
380   XBT_INFO("**************************");
381   XBT_INFO("** CRASH IN THE PROGRAM **");
382   XBT_INFO("**************************");
383   if (WIFSIGNALED(status))
384     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
385   else if (WIFEXITED(status))
386     XBT_INFO("From exit: %i", WEXITSTATUS(status));
387   if (WCOREDUMP(status))
388     XBT_INFO("A core dump was generated by the system.");
389   else
390     XBT_INFO("No core dump was generated by the system.");
391   XBT_INFO("Counter-example execution trace:");
392   MC_record_dump_path(mc_stack);
393   MC_dump_stack_safety(mc_stack);
394   MC_print_statistics(mc_stats);
395 }
396
397 #endif