Logo AND Algorithmique Numérique Distribuée

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