Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
jedule: obey our coding standards
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstring>
7 #include <cstdio>
8 #include <cstdlib>
9
10 #include <stdexcept>
11 #include <sstream>
12 #include <string>
13
14 #include "xbt/log.h"
15 #include "xbt/sysdep.h"
16
17 #include "simgrid/simix.h"
18
19 #include "src/kernel/context/Context.hpp"
20 #include "src/mc/mc_record.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/simix/ActorImpl.hpp"
23 #include "src/simix/smx_private.hpp"
24
25 #include "src/mc/mc_base.h"
26 #include "src/mc/Transition.hpp"
27
28 #if SIMGRID_HAVE_MC
29 #include "src/mc/checker/Checker.hpp"
30 #include "src/mc/mc_private.hpp"
31 #include "src/mc/mc_request.hpp"
32 #include "src/mc/mc_smx.hpp"
33 #include "src/mc/mc_state.hpp"
34 #endif
35
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
37   " Logging specific to MC record/replay facility");
38
39 namespace simgrid {
40 namespace mc {
41
42 void replay(RecordTrace const& trace)
43 {
44   simgrid::mc::wait_for_requests();
45
46   for (simgrid::mc::Transition const& transition : trace) {
47     XBT_DEBUG("Executing %i$%i", transition.pid, transition.argument);
48
49     // Choose a request:
50     smx_actor_t process = SIMIX_process_from_PID(transition.pid);
51     if (not process)
52       xbt_die("Unexpected process (pid:%d).", transition.pid);
53     smx_simcall_t simcall = &(process->simcall);
54     if (not simcall || simcall->call == SIMCALL_NONE)
55       xbt_die("No simcall for process %d.", transition.pid);
56     if (not simgrid::mc::request_is_visible(simcall) || not simgrid::mc::actor_is_enabled(process))
57       xbt_die("Unexpected simcall.");
58
59     // Execute the request:
60     SIMIX_simcall_handle(simcall, transition.argument);
61     simgrid::mc::wait_for_requests();
62   }
63 }
64
65 void replay(std::string path_string)
66 {
67   simgrid::mc::processes_time.resize(SIMIX_process_get_maxpid());
68   simgrid::mc::RecordTrace trace = simgrid::mc::parseRecordTrace(path_string.c_str());
69   simgrid::mc::replay(trace);
70   simgrid::mc::processes_time.clear();
71 }
72
73 RecordTrace parseRecordTrace(const char* data)
74 {
75   RecordTrace res;
76   XBT_INFO("path=%s", data);
77   if (data == nullptr || data[0] == '\0')
78     throw std::invalid_argument("Could not parse record path");
79
80   const char* current = data;
81   while (*current) {
82
83     simgrid::mc::Transition item;
84     int count = sscanf(current, "%d/%d", &item.pid, &item.argument);
85     if(count != 2 && count != 1)
86       throw std::invalid_argument("Could not parse record path");
87     res.push_back(item);
88
89     // Find next chunk:
90     const char* end = std::strchr(current, ';');
91     if(end == nullptr)
92       break;
93     else
94       current = end + 1;
95   }
96
97   return res;
98 }
99
100 #if SIMGRID_HAVE_MC
101
102 std::string traceToString(simgrid::mc::RecordTrace const& trace)
103 {
104   std::ostringstream stream;
105   for (auto i = trace.begin(); i != trace.end(); ++i) {
106     if (i != trace.begin())
107       stream << ';';
108     stream << i->pid;
109     if (i->argument)
110       stream << '/' << i->argument;
111   }
112   return stream.str();
113 }
114
115 void dumpRecordPath()
116 {
117   if (MC_record_is_active()) {
118     RecordTrace trace = mc_model_checker->getChecker()->getRecordTrace();
119     XBT_INFO("Path = %s", traceToString(trace).c_str());
120   }
121 }
122
123 #endif
124
125 }
126 }