Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove useless braces
[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 <xbt/log.h>
10 #include <xbt/sysdep.h>
11 #include <xbt/fifo.h>
12
13 #include "src/simix/smx_private.h"
14 #include "src/mc/mc_state.h"
15 #include "src/mc/mc_request.h"
16 #include "src/mc/mc_private.h"
17 #include "src/mc/mc_comm_pattern.h"
18 #include "src/mc/mc_smx.h"
19 #include "src/mc/mc_xbt.hpp"
20
21 using simgrid::mc::remote;
22
23 extern "C" {
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc,
26                                 "Logging specific to MC (state)");
27
28 /**
29  * \brief Creates a state data structure used by the exploration algorithm
30  */
31 mc_state_t MC_state_new()
32 {
33   mc_state_t state = xbt_new0(s_mc_state_t, 1);
34
35   state->max_pid = MC_smx_get_maxpid();
36   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
37   state->system_state = nullptr;
38   state->num = ++mc_stats->expanded_states;
39   state->in_visited_states = 0;
40   state->incomplete_comm_pattern = nullptr;
41   /* Stateful model checking */
42   if((_sg_mc_checkpoint > 0 && (mc_stats->expanded_states % _sg_mc_checkpoint == 0)) ||  _sg_mc_termination){
43     state->system_state = simgrid::mc::take_snapshot(state->num);
44     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
45       MC_state_copy_incomplete_communications_pattern(state);
46       MC_state_copy_index_communications_pattern(state);
47     }
48   }
49   return state;
50 }
51
52 /**
53  * \brief Deletes a state data structure
54  * \param trans The state to be deleted
55  */
56 void MC_state_delete(mc_state_t state, int free_snapshot){
57   if (state->system_state && free_snapshot)
58     delete state->system_state;
59   if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
60     xbt_free(state->index_comm);
61     xbt_free(state->incomplete_comm_pattern);
62   }
63   xbt_free(state->proc_status);
64   xbt_free(state);
65 }
66
67 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
68 {
69   assert(state);
70   state->proc_status[process->pid].state = MC_INTERLEAVE;
71   state->proc_status[process->pid].interleave_count = 0;
72 }
73
74 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process)
75 {
76   if (state->proc_status[process->pid].state == MC_INTERLEAVE)
77     state->proc_status[process->pid].state = MC_DONE;
78 }
79
80 unsigned int MC_state_interleave_size(mc_state_t state)
81 {
82   unsigned int i, size = 0;
83   for (i = 0; i < state->max_pid; i++)
84     if ((state->proc_status[i].state == MC_INTERLEAVE)
85         || (state->proc_status[i].state == MC_MORE_INTERLEAVE))
86       size++;
87   return size;
88 }
89
90 int MC_state_process_is_done(mc_state_t state, smx_process_t process)
91 {
92   return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
93 }
94
95 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req,
96                                    int value)
97 {
98   state->executed_req = *req;
99   state->req_num = value;
100
101   /* The waitany and testany request are transformed into a wait or test request over the
102    * corresponding communication action so it can be treated later by the dependence
103    * function. */
104   switch (req->call) {
105   case SIMCALL_COMM_WAITANY: {
106     state->internal_req.call = SIMCALL_COMM_WAIT;
107     state->internal_req.issuer = req->issuer;
108     smx_synchro_t remote_comm;
109     read_element(mc_model_checker->process(),
110       &remote_comm, remote(simcall_comm_waitany__get__comms(req)),
111       value, sizeof(remote_comm));
112     mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
113     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
114     simcall_comm_wait__set__timeout(&state->internal_req, 0);
115     break;
116   }
117
118   case SIMCALL_COMM_TESTANY:
119     state->internal_req.call = SIMCALL_COMM_TEST;
120     state->internal_req.issuer = req->issuer;
121
122     if (value > 0) {
123       smx_synchro_t remote_comm;
124       read_element(mc_model_checker->process(),
125         &remote_comm, remote(simcall_comm_testany__get__comms(req)),
126         value, sizeof(remote_comm));
127       mc_model_checker->process().read(&state->internal_comm, remote(remote_comm));
128     }
129
130     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
131     simcall_comm_test__set__result(&state->internal_req, value);
132     break;
133
134   case SIMCALL_COMM_WAIT:
135     state->internal_req = *req;
136     mc_model_checker->process().read_bytes(&state->internal_comm ,
137       sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req)));
138     simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm);
139     simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm);
140     break;
141
142   case SIMCALL_COMM_TEST:
143     state->internal_req = *req;
144     mc_model_checker->process().read_bytes(&state->internal_comm,
145       sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req)));
146     simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm);
147     simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm);
148     break;
149
150   case SIMCALL_MC_RANDOM: {
151     state->internal_req = *req;
152     int random_max = simcall_mc_random__get__max(req);
153     if (value != random_max)
154       for (auto& p : mc_model_checker->process().simix_processes()) {
155         mc_procstate_t procstate = &state->proc_status[p.copy.pid];
156         const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
157         if (p.copy.pid == issuer->pid) {
158           procstate->state = MC_MORE_INTERLEAVE;
159           break;
160         }
161       }
162     break;
163   }
164
165   default:
166     state->internal_req = *req;
167     break;
168   }
169 }
170
171 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
172 {
173   *value = state->req_num;
174   return &state->executed_req;
175 }
176
177 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
178 {
179   return &state->internal_req;
180 }
181
182 static inline smx_simcall_t MC_state_get_request_for_process(
183   mc_state_t state, int*value, smx_process_t process)
184 {
185   mc_procstate_t procstate = &state->proc_status[process->pid];
186
187   if (procstate->state != MC_INTERLEAVE
188       && procstate->state != MC_MORE_INTERLEAVE)
189       return nullptr;
190   if (!MC_process_is_enabled(process))
191     return nullptr;
192
193   switch (process->simcall.call) {
194
195       case SIMCALL_COMM_WAITANY:
196         *value = -1;
197         while (procstate->interleave_count <
198               read_length(mc_model_checker->process(),
199                 remote(simcall_comm_waitany__get__comms(&process->simcall)))) {
200           if (MC_request_is_enabled_by_idx
201               (&process->simcall, procstate->interleave_count++)) {
202             *value = procstate->interleave_count - 1;
203             break;
204           }
205         }
206
207         if (procstate->interleave_count >=
208             simgrid::mc::read_length(mc_model_checker->process(),
209               simgrid::mc::remote(simcall_comm_waitany__get__comms(&process->simcall))))
210           procstate->state = MC_DONE;
211
212         if (*value != -1)
213           return &process->simcall;
214
215         break;
216
217       case SIMCALL_COMM_TESTANY: {
218         unsigned start_count = procstate->interleave_count;
219         *value = -1;
220         while (procstate->interleave_count <
221                 read_length(mc_model_checker->process(),
222                   remote(simcall_comm_testany__get__comms(&process->simcall))))
223           if (MC_request_is_enabled_by_idx
224               (&process->simcall, procstate->interleave_count++)) {
225             *value = procstate->interleave_count - 1;
226             break;
227           }
228
229         if (procstate->interleave_count >=
230             read_length(mc_model_checker->process(),
231               remote(simcall_comm_testany__get__comms(&process->simcall))))
232           procstate->state = MC_DONE;
233
234         if (*value != -1 || start_count == 0)
235           return &process->simcall;
236
237         break;
238       }
239
240       case SIMCALL_COMM_WAIT: {
241         smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall);
242         s_smx_synchro_t act;
243         mc_model_checker->process().read_bytes(
244           &act, sizeof(act), remote(remote_act));
245         if (act.comm.src_proc && act.comm.dst_proc)
246           *value = 0;
247         else if (act.comm.src_proc == nullptr && act.comm.type == SIMIX_COMM_READY
248               && act.comm.detached == 1)
249           *value = 0;
250         else
251           *value = -1;
252         procstate->state = MC_DONE;
253         return &process->simcall;
254       }
255
256       case SIMCALL_MC_RANDOM:
257         if (procstate->state == MC_INTERLEAVE)
258           *value = simcall_mc_random__get__min(&process->simcall);
259         else if (state->req_num < simcall_mc_random__get__max(&process->simcall))
260           *value = state->req_num + 1;
261         procstate->state = MC_DONE;
262         return &process->simcall;
263
264       default:
265         procstate->state = MC_DONE;
266         *value = 0;
267         return &process->simcall;
268   }
269   return nullptr;
270 }
271
272 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
273 {
274   for (auto& p : mc_model_checker->process().simix_processes()) {
275     smx_simcall_t res = MC_state_get_request_for_process(state, value, &p.copy);
276     if (res)
277       return res;
278   }
279   return nullptr;
280 }
281
282 }