Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remote support for filling state->internal_comm
[simgrid.git] / src / mc / mc_smx.c
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 "mc_model_checker.h"
15
16 static
17 void MC_smx_process_info_clear(mc_smx_process_info_t p)
18 {
19   p->hostname = NULL;
20
21   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
22   free(p->name);
23   mmalloc_set_current_heap(heap);
24
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   mc_process_t 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   MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
66     MC_PROCESS_INDEX_ANY);
67
68   smx_process_t p;
69   xbt_dynar_reset(target);
70
71   // Load each element of the dynar from the MCed process:
72   int i = 0;
73   for (p = swag.head; p; ++i) {
74
75     s_mc_smx_process_info_t info;
76     info.address = p;
77     info.name = NULL;
78     info.hostname = NULL;
79     MC_process_read(process, MC_PROCESS_NO_FLAG,
80       &info.copy, p, sizeof(info.copy), MC_PROCESS_INDEX_ANY);
81     xbt_dynar_push(target, &info);
82
83     // Lookup next process address:
84     p = xbt_swag_getNext(&info.copy, swag.offset);
85   }
86   assert(i == swag.count);
87 }
88
89 void MC_process_smx_refresh(mc_process_t process)
90 {
91   if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
92     return;
93
94   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
95
96   // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
97
98   // simix_global_p = REMOTE(simix_global);
99   smx_global_t simix_global_p;
100   MC_process_read_variable(process, "simix_global", &simix_global_p, sizeof(simix_global_p));
101
102   // simix_global = REMOTE(*simix_global)
103   s_smx_global_t simix_global;
104   MC_process_read(process, MC_PROCESS_NO_FLAG, &simix_global, simix_global_p, sizeof(simix_global),
105     MC_PROCESS_INDEX_ANY);
106
107   MC_process_refresh_simix_process_list(
108     process, process->smx_process_infos, simix_global.process_list);
109   MC_process_refresh_simix_process_list(
110     process, process->smx_old_process_infos, simix_global.process_to_destroy);
111
112   process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
113   mmalloc_set_current_heap(heap);
114 }
115
116 /** Get the issuer of a simcall (`req->issuer`)
117  *
118  *  In split-process mode, it does the black magic necessary to get an address
119  *  of a (shallow) copy of the data structure the issuer SIMIX process in the local
120  *  address space.
121  *
122  *  @param process the MCed process
123  *  @param req     the simcall (copied in the local process)
124  */
125 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
126 {
127   if (MC_process_is_self(&mc_model_checker->process))
128     return req->issuer;
129
130   MC_process_smx_refresh(&mc_model_checker->process);
131
132   // This is the address of the smx_process in the MCed process:
133   void* address = req->issuer;
134
135   unsigned i;
136   mc_smx_process_info_t p;
137
138   // Lookup by address:
139   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, p)
140     if (p->address == address)
141       return &p->copy;
142   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, i, p)
143     if (p->address == address)
144       return &p->copy;
145
146   xbt_die("Issuer not found");
147 }
148
149 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
150 {
151   if (!process_remote_address)
152     return NULL;
153   if (MC_process_is_self(&mc_model_checker->process))
154     return process_remote_address;
155
156   mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
157   if (process_info)
158     return &process_info->copy;
159   else
160     return NULL;
161 }
162
163 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
164 {
165   if (MC_process_is_self(&mc_model_checker->process))
166     xbt_die("No process_info for local process is not enabled.");
167
168   unsigned index;
169   mc_smx_process_info_t process_info;
170   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, index, process_info)
171     if (process_info->address == process_remote_address)
172       return process_info;
173   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, index, process_info)
174     if (process_info->address == process_remote_address)
175       return process_info;
176   xbt_die("Process info not found");
177 }
178
179 const char* MC_smx_process_get_host_name(smx_process_t p)
180 {
181   if (MC_process_is_self(&mc_model_checker->process))
182     return SIMIX_host_get_name(p->smx_host);
183
184   mc_process_t process = &mc_model_checker->process;
185
186   // Currently, smx_host_t = xbt_dictelm_t.
187   // TODO, add an static_assert on this if switching to C++
188   // The host name is host->key and the host->key_len==strlen(host->key).
189   s_xbt_dictelm_t host_copy;
190   mc_smx_process_info_t info = MC_smx_process_get_info(p);
191   if (!info->hostname) {
192
193     // Read the hostname from the MCed process:
194     MC_process_read_simple(process, &host_copy, p->smx_host, sizeof(host_copy));
195     int len = host_copy.key_len + 1;
196     char hostname[len];
197     MC_process_read_simple(process, hostname, host_copy.key, len);
198
199     // Lookup the host name in the dictionary (or create it):
200     xbt_dictelm_t elt = xbt_dict_get_elm_or_null(mc_model_checker->hosts, hostname);
201     if (!elt) {
202       xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
203       xbt_dict_set(mc_model_checker->hosts, hostname, NULL, NULL);
204       elt = xbt_dict_get_elm_or_null(mc_model_checker->hosts, hostname);
205       assert(elt);
206       mmalloc_set_current_heap(heap);
207     }
208
209     info->hostname = elt->key;
210   }
211   return info->hostname;
212 }
213
214 const char* MC_smx_process_get_name(smx_process_t p)
215 {
216   mc_process_t process = &mc_model_checker->process;
217   if (MC_process_is_self(process))
218     return p->name;
219   if (!p->name)
220     return NULL;
221
222   mc_smx_process_info_t info = MC_smx_process_get_info(p);
223   if (!info->name) {
224     size_t size = 128;
225     char buffer[size];
226
227     size_t off = 0;
228     while (1) {
229       ssize_t n = pread(process->memory_file, buffer+off, size-off, (off_t)p->name + off);
230       if (n==-1) {
231         if (errno == EINTR)
232           continue;
233         else {
234           perror("MC_smx_process_get_name");
235           xbt_die("Could not read memory");
236         }
237       }
238       if (n==0)
239         return "?";
240       void* end = memchr(buffer+off, '\0', n);
241       if (end)
242         break;
243       off += n;
244       if (off == size)
245         return "?";
246     }
247     xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
248     info->name = strdup(buffer);
249     mmalloc_set_current_heap(heap);
250   }
251   return info->name;
252 }