Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups in the surf::traces
[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 /**
137  * \brief Re-executes from the state at position start all the transitions indicated by
138  *        a given model-checker stack.
139  * \param stack The stack with the transitions to execute.
140  * \param start Start index to begin the re-execution.
141  */
142 void MC_replay(xbt_fifo_t stack)
143 {
144   int value, count = 1;
145   char *req_str;
146   smx_simcall_t req = nullptr, saved_req = NULL;
147   xbt_fifo_item_t item, start_item;
148   mc_state_t state;
149   
150   XBT_DEBUG("**** Begin Replay ****");
151
152   /* Intermediate backtracking */
153   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
154     start_item = xbt_fifo_get_first_item(stack);
155     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
156     if(state->system_state){
157       simgrid::mc::restore_snapshot(state->system_state);
158       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
159         MC_restore_communications_pattern(state);
160       return;
161     }
162   }
163
164
165   /* Restore the initial state */
166   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
167   /* At the moment of taking the snapshot the raw heap was set, so restoring
168    * it will set it back again, we have to unset it to continue  */
169
170   start_item = xbt_fifo_get_last_item(stack);
171
172   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
173     // int n = xbt_dynar_length(incomplete_communications_pattern);
174     unsigned n = MC_smx_get_maxpid();
175     assert(n == xbt_dynar_length(incomplete_communications_pattern));
176     assert(n == xbt_dynar_length(initial_communications_pattern));
177     for (unsigned j=0; j < n ; j++) {
178       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
179       xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
180     }
181   }
182
183   /* Traverse the stack from the state at position start and re-execute the transitions */
184   for (item = start_item;
185        item != xbt_fifo_get_first_item(stack);
186        item = xbt_fifo_get_prev_item(item)) {
187
188     state = (mc_state_t) xbt_fifo_get_item_content(item);
189     saved_req = MC_state_get_executed_request(state, &value);
190     
191     if (saved_req) {
192       /* because we got a copy of the executed request, we have to fetch the  
193          real one, pointed by the request field of the issuer process */
194
195       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
196       req = &issuer->simcall;
197
198       /* Debug information */
199       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
200         req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
201         XBT_DEBUG("Replay: %s (%p)", req_str, state);
202         xbt_free(req_str);
203       }
204
205       /* TODO : handle test and testany simcalls */
206       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
207       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
208         call = MC_get_call_type(req);
209
210       simgrid::mc::handle_simcall(req, value);
211
212       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
213         MC_handle_comm_pattern(call, req, value, nullptr, 1);
214
215       mc_model_checker->wait_for_requests();
216
217       count++;
218     }
219
220     /* Update statistics */
221     mc_stats->visited_states++;
222     mc_stats->executed_transitions++;
223
224   }
225
226   XBT_DEBUG("**** End Replay ****");
227 }
228
229 void MC_replay_liveness(xbt_fifo_t stack)
230 {
231   xbt_fifo_item_t item;
232   simgrid::mc::Pair* pair = nullptr;
233   mc_state_t state = nullptr;
234   smx_simcall_t req = nullptr, saved_req = NULL;
235   int value, depth = 1;
236   char *req_str;
237
238   XBT_DEBUG("**** Begin Replay ****");
239
240   /* Intermediate backtracking */
241   if(_sg_mc_checkpoint > 0) {
242     item = xbt_fifo_get_first_item(stack);
243     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
244     if(pair->graph_state->system_state){
245       simgrid::mc::restore_snapshot(pair->graph_state->system_state);
246       return;
247     }
248   }
249
250   /* Restore the initial state */
251   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
252
253     /* Traverse the stack from the initial state and re-execute the transitions */
254     for (item = xbt_fifo_get_last_item(stack);
255          item != xbt_fifo_get_first_item(stack);
256          item = xbt_fifo_get_prev_item(item)) {
257
258       pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
259
260       state = (mc_state_t) pair->graph_state;
261
262       if (pair->exploration_started) {
263
264         saved_req = MC_state_get_executed_request(state, &value);
265
266         if (saved_req != nullptr) {
267           /* because we got a copy of the executed request, we have to fetch the
268              real one, pointed by the request field of the issuer process */
269           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
270           req = &issuer->simcall;
271
272           /* Debug information */
273           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
274             req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
275             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
276             xbt_free(req_str);
277           }
278
279         }
280
281         simgrid::mc::handle_simcall(req, value);
282         mc_model_checker->wait_for_requests();
283       }
284
285       /* Update statistics */
286       mc_stats->visited_pairs++;
287       mc_stats->executed_transitions++;
288
289       depth++;
290       
291     }
292
293   XBT_DEBUG("**** End Replay ****");
294 }
295
296 /**
297  * \brief Dumps the contents of a model-checker's stack and shows the actual
298  *        execution trace
299  * \param stack The stack to dump
300  */
301 void MC_dump_stack_safety(xbt_fifo_t stack)
302 {
303   MC_show_stack_safety(stack);
304   
305   mc_state_t state;
306
307   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
308     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
309 }
310
311
312 void MC_show_stack_safety(xbt_fifo_t stack)
313 {
314   int value;
315   mc_state_t state;
316   xbt_fifo_item_t item;
317   smx_simcall_t req;
318   char *req_str = nullptr;
319
320   for (item = xbt_fifo_get_last_item(stack);
321        item; item = xbt_fifo_get_prev_item(item)) {
322     state = (mc_state_t)xbt_fifo_get_item_content(item);
323     req = MC_state_get_executed_request(state, &value);
324     if (req) {
325       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
326       XBT_INFO("%s", req_str);
327       xbt_free(req_str);
328     }
329   }
330 }
331
332 void MC_show_deadlock(smx_simcall_t req)
333 {
334   /*char *req_str = nullptr; */
335   XBT_INFO("**************************");
336   XBT_INFO("*** DEAD-LOCK DETECTED ***");
337   XBT_INFO("**************************");
338   XBT_INFO("Locked request:");
339   /*req_str = simgrid::mc::request_to_string(req);
340      XBT_INFO("%s", req_str);
341      xbt_free(req_str); */
342   XBT_INFO("Counter-example execution trace:");
343   MC_dump_stack_safety(mc_stack);
344   MC_print_statistics(mc_stats);
345 }
346
347 void MC_show_non_termination(void){
348   XBT_INFO("******************************************");
349   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
350   XBT_INFO("******************************************");
351   XBT_INFO("Counter-example execution trace:");
352   MC_dump_stack_safety(mc_stack);
353   MC_print_statistics(mc_stats);
354 }
355
356 namespace simgrid {
357 namespace mc {
358
359 void show_stack_liveness(xbt_fifo_t stack)
360 {
361   int value;
362   simgrid::mc::Pair* pair;
363   xbt_fifo_item_t item;
364   smx_simcall_t req;
365   char *req_str = nullptr;
366
367   for (item = xbt_fifo_get_last_item(stack);
368        item; item = xbt_fifo_get_prev_item(item)) {
369     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
370     req = MC_state_get_executed_request(pair->graph_state, &value);
371     if (req && req->call != SIMCALL_NONE) {
372       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
373       XBT_INFO("%s", req_str);
374       xbt_free(req_str);
375     }
376   }
377 }
378
379 void dump_stack_liveness(xbt_fifo_t stack)
380 {
381   simgrid::mc::Pair* pair;
382   while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
383     delete pair;
384 }
385
386 }
387 }
388
389 void MC_print_statistics(mc_stats_t stats)
390 {
391   if(_sg_mc_comms_determinism) {
392     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
393       XBT_INFO("******************************************************");
394       XBT_INFO("**** Only-send-deterministic communication pattern ****");
395       XBT_INFO("******************************************************");
396       XBT_INFO("%s", initial_global_state->recv_diff);
397     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
398       XBT_INFO("******************************************************");
399       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
400       XBT_INFO("******************************************************");
401       XBT_INFO("%s", initial_global_state->send_diff);
402     }
403   }
404
405   if (stats->expanded_pairs == 0) {
406     XBT_INFO("Expanded states = %lu", stats->expanded_states);
407     XBT_INFO("Visited states = %lu", stats->visited_states);
408   } else {
409     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
410     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
411   }
412   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
413   if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
414     fprintf(dot_output, "}\n");
415     fclose(dot_output);
416   }
417   if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
418     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
419     if (_sg_mc_comms_determinism)
420       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
421   }
422   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
423     int ret=system("free");
424     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
425   }
426 }
427
428 void MC_automaton_load(const char *file)
429 {
430   if (simgrid::mc::property_automaton == nullptr)
431     simgrid::mc::property_automaton = xbt_automaton_new();
432
433   xbt_automaton_load(simgrid::mc::property_automaton, file);
434 }
435
436 // TODO, fix cross-process access (this function is not used)
437 static void MC_dump_stacks(FILE* file)
438 {
439   int nstack = 0;
440   for (auto const& stack : mc_model_checker->process().stack_areas()) {
441
442     xbt_die("Fix cross-process access to the context");
443     unw_context_t * context = (unw_context_t *)stack.context;
444     fprintf(file, "Stack %i:\n", nstack);
445
446     int nframe = 0;
447     char buffer[100];
448     unw_cursor_t c;
449     unw_init_local (&c, context);
450     unw_word_t off;
451     do {
452       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
453 #if defined(__x86_64__)
454       unw_word_t rip = 0;
455       unw_word_t rsp = 0;
456       unw_get_reg(&c, UNW_X86_64_RIP, &rip);
457       unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
458       fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
459         nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
460 #else
461       fprintf(file, "  %i: %s\n", nframe, name);
462 #endif
463       ++nframe;
464     } while(unw_step(&c));
465
466     ++nstack;
467   }
468 }
469 #endif
470
471 double MC_process_clock_get(smx_process_t process)
472 {
473   if (simgrid::mc::processes_time.empty())
474     return 0;
475   if (process != nullptr)
476     return simgrid::mc::processes_time[process->pid];
477   return -1;
478 }
479
480 void MC_process_clock_add(smx_process_t process, double amount)
481 {
482   simgrid::mc::processes_time[process->pid] += amount;
483 }
484
485 #if HAVE_MC
486 void MC_report_assertion_error(void)
487 {
488   XBT_INFO("**************************");
489   XBT_INFO("*** PROPERTY NOT VALID ***");
490   XBT_INFO("**************************");
491   XBT_INFO("Counter-example execution trace:");
492   MC_record_dump_path(mc_stack);
493   MC_dump_stack_safety(mc_stack);
494   MC_print_statistics(mc_stats);
495 }
496
497 void MC_report_crash(int status)
498 {
499   XBT_INFO("**************************");
500   XBT_INFO("** CRASH IN THE PROGRAM **");
501   XBT_INFO("**************************");
502   if (WIFSIGNALED(status))
503     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
504   else if (WIFEXITED(status))
505     XBT_INFO("From exit: %i", WEXITSTATUS(status));
506   if (WCOREDUMP(status))
507     XBT_INFO("A core dump was generated by the system.");
508   else
509     XBT_INFO("No core dump was generated by the system.");
510   XBT_INFO("Counter-example execution trace:");
511   MC_record_dump_path(mc_stack);
512   MC_dump_stack_safety(mc_stack);
513   MC_print_statistics(mc_stats);
514 }
515
516 #endif