Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
384501de4d8223628f43324fcf9e3ce277159b54
[simgrid.git] / src / mc / mc_base.cpp
1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid_config.h>
7
8 #include "mc/mc.h"
9 #include "src/mc/mc_base.h"
10 #include "src/mc/mc_replay.h"
11 #include "src/simix/smx_private.h"
12
13 #if SIMGRID_HAVE_MC
14 #include "src/mc/ModelChecker.hpp"
15
16 using simgrid::mc::remote;
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(mc, "All MC categories");
20
21 int MC_random(int min, int max)
22 {
23 #if SIMGRID_HAVE_MC
24   xbt_assert(mc_model_checker == nullptr);
25 #endif
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);
28 }
29
30 namespace simgrid {
31 namespace mc {
32
33 void wait_for_requests()
34 {
35 #if SIMGRID_HAVE_MC
36   xbt_assert(mc_model_checker == nullptr);
37 #endif
38
39   smx_actor_t process;
40   smx_simcall_t req;
41   unsigned int iter;
42
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);
49     }
50   }
51 #if SIMGRID_HAVE_MC
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);
55   }
56 #endif
57 }
58
59
60 bool request_is_visible(smx_simcall_t req)
61 {
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
71       ;
72 }
73
74 }
75 }
76
77 static int prng_random(int min, int max)
78 {
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
83
84   // Use rejection in order to avoid skew
85   unsigned long x;
86   do {
87 #ifndef _WIN32
88     x = (unsigned long) random();
89 #else
90     x = (unsigned long) rand();
91 #endif
92   } while( x >= accept_size );
93   return min + (x % output_size);
94 }
95
96 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
97 {
98   if (not MC_is_active() && not MC_record_path)
99     return prng_random(min, max);
100   return simcall->mc_value;
101 }