Logo AND Algorithmique Numérique Distribuée

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