Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill dead code
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* context_sysv - context switching with ucontexts from System V           */
2
3 /* Copyright (c) 2009-2014. The SimGrid Team.
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 #include <stdarg.h>
10
11 #include "xbt/parmap.h"
12 #include "smx_private.h"
13 #include "internal_config.h"
14 #include "context_sysv_config.h"        /* loads context system definitions */
15 #include "mc/mc.h"
16
17 #ifdef _XBT_WIN32
18 #  include <xbt/win32_ucontext.h>     /* context relative declarations */
19 #else
20 #  include <ucontext.h>           /* context relative declarations */
21 #endif
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24
25 typedef struct s_smx_ctx_sysv {
26   s_smx_ctx_base_t super;       /* Fields of super implementation */
27   ucontext_t uc;                /* the ucontext that executes the code */
28   char *stack;                  /* the thread stack */
29 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
30
31 #ifdef CONTEXT_THREADS
32 static xbt_parmap_t sysv_parmap;
33 static smx_ctx_sysv_t* sysv_workers_context;   /* space to save the worker's context in each thread */
34 static unsigned long sysv_threads_working;     /* number of threads that have started their work */
35 static xbt_os_thread_key_t sysv_worker_id_key; /* thread-specific storage for the thread id */
36 #endif
37 static unsigned long sysv_process_index = 0;   /* index of the next process to run in the
38                                                 * list of runnable processes */
39 static smx_ctx_sysv_t sysv_maestro_context;
40
41 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
42 static void smx_ctx_sysv_free(smx_context_t context);
43 static smx_context_t
44 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
45     void_pfn_smxprocess_t cleanup_func, smx_process_t process);
46
47 static void smx_ctx_sysv_wrapper(int count, ...);
48
49 static void smx_ctx_sysv_stop_serial(smx_context_t context);
50 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
51 static void smx_ctx_sysv_resume_serial(smx_process_t first_process);
52 static void smx_ctx_sysv_runall_serial(void);
53
54 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
55 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
56 static void smx_ctx_sysv_resume_parallel(smx_process_t first_process);
57 static void smx_ctx_sysv_runall_parallel(void);
58
59 /* This is a bit paranoid about sizeof(smx_ctx_sysv_t) not being a multiple of
60  * sizeof(int), but it doesn't harm. */
61 #define CTX_ADDR_LEN                            \
62   (sizeof(smx_ctx_sysv_t) / sizeof(int) +       \
63    !!(sizeof(smx_ctx_sysv_t) % sizeof(int)))
64
65 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
66 {
67   smx_ctx_base_factory_init(factory);
68   XBT_VERB("Activating SYSV context factory");
69
70   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
71   (*factory)->create_context = smx_ctx_sysv_create_context;
72   /* Do not overload that method (*factory)->finalize */
73   (*factory)->free = smx_ctx_sysv_free;
74   (*factory)->name = "smx_sysv_context_factory";
75
76   if (SIMIX_context_is_parallel()) {
77 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
78     int nthreads = SIMIX_context_get_nthreads();
79     sysv_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
80     sysv_workers_context = xbt_new(smx_ctx_sysv_t, nthreads);
81     sysv_maestro_context = NULL;
82     xbt_os_thread_key_create(&sysv_worker_id_key);
83     (*factory)->stop = smx_ctx_sysv_stop_parallel;
84     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
85     (*factory)->runall = smx_ctx_sysv_runall_parallel;
86 #else
87     THROWF(arg_error, 0, "No thread support for parallel context execution");
88 #endif
89   } else {
90     (*factory)->stop = smx_ctx_sysv_stop_serial;
91     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
92     (*factory)->runall = smx_ctx_sysv_runall_serial;
93   }    
94 }
95
96 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
97
98 #ifdef CONTEXT_THREADS
99   if (sysv_parmap)
100     xbt_parmap_destroy(sysv_parmap);
101   xbt_free(sysv_workers_context);
102 #endif
103   return smx_ctx_base_factory_finalize(factory);
104 }
105
106 static smx_context_t
107 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
108                             void_pfn_smxprocess_t cleanup_func,
109                             smx_process_t process)
110 {
111   int ctx_addr[CTX_ADDR_LEN];
112   smx_ctx_sysv_t context =
113     (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(
114       sizeof(s_smx_ctx_sysv_t),
115       code,
116       argc,
117       argv,
118       cleanup_func,
119       process);
120
121   /* if the user provided a function for the process then use it,
122      otherwise it is the context for maestro */
123   if (code) {
124
125     context->stack = SIMIX_context_stack_new();
126     getcontext(&(context->uc));
127
128     context->uc.uc_link = NULL;
129
130     context->uc.uc_stack.ss_sp =
131         pth_skaddr_makecontext(context->stack, smx_context_usable_stack_size);
132
133     context->uc.uc_stack.ss_size =
134         pth_sksize_makecontext(context->stack, smx_context_usable_stack_size);
135
136     memcpy(ctx_addr, &context, sizeof(smx_ctx_sysv_t));
137     switch (CTX_ADDR_LEN) {
138     case 1:
139       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
140                   1, ctx_addr[0]);
141       break;
142     case 2:
143       makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
144                   2, ctx_addr[0], ctx_addr[1]);
145       break;
146     default:
147       xbt_die("Ucontexts are not supported on this arch yet (addr len = %zu/%zu = %zu)",
148               sizeof(smx_ctx_sysv_t), sizeof(int), CTX_ADDR_LEN);
149     }
150   } else {
151     if (process != NULL && sysv_maestro_context == NULL)
152       sysv_maestro_context = context;
153   }
154
155   if (MC_is_active() && code) {
156     MC_new_stack_area(context->stack, ((smx_context_t)context)->process,
157                       &(context->uc), smx_context_usable_stack_size);
158   }
159
160   return (smx_context_t) context;
161 }
162
163 static void smx_ctx_sysv_free(smx_context_t context)
164 {
165
166   if (context) {
167     SIMIX_context_stack_delete(((smx_ctx_sysv_t)context)->stack);
168   }
169   smx_ctx_base_free(context);
170 }
171
172 static void smx_ctx_sysv_wrapper(int first, ...)
173
174   int ctx_addr[CTX_ADDR_LEN];
175   smx_ctx_sysv_t context;
176
177   ctx_addr[0] = first;
178   if (CTX_ADDR_LEN > 1) {
179     va_list ap;
180     int i;
181     va_start(ap, first);
182     for (i = 1; i < CTX_ADDR_LEN; i++)
183       ctx_addr[i] = va_arg(ap, int);
184     va_end(ap);
185   }
186   memcpy(&context, ctx_addr, sizeof(smx_ctx_sysv_t));
187   (context->super.code) (context->super.argc, context->super.argv);
188
189   simix_global->context_factory->stop((smx_context_t) context);
190 }
191
192 static void smx_ctx_sysv_stop_serial(smx_context_t context)
193 {
194   smx_ctx_base_stop(context);
195   smx_ctx_sysv_suspend_serial(context);
196 }
197
198 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
199 {
200   /* determine the next context */
201   smx_context_t next_context;
202   unsigned long int i = sysv_process_index++;
203
204   if (i < xbt_dynar_length(simix_global->process_to_run)) {
205     /* execute the next process */
206     XBT_DEBUG("Run next process");
207     next_context = xbt_dynar_get_as(
208         simix_global->process_to_run,i, smx_process_t)->context;
209   }
210   else {
211     /* all processes were run, return to maestro */
212     XBT_DEBUG("No more process to run");
213     next_context = (smx_context_t) sysv_maestro_context;
214   }
215   SIMIX_context_set_current(next_context);
216   swapcontext(&((smx_ctx_sysv_t) context)->uc,
217       &((smx_ctx_sysv_t) next_context)->uc);
218 }
219
220 static void smx_ctx_sysv_resume_serial(smx_process_t first_process)
221 {
222   smx_context_t context = first_process->context;
223   SIMIX_context_set_current(context);
224   swapcontext(&sysv_maestro_context->uc,
225       &((smx_ctx_sysv_t) context)->uc);
226 }
227
228 static void smx_ctx_sysv_runall_serial(void)
229 {
230   smx_process_t first_process =
231       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
232   sysv_process_index = 1;
233
234   /* execute the first process */
235   smx_ctx_sysv_resume_serial(first_process);
236 }
237
238 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
239 {
240   smx_ctx_base_stop(context);
241   smx_ctx_sysv_suspend_parallel(context);
242 }
243
244 /* This function is called by maestro at the beginning of a scheduling round to get all working threads executing some stuff
245  * It is much easier to understand what happens if you see the working threads as bodies that swap their soul for the
246  *    ones of the simulated processes that must run.
247  */
248 static void smx_ctx_sysv_runall_parallel(void) {
249 #ifdef CONTEXT_THREADS
250   sysv_threads_working = 0;
251   // parmap_apply ensures that every working thread get an index in the process_to_run array (through an atomic fetch_and_add),
252   //  and runs the smx_ctx_sysv_resume_parallel function on that index
253   xbt_parmap_apply(sysv_parmap, (void_f_pvoid_t) smx_ctx_sysv_resume_parallel,
254       simix_global->process_to_run);
255 #else
256   xbt_die("You asked for a parallel execution, but you don't have any threads.");
257 #endif
258 }
259
260 /* This function is in charge of running one particular simulated process on the current thread */
261 static void smx_ctx_sysv_resume_parallel(smx_process_t simulated_process_to_run) {
262 #ifdef CONTEXT_THREADS
263   unsigned long worker_id = __sync_fetch_and_add(&sysv_threads_working, 1); // what is my containing body?
264   xbt_os_thread_set_specific(sysv_worker_id_key, (void*) worker_id);        // Store the number of my containing body in os-thread-specific area
265   smx_ctx_sysv_t worker_context = (smx_ctx_sysv_t)SIMIX_context_self();     // get my current soul
266   sysv_workers_context[worker_id] = worker_context;                         // write down that this soul is hosted in that body (for now)
267   ucontext_t* worker_stack = &worker_context->uc;                           // retrieves the system-level info that fuels this soul
268
269   smx_context_t context = simulated_process_to_run->context;                // That's the first soul that I should become
270   SIMIX_context_set_current(context);                                       // write in simix that I switched my soul
271   swapcontext(worker_stack, &((smx_ctx_sysv_t) context)->uc);               // actually do that using the relevant syscall
272   // No body runs that soul anymore at this point. Instead the current body took the soul of simulated process
273   // The simulated process wakes back after the call to "SIMIX_context_suspend(self->context);" within smx_process.c::SIMIX_process_yield()
274
275   // From now on, the simulated processes will change their soul with the next soul to execute (in suspend_parallel, below).
276   // When nobody is to be executed in this scheduling round, the last simulated process will take back the initial soul of the current working thread
277 #endif
278 }
279
280 /* This function is called when a simulated process wants to yield back to the maestro in a blocking simcall.
281  *    This naturally occurs within SIMIX_context_suspend(self->context), called from SIMIX_process_yield()
282  * Actually, it does not really yield back to maestro, but into the next process that must be executed.
283  * If no one is to be executed, then it yields to the initial soul that was in this working thread (that was saved in resume_parallel).
284  */
285 static void smx_ctx_sysv_suspend_parallel(smx_context_t context) {
286 #ifdef CONTEXT_THREADS
287   /* determine the next context */
288   smx_process_t next_work = xbt_parmap_next(sysv_parmap);  // get the next soul to embody now
289   smx_context_t next_context;
290   ucontext_t* next_stack;                                  // will contain the next soul to run, either simulated or initial minion's one
291
292   if (next_work != NULL) {                                 // there is a next soul to embody (ie, a next process to resume)
293     XBT_DEBUG("Run next process");
294     next_context = next_work->context;
295   }
296   else {
297     /* all processes were run, go to the barrier */
298     XBT_DEBUG("No more processes to run");
299     unsigned long worker_id =                             // Get back the identity of my body that was stored when starting the scheduling round
300         (unsigned long) xbt_os_thread_get_specific(sysv_worker_id_key);
301     next_context = (smx_context_t)sysv_workers_context[worker_id];  // deduce the initial soul of that body
302                                                                     // When given that soul, the body will wait for the next scheduling round
303   }
304
305   next_stack = &((smx_ctx_sysv_t)next_context)->uc;
306
307   SIMIX_context_set_current(next_context);
308   swapcontext(&((smx_ctx_sysv_t) context)->uc, next_stack);  // get that next soul
309 #endif
310 }