Logo AND Algorithmique Numérique Distribuée

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