Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fcf7fd10a6d68f872397d6767908b7d4d801fc1a
[simgrid.git] / src / simix / smx_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 "simgrid/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/mallocator.h"
16 #include "xbt/config.h"
17 #include "xbt/xbt_os_time.h"
18 #include "xbt/function_types.h"
19 #include "xbt/ex_interface.h"
20 #include "instr/instr_private.h"
21 #include "smx_process_private.h"
22 #include "smx_host_private.h"
23 #include "smx_io_private.h"
24 #include "smx_network_private.h"
25 #include "smx_smurf_private.h"
26 #include "smx_synchro_private.h"
27
28 #ifdef _XBT_WIN32
29 #  include <win32_ucontext.h>     /* context relative declarations */
30 #else
31 #  include <ucontext.h>           /* context relative declarations */
32 #endif
33
34 /* Define only for SimGrid benchmarking purposes */
35 //#define TIME_BENCH_PER_SR /* this aims at measuring the time spent in each scheduling round per each thread. The code is thus run in sequential to bench separately each SSR */
36 //#define TIME_BENCH_AMDAHL /* this aims at measuring the porting of time that could be parallelized at maximum (to get the optimal speedup by applying the amdahl law). */
37
38 /********************************** Simix Global ******************************/
39 typedef struct s_smx_global {
40   smx_context_factory_t context_factory;
41   xbt_dynar_t process_to_run;
42   xbt_dynar_t process_that_ran;
43   xbt_swag_t process_list;
44   xbt_swag_t process_to_destroy;
45   smx_process_t maestro_process;
46   xbt_dict_t registered_functions;
47   smx_creation_func_t create_process_function;
48   void_pfn_smxprocess_t_smxprocess_t kill_process_function;
49   void_pfn_smxprocess_t cleanup_process_function;
50   xbt_mallocator_t action_mallocator;
51   void_pfn_smxhost_t autorestart;
52
53 #ifdef TIME_BENCH_AMDAHL
54   xbt_os_timer_t timer_seq; /* used to bench the sequential and parallel parts of the simulation, if requested to */
55   xbt_os_timer_t timer_par;
56 #endif
57 } s_smx_global_t, *smx_global_t;
58
59 extern smx_global_t simix_global;
60 extern unsigned long simix_process_maxpid;
61
62 extern xbt_dict_t watched_hosts_lib;
63
64 /******************************** Exceptions *********************************/
65
66 #define SMX_EXCEPTION(issuer, c, v, m)                                  \
67   if (1) {                                                              \
68     smx_process_t _smx_throw_issuer = (issuer);                         \
69     THROW_PREPARE(_smx_throw_issuer->running_ctx, (c), (v), xbt_strdup(m)); \
70     _smx_throw_issuer->doexception = 1;                                 \
71   } else ((void)0)
72
73 #define SMX_THROW() RETHROW
74
75 /* ******************************** File ************************************ */
76 typedef struct s_smx_file {
77   surf_file_t surf_file;
78 } s_smx_file_t;
79
80 typedef struct s_smx_stat {
81   s_file_stat_t surf_stat;
82 } s_smx_stat_t;
83
84 /*********************************** Time ************************************/
85
86 /** @brief Timer datatype */
87 typedef struct s_smx_timer {
88   double date;
89   void* func;
90   void* args;
91 } s_smx_timer_t;
92
93 /********************************* Action *************************************/
94
95 typedef enum {
96   SIMIX_ACTION_EXECUTE,
97   SIMIX_ACTION_PARALLEL_EXECUTE,
98   SIMIX_ACTION_COMMUNICATE,
99   SIMIX_ACTION_SLEEP,
100   SIMIX_ACTION_SYNCHRO,
101   SIMIX_ACTION_IO
102 } e_smx_action_type_t;
103
104 typedef enum {
105   SIMIX_COMM_SEND,
106   SIMIX_COMM_RECEIVE,
107   SIMIX_COMM_READY,
108   SIMIX_COMM_DONE
109 } e_smx_comm_type_t;
110
111 typedef enum {
112   SIMIX_IO_OPEN,
113   SIMIX_IO_WRITE,
114   SIMIX_IO_READ,
115   SIMIX_IO_STAT
116 } e_smx_io_type_t;
117
118 /** @brief Action datatype */
119 typedef struct s_smx_action {
120
121   e_smx_action_type_t type;          /* Type of SIMIX action*/
122   e_smx_state_t state;               /* State of the action */
123   char *name;                        /* Action name if any */
124   xbt_fifo_t simcalls;               /* List of simcalls waiting for this action */
125
126   /* Data specific to each action type */
127   union {
128
129     struct {
130       smx_host_t host;                /* The host where the execution takes place */
131       surf_action_t surf_exec;        /* The Surf execution action encapsulated */
132     } execution; /* Possibly parallel execution */
133
134     struct {
135       e_smx_comm_type_t type;         /* Type of the communication (SIMIX_COMM_SEND or SIMIX_COMM_RECEIVE) */
136       smx_rdv_t rdv;                  /* Rendez-vous where the comm is queued */
137       int refcount;                   /* Number of processes involved in the cond */
138       int detached;                   /* If detached or not */
139
140       void (*clean_fun)(void*);       /* Function to clean the detached src_buf if something goes wrong */
141       int (*match_fun)(void*,void*,smx_action_t);  /* Filter function used by the other side. It is used when
142                                          looking if a given communication matches my needs. For that, myself must match the
143                                          expectations of the other side, too. See  */
144
145       /* Surf action data */
146       surf_action_t surf_comm;        /* The Surf communication action encapsulated */
147       surf_action_t src_timeout;      /* Surf's actions to instrument the timeouts */
148       surf_action_t dst_timeout;      /* Surf's actions to instrument the timeouts */
149       smx_process_t src_proc;
150       smx_process_t dst_proc;
151       double rate;
152       double task_size;
153
154       /* Data to be transfered */
155       void *src_buff;
156       void *dst_buff;
157       size_t src_buff_size;
158       size_t *dst_buff_size;
159       unsigned copied:1;              /* whether the data were already copied */
160
161       void* src_data;                 /* User data associated to communication */
162       void* dst_data;
163     } comm;    
164
165     struct {
166       smx_host_t host;                /* The host that is sleeping */
167       surf_action_t surf_sleep;       /* The Surf sleeping action encapsulated */
168     } sleep;
169
170     struct {
171       surf_action_t sleep;
172     } synchro;
173
174     struct {
175       smx_host_t host;
176       surf_action_t surf_io;
177     } io;
178   };
179
180 #ifdef HAVE_LATENCY_BOUND_TRACKING
181   int latency_limited;
182 #endif
183
184 #ifdef HAVE_TRACING
185   char *category;                     /* simix action category for instrumentation */
186 #endif
187 } s_smx_action_t;
188
189 /* FIXME: check if we can delete this function */
190 static XBT_INLINE e_smx_state_t SIMIX_action_map_state(e_surf_action_state_t state)
191 {
192   switch (state) {
193     case SURF_ACTION_READY:
194       return SIMIX_READY;
195     case SURF_ACTION_RUNNING:
196       return SIMIX_RUNNING;
197     case SURF_ACTION_FAILED:
198       return SIMIX_FAILED;
199     case SURF_ACTION_DONE:
200       return SIMIX_DONE;
201     default:
202       xbt_die("Unexpected SURF action state");
203   }
204 }
205
206 void SIMIX_context_mod_init(void);
207 void SIMIX_context_mod_exit(void);
208
209 void SIMIX_context_set_current(smx_context_t context);
210 smx_context_t SIMIX_context_get_current(void);
211
212 /* All factories init */
213
214 typedef struct s_smx_ctx_sysv {
215   s_smx_ctx_base_t super;       /* Fields of super implementation */
216   ucontext_t uc;                /* the ucontext that executes the code */
217 #ifdef HAVE_VALGRIND_VALGRIND_H
218   unsigned int valgrind_stack_id;       /* the valgrind stack id */
219 #endif
220   char stack[0];                /* the thread stack (must remain the last element of the structure) */
221 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
222
223 void SIMIX_ctx_thread_factory_init(smx_context_factory_t *factory);
224 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory);
225 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
226
227 /* ****************************** */
228 /* context manipulation functions */
229 /* ****************************** */
230
231 /* Scenario for the end of a context:
232  *
233  * CASE 1: death after end of the main function
234  *   the context_wrapper, called internally by the context module, calls 
235  *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
236  *   cleanup_func if any (in context settings), add current process to trashbin
237  *   and yields back to maestro.
238  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
239  *   all the process and context data structures, and frees the memory 
240  *
241  * CASE 2: brutal death
242  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
243  *   schedules the process. Then the process is awaken in the middle of the
244  *   SIMIX_process_yield function, and at the end of it, it checks that
245  *   iwannadie == 1, and call SIMIX_context_stop(same than first case afterward)
246  */
247
248 /**
249  * \brief creates a new context for a user level process
250  * \param code a main function
251  * \param argc the number of arguments of the main function
252  * \param argv the vector of arguments of the main function
253  * \param cleanup_func the function to call when the context stops
254  * \param cleanup_arg the argument of the cleanup_func function
255  */
256 static XBT_INLINE smx_context_t SIMIX_context_new(xbt_main_func_t code,
257                                                   int argc, char **argv,
258                                                   void_pfn_smxprocess_t cleanup_func,
259                                                   smx_process_t simix_process)
260 {
261
262   return simix_global->context_factory->create_context(code,
263                                                        argc, argv,
264                                                        cleanup_func,
265                                                        simix_process);
266 }
267
268 /**
269  * \brief destroy a context 
270  * \param context the context to destroy
271  * Argument must be stopped first -- runs in maestro context
272  */
273 static XBT_INLINE void SIMIX_context_free(smx_context_t context)
274 {
275   simix_global->context_factory->free(context);
276 }
277
278 /**
279  * \brief stops the execution of a context
280  * \param context to stop
281  */
282 static XBT_INLINE void SIMIX_context_stop(smx_context_t context)
283 {
284   simix_global->context_factory->stop(context);
285 }
286
287 /**
288  \brief suspends a context and return the control back to the one which
289         scheduled it
290  \param context the context to be suspended (it must be the running one)
291  */
292 static XBT_INLINE void SIMIX_context_suspend(smx_context_t context)
293 {
294   simix_global->context_factory->suspend(context);
295 }
296
297 /**
298  \brief Executes all the processes to run (in parallel if possible).
299  */
300 static XBT_INLINE void SIMIX_context_runall(void)
301 {
302   simix_global->context_factory->runall();
303 }
304
305 /**
306  \brief returns the current running context 
307  */
308 static XBT_INLINE smx_context_t SIMIX_context_self(void)
309 {
310   if (simix_global && simix_global->context_factory) {
311     return simix_global->context_factory->self();
312   }
313
314   return NULL;
315 }
316
317 /**
318  \brief returns the data associated to a context
319  \param context The context
320  \return The data
321  */
322 static XBT_INLINE void* SIMIX_context_get_data(smx_context_t context)
323 {
324   return simix_global->context_factory->get_data(context);
325 }
326
327 XBT_PUBLIC(int) SIMIX_process_get_maxpid(void);
328
329 void SIMIX_post_create_environment(void);
330
331 #endif