Logo AND Algorithmique Numérique Distribuée

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