Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a more specific exception.
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-2017. 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 std::string MC_record_path;
40
41 namespace simgrid {
42 namespace mc {
43
44 void replay(RecordTrace const& trace)
45 {
46   simgrid::mc::wait_for_requests();
47
48   for (simgrid::mc::Transition const& transition : trace) {
49     XBT_DEBUG("Executing %i$%i", transition.pid, transition.argument);
50
51     // Choose a request:
52     smx_actor_t process = SIMIX_process_from_PID(transition.pid);
53     if (not process)
54       xbt_die("Unexpected process (pid:%d).", transition.pid);
55     smx_simcall_t simcall = &(process->simcall);
56     if (not simcall || simcall->call == SIMCALL_NONE)
57       xbt_die("No simcall for process %d.", transition.pid);
58     if (not simgrid::mc::request_is_visible(simcall) || not simgrid::mc::actor_is_enabled(process))
59       xbt_die("Unexpected simcall.");
60
61     // Execute the request:
62     SIMIX_simcall_handle(simcall, transition.argument);
63     simgrid::mc::wait_for_requests();
64   }
65 }
66
67 void replay(std::string path_string)
68 {
69   simgrid::mc::processes_time.resize(SIMIX_process_get_maxpid());
70   simgrid::mc::RecordTrace trace = simgrid::mc::parseRecordTrace(path_string.c_str());
71   simgrid::mc::replay(trace);
72   simgrid::mc::processes_time.clear();
73 }
74
75 RecordTrace parseRecordTrace(const char* data)
76 {
77   RecordTrace res;
78   XBT_INFO("path=%s", data);
79   if (data == nullptr || data[0] == '\0')
80     throw std::invalid_argument("Could not parse record path");
81
82   const char* current = data;
83   while (*current) {
84
85     simgrid::mc::Transition item;
86     int count = sscanf(current, "%d/%d", &item.pid, &item.argument);
87     if(count != 2 && count != 1)
88       throw std::invalid_argument("Could not parse record path");
89     res.push_back(item);
90
91     // Find next chunk:
92     const char* end = std::strchr(current, ';');
93     if(end == nullptr)
94       break;
95     else
96       current = end + 1;
97   }
98
99   return res;
100 }
101
102 #if SIMGRID_HAVE_MC
103
104 std::string traceToString(simgrid::mc::RecordTrace const& trace)
105 {
106   std::ostringstream stream;
107   for (auto i = trace.begin(); i != trace.end(); ++i) {
108     if (i != trace.begin())
109       stream << ';';
110     stream << i->pid;
111     if (i->argument)
112       stream << '/' << i->argument;
113   }
114   return stream.str();
115 }
116
117 void dumpRecordPath()
118 {
119   if (MC_record_is_active()) {
120     RecordTrace trace = mc_model_checker->getChecker()->getRecordTrace();
121     XBT_INFO("Path = %s", traceToString(trace).c_str());
122   }
123 }
124
125 #endif
126
127 }
128 }