Logo AND Algorithmique Numérique Distribuée

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