Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix: do not hardcode the number of workers in the parmap, use the command line...
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - context switching with ucontextes from System V           */
2
3 /* Copyright (c) 2009, 2010. 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 "simix/private.h"
10 #include "xbt/parmap.h"
11
12
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 #  include <valgrind/valgrind.h>
15 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
16
17 #ifdef _XBT_WIN32
18 #include "win32_ucontext.h"
19 #include "win32_ucontext.c"
20 #else
21 #include "ucontext.h"
22 #endif
23
24 typedef char * raw_stack_t;
25 typedef void (*rawctx_entry_point_t)(void *);
26
27 typedef struct s_smx_ctx_raw {
28   s_smx_ctx_base_t super;       /* Fields of super implementation */
29   char *malloced_stack; /* malloced area containing the stack */
30   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
31   raw_stack_t old_stack_top; /* to whom I should return the control */
32 #ifdef HAVE_VALGRIND_VALGRIND_H
33   unsigned int valgrind_stack_id;       /* the valgrind stack id */
34 #endif
35 #ifdef TIME_BENCH
36   unsigned int thread;  /* Just for measuring purposes */
37 #endif
38 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
39
40 smx_ctx_raw_t maestro_raw_context;
41
42 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
43                                    rawctx_entry_point_t entry_point, void* arg);
44 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
45
46 #ifdef PROCESSOR_i686
47 __asm__ (
48    ".text\n"
49    ".globl raw_makecontext\n"
50    ".type raw_makecontext,@function\n"
51    "raw_makecontext:\n"
52    "   movl 4(%esp),%eax\n"   /* stack */
53    "   addl 8(%esp),%eax\n"   /* size  */
54    "   movl 12(%esp),%ecx\n"  /* func  */
55    "   movl 16(%esp),%edx\n"  /* arg   */
56    "   movl %edx, -4(%eax)\n"
57    "   movl $0,   -8(%eax)\n" /* @return for func */
58    "   movl %ecx,-12(%eax)\n"
59    "   movl $0,  -16(%eax)\n" /* ebp */
60    "   movl $0,  -20(%eax)\n" /* ebx */
61    "   movl $0,  -24(%eax)\n" /* esi */
62    "   movl $0,  -28(%eax)\n" /* edi */
63    "   subl $28,%eax\n"
64    "   retl\n"
65 );
66
67 __asm__ (
68    ".text\n"
69    ".globl raw_swapcontext\n"
70    ".type raw_swapcontext,@function\n"
71    "raw_swapcontext:\n"
72    "   movl 4(%esp),%eax\n" /* old */
73    "   movl 8(%esp),%edx\n" /* new */
74    "   pushl %ebp\n"
75    "   pushl %ebx\n"
76    "   pushl %esi\n"
77    "   pushl %edi\n"
78    "   movl %esp,(%eax)\n"
79    "   movl %edx,%esp\n"
80    "   popl %edi\n"
81    "   popl %esi\n"
82    "   popl %ebx\n"
83    "   popl %ebp\n"
84    "   retl\n"
85 );
86 #elif PROCESSOR_x86_64
87 __asm__ (
88    ".text\n"
89    ".globl raw_makecontext\n"
90    ".type raw_makecontext,@function\n"
91    "raw_makecontext:\n" /* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
92    "   movq %rdi,%rax\n"      /* stack */
93    "   addq %rsi,%rax\n"      /* size  */
94    "   movq $0,   -8(%rax)\n" /* @return for func */
95    "   movq %rdx,-16(%rax)\n" /* func */
96    "   movq %rcx,-24(%rax)\n" /* arg/rdi */
97    "   movq $0,  -32(%rax)\n" /* rsi */
98    "   movq $0,  -40(%rax)\n" /* rdx */
99    "   movq $0,  -48(%rax)\n" /* rcx */
100    "   movq $0,  -56(%rax)\n" /* r8  */
101    "   movq $0,  -64(%rax)\n" /* r9  */
102    "   movq $0,  -72(%rax)\n" /* rbp */
103    "   movq $0,  -80(%rax)\n" /* rbx */
104    "   movq $0,  -88(%rax)\n" /* r12 */
105    "   movq $0,  -96(%rax)\n" /* r13 */
106    "   movq $0, -104(%rax)\n" /* r14 */
107    "   movq $0, -112(%rax)\n" /* r15 */
108    "   subq $112,%rax\n"
109    "   retq\n"
110 );
111
112 __asm__ (
113    ".text\n"
114    ".globl raw_swapcontext\n"
115    ".type raw_swapcontext,@function\n"
116    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
117    "   pushq %rdi\n"
118    "   pushq %rsi\n"
119    "   pushq %rdx\n"
120    "   pushq %rcx\n"
121    "   pushq %r8\n"
122    "   pushq %r9\n"
123    "   pushq %rbp\n"
124    "   pushq %rbx\n"
125    "   pushq %r12\n"
126    "   pushq %r13\n"
127    "   pushq %r14\n"
128    "   pushq %r15\n"
129    "   movq %rsp,(%rdi)\n" /* old */
130    "   movq %rsi,%rsp\n" /* new */
131    "   popq %r15\n"
132    "   popq %r14\n"
133    "   popq %r13\n"
134    "   popq %r12\n"
135    "   popq %rbx\n"
136    "   popq %rbp\n"
137    "   popq %r9\n"
138    "   popq %r8\n"
139    "   popq %rcx\n"
140    "   popq %rdx\n"
141    "   popq %rsi\n"
142    "   popq %rdi\n"
143    "   retq\n"
144 );
145 #else
146 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
147                             rawctx_entry_point_t entry_point, void* arg) {
148    THROW_UNIMPLEMENTED;
149 }
150
151 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
152    THROW_UNIMPLEMENTED;
153 }
154
155 #endif
156
157 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
158
159 #ifdef CONTEXT_THREADS
160 static xbt_parmap_t parmap;
161 #endif
162
163 static smx_context_factory_t raw_factory;
164
165 #ifdef TIME_BENCH
166 #include "xbt/xbt_os_time.h"
167 #define NUM_THREADS 4
168 static xbt_os_timer_t timer;
169 static double time_thread_sr[NUM_THREADS];
170 static double time_thread_ssr[NUM_THREADS];
171 static double time_wasted_sr = 0;
172 static double time_wasted_ssr = 0;
173 static unsigned int sr_count = 0;
174 static unsigned int ssr_count = 0;
175 static char new_sr = 0;
176 #endif
177
178 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
179
180 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
181 {
182 #ifdef TIME_BENCH
183   XBT_CRITICAL("Total wasted time in %u SR: %lf", sr_count, time_wasted_sr);
184   XBT_CRITICAL("Total wasted time in %u SSR: %lf", ssr_count, time_wasted_ssr);
185 #endif
186
187 #ifdef CONTEXT_THREADS
188   if(parmap)
189       xbt_parmap_destroy(parmap);
190 #endif
191   return smx_ctx_base_factory_finalize(factory);
192 }
193
194
195 static smx_context_t
196 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
197     void_pfn_smxprocess_t cleanup_func,
198     void *data)
199 {
200
201   smx_ctx_raw_t context =
202       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
203           sizeof(s_smx_ctx_raw_t),
204           code,
205           argc,
206           argv,
207           cleanup_func,
208           data);
209
210   /* If the user provided a function for the process then use it
211      otherwise is the context for maestro */
212      if (code) {
213        context->malloced_stack = xbt_malloc0(smx_context_stack_size);
214        context->stack_top =
215            raw_makecontext(context->malloced_stack, smx_context_stack_size,
216                (void(*)(void*))smx_ctx_raw_wrapper,context);
217
218 #ifdef HAVE_VALGRIND_VALGRIND_H
219        context->valgrind_stack_id =
220            VALGRIND_STACK_REGISTER(context->malloced_stack,
221                context->malloced_stack + smx_context_stack_size);
222 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
223
224      }else{
225        maestro_raw_context = context;
226      }
227
228      return (smx_context_t) context;
229 }
230
231 static void smx_ctx_raw_free(smx_context_t context)
232 {
233
234   if (context) {
235
236 #ifdef HAVE_VALGRIND_VALGRIND_H
237     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
238         context)->valgrind_stack_id);
239 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
240
241     free(((smx_ctx_raw_t)context)->malloced_stack);
242   }
243   smx_ctx_base_free(context);
244 }
245
246 static void smx_ctx_raw_suspend(smx_context_t context)
247 {
248   smx_current_context = (smx_context_t)maestro_raw_context;
249   raw_swapcontext(
250       &((smx_ctx_raw_t) context)->stack_top,
251       ((smx_ctx_raw_t) context)->old_stack_top);
252 }
253
254 static void smx_ctx_raw_stop(smx_context_t context)
255 {
256   smx_ctx_base_stop(context);
257   smx_ctx_raw_suspend(context);
258 }
259
260 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
261
262   (context->super.code) (context->super.argc, context->super.argv);
263
264   smx_ctx_raw_stop((smx_context_t) context);
265 }
266
267 static void smx_ctx_raw_resume(smx_process_t process)
268 {
269   smx_ctx_raw_t context = (smx_ctx_raw_t)process->context;
270   smx_current_context = (smx_context_t)context;
271   raw_swapcontext(
272       &((smx_ctx_raw_t) context)->old_stack_top,
273       ((smx_ctx_raw_t) context)->stack_top);
274 }
275
276 #ifdef TIME_BENCH
277 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
278 {
279   smx_process_t process;
280   unsigned int cursor;
281
282   double elapsed = 0;
283   double tmax = 0;
284   unsigned long num_proc = xbt_dynar_length(processes);
285   unsigned int t=0;
286   unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
287
288   ssr_count++;
289   time_thread_ssr[0] = 0;
290   xbt_dynar_foreach(processes, cursor, process) {
291     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
292     if(cursor >= t * data_size + data_size){
293       if(time_thread_ssr[t] > tmax)
294         tmax = time_thread_ssr[t];
295       t++;
296       time_thread_ssr[t] = 0;
297     }
298
299     if(new_sr){
300       ((smx_ctx_raw_t)process->context)->thread = t;
301       time_thread_sr[t] = 0;
302     }
303
304     xbt_os_timer_start(timer);
305     smx_ctx_raw_resume(process);
306     xbt_os_timer_stop(timer);
307     elapsed = xbt_os_timer_elapsed(timer);
308     time_thread_ssr[t] += elapsed;
309     time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
310   }
311
312   if(new_sr)
313     new_sr = FALSE;
314
315   if(time_thread_ssr[t] > tmax)
316     tmax = time_thread_ssr[t];
317
318   for(cursor=0; cursor <= t; cursor++){
319     XBT_VERB("Time SSR thread %u = %lf (max %lf)", cursor, time_thread_ssr[cursor], tmax);
320     time_wasted_ssr += tmax - time_thread_ssr[cursor];
321   }
322
323   xbt_dynar_reset(processes);
324 }
325
326 void smx_ctx_raw_new_sr(void);
327 void smx_ctx_raw_new_sr(void)
328 {
329   int i;
330   double tmax = 0;
331   new_sr = TRUE;
332   sr_count++;
333   for(i=0; i < NUM_THREADS; i++){
334     if(time_thread_sr[i] > tmax)
335       tmax = time_thread_sr[i];
336   }
337
338   for(i=0; i < NUM_THREADS; i++){
339     XBT_VERB("Time SR thread %u = %lf (max %lf)", i, time_thread_sr[i], tmax);
340     time_wasted_sr += tmax - time_thread_sr[i];
341   }
342
343   XBT_VERB("New scheduling round");
344 }
345 #else
346 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
347 {
348   smx_process_t process;
349   unsigned int cursor;
350
351   xbt_dynar_foreach(processes, cursor, process) {
352     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
353     smx_ctx_raw_resume(process);
354   }
355   xbt_dynar_reset(processes);
356 }
357 #endif
358
359 static void smx_ctx_raw_runall_parallel(xbt_dynar_t processes)
360 {
361 #ifdef CONTEXT_THREADS
362   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_raw_resume, processes);
363 #endif
364   xbt_dynar_reset(processes);
365 }
366
367 static smx_context_t smx_ctx_raw_self_parallel(void)
368 {
369   return smx_current_context;
370 }
371
372 static int smx_ctx_raw_get_thread_id(){
373   return (int)(unsigned long)xbt_os_thread_get_extra_data();
374 }
375
376 static void smx_ctx_raw_runall(xbt_dynar_t processes)
377 {
378   if (xbt_dynar_length(processes) >= SIMIX_context_get_parallel_threshold()) {
379     XBT_DEBUG("Runall // %lu", xbt_dynar_length(processes));
380     raw_factory->self = smx_ctx_raw_self_parallel;
381     raw_factory->get_thread_id = smx_ctx_raw_get_thread_id;
382     smx_ctx_raw_runall_parallel(processes);
383   } else {
384     XBT_DEBUG("Runall serial %lu", xbt_dynar_length(processes));
385     raw_factory->self = smx_ctx_base_self;
386     raw_factory->get_thread_id = smx_ctx_base_get_thread_id;
387     smx_ctx_raw_runall_serial(processes);
388   }
389 }
390
391 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
392 {
393   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
394   smx_ctx_base_factory_init(factory);
395
396   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
397   (*factory)->create_context = smx_ctx_raw_create_context;
398   /* Do not overload that method (*factory)->finalize */
399   (*factory)->free = smx_ctx_raw_free;
400   (*factory)->stop = smx_ctx_raw_stop;
401   (*factory)->suspend = smx_ctx_raw_suspend;
402   (*factory)->name = "smx_raw_context_factory";
403
404   if (SIMIX_context_is_parallel()) {
405 #ifdef CONTEXT_THREADS
406     parmap = xbt_parmap_new(SIMIX_context_get_nthreads());
407 #endif
408     if (SIMIX_context_get_parallel_threshold() > 1) {
409       /* choose dynamically */
410       (*factory)->runall = smx_ctx_raw_runall;
411     }
412     else {
413       /* always parallel */
414       (*factory)->self = smx_ctx_raw_self_parallel;
415       (*factory)->get_thread_id = smx_ctx_raw_get_thread_id;
416       (*factory)->runall = smx_ctx_raw_runall_parallel;
417     }
418   }
419   else {
420     /* always serial */
421     (*factory)->self = smx_ctx_base_self;
422     (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
423     (*factory)->runall = smx_ctx_raw_runall_serial;
424   }
425   raw_factory = *factory;
426 #ifdef TIME_BENCH
427   timer = xbt_os_timer_new();
428 #endif
429 }