Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::vector for State::incomplete_comm_pattern (dexbtification)
[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.interleave(); });
60 }
61
62 }
63 }
64
65 void MC_state_interleave_process(simgrid::mc::State* state, smx_process_t process)
66 {
67   assert(state);
68   state->processStates[process->pid].state = simgrid::mc::ProcessInterleaveState::interleave;
69   state->processStates[process->pid].interleave_count = 0;
70 }
71
72 void MC_state_remove_interleave_process(simgrid::mc::State* state, smx_process_t process)
73 {
74   if (state->processStates[process->pid].state == simgrid::mc::ProcessInterleaveState::interleave)
75     state->processStates[process->pid].state = simgrid::mc::ProcessInterleaveState::done;
76 }
77
78 void MC_state_set_executed_request(simgrid::mc::State* state, smx_simcall_t req,
79                                    int value)
80 {
81   state->executed_req = *req;
82   state->req_num = value;
83
84   /* The waitany and testany request are transformed into a wait or test request over the
85    * corresponding communication action so it can be treated later by the dependence
86    * function. */
87   switch (req->call) {
88   case SIMCALL_COMM_WAITANY: {
89     state->internal_req.call = SIMCALL_COMM_WAIT;
90     state->internal_req.issuer = req->issuer;
91     smx_synchro_t remote_comm;
92     read_element(mc_model_checker->process(),
93       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
94       value, sizeof(remote_comm));
95     mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
96     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
97     simcall_comm_wait__set__timeout(&state->internal_req, 0);
98     break;
99   }
100
101   case SIMCALL_COMM_TESTANY:
102     state->internal_req.call = SIMCALL_COMM_TEST;
103     state->internal_req.issuer = req->issuer;
104
105     if (value > 0) {
106       smx_synchro_t remote_comm;
107       read_element(mc_model_checker->process(),
108         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
109         value, sizeof(remote_comm));
110       mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
111     }
112
113     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
114     simcall_comm_test__set__result(&state->internal_req, value);
115     break;
116
117   case SIMCALL_COMM_WAIT:
118     state->internal_req = *req;
119     mc_model_checker->process().read_bytes(&state->internal_comm ,
120       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
121     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
122     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
123     break;
124
125   case SIMCALL_COMM_TEST:
126     state->internal_req = *req;
127     mc_model_checker->process().read_bytes(&state->internal_comm,
128       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
129     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
130     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
131     break;
132
133   case SIMCALL_MC_RANDOM: {
134     state->internal_req = *req;
135     int random_max = simcall_mc_random__get__max(req);
136     if (value != random_max)
137       for (auto& p : mc_model_checker->process().simix_processes()) {
138         simgrid::mc::ProcessState* procstate = &state->processStates[p.copy.pid];
139         const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
140         if (p.copy.pid == issuer->pid) {
141           procstate->state = simgrid::mc::ProcessInterleaveState::more_interleave;
142           break;
143         }
144       }
145     break;
146   }
147
148   default:
149     state->internal_req = *req;
150     break;
151   }
152 }
153
154 smx_simcall_t MC_state_get_executed_request(simgrid::mc::State* state, int *value)
155 {
156   *value = state->req_num;
157   return &state->executed_req;
158 }
159
160 smx_simcall_t MC_state_get_internal_request(simgrid::mc::State* state)
161 {
162   return &state->internal_req;
163 }
164
165 static inline smx_simcall_t MC_state_get_request_for_process(
166   simgrid::mc::State* state, int*value, smx_process_t process)
167 {
168   simgrid::mc::ProcessState* procstate = &state->processStates[process->pid];
169
170   if (!procstate->interleave())
171     return nullptr;
172   if (!simgrid::mc::process_is_enabled(process))
173     return nullptr;
174
175   switch (process->simcall.call) {
176
177       case SIMCALL_COMM_WAITANY:
178         *value = -1;
179         while (procstate->interleave_count <
180               read_length(mc_model_checker->process(),
181                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
182           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
183               procstate->interleave_count++)) {
184             *value = procstate->interleave_count - 1;
185             break;
186           }
187         }
188
189         if (procstate->interleave_count >=
190             simgrid::mc::read_length(mc_model_checker->process(),
191               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
192           procstate->state = simgrid::mc::ProcessInterleaveState::done;
193
194         if (*value != -1)
195           return &process->simcall;
196
197         break;
198
199       case SIMCALL_COMM_TESTANY: {
200         unsigned start_count = procstate->interleave_count;
201         *value = -1;
202         while (procstate->interleave_count <
203                 read_length(mc_model_checker->process(),
204                   remote(simcall_comm_testany__get__comms(&process->simcall))))
205           if (simgrid::mc::request_is_enabled_by_idx(&process->simcall,
206               procstate->interleave_count++)) {
207             *value = procstate->interleave_count - 1;
208             break;
209           }
210
211         if (procstate->interleave_count >=
212             read_length(mc_model_checker->process(),
213               remote(simcall_comm_testany__get__comms(&process->simcall))))
214           procstate->state = simgrid::mc::ProcessInterleaveState::done;
215
216         if (*value != -1 || start_count == 0)
217           return &process->simcall;
218
219         break;
220       }
221
222       case SIMCALL_COMM_WAIT: {
223         smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
224         s_smx_synchro_t act;
225         mc_model_checker->process().read_bytes(
226           &act, sizeof(act), remote(remote_act));
227         if (act.comm.src_proc && act.comm.dst_proc)
228           *value = 0;
229         else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
230               && act.comm.detached == 1)
231           *value = 0;
232         else
233           *value = -1;
234         procstate->state = simgrid::mc::ProcessInterleaveState::done;
235         return &process->simcall;
236       }
237
238       case SIMCALL_MC_RANDOM:
239         if (procstate->state == simgrid::mc::ProcessInterleaveState::interleave)
240           *value = simcall_mc_random__get__min(&process->simcall);
241         else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
242           *value = state->req_num + 1;
243         procstate->state = simgrid::mc::ProcessInterleaveState::done;
244         return &process->simcall;
245
246       default:
247         procstate->state = simgrid::mc::ProcessInterleaveState::done;
248         *value = 0;
249         return &process->simcall;
250   }
251   return nullptr;
252 }
253
254 smx_simcall_t MC_state_get_request(simgrid::mc::State* state, int *value)
255 {
256   for (auto& p : mc_model_checker->process().simix_processes()) {
257     smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);
258     if (res)
259       return res;
260   }
261   return nullptr;
262 }