Logo AND Algorithmique Numérique Distribuée

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