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 "ModelChecker.hpp"
19 void MC_smx_process_info_clear(mc_smx_process_info_t p)
26 xbt_dynar_t MC_smx_process_info_list_new(void)
29 sizeof(s_mc_smx_process_info_t),
30 ( void_f_pvoid_t) &MC_smx_process_info_clear);
34 bool is_in_dynar(smx_process_t p, xbt_dynar_t dynar)
36 return (uintptr_t) p >= (uintptr_t) dynar->data
37 && (uintptr_t) p < ((uintptr_t) dynar->data + dynar->used * dynar->elmsize);
41 mc_smx_process_info_t MC_smx_process_get_info(smx_process_t p)
43 assert(is_in_dynar(p, mc_model_checker->process().smx_process_infos)
44 || is_in_dynar(p, mc_model_checker->process().smx_old_process_infos));
45 mc_smx_process_info_t process_info =
46 (mc_smx_process_info_t)
47 ((char*) p - offsetof(s_mc_smx_process_info_t, copy));
51 /** Load the remote swag of processes into a dynar
53 * @param process MCed process
54 * @param target Local dynar (to be filled with copies of `s_smx_process_t`)
55 * @param remote_swag Address of the process SWAG in the remote list
57 static void MC_process_refresh_simix_process_list(
59 xbt_dynar_t target, xbt_swag_t remote_swag)
61 // swag = REMOTE(*simix_global->process_list)
63 MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
64 MC_PROCESS_INDEX_ANY);
67 xbt_dynar_reset(target);
69 // Load each element of the dynar from the MCed process:
71 for (p = (smx_process_t) swag.head; p; ++i) {
73 s_mc_smx_process_info_t info;
77 MC_process_read(process, MC_PROCESS_NO_FLAG,
78 &info.copy, p, sizeof(info.copy), MC_PROCESS_INDEX_ANY);
79 xbt_dynar_push(target, &info);
81 // Lookup next process address:
82 p = (smx_process_t) xbt_swag_getNext(&info.copy, swag.offset);
84 assert(i == swag.count);
87 void MC_process_smx_refresh(mc_process_t process)
89 if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
92 // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
94 // simix_global_p = REMOTE(simix_global);
95 smx_global_t simix_global_p;
96 MC_process_read_variable(process, "simix_global", &simix_global_p, sizeof(simix_global_p));
98 // simix_global = REMOTE(*simix_global)
99 s_smx_global_t simix_global;
100 MC_process_read(process, MC_PROCESS_NO_FLAG, &simix_global, simix_global_p, sizeof(simix_global),
101 MC_PROCESS_INDEX_ANY);
103 MC_process_refresh_simix_process_list(
104 process, process->smx_process_infos, simix_global.process_list);
105 MC_process_refresh_simix_process_list(
106 process, process->smx_old_process_infos, simix_global.process_to_destroy);
108 process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
111 /** Get the issuer of a simcall (`req->issuer`)
113 * In split-process mode, it does the black magic necessary to get an address
114 * of a (shallow) copy of the data structure the issuer SIMIX process in the local
117 * @param process the MCed process
118 * @param req the simcall (copied in the local process)
120 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
122 if (MC_process_is_self(&mc_model_checker->process()))
125 MC_process_smx_refresh(&mc_model_checker->process());
127 // This is the address of the smx_process in the MCed process:
128 void* address = req->issuer;
131 mc_smx_process_info_t p;
133 // Lookup by address:
134 xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, i, p)
135 if (p->address == address)
137 xbt_dynar_foreach_ptr(mc_model_checker->process().smx_old_process_infos, i, p)
138 if (p->address == address)
141 xbt_die("Issuer not found");
144 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
146 if (!process_remote_address)
148 if (MC_process_is_self(&mc_model_checker->process()))
149 return process_remote_address;
151 mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
153 return &process_info->copy;
158 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
160 if (MC_process_is_self(&mc_model_checker->process()))
161 xbt_die("No process_info for local process is not enabled.");
164 mc_smx_process_info_t process_info;
165 xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, index, process_info)
166 if (process_info->address == process_remote_address)
168 xbt_dynar_foreach_ptr(mc_model_checker->process().smx_old_process_infos, index, process_info)
169 if (process_info->address == process_remote_address)
171 xbt_die("Process info not found");
174 const char* MC_smx_process_get_host_name(smx_process_t p)
176 if (MC_process_is_self(&mc_model_checker->process()))
177 return SIMIX_host_get_name(p->smx_host);
179 mc_process_t process = &mc_model_checker->process();
181 // Currently, smx_host_t = xbt_dictelm_t.
182 // TODO, add an static_assert on this if switching to C++
183 // The host name is host->key and the host->key_len==strlen(host->key).
184 s_xbt_dictelm_t host_copy;
185 mc_smx_process_info_t info = MC_smx_process_get_info(p);
186 if (!info->hostname) {
188 // Read the hostname from the MCed process:
189 MC_process_read_simple(process, &host_copy, p->smx_host, sizeof(host_copy));
190 int len = host_copy.key_len + 1;
192 MC_process_read_simple(process, hostname, host_copy.key, len);
193 info->hostname = mc_model_checker->get_host_name(hostname);
195 return info->hostname;
198 const char* MC_smx_process_get_name(smx_process_t p)
200 mc_process_t process = &mc_model_checker->process();
201 if (MC_process_is_self(process))
206 mc_smx_process_info_t info = MC_smx_process_get_info(p);
208 info->name = MC_process_read_string(process, p->name);
213 int MC_smpi_process_count(void)
215 if (MC_process_is_self(&mc_model_checker->process()))
216 return smpi_process_count();
219 MC_process_read_variable(&mc_model_checker->process(), "process_count",
225 unsigned long MC_smx_get_maxpid(void)
227 unsigned long maxpid;
228 MC_process_read_variable(&mc_model_checker->process(), "simix_process_maxpid",
229 &maxpid, sizeof(maxpid));