Logo AND Algorithmique Numérique Distribuée

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