1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <simgrid/simix.h>
10 #include "../simix/smx_private.h"
11 #include "mc_record.h"
14 #include "mc_process.h"
15 #include "mc_model_checker.h"
18 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
21 * \brief Schedules all the process that are ready to run
23 void MC_wait_for_requests(void)
25 smx_process_t process;
29 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
30 SIMIX_process_runall();
31 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
32 req = &process->simcall;
33 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
34 SIMIX_simcall_handle(req, 0);
39 int MC_request_is_enabled(smx_simcall_t req)
41 unsigned int index = 0;
42 smx_synchro_t act = 0;
48 case SIMCALL_COMM_WAIT:
49 /* FIXME: check also that src and dst processes are not suspended */
50 act = simcall_comm_wait__get__comm(req);
51 if (simcall_comm_wait__get__timeout(req) >= 0) {
52 /* If it has a timeout it will be always be enabled, because even if the
53 * communication is not ready, it can timeout and won't block. */
54 if (_sg_mc_timeout == 1)
57 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
58 if (act->comm.detached && act->comm.src_proc == NULL
59 && act->comm.type == SIMIX_COMM_READY)
60 return (act->comm.dst_proc != NULL);
62 return (act->comm.src_proc && act->comm.dst_proc);
64 case SIMCALL_COMM_WAITANY:
65 /* Check if it has at least one communication ready */
66 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act)
67 if (act->comm.src_proc && act->comm.dst_proc)
72 /* The rest of the requests are always enabled */
77 int MC_request_is_visible(smx_simcall_t req)
79 return req->call == SIMCALL_COMM_ISEND
80 || req->call == SIMCALL_COMM_IRECV
81 || req->call == SIMCALL_COMM_WAIT
82 || req->call == SIMCALL_COMM_WAITANY
83 || req->call == SIMCALL_COMM_TEST
84 || req->call == SIMCALL_COMM_TESTANY
85 || req->call == SIMCALL_MC_RANDOM
87 || req->call == SIMCALL_MC_SNAPSHOT
88 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
93 int MC_random(int min, int max)
95 /*FIXME: return mc_current_state->executed_transition->random.value; */
96 return simcall_mc_random(min, max);
99 static int prng_random(int min, int max)
101 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
102 unsigned long input_size = (unsigned long) RAND_MAX + 1;
103 unsigned long reject_size = input_size % output_size;
104 unsigned long accept_size = input_size - reject_size; // module*accept_size
106 // Use rejection in order to avoid skew
114 } while( x >= accept_size );
115 return min + (x % output_size);
118 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
120 if (!MC_is_active() && !MC_record_path){
121 return prng_random(min, max);
124 return simcall->mc_value;