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"
17 void MC_smx_process_info_clear(mc_smx_process_info_t p)
21 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
23 mmalloc_set_current_heap(heap);
28 xbt_dynar_t MC_smx_process_info_list_new(void)
31 sizeof(s_mc_smx_process_info_t),
32 ( void_f_pvoid_t) &MC_smx_process_info_clear);
36 bool is_in_dynar(smx_process_t p, xbt_dynar_t dynar)
38 return (uintptr_t) p >= (uintptr_t) dynar->data
39 && (uintptr_t) p < ((uintptr_t) dynar->data + dynar->used * dynar->elmsize);
43 mc_smx_process_info_t MC_smx_process_get_info(smx_process_t p)
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));
53 /** Load the remote swag of processes into a dynar
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
59 static void MC_process_refresh_simix_process_list(
61 xbt_dynar_t target, xbt_swag_t remote_swag)
63 // swag = REMOTE(*simix_global->process_list)
65 MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
66 MC_PROCESS_INDEX_ANY);
69 xbt_dynar_reset(target);
71 // Load each element of the dynar from the MCed process:
73 for (p = swag.head; p; ++i) {
75 s_mc_smx_process_info_t info;
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);
83 // Lookup next process address:
84 p = xbt_swag_getNext(&info.copy, swag.offset);
86 assert(i == swag.count);
89 void MC_process_smx_refresh(mc_process_t process)
91 if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
94 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
96 // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
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));
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);
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);
112 process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
113 mmalloc_set_current_heap(heap);
116 /** Get the issuer of a simcall (`req->issuer`)
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
122 * @param process the MCed process
123 * @param req the simcall (copied in the local process)
125 smx_process_t MC_smx_simcall_get_issuer(smx_simcall_t req)
127 if (MC_process_is_self(&mc_model_checker->process))
130 MC_process_smx_refresh(&mc_model_checker->process);
132 // This is the address of the smx_process in the MCed process:
133 void* address = req->issuer;
136 mc_smx_process_info_t p;
138 // Lookup by address:
139 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, p)
140 if (p->address == address)
142 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, i, p)
143 if (p->address == address)
146 xbt_die("Issuer not found");
149 smx_process_t MC_smx_resolve_process(smx_process_t process_remote_address)
151 if (!process_remote_address)
153 if (MC_process_is_self(&mc_model_checker->process))
154 return process_remote_address;
156 mc_smx_process_info_t process_info = MC_smx_resolve_process_info(process_remote_address);
158 return &process_info->copy;
163 mc_smx_process_info_t MC_smx_resolve_process_info(smx_process_t process_remote_address)
165 if (MC_process_is_self(&mc_model_checker->process))
166 xbt_die("No process_info for local process is not enabled.");
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)
173 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_old_process_infos, index, process_info)
174 if (process_info->address == process_remote_address)
176 xbt_die("Process info not found");
179 const char* MC_smx_process_get_host_name(smx_process_t p)
181 if (MC_process_is_self(&mc_model_checker->process))
182 return SIMIX_host_get_name(p->smx_host);
184 mc_process_t process = &mc_model_checker->process;
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) {
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;
197 MC_process_read_simple(process, hostname, host_copy.key, len);
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);
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);
206 mmalloc_set_current_heap(heap);
209 info->hostname = elt->key;
211 return info->hostname;
214 const char* MC_smx_process_get_name(smx_process_t p)
216 mc_process_t process = &mc_model_checker->process;
217 if (MC_process_is_self(process))
222 mc_smx_process_info_t info = MC_smx_process_get_info(p);
229 ssize_t n = pread(process->memory_file, buffer+off, size-off, (off_t)p->name + off);
234 perror("MC_smx_process_get_name");
235 xbt_die("Could not read memory");
240 void* end = memchr(buffer+off, '\0', n);
247 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
248 info->name = strdup(buffer);
249 mmalloc_set_current_heap(heap);