Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First bricks for auto_restart support in SIMIX/MSG.
[simgrid.git] / include / simgrid / simix.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_SIMIX_H
8 #define _SIMIX_SIMIX_H
9
10 #include "xbt/misc.h"
11 #include "xbt/fifo.h"
12 #include "xbt/dict.h"
13 #include "xbt/file_stat.h"
14 #include "xbt/function_types.h"
15 #include "xbt/parmap.h"
16 #include "xbt/swag.h"
17
18 SG_BEGIN_DECL()
19
20
21 /* ******************************** Host ************************************ */
22 /** @defgroup m_datatypes_management_details Details on SIMIX datatypes */
23 /** @brief Host datatype
24     @ingroup m_datatypes_management
25
26     A <em>location</em> (or <em>host</em>) is any possible place where
27     a process may run. Thus it is represented as a <em>physical
28     resource with computing capabilities</em>, some <em>mailboxes</em>
29     to enable running process to communicate with remote ones, and
30     some <em>private data</em> that can be only accessed by local
31     process.
32
33     \see m_host_management
34   @{ */
35 typedef struct s_smx_host *smx_host_t;
36 typedef enum {
37   SIMIX_WAITING,
38   SIMIX_READY,
39   SIMIX_RUNNING,
40   SIMIX_DONE,
41   SIMIX_CANCELED,
42   SIMIX_FAILED,
43   SIMIX_SRC_HOST_FAILURE,
44   SIMIX_DST_HOST_FAILURE,
45   SIMIX_SRC_TIMEOUT,
46   SIMIX_DST_TIMEOUT,
47   SIMIX_LINK_FAILURE
48 } e_smx_state_t;
49 /** @} */
50
51
52 typedef struct s_smx_timer* smx_timer_t;
53
54 /* ******************************** Synchro ************************************ */
55 typedef struct s_smx_mutex *smx_mutex_t;
56 typedef struct s_smx_cond *smx_cond_t;
57 typedef struct s_smx_sem *smx_sem_t;
58
59 /********************************** File *************************************/
60 typedef struct s_smx_file *smx_file_t;
61 typedef struct s_smx_stat *smx_stat_t;
62
63 /********************************** Action *************************************/
64 typedef struct s_smx_action *smx_action_t; /* FIXME: replace by specialized action handlers */
65
66
67
68 /* ****************************** Process *********************************** */
69 /** @brief Process datatype
70     @ingroup m_datatypes_management
71
72     A processt may be defined as a <em>code</em>, with some <em>private
73     data</em>, executing in a <em>location</em>.
74     \see m_process_management
75   @{ */
76 typedef struct s_smx_process *smx_process_t;
77 /** @} */
78
79
80 /*
81  * Type of function that creates a process.
82  * The function must accept the following parameters:
83  * void* process: the process created will be stored there
84  * const char *name: a name for the object. It is for user-level information and can be NULL
85  * xbt_main_func_t code: is a function describing the behavior of the process
86  * void *data: data a pointer to any data one may want to attach to the new object.
87  * smx_host_t host: the location where the new process is executed
88  * int argc, char **argv: parameters passed to code
89  * xbt_dict_t pros: properties
90  */
91 typedef void (*smx_creation_func_t) ( /* process */ smx_process_t*,
92                                       /* name */ const char*,
93                                       /* code */ xbt_main_func_t,
94                                       /* userdata */ void*,
95                                       /* hostname */ const char*,
96                                       /* kill_time */ double,
97                                       /* argc */ int,
98                                       /* argv */ char**,
99                                       /* props */ xbt_dict_t,
100                                       /* auto_restart */ int);
101
102
103 /******************************* Networking ***********************************/
104 typedef struct s_smx_rvpoint *smx_rdv_t;
105
106 /******************************** Context *************************************/
107 typedef struct s_smx_context *smx_context_t;
108 typedef struct s_smx_context_factory *smx_context_factory_t;
109
110 /* Process creation/destruction callbacks */
111 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
112
113
114 /* The following function pointer types describe the interface that any context
115    factory should implement */
116
117
118 typedef smx_context_t(*smx_pfn_context_factory_create_context_t)
119   (xbt_main_func_t, int, char **, void_pfn_smxprocess_t, void* data);
120 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
121 typedef void (*smx_pfn_context_free_t) (smx_context_t);
122 typedef void (*smx_pfn_context_start_t) (smx_context_t);
123 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
124 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
125 typedef void (*smx_pfn_context_runall_t) (void);
126 typedef smx_context_t (*smx_pfn_context_self_t) (void);
127 typedef void* (*smx_pfn_context_get_data_t) (smx_context_t context);
128
129 /* interface of the context factories */
130 typedef struct s_smx_context_factory {
131   const char *name;
132   smx_pfn_context_factory_create_context_t create_context;
133   smx_pfn_context_factory_finalize_t finalize;
134   smx_pfn_context_free_t free;
135   smx_pfn_context_stop_t stop;
136   smx_pfn_context_suspend_t suspend;
137   smx_pfn_context_runall_t runall;
138   smx_pfn_context_self_t self;
139   smx_pfn_context_get_data_t get_data;
140 } s_smx_context_factory_t;
141
142 /* Hack: let msg load directly the right factory */
143 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
144 XBT_PUBLIC(smx_ctx_factory_initializer_t) smx_factory_initializer_to_use;
145 extern char* smx_context_factory_name;
146 extern int smx_context_stack_size;
147
148 #ifdef HAVE_THREAD_LOCAL_STORAGE
149 extern __thread smx_context_t smx_current_context;
150 #else
151 extern smx_context_t smx_current_context;
152 #endif
153
154 /* *********************** */
155 /* Context type definition */
156 /* *********************** */
157 /* the following function pointers types describe the interface that all context
158    concepts must implement */
159 /* each context type derive from this structure, so they must contain this structure
160  * at their beginning -- OOP in C :/ */
161 typedef struct s_smx_context {
162   s_xbt_swag_hookup_t hookup;
163   xbt_main_func_t code;
164   void_pfn_smxprocess_t cleanup_func;
165   void *data;   /* Here SIMIX stores the smx_process_t containing the context */
166   char **argv;
167   int argc;
168   unsigned iwannadie:1;
169 } s_smx_ctx_base_t;
170
171 /* methods of this class */
172 XBT_PUBLIC(void) smx_ctx_base_factory_init(smx_context_factory_t *factory);
173 XBT_PUBLIC(int) smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
174
175 XBT_PUBLIC(smx_context_t)
176 smx_ctx_base_factory_create_context_sized(size_t size,
177                                           xbt_main_func_t code, int argc,
178                                           char **argv,
179                                           void_pfn_smxprocess_t cleanup,
180                                           void* data);
181 XBT_PUBLIC(void) smx_ctx_base_free(smx_context_t context);
182 XBT_PUBLIC(void) smx_ctx_base_stop(smx_context_t context);
183 XBT_PUBLIC(smx_context_t) smx_ctx_base_self(void);
184 XBT_PUBLIC(void) *smx_ctx_base_get_data(smx_context_t context);
185
186 XBT_PUBLIC(xbt_dynar_t) SIMIX_process_get_runnable(void);
187 XBT_PUBLIC(smx_process_t) SIMIX_process_from_PID(int PID);
188 XBT_PUBLIC(xbt_dynar_t) SIMIX_processes_as_dynar(void);
189
190 /* parallelism */
191 XBT_PUBLIC(int) SIMIX_context_is_parallel(void);
192 XBT_PUBLIC(int) SIMIX_context_get_nthreads(void);
193 XBT_PUBLIC(void) SIMIX_context_set_nthreads(int nb_threads);
194 XBT_PUBLIC(int) SIMIX_context_get_parallel_threshold(void);
195 XBT_PUBLIC(void) SIMIX_context_set_parallel_threshold(int threshold);
196 XBT_PUBLIC(e_xbt_parmap_mode_t) SIMIX_context_get_parallel_mode(void);
197 XBT_PUBLIC(void) SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
198
199
200
201 /********************************** Global ************************************/
202 /* Initialization and exit */
203 XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv);
204 XBT_PUBLIC(void) SIMIX_clean(void);
205
206
207 XBT_PUBLIC(void) SIMIX_function_register_process_cleanup(void_pfn_smxprocess_t function);
208 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
209 XBT_PUBLIC(void) SIMIX_function_register_process_kill(void_pfn_smxprocess_t function);
210
211 /* Simulation execution */
212 XBT_PUBLIC(void) SIMIX_run(void);    
213 XBT_PUBLIC(double) SIMIX_get_clock(void);
214
215 /* Timer functions FIXME: should these be public? */
216 XBT_PUBLIC(void) SIMIX_timer_set(double date, void *function, void *arg);
217 XBT_PUBLIC(double) SIMIX_timer_next(void);
218
219 XBT_PUBLIC(void) SIMIX_display_process_status(void);
220
221 /******************************* Environment **********************************/
222 XBT_PUBLIC(void) SIMIX_create_environment(const char *file);
223
224 /******************************** Deployment **********************************/
225
226 XBT_PUBLIC(void) SIMIX_function_register(const char *name, xbt_main_func_t code);
227 XBT_PUBLIC(void) SIMIX_function_register_default(xbt_main_func_t code);
228 XBT_PUBLIC(xbt_main_func_t) SIMIX_get_registered_function(const char *name);
229 XBT_PUBLIC(void) SIMIX_launch_application(const char *file);
230 XBT_PUBLIC(void) SIMIX_process_set_function(const char* process_host,
231                                             const char *process_function,
232                                             xbt_dynar_t arguments,
233                                             double process_start_time,
234                                             double process_kill_time);
235
236 /*********************************** Host *************************************/
237 XBT_PUBLIC(xbt_dict_t) SIMIX_host_get_dict(void);
238 XBT_PUBLIC(smx_host_t) SIMIX_host_get_by_name(const char *name);
239 XBT_PUBLIC(smx_host_t) SIMIX_host_self(void);
240 XBT_PUBLIC(const char*) SIMIX_host_self_get_name(void);
241 XBT_PUBLIC(const char*) SIMIX_host_get_name(smx_host_t host); /* FIXME: make private: only the name of SIMIX_host_self() should be public without request */
242 XBT_PUBLIC(void) SIMIX_host_self_set_data(void *data);
243 XBT_PUBLIC(void*) SIMIX_host_self_get_data(void);
244 XBT_PUBLIC(void*) SIMIX_host_get_data(smx_host_t host);
245 XBT_PUBLIC(void) SIMIX_host_set_data(smx_host_t host, void *data);
246
247 /********************************* Process ************************************/
248 XBT_PUBLIC(int) SIMIX_process_count(void);
249 XBT_PUBLIC(smx_process_t) SIMIX_process_self(void);
250 XBT_PUBLIC(const char*) SIMIX_process_self_get_name(void);
251 XBT_PUBLIC(void) SIMIX_process_self_set_data(smx_process_t self, void *data);
252 XBT_PUBLIC(void*) SIMIX_process_self_get_data(smx_process_t self);
253 XBT_PUBLIC(smx_context_t) SIMIX_process_get_context(smx_process_t);
254 XBT_PUBLIC(void) SIMIX_process_set_context(smx_process_t p,smx_context_t c);
255 XBT_PUBLIC(int) SIMIX_process_has_pending_comms(smx_process_t process);
256 XBT_PUBLIC(void) SIMIX_process_on_exit_runall(smx_process_t process);
257 XBT_PUBLIC(void) SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data);
258
259 /****************************** Communication *********************************/
260 XBT_PUBLIC(void) SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, void*, size_t));
261 XBT_PUBLIC(void) SIMIX_comm_copy_pointer_callback(smx_action_t comm, void* buff, size_t buff_size);
262 XBT_PUBLIC(void) SIMIX_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size);
263 XBT_PUBLIC(void) smpi_comm_copy_data_callback(smx_action_t comm, void* buff, size_t buff_size);
264
265 XBT_PUBLIC(smx_action_t) SIMIX_comm_get_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
266 XBT_PUBLIC(int) SIMIX_comm_has_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
267 XBT_PUBLIC(int) SIMIX_comm_has_recv_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
268 XBT_PUBLIC(void) SIMIX_comm_finish(smx_action_t action);
269
270 /******************************************************************************/
271 /*                            SIMIX simcalls                                  */
272 /******************************************************************************/
273 /* These functions are a system call-like interface to the simulation kernel. */
274 /* They can also be called from maestro's context, and they are thread safe.  */
275 /******************************************************************************/
276
277 /******************************* Host simcalls ********************************/
278 /* TODO use handlers and keep smx_host_t hidden from higher levels */
279 XBT_PUBLIC(xbt_dict_t) simcall_host_get_dict(void);
280 XBT_PUBLIC(smx_host_t) simcall_host_get_by_name(const char *name);
281 XBT_PUBLIC(const char *) simcall_host_get_name(smx_host_t host);
282 XBT_PUBLIC(xbt_dict_t) simcall_host_get_properties(smx_host_t host);
283 XBT_PUBLIC(double) simcall_host_get_speed(smx_host_t host);
284 XBT_PUBLIC(double) simcall_host_get_available_speed(smx_host_t host);
285 /* Two possible states, 1 - CPU ON and 0 CPU OFF */
286 XBT_PUBLIC(int) simcall_host_get_state(smx_host_t host);
287 XBT_PUBLIC(void *) simcall_host_get_data(smx_host_t host);
288
289 XBT_PUBLIC(void) simcall_host_set_data(smx_host_t host, void *data);
290
291 XBT_PUBLIC(smx_action_t) simcall_host_execute(const char *name, smx_host_t host,
292                                                 double computation_amount,
293                                                 double priority);
294 XBT_PUBLIC(smx_action_t) simcall_host_parallel_execute(const char *name,
295                                                      int host_nb,
296                                                      smx_host_t *host_list,
297                                                      double *computation_amount,
298                                                      double *communication_amount,
299                                                      double amount,
300                                                      double rate);
301 XBT_PUBLIC(void) simcall_host_execution_destroy(smx_action_t execution);
302 XBT_PUBLIC(void) simcall_host_execution_cancel(smx_action_t execution);
303 XBT_PUBLIC(double) simcall_host_execution_get_remains(smx_action_t execution);
304 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_get_state(smx_action_t execution);
305 XBT_PUBLIC(void) simcall_host_execution_set_priority(smx_action_t execution, double priority);
306 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_wait(smx_action_t execution);
307
308
309 /**************************** Process simcalls ********************************/
310 /* Constructor and Destructor */
311 XBT_PUBLIC(void) simcall_process_create(smx_process_t *process,
312                                           const char *name,
313                                           xbt_main_func_t code,
314                                           void *data,
315                                           const char *hostname,
316                                           double kill_time,
317                                           int argc, char **argv,
318                                           xbt_dict_t properties,
319                                           int auto_restart);
320
321 XBT_PUBLIC(void) simcall_process_kill(smx_process_t process);
322 XBT_PUBLIC(void) simcall_process_killall(void);
323
324 /* Process handling */
325 XBT_PUBLIC(void) simcall_process_cleanup(smx_process_t process);
326 XBT_PUBLIC(void) simcall_process_change_host(smx_process_t process,
327                  smx_host_t dest);
328 XBT_PUBLIC(void) simcall_process_suspend(smx_process_t process);
329 XBT_PUBLIC(void) simcall_process_resume(smx_process_t process);
330
331 /* Getters and Setters */
332 XBT_PUBLIC(int) simcall_process_count(void);
333 XBT_PUBLIC(void *) simcall_process_get_data(smx_process_t process);
334 XBT_PUBLIC(void) simcall_process_set_data(smx_process_t process, void *data);
335 XBT_PUBLIC(smx_host_t) simcall_process_get_host(smx_process_t process);
336 XBT_PUBLIC(const char *) simcall_process_get_name(smx_process_t process);
337 XBT_PUBLIC(int) simcall_process_is_suspended(smx_process_t process);
338 XBT_PUBLIC(xbt_dict_t) simcall_process_get_properties(smx_process_t host);
339 XBT_PUBLIC(void) simcall_process_set_kill_time(smx_process_t process, double kill_time);
340 XBT_PUBLIC(void) simcall_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data);
341 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_process_t process, int auto_restart);
342 /* Sleep control */
343 XBT_PUBLIC(e_smx_state_t) simcall_process_sleep(double duration);
344
345 /************************** Comunication simcalls *****************************/
346 /***** Rendez-vous points *****/
347
348 XBT_PUBLIC(smx_rdv_t) simcall_rdv_create(const char *name);
349 XBT_PUBLIC(void) simcall_rdv_destroy(smx_rdv_t rvp);
350 XBT_PUBLIC(smx_rdv_t) simcall_rdv_get_by_name(const char *name);
351 XBT_PUBLIC(int) simcall_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host);
352 XBT_PUBLIC(smx_action_t) simcall_rdv_get_head(smx_rdv_t rdv);
353
354 XBT_PUBLIC(xbt_dict_t) SIMIX_get_rdv_points(void);
355
356 /***** Communication simcalls *****/
357
358 XBT_PUBLIC(void) simcall_comm_send(smx_rdv_t rdv, double task_size,
359                                      double rate, void *src_buff,
360                                      size_t src_buff_size,
361                                      int (*match_fun)(void *, void *, smx_action_t),
362                                      void *data, double timeout);
363
364 XBT_PUBLIC(smx_action_t) simcall_comm_isend(smx_rdv_t rdv, double task_size,
365                                               double rate, void *src_buff,
366                                               size_t src_buff_size,
367                                               int (*match_fun)(void *, void *, smx_action_t),
368                                               void (*clean_fun)(void *),
369                                               void *data, int detached);
370
371 XBT_PUBLIC(void) simcall_comm_recv(smx_rdv_t rdv, void *dst_buff,
372                                      size_t * dst_buff_size,
373                                      int (*match_fun)(void *, void *, smx_action_t),
374                                      void *data, double timeout);
375
376 XBT_PUBLIC(smx_action_t) simcall_comm_irecv(smx_rdv_t rdv, void *dst_buff,
377                                               size_t * dst_buff_size,
378                                               int (*match_fun)(void *, void *, smx_action_t),
379                                               void *data);
380
381 XBT_PUBLIC(void) simcall_comm_destroy(smx_action_t comm);
382
383 XBT_PUBLIC(void) simcall_comm_cancel(smx_action_t comm);
384
385 /* FIXME: waitany is going to be a vararg function, and should take a timeout */
386 XBT_PUBLIC(unsigned int) simcall_comm_waitany(xbt_dynar_t comms);
387 XBT_PUBLIC(void) simcall_comm_wait(smx_action_t comm, double timeout);
388 XBT_PUBLIC(int) simcall_comm_test(smx_action_t comm);
389 XBT_PUBLIC(int) simcall_comm_testany(xbt_dynar_t comms);
390
391 /* Getters and setters */
392 XBT_PUBLIC(double) simcall_comm_get_remains(smx_action_t comm);
393 XBT_PUBLIC(e_smx_state_t) simcall_comm_get_state(smx_action_t comm);
394 XBT_PUBLIC(void *) simcall_comm_get_src_data(smx_action_t comm);
395 XBT_PUBLIC(void *) simcall_comm_get_dst_data(smx_action_t comm);
396 XBT_PUBLIC(smx_process_t) simcall_comm_get_src_proc(smx_action_t comm);
397 XBT_PUBLIC(smx_process_t) simcall_comm_get_dst_proc(smx_action_t comm);
398
399 #ifdef HAVE_LATENCY_BOUND_TRACKING
400 XBT_PUBLIC(int) simcall_comm_is_latency_bounded(smx_action_t comm);
401 #endif
402
403 #ifdef HAVE_TRACING
404 /************************** Tracing handling **********************************/
405 XBT_PUBLIC(void) simcall_set_category(smx_action_t action, const char *category);
406 #endif
407
408 /************************** Synchro simcalls **********************************/
409
410 XBT_PUBLIC(smx_mutex_t) simcall_mutex_init(void);
411 XBT_PUBLIC(void) simcall_mutex_destroy(smx_mutex_t mutex);
412 XBT_PUBLIC(void) simcall_mutex_lock(smx_mutex_t mutex);
413 XBT_PUBLIC(int) simcall_mutex_trylock(smx_mutex_t mutex);
414 XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex);
415
416 XBT_PUBLIC(smx_cond_t) simcall_cond_init(void);
417 XBT_PUBLIC(void) simcall_cond_destroy(smx_cond_t cond);
418 XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond);
419 XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex);
420 XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond,
421                                          smx_mutex_t mutex,
422                                          double max_duration);
423 XBT_PUBLIC(void) simcall_cond_broadcast(smx_cond_t cond);
424
425 XBT_PUBLIC(smx_sem_t) simcall_sem_init(int capacity);
426 XBT_PUBLIC(void) simcall_sem_destroy(smx_sem_t sem);
427 XBT_PUBLIC(void) simcall_sem_release(smx_sem_t sem);
428 XBT_PUBLIC(void) simcall_sem_release_forever(smx_sem_t sem);
429 XBT_PUBLIC(int) simcall_sem_would_block(smx_sem_t sem);
430 XBT_PUBLIC(void) simcall_sem_block_onto(smx_sem_t sem);
431 XBT_PUBLIC(void) simcall_sem_acquire(smx_sem_t sem);
432 XBT_PUBLIC(void) simcall_sem_acquire_timeout(smx_sem_t sem,
433                                            double max_duration);
434 XBT_PUBLIC(unsigned int) simcall_sem_acquire_any(xbt_dynar_t sems);
435 XBT_PUBLIC(int) simcall_sem_get_capacity(smx_sem_t sem);
436
437 XBT_PUBLIC(double) simcall_file_read(void* ptr, size_t size, size_t nmemb, smx_file_t stream);
438 XBT_PUBLIC(size_t) simcall_file_write(const void* ptr, size_t size, size_t nmemb, smx_file_t stream);
439 XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode);
440 XBT_PUBLIC(int) simcall_file_close(smx_file_t fp);
441 XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf);
442
443 SG_END_DECL()
444 #endif                          /* _SIMIX_SIMIX_H */