Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move parts of the kernel to the right subdir
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-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 <assert.h>
8
9 #include <boost/range/algorithm.hpp>
10
11 #include <xbt/log.h>
12 #include <xbt/sysdep.h>
13
14 #include "src/simix/smx_private.h"
15 #include "src/mc/mc_state.h"
16 #include "src/mc/mc_request.h"
17 #include "src/mc/mc_private.h"
18 #include "src/mc/mc_comm_pattern.h"
19 #include "src/mc/mc_smx.h"
20 #include "src/mc/mc_xbt.hpp"
21 #include "src/mc/Transition.hpp"
22
23 using simgrid::mc::remote;
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
26                                 "Logging specific to MC (state)");
27
28 /**
29  * \brief Creates a state data structure used by the exploration algorithm
30  */
31 simgrid::mc::State* MC_state_new(unsigned long state_number)
32 {
33   simgrid::mc::State* state = new simgrid::mc::State();
34   state->processStates.resize(MC_smx_get_maxpid());
35   state->num = state_number;
36   /* Stateful model checking */
37   if((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) ||  _sg_mc_termination){
38     state->system_state = simgrid::mc::take_snapshot(state->num);
39     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
40       MC_state_copy_incomplete_communications_pattern(state);
41       MC_state_copy_index_communications_pattern(state);
42     }
43   }
44   return state;
45 }
46
47 namespace simgrid {
48 namespace mc {
49
50 State::State()
51 {
52   this->internal_comm.clear();
53   std::memset(&this->internal_req, 0, sizeof(this->internal_req));
54   std::memset(&this->executed_req, 0, sizeof(this->executed_req));
55 }
56
57 std::size_t State::interleaveSize() const
58 {
59   return boost::range::count_if(this->processStates,
60     [](simgrid::mc::ProcessState const& p) { return p.isToInterleave(); });
61 }
62
63 Transition State::getTransition() const
64 {
65   return this->transition;
66 }
67
68 }
69 }
70
71 static inline smx_simcall_t MC_state_get_request_for_process(
72   simgrid::mc::State* state, smx_process_t process)
73 {
74   simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
75   state->transition.pid = -1;
76   state->transition.argument = -1;
77
78   if (!procstate->isToInterleave())
79     return nullptr;
80   if (!simgrid::mc::process_is_enabled(process))
81     return nullptr;
82
83   smx_simcall_t req = nullptr;
84   switch (process->simcall.call) {
85       case SIMCALL_COMM_WAITANY:
86         state->transition.argument = -1;
87         while (procstate->interleave_count <
88               read_length(mc_model_checker->process(),
89                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
90           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
91               procstate->interleave_count++)) {
92             state->transition.argument = procstate->interleave_count - 1;
93             break;
94           }
95         }
96
97         if (procstate->interleave_count >=
98             simgrid::mc::read_length(mc_model_checker->process(),
99               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
100           procstate->setDone();
101         if (state->transition.argument != -1)
102           req = &process->simcall;
103         break;
104
105       case SIMCALL_COMM_TESTANY: {
106         unsigned start_count = procstate->interleave_count;
107         state->transition.argument = -1;
108         while (procstate->interleave_count <
109                 simcall_comm_testany__get__count(&process->simcall))
110           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
111               procstate->interleave_count++)) {
112             state->transition.argument = procstate->interleave_count - 1;
113             break;
114           }
115
116         if (procstate->interleave_count >=
117             simcall_comm_testany__get__count(&process->simcall))
118           procstate->setDone();
119
120         if (state->transition.argument != -1 || start_count == 0)
121            req = &process->simcall;
122
123         break;
124       }
125
126       case SIMCALL_COMM_WAIT: {
127         simgrid::mc::RemotePtr<simgrid::simix::Comm> remote_act = remote(
128           static_cast<simgrid::simix::Comm*>(simcall_comm_wait__get__comm(&process->simcall)));
129         simgrid::mc::Remote<simgrid::simix::Comm> temp_act;
130         mc_model_checker->process().read(temp_act, remote_act);
131         simgrid::simix::Comm* act = temp_act.getBuffer();
132         if (act->src_proc && act->dst_proc)
133           state->transition.argument = 0;
134         else if (act->src_proc == nullptr && act->type == SIMIX_COMM_READY
135               && act->detached == 1)
136           state->transition.argument = 0;
137         else
138           state->transition.argument = -1;
139         procstate->setDone();
140         req = &process->simcall;
141         break;
142       }
143
144       case SIMCALL_MC_RANDOM: {
145         int min_value = simcall_mc_random__get__min(&process->simcall);
146         state->transition.argument = procstate->interleave_count + min_value;
147         procstate->interleave_count++;
148         if (state->transition.argument == simcall_mc_random__get__max(&process->simcall))
149           procstate->setDone();
150         req = &process->simcall;
151         break;
152       }
153
154       default:
155         procstate->setDone();
156         state->transition.argument = 0;
157         req = &process->simcall;
158         break;
159   }
160   if (!req)
161     return nullptr;
162
163   // Fetch the data of the request and translate it:
164
165   state->transition.pid = process->pid;
166
167   state->executed_req = *req;
168
169   /* The waitany and testany request are transformed into a wait or test request
170    * over the corresponding communication action so it can be treated later by
171    * the dependence function. */
172   switch (req->call) {
173   case SIMCALL_COMM_WAITANY: {
174     state->internal_req.call = SIMCALL_COMM_WAIT;
175     state->internal_req.issuer = req->issuer;
176     smx_synchro_t remote_comm;
177     read_element(mc_model_checker->process(),
178       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
179       state->transition.argument, sizeof(remote_comm));
180     mc_model_checker->process().read(state->internal_comm, remote(
181       static_cast<simgrid::simix::Comm*>(remote_comm)));
182     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
183     simcall_comm_wait__set__timeout(&state->internal_req, 0);
184     break;
185   }
186
187   case SIMCALL_COMM_TESTANY:
188     state->internal_req.call = SIMCALL_COMM_TEST;
189     state->internal_req.issuer = req->issuer;
190
191     if (state->transition.argument > 0) {
192       smx_synchro_t remote_comm = mc_model_checker->process().read(
193         remote(simcall_comm_testany__get__comms(req) + state->transition.argument));
194       mc_model_checker->process().read(state->internal_comm, remote(
195         static_cast<simgrid::simix::Comm*>(remote_comm)));
196     }
197
198     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
199     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
200     break;
201
202   case SIMCALL_COMM_WAIT:
203     state->internal_req = *req;
204     mc_model_checker->process().read_bytes(&state->internal_comm ,
205       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
206     simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
207     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
208     break;
209
210   case SIMCALL_COMM_TEST:
211     state->internal_req = *req;
212     mc_model_checker->process().read_bytes(&state->internal_comm,
213       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
214     simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
215     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
216     break;
217
218   default:
219     state->internal_req = *req;
220     break;
221   }
222
223   return req;
224 }
225
226 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
227 {
228   for (auto& p : mc_model_checker->process().simix_processes()) {
229     smx_simcall_t res = MC_state_get_request_for_process(state, p.copy.getBuffer());
230     if (res)
231       return res;
232   }
233   return nullptr;
234 }