Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Fix bad cast, CpuL07::getModel() is a CpuL07Model* not a HostL07Model*
[simgrid.git] / src / mc / mc_smx.cpp
1 /* Copyright (c) 2015. 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 <assert.h>
8
9 #include <xbt/log.h>
10
11 #include "src/simix/smx_private.h"
12
13 #include "src/mc/mc_smx.h"
14 #include "ModelChecker.hpp"
15
16 using simgrid::mc::remote;
17
18 extern "C" {
19
20 static
21 void MC_smx_process_info_clear(mc_smx_process_info_t p)
22 {
23   p->hostname = NULL;
24   free(p->name);
25   p->name = NULL;
26 }
27
28 xbt_dynar_t MC_smx_process_info_list_new(void)
29 {
30   return xbt_dynar_new(
31     sizeof(s_mc_smx_process_info_t),
32     ( void_f_pvoid_t) &MC_smx_process_info_clear);
33 }
34
35 static inline
36 bool is_in_dynar(smx_process_t p, xbt_dynar_t dynar)
37 {
38   return (uintptr_t) p >= (uintptr_t) dynar->data
39     && (uintptr_t) p < ((uintptr_t) dynar->data + dynar->used * dynar->elmsize);
40 }
41
42 static inline
43 mc_smx_process_info_t MC_smx_process_get_info(smx_process_t p)
44 {
45   assert(is_in_dynar(p, mc_model_checker->process().smx_process_infos)
46     || is_in_dynar(p, mc_model_checker->process().smx_old_process_infos));
47   mc_smx_process_info_t process_info =
48     (mc_smx_process_info_t)
49       ((char*) p - offsetof(s_mc_smx_process_info_t, copy));
50   return process_info;
51 }
52
53 /** Load the remote swag of processes into a dynar
54  *
55  *  @param process     MCed process
56  *  @param target      Local dynar (to be filled with copies of `s_smx_process_t`)
57  *  @param remote_swag Address of the process SWAG in the remote list
58  */
59 static void MC_process_refresh_simix_process_list(
60   simgrid::mc::Process* process,
61   xbt_dynar_t target, xbt_swag_t remote_swag)
62 {
63   // swag = REMOTE(*simix_global->process_list)
64   s_xbt_swag_t swag;
65   process->read_bytes(&swag, sizeof(swag), remote(remote_swag));
66
67   smx_process_t p;
68   xbt_dynar_reset(target);
69
70   // Load each element of the dynar from the MCed process:
71   int i = 0;
72   for (p = (smx_process_t) swag.head; p; ++i) {
73
74     s_mc_smx_process_info_t info;
75     info.address = p;
76     info.name = NULL;
77     info.hostname = NULL;
78     process->read_bytes(&info.copy, sizeof(info.copy), remote(p));
79     xbt_dynar_push(target, &info);
80
81     // Lookup next process address:
82     p = (smx_process_t) xbt_swag_getNext(&info.copy, swag.offset);
83   }
84   assert(i == swag.count);
85 }
86
87 void MC_process_smx_refresh(simgrid::mc::Process* process)
88 {
89   xbt_assert(mc_mode == MC_MODE_SERVER);
90   if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
91     return;
92
93   // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
94
95   // simix_global_p = REMOTE(simix_global);
96   smx_global_t simix_global_p;
97   process->read_variable("simix_global", &simix_global_p, sizeof(simix_global_p));
98
99   // simix_global = REMOTE(*simix_global)
100   s_smx_global_t simix_global;
101   process->read_bytes(&simix_global, sizeof(simix_global),
102     remote(simix_global_p));
103
104   MC_process_refresh_simix_process_list(
105     process, process->smx_process_infos, simix_global.process_list);
106   MC_process_refresh_simix_process_list(
107     process, process->smx_old_process_infos, simix_global.process_to_destroy);
108
109   process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
110 }
111
112 /** Get the issuer of a simcall (`req->issuer`)
113  *
114  *  In split-process mode, it does the black magic necessary to get an address
115  *  of a (shallow) copy of the data structure the issuer SIMIX process in the local
116  *  address space.
117  *
118  *  @param process the MCed process
119  *  @param req     the simcall (copied in the local process)
120  */
121 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
122 {
123   if (mc_mode == MC_MODE_CLIENT)
124     return req->issuer;
125
126   MC_process_smx_refresh(&mc_model_checker->process());
127
128   // This is the address of the smx_process in the MCed process:
129   void* address = req->issuer;
130
131   unsigned i;
132   mc_smx_process_info_t p;
133
134   // Lookup by address:
135   xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, i, p)
136     if (p->address == address)
137       return &p->copy;
138   xbt_dynar_foreach_ptr(mc_model_checker->process().smx_old_process_infos, i, p)
139     if (p->address == address)
140       return &p->copy;
141
142   xbt_die("Issuer not found");
143 }
144
145 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
146 {
147   if (!process_remote_address)
148     return NULL;
149   if (mc_mode == MC_MODE_CLIENT)
150     return process_remote_address;
151
152   mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
153   if (process_info)
154     return &process_info->copy;
155   else
156     return NULL;
157 }
158
159 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
160 {
161   if (mc_mode == MC_MODE_CLIENT)
162     xbt_die("No process_info for local process is not enabled.");
163
164   unsigned index;
165   mc_smx_process_info_t process_info;
166   xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, index, process_info)
167     if (process_info->address == process_remote_address)
168       return process_info;
169   xbt_dynar_foreach_ptr(mc_model_checker->process().smx_old_process_infos, index, process_info)
170     if (process_info->address == process_remote_address)
171       return process_info;
172   xbt_die("Process info not found");
173 }
174
175 const char* MC_smx_process_get_host_name(smx_process_t p)
176 {
177   if (mc_mode == MC_MODE_CLIENT)
178     return SIMIX_host_get_name(p->host);
179
180   simgrid::mc::Process* process = &mc_model_checker->process();
181
182   // FIXME, simgrid::Host
183   // Currently, smx_host_t = xbt_dictelm_t.
184   // TODO, add an static_assert on this if switching to C++
185   // The host name is host->key and the host->key_len==strlen(host->key).
186   s_xbt_dictelm_t host_copy;
187   mc_smx_process_info_t info = MC_smx_process_get_info(p);
188   if (!info->hostname) {
189
190     // Read the hostname from the MCed process:
191     process->read_bytes(&host_copy, sizeof(host_copy), remote(p->host));
192     int len = host_copy.key_len + 1;
193     char hostname[len];
194     process->read_bytes(hostname, len, remote(host_copy.key));
195     info->hostname = mc_model_checker->get_host_name(hostname);
196   }
197   return info->hostname;
198 }
199
200 const char* MC_smx_process_get_name(smx_process_t p)
201 {
202   simgrid::mc::Process* process = &mc_model_checker->process();
203   if (mc_mode == MC_MODE_CLIENT)
204     return p->name;
205   if (!p->name)
206     return NULL;
207
208   mc_smx_process_info_t info = MC_smx_process_get_info(p);
209   if (!info->name) {
210     info->name = process->read_string(p->name);
211   }
212   return info->name;
213 }
214
215 #ifdef HAVE_SMPI
216 int MC_smpi_process_count(void)
217 {
218   if (mc_mode == MC_MODE_CLIENT)
219     return smpi_process_count();
220   else {
221     int res;
222     mc_model_checker->process().read_variable("process_count",
223       &res, sizeof(res));
224     return res;
225   }
226 }
227 #endif
228
229 unsigned long MC_smx_get_maxpid(void)
230 {
231   unsigned long maxpid;
232   mc_model_checker->process().read_variable("simix_process_maxpid",
233     &maxpid, sizeof(maxpid));
234   return maxpid;
235 }
236
237 }