Logo AND Algorithmique Numérique Distribuée

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