1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid_config.h>
9 #include "src/mc/mc_base.h"
10 #include "src/mc/mc_replay.h"
11 #include "src/simix/smx_private.h"
14 #include "src/mc/ModelChecker.hpp"
16 using simgrid::mc::remote;
19 XBT_LOG_NEW_DEFAULT_CATEGORY(mc, "All MC categories");
21 int MC_random(int min, int max)
24 xbt_assert(mc_model_checker == nullptr);
26 /* TODO, if the MC is disabled we do not really need to make a simcall for this :) */
27 return simcall_mc_random(min, max);
33 void wait_for_requests()
36 xbt_assert(mc_model_checker == nullptr);
43 while (not xbt_dynar_is_empty(simix_global->process_to_run)) {
44 SIMIX_process_runall();
45 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
46 req = &process->simcall;
47 if (req->call != SIMCALL_NONE && not simgrid::mc::request_is_visible(req))
48 SIMIX_simcall_handle(req, 0);
52 xbt_dynar_reset(simix_global->actors_vector);
53 for (std::pair<aid_t, smx_actor_t> kv : simix_global->process_list) {
54 xbt_dynar_push_as(simix_global->actors_vector, smx_actor_t, kv.second);
60 bool request_is_visible(smx_simcall_t req)
62 return req->call == SIMCALL_COMM_ISEND
63 || req->call == SIMCALL_COMM_IRECV
64 || req->call == SIMCALL_COMM_WAIT
65 || req->call == SIMCALL_COMM_WAITANY
66 || req->call == SIMCALL_COMM_TEST
67 || req->call == SIMCALL_COMM_TESTANY
68 || req->call == SIMCALL_MC_RANDOM
69 || req->call == SIMCALL_MUTEX_LOCK
70 || req->call == SIMCALL_MUTEX_TRYLOCK
77 static int prng_random(int min, int max)
79 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
80 unsigned long input_size = (unsigned long) RAND_MAX + 1;
81 unsigned long reject_size = input_size % output_size;
82 unsigned long accept_size = input_size - reject_size; // module*accept_size
84 // Use rejection in order to avoid skew
88 x = (unsigned long) random();
90 x = (unsigned long) rand();
92 } while( x >= accept_size );
93 return min + (x % output_size);
96 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
98 if (not MC_is_active() && not MC_record_path)
99 return prng_random(min, max);
100 return simcall->mc_value;