Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-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 <cstring>
8 #include <cstdio>
9 #include <cstdlib>
10
11 #include <stdexcept>
12 #include <sstream>
13 #include <string>
14
15 #include <xbt/log.h>
16 #include <xbt/sysdep.h>
17
18 #include "simgrid/simix.h"
19
20 #include "src/simix/smx_private.h"
21 #include "src/simix/smx_process_private.h"
22
23 #include "src/mc/mc_replay.h"
24 #include "src/mc/mc_record.h"
25 #include "src/mc/mc_base.h"
26 #include "src/mc/Transition.hpp"
27
28 #if HAVE_MC
29 #include "src/mc/mc_request.h"
30 #include "src/mc/mc_private.h"
31 #include "src/mc/mc_state.h"
32 #include "src/mc/mc_smx.h"
33 #include "src/mc/Checker.hpp"
34 #endif
35
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
37   " Logging specific to MC record/replay facility");
38
39 extern "C" {
40 char* MC_record_path = nullptr;
41 }
42
43 namespace simgrid {
44 namespace mc {
45
46 void replay(RecordTrace const& trace)
47 {
48   simgrid::mc::wait_for_requests();
49
50   for (simgrid::mc::Transition const& transition : trace) {
51     XBT_DEBUG("Executing %i$%i", transition.pid, transition.argument);
52
53     // Choose a request:
54     smx_process_t process = SIMIX_process_from_PID(transition.pid);
55     if (!process)
56       xbt_die("Unexpected process.");
57     smx_simcall_t simcall = &(process->simcall);
58     if(!simcall || simcall->call == SIMCALL_NONE)
59       xbt_die("No simcall for this process.");
60     if (!simgrid::mc::request_is_visible(simcall)
61         || !simgrid::mc::request_is_enabled(simcall))
62       xbt_die("Unexpected simcall.");
63
64     // Execute the request:
65     SIMIX_simcall_handle(simcall, transition.argument);
66     simgrid::mc::wait_for_requests();
67   }
68 }
69
70 void replay(const char* path_string)
71 {
72   simgrid::mc::processes_time.resize(simix_process_maxpid);
73   simgrid::mc::RecordTrace trace = simgrid::mc::parseRecordTrace(path_string);
74   simgrid::mc::replay(trace);
75   simgrid::mc::processes_time.clear();
76 }
77
78 RecordTrace parseRecordTrace(const char* data)
79 {
80   RecordTrace res;
81   XBT_INFO("path=%s", data);
82   if (data == nullptr || data[0] == '\0')
83     throw std::runtime_error("Could not parse record path");
84
85   const char* current = data;
86   while (*current) {
87
88     simgrid::mc::Transition item;
89     int count = sscanf(current, "%u/%u", &item.pid, &item.argument);
90     if(count != 2 && count != 1)
91       throw std::runtime_error("Could not parse record path");
92     res.push_back(item);
93
94     // Find next chunk:
95     const char* end = std::strchr(current, ';');
96     if(end == nullptr)
97       break;
98     else
99       current = end + 1;
100   }
101
102   return res;
103 }
104
105 #if HAVE_MC
106
107 std::string traceToString(simgrid::mc::RecordTrace const& trace)
108 {
109   std::ostringstream stream;
110   for (auto i = trace.begin(); i != trace.end(); ++i) {
111     if (i != trace.begin())
112       stream << ';';
113     stream << i->pid;
114     if (i->argument)
115       stream << '/' << i->argument;
116   }
117   return stream.str();
118 }
119
120 void dumpRecordPath()
121 {
122   if (MC_record_is_active()) {
123     RecordTrace trace = mc_model_checker->getChecker()->getRecordTrace();
124     XBT_INFO("Path = %s", traceToString(trace).c_str());
125   }
126 }
127
128 #endif
129
130 }
131 }