Logo AND Algorithmique Numérique Distribuée

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