Logo AND Algorithmique Numérique Distribuée

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