Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move libsosp into its own directory
[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 "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.h"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/simix/ActorImpl.hpp"
13
14 #if SIMGRID_HAVE_MC
15 #include "src/mc/checker/Checker.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "src/mc/mc_request.hpp"
18 #include "src/mc/mc_smx.hpp"
19 #include "src/mc/mc_state.hpp"
20 #endif
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc,
23   " Logging specific to MC record/replay facility");
24
25 namespace simgrid {
26 namespace mc {
27
28 void replay(RecordTrace const& trace)
29 {
30   simgrid::mc::wait_for_requests();
31
32   for (simgrid::mc::Transition const& transition : trace) {
33     XBT_DEBUG("Executing %i$%i", transition.pid, transition.argument);
34
35     // Choose a request:
36     smx_actor_t process = SIMIX_process_from_PID(transition.pid);
37     if (not process)
38       xbt_die("Unexpected process (pid:%d).", transition.pid);
39     smx_simcall_t simcall = &(process->simcall);
40     if (not simcall || simcall->call == SIMCALL_NONE)
41       xbt_die("No simcall for process %d.", transition.pid);
42     if (not simgrid::mc::request_is_visible(simcall) || not simgrid::mc::actor_is_enabled(process))
43       xbt_die("Unexpected simcall.");
44
45     // Execute the request:
46     SIMIX_simcall_handle(simcall, transition.argument);
47     simgrid::mc::wait_for_requests();
48   }
49 }
50
51 void replay(std::string path_string)
52 {
53   simgrid::mc::processes_time.resize(SIMIX_process_get_maxpid());
54   simgrid::mc::RecordTrace trace = simgrid::mc::parseRecordTrace(path_string.c_str());
55   simgrid::mc::replay(trace);
56   simgrid::mc::processes_time.clear();
57 }
58
59 RecordTrace parseRecordTrace(const char* data)
60 {
61   RecordTrace res;
62   XBT_INFO("path=%s", data);
63   if (data == nullptr || data[0] == '\0')
64     throw std::invalid_argument("Could not parse record path");
65
66   const char* current = data;
67   while (*current) {
68
69     simgrid::mc::Transition item;
70     int count = sscanf(current, "%d/%d", &item.pid, &item.argument);
71     if(count != 2 && count != 1)
72       throw std::invalid_argument("Could not parse record path");
73     res.push_back(item);
74
75     // Find next chunk:
76     const char* end = std::strchr(current, ';');
77     if(end == nullptr)
78       break;
79     else
80       current = end + 1;
81   }
82
83   return res;
84 }
85
86 #if SIMGRID_HAVE_MC
87
88 std::string traceToString(simgrid::mc::RecordTrace const& trace)
89 {
90   std::ostringstream stream;
91   for (auto i = trace.begin(); i != trace.end(); ++i) {
92     if (i != trace.begin())
93       stream << ';';
94     stream << i->pid;
95     if (i->argument)
96       stream << '/' << i->argument;
97   }
98   return stream.str();
99 }
100
101 void dumpRecordPath()
102 {
103   if (MC_record_is_active()) {
104     RecordTrace trace = mc_model_checker->getChecker()->getRecordTrace();
105     XBT_INFO("Path = %s", traceToString(trace).c_str());
106   }
107 }
108
109 #endif
110
111 }
112 }