Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
14
15 /**
16  * \brief Schedules all the process that are ready to run
17  */
18 void MC_wait_for_requests(void)
19 {
20   smx_process_t process;
21   smx_simcall_t req;
22   unsigned int iter;
23
24   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
25     SIMIX_process_runall();
26     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
27       req = &process->simcall;
28       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
29         SIMIX_simcall_handle(req, 0);
30     }
31   }
32 }
33
34 int MC_request_is_enabled(smx_simcall_t req)
35 {
36   unsigned int index = 0;
37   smx_synchro_t act = 0;
38   smx_mutex_t mutex = NULL;
39
40   switch (req->call) {
41   case SIMCALL_NONE:
42     return FALSE;
43
44   case SIMCALL_COMM_WAIT:
45     /* FIXME: check also that src and dst processes are not suspended */
46     act = simcall_comm_wait__get__comm(req);
47     if (simcall_comm_wait__get__timeout(req) >= 0) {
48       /* If it has a timeout it will be always be enabled, because even if the
49        * communication is not ready, it can timeout and won't block. */
50       if (_sg_mc_timeout == 1)
51         return TRUE;
52     } else {
53       /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
54       if (act->comm.detached && act->comm.src_proc == NULL
55           && act->comm.type == SIMIX_COMM_READY)
56         return (act->comm.dst_proc != NULL);
57     }
58     return (act->comm.src_proc && act->comm.dst_proc);
59
60   case SIMCALL_COMM_WAITANY:
61     /* Check if it has at least one communication ready */
62     xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act)
63       if (act->comm.src_proc && act->comm.dst_proc)
64         return TRUE;
65     return FALSE;
66
67   case SIMCALL_MUTEX_LOCK:
68     mutex = simcall_mutex_lock__get__mutex(req);
69     if(mutex->owner == NULL)
70       return TRUE;
71     else
72       return (mutex->owner->pid == req->issuer->pid);
73
74   default:
75     /* The rest of the requests are always enabled */
76     return TRUE;
77   }
78 }
79
80 int MC_request_is_visible(smx_simcall_t req)
81 {
82   return req->call == SIMCALL_COMM_ISEND
83       || req->call == SIMCALL_COMM_IRECV
84       || req->call == SIMCALL_COMM_WAIT
85       || req->call == SIMCALL_COMM_WAITANY
86       || req->call == SIMCALL_COMM_TEST
87       || req->call == SIMCALL_COMM_TESTANY
88       || req->call == SIMCALL_MC_RANDOM
89       || req->call == SIMCALL_MUTEX_LOCK
90 #ifdef HAVE_MC
91       || req->call == SIMCALL_MC_SNAPSHOT
92       || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
93 #endif
94       ;
95 }
96
97 int MC_random(int min, int max)
98 {
99   /*FIXME: return mc_current_state->executed_transition->random.value; */
100   return simcall_mc_random(min, max);
101 }
102
103 static int prng_random(int min, int max)
104 {
105   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
106   unsigned long input_size = (unsigned long) RAND_MAX + 1;
107   unsigned long reject_size = input_size % output_size;
108   unsigned long accept_size = input_size - reject_size; // module*accept_size
109
110   // Use rejection in order to avoid skew
111   long x;
112   do {
113 #ifndef _XBT_WIN32
114     x = random();
115 #else
116     x = rand();
117 #endif
118   } while( x >= accept_size );
119   return min + (x % output_size);
120 }
121
122 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
123 {
124   if (!MC_is_active() && !MC_record_path){
125     return prng_random(min, max);
126   }
127
128   return simcall->mc_value;
129 }