Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ec97313473a56f17344b3e79aa02da5abc24f524
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-2022. 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 "src/mc/mc_record.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
8 #include "src/kernel/context/Context.hpp"
9 #include "src/mc/Transition.hpp"
10 #include "src/mc/mc_base.hpp"
11 #include "src/mc/mc_replay.hpp"
12
13 #if SIMGRID_HAVE_MC
14 #include "src/mc/checker/Checker.hpp"
15 #include "src/mc/mc_private.hpp"
16 #include "src/mc/mc_state.hpp"
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc, "Logging specific to MC record/replay facility");
20
21 namespace simgrid {
22 namespace mc {
23
24 void replay(RecordTrace const& trace)
25 {
26   simgrid::mc::execute_actors();
27
28   for (simgrid::mc::Transition const& transition : trace) {
29     XBT_DEBUG("Executing %ld$%i", transition.aid_, transition.times_considered_);
30
31     // Choose a request:
32     kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(transition.aid_);
33     xbt_assert(actor != nullptr, "Unexpected actor (id:%ld).", transition.aid_);
34     const s_smx_simcall* simcall = &(actor->simcall_);
35     xbt_assert(simcall->call_ != simix::Simcall::NONE, "No simcall for process %ld.", transition.aid_);
36     xbt_assert(simgrid::mc::request_is_visible(simcall) && simgrid::mc::actor_is_enabled(actor), "Unexpected simcall.");
37
38     // Execute the request:
39     simcall->issuer_->simcall_handle(transition.times_considered_);
40     simgrid::mc::execute_actors();
41   }
42 }
43
44 void replay(const std::string& path_string)
45 {
46   simgrid::mc::processes_time.resize(simgrid::kernel::actor::get_maxpid());
47   simgrid::mc::RecordTrace trace = simgrid::mc::parseRecordTrace(path_string.c_str());
48   simgrid::mc::replay(trace);
49   simgrid::mc::processes_time.clear();
50 }
51
52 RecordTrace parseRecordTrace(const char* data)
53 {
54   RecordTrace res;
55   XBT_INFO("path=%s", data);
56   if (data == nullptr || data[0] == '\0')
57     throw std::invalid_argument("Could not parse record path");
58
59   const char* current = data;
60   while (*current) {
61     long aid;
62     int times_considered;
63     int count = sscanf(current, "%ld/%d", &aid, &times_considered);
64     simgrid::mc::Transition item(aid, times_considered);
65
66     if(count != 2 && count != 1)
67       throw std::invalid_argument("Could not parse record path");
68     res.push_back(item);
69
70     // Find next chunk:
71     const char* end = std::strchr(current, ';');
72     if(end == nullptr)
73       break;
74     else
75       current = end + 1;
76   }
77
78   return res;
79 }
80
81 #if SIMGRID_HAVE_MC
82
83 std::string traceToString(simgrid::mc::RecordTrace const& trace)
84 {
85   std::ostringstream stream;
86   for (auto i = trace.begin(); i != trace.end(); ++i) {
87     if (i != trace.begin())
88       stream << ';';
89     stream << i->aid_;
90     if (i->times_considered_)
91       stream << '/' << i->times_considered_;
92   }
93   return stream.str();
94 }
95
96 void dumpRecordPath()
97 {
98   RecordTrace trace = mc_model_checker->getChecker()->get_record_trace();
99   XBT_INFO("Path = %s", traceToString(trace).c_str());
100 }
101
102 #endif
103
104 }
105 }