Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not take NULL to mean 'the current address space' in dwarf expressions
[simgrid.git] / src / mc / mc_base.c
1 /* Copyright (c) 2008-2014. 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 <simgrid/simix.h>
8
9 #include "mc_base.h"
10 #include "../simix/smx_private.h"
11 #include "mc_record.h"
12
13 #ifdef HAVE_MC
14 #include "mc_process.h"
15 #include "mc_model_checker.h"
16 #include "mc_protocol.h"
17 #endif
18
19 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
20
21 void MC_wait_for_requests(void)
22 {
23   smx_process_t process;
24   smx_simcall_t req;
25   unsigned int iter;
26
27   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
28     SIMIX_process_runall();
29     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
30       req = &process->simcall;
31       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
32         SIMIX_simcall_handle(req, 0);
33     }
34   }
35 }
36
37 int MC_request_is_enabled(smx_simcall_t req)
38 {
39   unsigned int index = 0;
40   smx_synchro_t act = 0;
41 #ifdef HAVE_MC
42   s_smx_synchro_t temp_synchro;
43 #endif
44
45   switch (req->call) {
46   case SIMCALL_NONE:
47     return FALSE;
48
49   case SIMCALL_COMM_WAIT:
50     /* FIXME: check also that src and dst processes are not suspended */
51     act = simcall_comm_wait__get__comm(req);
52
53 #ifdef HAVE_MC
54     // Fetch from MCed memory:
55     if (!MC_process_is_self(&mc_model_checker->process)) {
56       MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
57         &temp_synchro, act, sizeof(temp_synchro),
58         MC_PROCESS_INDEX_ANY);
59       act = &temp_synchro;
60     }
61 #endif
62
63     if (simcall_comm_wait__get__timeout(req) >= 0) {
64       /* If it has a timeout it will be always be enabled, because even if the
65        * communication is not ready, it can timeout and won't block. */
66       if (_sg_mc_timeout == 1)
67         return TRUE;
68     } else {
69       /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
70       if (act->comm.detached && act->comm.src_proc == NULL
71           && act->comm.type == SIMIX_COMM_READY)
72         return (act->comm.dst_proc != NULL);
73     }
74     return (act->comm.src_proc && act->comm.dst_proc);
75
76   case SIMCALL_COMM_WAITANY:
77     /* Check if it has at least one communication ready */
78     xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
79
80 #ifdef HAVE_MC
81       // Fetch from MCed memory:
82       if (!MC_process_is_self(&mc_model_checker->process)) {
83         MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
84           &temp_synchro, act, sizeof(temp_synchro),
85           MC_PROCESS_INDEX_ANY);
86         act = &temp_synchro;
87       }
88 #endif
89
90       if (act->comm.src_proc && act->comm.dst_proc)
91         return TRUE;
92     }
93     return FALSE;
94
95   case SIMCALL_MUTEX_LOCK: {
96     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
97 #ifdef HAVE_MC
98     s_smx_mutex_t temp_mutex;
99     if (!MC_process_is_self(&mc_model_checker->process)) {
100       MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
101         &temp_mutex, mutex, sizeof(temp_mutex),
102         MC_PROCESS_INDEX_ANY);
103       mutex = &temp_mutex;
104     }
105 #endif
106     if(mutex->owner == NULL)
107       return TRUE;
108     else
109       return (mutex->owner->pid == req->issuer->pid);
110     }
111
112   default:
113     /* The rest of the requests are always enabled */
114     return TRUE;
115   }
116 }
117
118 int MC_request_is_visible(smx_simcall_t req)
119 {
120   return req->call == SIMCALL_COMM_ISEND
121       || req->call == SIMCALL_COMM_IRECV
122       || req->call == SIMCALL_COMM_WAIT
123       || req->call == SIMCALL_COMM_WAITANY
124       || req->call == SIMCALL_COMM_TEST
125       || req->call == SIMCALL_COMM_TESTANY
126       || req->call == SIMCALL_MC_RANDOM
127       || req->call == SIMCALL_MUTEX_LOCK
128 #ifdef HAVE_MC
129       || req->call == SIMCALL_MC_SNAPSHOT
130       || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
131 #endif
132       ;
133 }
134
135 int MC_random(int min, int max)
136 {
137   /*FIXME: return mc_current_state->executed_transition->random.value; */
138   return simcall_mc_random(min, max);
139 }
140
141 static int prng_random(int min, int max)
142 {
143   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
144   unsigned long input_size = (unsigned long) RAND_MAX + 1;
145   unsigned long reject_size = input_size % output_size;
146   unsigned long accept_size = input_size - reject_size; // module*accept_size
147
148   // Use rejection in order to avoid skew
149   long x;
150   do {
151 #ifndef _XBT_WIN32
152     x = random();
153 #else
154     x = rand();
155 #endif
156   } while( x >= accept_size );
157   return min + (x % output_size);
158 }
159
160 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
161 {
162   if (!MC_is_active() && !MC_record_path){
163     return prng_random(min, max);
164   }
165
166   return simcall->mc_value;
167 }