Logo AND Algorithmique Numérique Distribuée

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