Logo AND Algorithmique Numérique Distribuée

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