3 /* process_sg - GRAS process handling on simulator */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "gras_modinter.h" /* module initialization interface */
13 #include "gras/Virtu/virtu_sg.h"
14 #include "gras/Msg/msg_interface.h" /* For some checks at simulation end */
15 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
19 static long int PID = 1;
22 void gras_agent_spawn(const char *name, void *data,
23 xbt_main_func_t code, int argc, char *argv[], xbt_dict_t properties) {
25 SIMIX_process_create(name, code,
28 argc, argv, properties);
31 /* **************************************************************************
32 * Process constructor/destructor (semi-public interface)
33 * **************************************************************************/
37 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
38 gras_procdata_t *pd=xbt_new0(gras_procdata_t,1);
39 gras_trp_procdata_t trp_pd;
41 SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
47 /* First process on this host */
48 hd=xbt_new(gras_hostdata_t,1);
50 hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
51 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
56 trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
59 if (SIMIX_process_self() != NULL ) {
60 pd->ppid = gras_os_getpid();
64 trp_pd->msg_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
66 trp_pd->meas_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
68 VERB2("Creating process '%s' (%d)",
69 SIMIX_process_get_name(SIMIX_process_self()),
75 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
76 gras_socket_t sock_iter;
79 (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
81 (gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
83 gras_msg_procdata_t msg_pd=
84 (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
85 gras_trp_procdata_t trp_pd=
86 (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
88 xbt_queue_free(&trp_pd->msg_selectable_sockets);
90 xbt_queue_free(&trp_pd->meas_selectable_sockets);
93 xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
95 VERB2("GRAS: Finalizing process '%s' (%d)",
96 SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
98 if (xbt_dynar_length(msg_pd->msg_queue))
99 WARN1("process %d terminated, but some messages are still queued",
102 /* if each process has its sockets list, we need to close them when the
104 xbt_dynar_foreach(sockets,cursor,sock_iter) {
105 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
107 gras_socket_close(sock_iter);
109 if ( ! --(hd->refcount)) {
110 xbt_dynar_free(&hd->ports);
113 gras_procdata_exit();
117 /* **************************************************************************
118 * Process data (public interface)
119 * **************************************************************************/
121 gras_procdata_t *gras_procdata_get(void) {
123 (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
125 xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
130 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
132 (gras_procdata_t *)SIMIX_process_get_data(p);
134 xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
135 SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
137 return gras_libdata_by_name_from_procdata(name, pd);
140 /** @brief retrieve the value of a given process property (or NULL if not defined) */
141 const char* gras_process_property_value(const char* name) {
142 return xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()), name);
145 /** @brief retrieve the process properties dictionnary
146 * @warning it's the original one, not a copy. Don't mess with it
148 xbt_dict_t gras_process_properties(void)
150 return SIMIX_process_get_properties(SIMIX_process_self());
153 /* **************************************************************************
154 * OS virtualization function
155 * **************************************************************************/
157 const char* xbt_procname(void) {
158 const char *res = NULL;
159 smx_process_t process = SIMIX_process_self();
160 if ((process != NULL) && (process->simdata))
161 res = SIMIX_process_get_name(process);
168 int gras_os_getpid(void) {
170 smx_process_t process = SIMIX_process_self();
172 if ((process != NULL) && (process->data))
173 return ((gras_procdata_t*)process->data)->pid;
179 /** @brief retrieve the value of a given host property (or NULL if not defined) */
180 const char* gras_os_host_property_value(const char* name) {
181 return xbt_dict_get_or_null(SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self())), name);
184 /** @brief retrieve the host properties dictionnary
185 * @warning it's the original one, not a copy. Don't mess with it
187 xbt_dict_t gras_os_host_properties(void) {
188 return SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
191 /* **************************************************************************
192 * Interface with SIMIX
193 * **************************************************************************/
195 void gras_global_init(int *argc,char **argv) {
196 SIMIX_global_init(argc,argv);
198 void gras_create_environment(const char *file) {
199 SIMIX_create_environment(file);
201 void gras_function_register(const char *name, xbt_main_func_t code) {
202 SIMIX_function_register(name, code);
206 smx_cond_t cond = NULL;
208 xbt_fifo_t actions_done = xbt_fifo_new();
209 xbt_fifo_t actions_failed = xbt_fifo_new();
211 /* Clean IO before the run */
215 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
216 while ( (action = xbt_fifo_pop(actions_failed)) ) {
217 DEBUG1("** %s failed **",action->name);
218 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
219 SIMIX_cond_broadcast(cond);
221 /* action finished, destroy it */
222 // SIMIX_action_destroy(action);
225 while ( (action = xbt_fifo_pop(actions_done)) ) {
226 DEBUG1("** %s done **",action->name);
227 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
228 SIMIX_cond_broadcast(cond);
230 /* action finished, destroy it */
231 //SIMIX_action_destroy(action);
234 xbt_fifo_free(actions_failed);
235 xbt_fifo_free(actions_done);
239 void gras_launch_application(const char *file) {
240 SIMIX_launch_application(file);