1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "simix/smx_private.h"
14 #include "mc_model_checker.h"
19 void MC_smx_process_info_clear(mc_smx_process_info_t p)
23 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
25 mmalloc_set_current_heap(heap);
30 xbt_dynar_t MC_smx_process_info_list_new(void)
33 sizeof(s_mc_smx_process_info_t),
34 ( void_f_pvoid_t) &MC_smx_process_info_clear);
38 bool is_in_dynar(smx_process_t p, xbt_dynar_t dynar)
40 return (uintptr_t) p >= (uintptr_t) dynar->data
41 && (uintptr_t) p < ((uintptr_t) dynar->data + dynar->used * dynar->elmsize);
45 mc_smx_process_info_t MC_smx_process_get_info(smx_process_t p)
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));
55 /** Load the remote swag of processes into a dynar
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
61 static void MC_process_refresh_simix_process_list(
63 xbt_dynar_t target, xbt_swag_t remote_swag)
65 // swag = REMOTE(*simix_global->process_list)
67 MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
68 MC_PROCESS_INDEX_ANY);
71 xbt_dynar_reset(target);
73 // Load each element of the dynar from the MCed process:
75 for (p = (smx_process_t) swag.head; p; ++i) {
77 s_mc_smx_process_info_t info;
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);
85 // Lookup next process address:
86 p = (smx_process_t) xbt_swag_getNext(&info.copy, swag.offset);
88 assert(i == swag.count);
91 void MC_process_smx_refresh(mc_process_t process)
93 if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
96 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
98 // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
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));
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);
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);
114 process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
115 mmalloc_set_current_heap(heap);
118 /** Get the issuer of a simcall (`req->issuer`)
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
124 * @param process the MCed process
125 * @param req the simcall (copied in the local process)
127 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
129 if (MC_process_is_self(&mc_model_checker->process))
132 MC_process_smx_refresh(&mc_model_checker->process);
134 // This is the address of the smx_process in the MCed process:
135 void* address = req->issuer;
138 mc_smx_process_info_t p;
140 // Lookup by address:
141 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, p)
142 if (p->address == address)
144 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, i, p)
145 if (p->address == address)
148 xbt_die("Issuer not found");
151 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
153 if (!process_remote_address)
155 if (MC_process_is_self(&mc_model_checker->process))
156 return process_remote_address;
158 mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
160 return &process_info->copy;
165 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
167 if (MC_process_is_self(&mc_model_checker->process))
168 xbt_die("No process_info for local process is not enabled.");
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)
175 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, index, process_info)
176 if (process_info->address == process_remote_address)
178 xbt_die("Process info not found");
181 const char* MC_smx_process_get_host_name(smx_process_t p)
183 if (MC_process_is_self(&mc_model_checker->process))
184 return SIMIX_host_get_name(p->smx_host);
186 mc_process_t process = &mc_model_checker->process;
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) {
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;
199 MC_process_read_simple(process, hostname, host_copy.key, len);
201 // Lookup the host name in the dictionary (or create it):
202 xbt_dictelm_t elt = xbt_dict_get_elm_or_null(mc_model_checker->hosts, hostname);
204 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
205 xbt_dict_set(mc_model_checker->hosts, hostname, NULL, NULL);
206 elt = xbt_dict_get_elm_or_null(mc_model_checker->hosts, hostname);
208 mmalloc_set_current_heap(heap);
211 info->hostname = elt->key;
213 return info->hostname;
216 const char* MC_smx_process_get_name(smx_process_t p)
218 mc_process_t process = &mc_model_checker->process;
219 if (MC_process_is_self(process))
224 mc_smx_process_info_t info = MC_smx_process_get_info(p);
226 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
227 info->name = MC_process_read_string(process, p->name);
228 mmalloc_set_current_heap(heap);
233 int MC_smpi_process_count(void)
235 if (MC_process_is_self(&mc_model_checker->process))
236 return smpi_process_count();
239 MC_process_read_variable(&mc_model_checker->process, "process_count",