Logo AND Algorithmique Numérique Distribuée

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