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"
16 xbt_dynar_t MC_smx_process_info_list_new(void)
19 sizeof(s_mc_smx_process_info_t), NULL);
23 bool is_in_dynar(smx_process_t p, xbt_dynar_t dynar)
25 return (uintptr_t) p >= (uintptr_t) dynar->data
26 && (uintptr_t) p < ((uintptr_t) dynar->data + dynar->used * dynar->elmsize);
30 mc_smx_process_info_t MC_smx_process_get_info(smx_process_t p)
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));
40 /** Load the remote swag of processes into a dynar
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
46 static void MC_process_refresh_simix_process_list(
48 xbt_dynar_t target, xbt_swag_t remote_swag)
50 // swag = REMOTE(*simix_global->process_list)
52 MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
53 MC_PROCESS_INDEX_ANY);
56 xbt_dynar_reset(target);
58 // Load each element of the dynar from the MCed process:
60 for (p = swag.head; p; ++i) {
62 s_mc_smx_process_info_t info;
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);
69 // Lookup next process address:
70 p = xbt_swag_getNext(&info.copy, swag.offset);
72 assert(i == swag.count);
75 void MC_process_smx_refresh(mc_process_t process)
77 if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
80 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
82 // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
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));
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);
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);
98 process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
99 mmalloc_set_current_heap(heap);
102 /** Get the issuer of a simcall (`req->issuer`)
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
108 * @param process the MCed process
109 * @param req the simcall (copied in the local process)
111 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
113 if (MC_process_is_self(&mc_model_checker->process))
116 MC_process_smx_refresh(&mc_model_checker->process);
118 // This is the address of the smx_process in the MCed process:
119 void* address = req->issuer;
122 mc_smx_process_info_t p;
124 // Lookup by address:
125 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, p)
126 if (p->address == address)
128 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, i, p)
129 if (p->address == address)
132 xbt_die("Issuer not found");
135 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
137 if (MC_process_is_self(&mc_model_checker->process))
138 return process_remote_address;
140 mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
142 return &process_info->copy;
147 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
149 if (MC_process_is_self(&mc_model_checker->process))
150 xbt_die("No process_info for local process is not enabled.");
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)
157 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, index, process_info)
158 if (process_info->address == process_remote_address)
160 xbt_die("Process info not found");
163 const char* MC_smx_process_get_host_name(smx_process_t p)
165 if (MC_process_is_self(&mc_model_checker->process))
166 return SIMIX_host_get_name(p->smx_host);
168 mc_process_t process = &mc_model_checker->process;
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) {
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;
181 MC_process_read_simple(process, hostname, host_copy.key, len);
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);
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);
190 mmalloc_set_current_heap(heap);
193 info->hostname = elt->key;
195 return info->hostname;