Logo AND Algorithmique Numérique Distribuée

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