Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
added MPI_Get_processor_name()
[simgrid.git] / src / simix / private.h
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef SIMIX_PRIVATE_H
10 #define SIMIX_PRIVATE_H
11
12 #include "simix/simix.h"
13 #include "surf/surf.h"
14 #include "xbt/fifo.h"
15 #include "xbt/swag.h"
16 #include "xbt/dict.h"
17 #include "xbt/config.h"
18 #include "xbt/function_types.h"
19 #include "xbt/ex_interface.h"
20
21 /******************************** Datatypes ***********************************/
22
23
24 /*********************************** Host *************************************/
25
26 /** @brief Host datatype
27     @ingroup m_datatypes_management_details */
28 typedef struct s_smx_host {
29   char *name;              /**< @brief host name if any */
30   void *host;              /* SURF modeling */
31   xbt_swag_t process_list;
32   void *data;              /**< @brief user data */
33 } s_smx_host_t;
34
35 /********************************** Simix Global ******************************/
36
37 typedef struct s_smx_context_factory *smx_context_factory_t;
38
39 typedef struct SIMIX_Global {
40   smx_context_factory_t context_factory;
41   xbt_dict_t host;
42   xbt_swag_t process_to_run;
43   xbt_swag_t process_list;
44   xbt_swag_t process_to_destroy;
45   smx_process_t current_process;
46   smx_process_t maestro_process;
47   xbt_dict_t registered_functions;
48   smx_creation_func_t create_process_function;
49   void_f_pvoid_t kill_process_function;
50   void_f_pvoid_t cleanup_process_function;
51 } s_SIMIX_Global_t, *SIMIX_Global_t;
52
53 extern SIMIX_Global_t simix_global;
54
55 /******************************** Process *************************************/
56
57 /** @brief Process datatype 
58     @ingroup m_datatypes_management_details @{ */
59      typedef struct s_smx_process {
60        s_xbt_swag_hookup_t process_hookup;
61        s_xbt_swag_hookup_t synchro_hookup;
62        s_xbt_swag_hookup_t host_proc_hookup;
63        s_xbt_swag_hookup_t destroy_hookup;
64
65        char *name;              /**< @brief process name if any */
66        smx_host_t smx_host;     /* the host on which the process is running */
67        smx_context_t context;   /* the context that executes the scheduler function */
68        ex_ctx_t *exception;
69        int blocked : 1;
70        int suspended : 1;
71        int iwannadie : 1;
72        smx_mutex_t mutex;       /* mutex on which the process is blocked  */
73        smx_cond_t cond;         /* cond on which the process is blocked  */
74        smx_sem_t sem;         /* semaphore on which the process is blocked  */
75        smx_action_t waiting_action;
76        xbt_dict_t properties;
77        void *data;              /* kept for compatibility, it should be replaced with moddata */
78
79      } s_smx_process_t;
80 /** @} */
81
82 typedef struct s_smx_process_arg {
83   const char *name;
84   xbt_main_func_t code;
85   void *data;
86   char *hostname;
87   int argc;
88   char **argv;
89   double kill_time;
90   xbt_dict_t properties;
91 } s_smx_process_arg_t, *smx_process_arg_t;
92
93 void SIMIX_create_maestro_process(void);
94 void SIMIX_process_empty_trash(void);
95 void SIMIX_process_schedule(smx_process_t process);
96 ex_ctx_t *SIMIX_process_get_exception(void);
97 void SIMIX_process_exception_terminate(xbt_ex_t * e);
98
99 /*************************** Mutex and Conditional ****************************/
100
101 typedef struct s_smx_mutex {
102
103   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
104   xbt_swag_t sleeping;          /* list of sleeping process */
105   int refcount;
106   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_mutex */
107
108 } s_smx_mutex_t;
109
110 typedef struct s_smx_cond {
111
112   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
113   xbt_swag_t sleeping;          /* list of sleeping process */
114   smx_mutex_t mutex;
115   xbt_fifo_t actions;           /* list of actions */
116   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_cond */
117
118 } s_smx_cond_t;
119
120 typedef struct s_smx_sem {
121   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_sem */
122   xbt_swag_t sleeping;          /* list of sleeping process */
123   int capacity;
124   xbt_fifo_t actions;           /* list of actions */
125   /* KEEP IT IN SYNC WITH src/xbt_sg_thread.c::struct s_xbt_sem */
126
127 } s_smx_sem_t;
128
129 /******************************* Networking ***********************************/
130
131 /** @brief Rendez-vous point datatype */
132 typedef struct s_smx_rvpoint {
133   char *name;
134   smx_mutex_t read;
135   smx_mutex_t write;
136   xbt_fifo_t comm_fifo;  
137   void *data;
138 } s_smx_rvpoint_t;
139
140 typedef struct s_smx_comm {
141
142
143   smx_comm_type_t type;   /* Type of the communication (comm_send,comm_recv) */
144   smx_rdv_t rdv;          /* Rendez-vous where the comm is queued */
145   smx_sem_t sem;        /* Semaphore associated to the surf simulation */
146   int refcount;           /* Number of processes involved in the cond */
147
148   /* Surf action data */
149   smx_process_t src_proc;
150   smx_process_t dst_proc;
151   smx_action_t src_timeout;
152   smx_action_t dst_timeout;
153   smx_action_t act;
154   double rate;
155   double task_size;
156
157   /* Data to be transfered */
158   void *src_buff;
159   void *dst_buff;
160   size_t src_buff_size;
161   size_t *dst_buff_size;
162   void *data;             /* User data associated to communication */
163 } s_smx_comm_t;
164
165 void SIMIX_network_copy_data(smx_comm_t comm);
166 smx_comm_t SIMIX_communication_new(smx_comm_type_t type);
167 static inline void SIMIX_communication_use(smx_comm_t comm);
168 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout);
169 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm);
170 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm);
171
172 /********************************* Action *************************************/
173
174 typedef enum {ready, ongoing, done, failed} smx_action_state_t;
175
176 /** @brief Action datatype
177     @ingroup m_datatypes_management_details */
178 typedef struct s_smx_action {
179   char *name;              /**< @brief action name if any */
180   xbt_fifo_t cond_list;    /*< conditional variables that must be signaled when the action finish. */
181   xbt_fifo_t sem_list;    /*< semaphores that must be signaled when the action finish. */
182   void *data;              /**< @brief user data */
183   int refcount;            /**< @brief reference counter */
184   surf_action_t surf_action;    /* SURF modeling of computation  */
185   smx_host_t source;
186 } s_smx_action_t;
187
188 /************************** Configuration support *****************************/
189
190 extern int _simix_init_status;  /* 0: beginning of time; FIXME: KILLME ?
191                                    1: pre-inited (cfg_set created);
192                                    2: inited (running) */
193
194 #define SIMIX_CHECK_HOST()  xbt_assert0(surf_workstation_model->extension.workstation. \
195                                   get_state(SIMIX_host_self()->host)==SURF_RESOURCE_ON,\
196                                   "Host failed, you cannot call this function.")
197
198 smx_host_t __SIMIX_host_create(const char *name, void *workstation, void *data);
199 void __SIMIX_host_destroy(void *host);
200 void __SIMIX_cond_wait(smx_cond_t cond);
201 void __SIMIX_cond_display_actions(smx_cond_t cond);
202 void __SIMIX_action_display_conditions(smx_action_t action);
203
204 /******************************** Context *************************************/
205
206 void SIMIX_context_mod_init(void);
207
208 void SIMIX_context_mod_exit(void);
209
210 /* *********************** */
211 /* Context type definition */
212 /* *********************** */
213 /* the following function pointers types describe the interface that all context
214    concepts must implement */
215
216 /* each context type must contain this macro at its begining -- OOP in C :/ */
217 #define SMX_CTX_BASE_T \
218   s_xbt_swag_hookup_t hookup; \
219   xbt_main_func_t code; \
220   int argc; \
221   char **argv; \
222   void_f_pvoid_t cleanup_func; \
223   void *cleanup_arg; \
224
225 /* all other context types derive from this structure */
226 typedef struct s_smx_context {
227   SMX_CTX_BASE_T;
228 } s_smx_context_t;
229
230 /* *********************** */
231 /* factory type definition */
232 /* *********************** */
233
234 /* Each context implementation define its own context factory
235  * A context factory is responsable of the creation and manipulation of the 
236  * execution context of all the simulated processes (and maestro) using the
237  * selected implementation.
238  *
239  * For example, the context switch based on java thread use the
240  * java implementation of the context and the java factory to build and control
241  * the contexts depending on this implementation.
242
243  * The following function pointer types describe the interface that any context 
244  * factory should implement.
245  */
246
247 /* function used to create a new context */
248 typedef smx_context_t (*smx_pfn_context_factory_create_context_t) 
249                       (xbt_main_func_t, int, char**, void_f_pvoid_t, void*);
250
251 /* this function finalize the specified context factory */
252 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
253
254 /* function used to destroy the specified context */
255 typedef void (*smx_pfn_context_free_t) (smx_context_t);
256
257 /* function used to start the specified context */
258 typedef void (*smx_pfn_context_start_t) (smx_context_t);
259
260 /* function used to stop the current context */
261 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
262
263 /* function used to suspend the current context */
264 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
265
266 /* function used to resume the current context */
267 typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context, 
268                                           smx_context_t new_context);
269
270 /* interface of the context factories */
271 typedef struct s_smx_context_factory {
272   smx_pfn_context_factory_create_context_t create_context;
273   smx_pfn_context_factory_finalize_t finalize;
274   smx_pfn_context_free_t free;
275   smx_pfn_context_start_t start;
276   smx_pfn_context_stop_t stop;
277   smx_pfn_context_suspend_t suspend;
278   smx_pfn_context_resume_t resume;
279   const char *name;
280 } s_smx_context_factory_t;
281
282 /* Selects a context factory associated with the name specified by the parameter name.
283  * If successful the function returns 0. Otherwise the function returns the error code.
284  */
285 int SIMIX_context_select_factory(const char *name);
286
287 /* Initializes a context factory from the name specified by the parameter name.
288  * If the factory cannot be found, an exception is raised.
289  */
290 void SIMIX_context_init_factory_by_name(smx_context_factory_t * factory, const char *name);
291
292 /* All factories init */
293 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory);
294
295 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory);
296 void SIMIX_ctx_lua_factory_init(smx_context_factory_t * factory);
297 void SIMIX_ctx_lua_factory_set_state(void *state);
298
299
300 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
301
302 /* ****************************** */
303 /* context manipulation functions */
304 /* ****************************** */
305
306 /* Scenario for the end of a context:
307  *
308  * CASE 1: death after end of the main function
309  *   the context_wrapper, called internally by the context module, calls 
310  *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
311  *   cleanup_func if any (in context settings), add current process to trashbin
312  *   and yields back to maestro.
313  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
314  *   all the process and context data structures, and frees the memory 
315  *
316  * CASE 2: brutal death
317  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
318  *   schedules the process. Then the process is awaken in the middle of the
319  *   SIMIX_process_yield function, and at the end of it, it checks that
320  *   iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
321  */
322
323 /**
324  * \brief creates a new context for a user level process
325  * \param code a main function
326  * \param argc the number of arguments of the main function
327  * \param argv the vector of arguments of the main function
328  * \param cleanup_func the function to call when the context stops
329  * \param cleanup_arg the argument of the cleanup_func function
330  */
331 static inline smx_context_t SIMIX_context_new(xbt_main_func_t code, int argc,
332                                               char** argv,
333                                               void_f_pvoid_t cleanup_func,
334                                               void* cleanup_arg)
335 {
336   return (*(simix_global->context_factory->create_context))
337            (code, argc, argv, cleanup_func, cleanup_arg);
338 }
339
340 /**
341  * \brief destroy a context 
342  * \param context the context to destroy
343  * Argument must be stopped first -- runs in maestro context
344  */
345 static inline void SIMIX_context_free(smx_context_t context)
346 {
347   (*(simix_global->context_factory->free)) (context);
348 }
349
350 /**
351  * \brief prepares aa context to be run
352  * \param context the context to start
353  * It will however run effectively only when calling #SIMIX_process_schedule
354  */
355 static inline void SIMIX_context_start(smx_context_t context)
356 {
357   (*(simix_global->context_factory->start)) (context);
358 }
359
360 /**
361  * \brief stops the execution of a context
362  * \param context to stop
363  */
364 static inline void SIMIX_context_stop(smx_context_t context)
365 {
366   (*(simix_global->context_factory->stop)) (context);
367 }
368
369 /**
370  \brief resumes the execution of a context
371  \param old_context the actual context from which is resuming
372  \param new_context the context to resume
373  */
374 static inline void SIMIX_context_resume(smx_context_t old_context,
375                                         smx_context_t new_context)
376 {
377   (*(simix_global->context_factory->resume)) (old_context, new_context);
378 }
379
380 /**
381  \brief suspends a context and return the control back to the one which
382         scheduled it
383  \param context the context to be suspended (it must be the running one)
384  */
385 static inline void SIMIX_context_suspend(smx_context_t context)
386 {
387   (*(simix_global->context_factory->suspend)) (context);
388 }
389
390 #endif