Logo AND Algorithmique Numérique Distribuée

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