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_smurf.c
1 #include "smx_private.h"
2 #include "xbt/fifo.h"
3 #include "xbt/xbt_os_thread.h"
4
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_smurf, simix,
6                                 "Logging specific to SIMIX (SMURF)");
7
8 XBT_INLINE smx_simcall_t SIMIX_simcall_mine()
9 {
10   smx_process_t issuer = SIMIX_process_self();
11   return &issuer->simcall;
12 }
13
14 /**
15  * \brief Makes the current process do a simcall to the kernel and yields
16  * until completion. If the current thread is maestro, we don't yield and
17  * execute the simcall directly.
18  * \param self the current process
19  */
20 void SIMIX_simcall_push(smx_process_t self)
21 {
22   if (self != simix_global->maestro_process) {
23     XBT_DEBUG("Yield process '%s' on simcall %s (%d)", self->name,
24               SIMIX_simcall_name(self->simcall.call), (int)self->simcall.call);
25     SIMIX_process_yield(self);
26   } else {
27     SIMIX_simcall_pre(&self->simcall, 0);
28   }
29 }
30
31 void SIMIX_simcall_answer(smx_simcall_t simcall)
32 {
33   if (simcall->issuer != simix_global->maestro_process){
34     XBT_DEBUG("Answer simcall %s (%d) issued by %s (%p)", SIMIX_simcall_name(simcall->call), (int)simcall->call,
35         simcall->issuer->name, simcall->issuer);
36     simcall->issuer->simcall.call = SIMCALL_NONE;
37 /*    This check should be useless and slows everyone. Reactivate if you see something
38  *    weird in process scheduling.
39  */
40 /*    if(!xbt_dynar_member(simix_global->process_to_run, &(simcall->issuer))) */
41     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, simcall->issuer);
42 /*    else DIE_IMPOSSIBLE; */
43   }
44 }
45
46 void SIMIX_simcall_pre(smx_simcall_t simcall, int value)
47 {
48   XBT_DEBUG("Handling simcall %p: %s", simcall, SIMIX_simcall_name(simcall->call));
49
50   switch (simcall->call) {
51     case SIMCALL_COMM_TEST:
52       SIMIX_pre_comm_test(simcall);
53       break;
54
55     case SIMCALL_COMM_TESTANY:
56       SIMIX_pre_comm_testany(simcall, value);
57       break;
58
59     case SIMCALL_COMM_WAIT:
60       SIMIX_pre_comm_wait(simcall,
61           simcall->comm_wait.comm,
62           simcall->comm_wait.timeout,
63           value);
64       break;
65
66     case SIMCALL_COMM_WAITANY:
67       SIMIX_pre_comm_waitany(simcall, value);
68       break;
69
70     case SIMCALL_COMM_SEND:
71     {
72       smx_action_t comm = SIMIX_comm_isend(
73           simcall->issuer,
74           simcall->comm_send.rdv,
75           simcall->comm_send.task_size,
76           simcall->comm_send.rate,
77           simcall->comm_send.src_buff,
78           simcall->comm_send.src_buff_size,
79           simcall->comm_send.match_fun,
80           NULL, /* no clean function since it's not detached */
81           simcall->comm_send.data,
82           0);
83       SIMIX_pre_comm_wait(simcall, comm, simcall->comm_send.timeout, 0);
84       break;
85     }
86
87     case SIMCALL_COMM_ISEND:
88       simcall->comm_isend.result = SIMIX_comm_isend(
89           simcall->issuer,
90           simcall->comm_isend.rdv,
91           simcall->comm_isend.task_size,
92           simcall->comm_isend.rate,
93           simcall->comm_isend.src_buff,
94           simcall->comm_isend.src_buff_size,
95           simcall->comm_isend.match_fun,
96           simcall->comm_isend.clean_fun,
97           simcall->comm_isend.data,
98           simcall->comm_isend.detached);
99       SIMIX_simcall_answer(simcall);
100       break;
101
102     case SIMCALL_COMM_RECV:
103     {
104       smx_action_t comm = SIMIX_comm_irecv(
105           simcall->issuer,
106           simcall->comm_recv.rdv,
107           simcall->comm_recv.dst_buff,
108           simcall->comm_recv.dst_buff_size,
109           simcall->comm_recv.match_fun,
110           simcall->comm_recv.data);
111       SIMIX_pre_comm_wait(simcall, comm, simcall->comm_recv.timeout, 0);
112       break;
113     }
114
115     case SIMCALL_COMM_IRECV:
116       simcall->comm_irecv.result = SIMIX_comm_irecv(
117           simcall->issuer,
118           simcall->comm_irecv.rdv,
119           simcall->comm_irecv.dst_buff,
120           simcall->comm_irecv.dst_buff_size,
121           simcall->comm_irecv.match_fun,
122           simcall->comm_irecv.data);
123       SIMIX_simcall_answer(simcall);
124       break;
125
126     case SIMCALL_COMM_IPROBE:
127       simcall->comm_iprobe.result = SIMIX_comm_iprobe(
128           simcall->issuer,
129           simcall->comm_iprobe.rdv,
130           simcall->comm_iprobe.src,
131           simcall->comm_iprobe.tag,
132           simcall->comm_iprobe.match_fun,
133           simcall->comm_iprobe.data);
134       SIMIX_simcall_answer(simcall);
135       break;
136
137     case SIMCALL_COMM_DESTROY:
138       SIMIX_comm_destroy(simcall->comm_destroy.comm);
139       SIMIX_simcall_answer(simcall);
140       break;
141
142     case SIMCALL_COMM_CANCEL:
143       SIMIX_comm_cancel(simcall->comm_cancel.comm);
144       SIMIX_simcall_answer(simcall);
145       break;
146
147     case SIMCALL_COMM_GET_REMAINS:
148       simcall->comm_get_remains.result =
149           SIMIX_comm_get_remains(simcall->comm_get_remains.comm);
150       SIMIX_simcall_answer(simcall);
151       break;
152
153     case SIMCALL_COMM_GET_STATE:
154       simcall->comm_get_state.result =
155           SIMIX_comm_get_state(simcall->comm_get_state.comm);
156       SIMIX_simcall_answer(simcall);
157       break;
158
159     case SIMCALL_COMM_GET_SRC_DATA:
160       simcall->comm_get_src_data.result = SIMIX_comm_get_src_data(simcall->comm_get_src_data.comm);
161       SIMIX_simcall_answer(simcall);
162       break;
163
164     case SIMCALL_COMM_GET_DST_DATA:
165       simcall->comm_get_dst_data.result = SIMIX_comm_get_dst_data(simcall->comm_get_dst_data.comm);
166       SIMIX_simcall_answer(simcall);
167       break;
168
169     case SIMCALL_COMM_GET_SRC_PROC:
170       simcall->comm_get_src_proc.result =
171           SIMIX_comm_get_src_proc(simcall->comm_get_src_proc.comm);
172       SIMIX_simcall_answer(simcall);
173       break;
174
175     case SIMCALL_COMM_GET_DST_PROC:
176       simcall->comm_get_dst_proc.result =
177           SIMIX_comm_get_dst_proc(simcall->comm_get_dst_proc.comm);
178       SIMIX_simcall_answer(simcall);
179       break;
180
181 #ifdef HAVE_LATENCY_BOUND_TRACKING
182     case SIMCALL_COMM_IS_LATENCY_BOUNDED:
183       simcall->comm_is_latency_bounded.result =
184           SIMIX_comm_is_latency_bounded(simcall->comm_is_latency_bounded.comm);
185       SIMIX_simcall_answer(simcall);
186       break;
187 #endif
188
189     case SIMCALL_RDV_CREATE:
190       simcall->rdv_create.result = SIMIX_rdv_create(simcall->rdv_create.name);
191       SIMIX_simcall_answer(simcall);
192       break;
193
194     case SIMCALL_RDV_DESTROY:
195       SIMIX_rdv_destroy(simcall->rdv_destroy.rdv);
196       SIMIX_simcall_answer(simcall);
197       break;
198
199     case SIMCALL_RDV_GEY_BY_NAME:
200       simcall->rdv_get_by_name.result =
201         SIMIX_rdv_get_by_name(simcall->rdv_get_by_name.name);
202       SIMIX_simcall_answer(simcall);
203       break;
204
205     case SIMCALL_RDV_COMM_COUNT_BY_HOST:
206       simcall->rdv_comm_count_by_host.result = SIMIX_rdv_comm_count_by_host(
207           simcall->rdv_comm_count_by_host.rdv,
208           simcall->rdv_comm_count_by_host.host);
209       SIMIX_simcall_answer(simcall);
210       break;
211
212     case SIMCALL_RDV_GET_HEAD:
213       simcall->rdv_get_head.result = SIMIX_rdv_get_head(simcall->rdv_get_head.rdv);
214       SIMIX_simcall_answer(simcall);
215       break;
216
217     case SIMCALL_RDV_SET_RECV:
218       SIMIX_rdv_set_receiver(simcall->rdv_set_rcv_proc.rdv, simcall->rdv_set_rcv_proc.receiver);
219       SIMIX_simcall_answer(simcall);
220       break;
221
222     case SIMCALL_RDV_GET_RECV:
223       simcall->rdv_get_rcv_proc.result = SIMIX_rdv_get_receiver(simcall->rdv_set_rcv_proc.rdv);
224       SIMIX_simcall_answer(simcall);
225       break;
226
227     case SIMCALL_HOST_GET_BY_NAME:
228       simcall->host_get_by_name.result =
229         SIMIX_host_get_by_name(simcall->host_get_by_name.name);
230       SIMIX_simcall_answer(simcall);
231       break;
232
233     case SIMCALL_HOST_GET_NAME:
234       simcall->host_get_name.result =  SIMIX_host_get_name(simcall->host_get_name.host);
235       SIMIX_simcall_answer(simcall);
236       break;
237
238     case SIMCALL_HOST_GET_PROPERTIES:
239       simcall->host_get_properties.result =
240         SIMIX_host_get_properties(simcall->host_get_properties.host);
241       SIMIX_simcall_answer(simcall);
242       break;
243
244     case SIMCALL_HOST_GET_SPEED:
245       simcall->host_get_speed.result = 
246         SIMIX_host_get_speed(simcall->host_get_speed.host);
247       SIMIX_simcall_answer(simcall);
248       break;
249
250     case SIMCALL_HOST_GET_AVAILABLE_SPEED:
251       simcall->host_get_available_speed.result =
252         SIMIX_host_get_available_speed(simcall->host_get_available_speed.host);
253       SIMIX_simcall_answer(simcall);
254       break;
255
256     case SIMCALL_HOST_GET_STATE:
257       simcall->host_get_state.result = 
258         SIMIX_host_get_state(simcall->host_get_state.host);
259       SIMIX_simcall_answer(simcall);
260       break;
261
262     case SIMCALL_HOST_GET_DATA:
263       simcall->host_get_data.result =  SIMIX_host_get_data(simcall->host_get_data.host);
264       SIMIX_simcall_answer(simcall);
265       break;
266
267     case SIMCALL_HOST_SET_DATA:
268       SIMIX_host_set_data(simcall->host_set_data.host, simcall->host_set_data.data);
269       SIMIX_simcall_answer(simcall);
270       break;
271
272     case SIMCALL_HOST_EXECUTE:
273       simcall->host_execute.result = SIMIX_host_execute(
274     simcall->host_execute.name,
275     simcall->host_execute.host,
276     simcall->host_execute.computation_amount,
277     simcall->host_execute.priority);
278       SIMIX_simcall_answer(simcall);
279       break;
280
281     case SIMCALL_HOST_PARALLEL_EXECUTE:
282       simcall->host_parallel_execute.result = SIMIX_host_parallel_execute(
283     simcall->host_parallel_execute.name,
284     simcall->host_parallel_execute.host_nb,
285     simcall->host_parallel_execute.host_list,
286     simcall->host_parallel_execute.computation_amount,
287     simcall->host_parallel_execute.communication_amount,
288     simcall->host_parallel_execute.amount,
289     simcall->host_parallel_execute.rate);
290       SIMIX_simcall_answer(simcall);
291       break;
292
293     case SIMCALL_HOST_EXECUTION_DESTROY:
294       SIMIX_host_execution_destroy(simcall->host_execution_destroy.execution);
295       SIMIX_simcall_answer(simcall);
296       break;
297
298     case SIMCALL_HOST_EXECUTION_CANCEL:
299       SIMIX_host_execution_cancel(simcall->host_execution_cancel.execution);
300       SIMIX_simcall_answer(simcall);
301       break;
302
303     case SIMCALL_HOST_EXECUTION_GET_REMAINS:
304       simcall->host_execution_get_remains.result =
305         SIMIX_host_execution_get_remains(simcall->host_execution_get_remains.execution);
306       SIMIX_simcall_answer(simcall);
307       break;
308
309     case SIMCALL_HOST_EXECUTION_GET_STATE:
310       simcall->host_execution_get_state.result =
311         SIMIX_host_execution_get_state(simcall->host_execution_get_state.execution);
312       SIMIX_simcall_answer(simcall);
313       break;
314
315     case SIMCALL_HOST_EXECUTION_SET_PRIORITY:
316       SIMIX_host_execution_set_priority(
317     simcall->host_execution_set_priority.execution,
318     simcall->host_execution_set_priority.priority);
319       SIMIX_simcall_answer(simcall);
320       break;
321
322     case SIMCALL_HOST_EXECUTION_WAIT:
323       SIMIX_pre_host_execution_wait(simcall);
324       break;
325
326     case SIMCALL_PROCESS_CREATE:
327       SIMIX_process_create(
328           simcall->process_create.process,
329     simcall->process_create.name,
330     simcall->process_create.code,
331     simcall->process_create.data,
332     simcall->process_create.hostname,
333     simcall->process_create.kill_time,
334     simcall->process_create.argc,
335     simcall->process_create.argv,
336     simcall->process_create.properties,
337     simcall->process_create.auto_restart);
338       SIMIX_simcall_answer(simcall);
339       break;
340
341     case SIMCALL_PROCESS_KILL:
342       SIMIX_process_kill(simcall->process_kill.process,simcall->issuer);
343       SIMIX_simcall_answer(simcall);
344       break;
345
346     case SIMCALL_PROCESS_KILLALL:
347       SIMIX_process_killall(simcall->issuer);
348       SIMIX_simcall_answer(simcall);
349       break;
350
351     case SIMCALL_PROCESS_CLEANUP:
352       SIMIX_process_cleanup(simcall->process_cleanup.process);
353       SIMIX_simcall_answer(simcall);
354       break;
355
356     case SIMCALL_PROCESS_CHANGE_HOST:
357       SIMIX_pre_process_change_host(
358           simcall->process_change_host.process,
359           simcall->process_change_host.dest);
360       SIMIX_simcall_answer(simcall);
361       break;
362
363     case SIMCALL_PROCESS_SUSPEND:
364       SIMIX_pre_process_suspend(simcall);
365       break;
366
367     case SIMCALL_PROCESS_RESUME:
368       SIMIX_process_resume(simcall->process_resume.process, simcall->issuer);
369       SIMIX_simcall_answer(simcall);
370       break;
371
372     case SIMCALL_PROCESS_COUNT:
373       simcall->process_count.result = SIMIX_process_count();
374       SIMIX_simcall_answer(simcall);
375       break;
376
377     case SIMCALL_PROCESS_GET_DATA:
378       simcall->process_get_data.result =
379         SIMIX_process_get_data(simcall->process_get_data.process);
380       SIMIX_simcall_answer(simcall);
381       break;
382     case SIMCALL_PROCESS_ON_EXIT:
383       SIMIX_process_on_exit(simcall->process_on_exit.process,
384                             simcall->process_on_exit.fun,
385                             simcall->process_on_exit.data);
386       SIMIX_simcall_answer(simcall);
387     break;
388     case SIMCALL_PROCESS_RESTART:
389       simcall->process_restart.result = SIMIX_process_restart(simcall->process_restart.process, simcall->issuer);
390       SIMIX_simcall_answer(simcall);
391     break;
392     case SIMCALL_PROCESS_AUTO_RESTART_SET:
393       SIMIX_process_auto_restart_set(simcall->process_auto_restart.process,simcall->process_auto_restart.auto_restart);
394       SIMIX_simcall_answer(simcall);
395     break;
396     case SIMCALL_PROCESS_SET_DATA:
397       SIMIX_process_set_data(
398     simcall->process_set_data.process,
399     simcall->process_set_data.data);
400       SIMIX_simcall_answer(simcall);
401       break;
402
403     case SIMCALL_PROCESS_GET_HOST:
404       simcall->process_get_host.result = SIMIX_process_get_host(simcall->process_get_host.process);
405       SIMIX_simcall_answer(simcall);
406       break;
407
408     case SIMCALL_PROCESS_GET_NAME:
409       simcall->process_get_name.result = SIMIX_process_get_name(simcall->process_get_name.process);
410       SIMIX_simcall_answer(simcall);
411       break;
412
413     case SIMCALL_PROCESS_IS_SUSPENDED:
414       simcall->process_is_suspended.result =
415         SIMIX_process_is_suspended(simcall->process_is_suspended.process);
416       SIMIX_simcall_answer(simcall);
417       break;
418
419     case SIMCALL_PROCESS_GET_PROPERTIES:
420       simcall->process_get_properties.result =
421         SIMIX_process_get_properties(simcall->process_get_properties.process);
422       SIMIX_simcall_answer(simcall);
423       break;
424
425     case SIMCALL_PROCESS_SLEEP:
426       SIMIX_pre_process_sleep(simcall);
427       break;
428
429 #ifdef HAVE_TRACING
430     case SIMCALL_SET_CATEGORY:
431       SIMIX_set_category(
432           simcall->set_category.action,
433           simcall->set_category.category);
434       SIMIX_simcall_answer(simcall);
435       break;
436 #endif
437
438     case SIMCALL_MUTEX_INIT:
439       simcall->mutex_init.result = SIMIX_mutex_init();
440       SIMIX_simcall_answer(simcall);
441       break;
442
443     case SIMCALL_MUTEX_DESTROY:
444       SIMIX_mutex_destroy(simcall->mutex_destroy.mutex);
445       SIMIX_simcall_answer(simcall);
446       break;
447
448     case SIMCALL_MUTEX_LOCK:
449       SIMIX_pre_mutex_lock(simcall);
450       break;
451
452     case SIMCALL_MUTEX_TRYLOCK:
453       simcall->mutex_trylock.result =
454         SIMIX_mutex_trylock(simcall->mutex_trylock.mutex, simcall->issuer);
455       SIMIX_simcall_answer(simcall);
456       break;
457
458     case SIMCALL_MUTEX_UNLOCK:
459       SIMIX_mutex_unlock(simcall->mutex_unlock.mutex, simcall->issuer);
460       SIMIX_simcall_answer(simcall);
461       break;
462
463     case SIMCALL_COND_INIT:
464       simcall->cond_init.result = SIMIX_cond_init();
465       SIMIX_simcall_answer(simcall);
466       break;
467
468     case SIMCALL_COND_DESTROY:
469       SIMIX_cond_destroy(simcall->cond_destroy.cond);
470       SIMIX_simcall_answer(simcall);
471       break;
472
473     case SIMCALL_COND_SIGNAL:
474       SIMIX_cond_signal(simcall->cond_signal.cond);
475       SIMIX_simcall_answer(simcall);
476       break;
477
478     case SIMCALL_COND_WAIT:
479       SIMIX_pre_cond_wait(simcall);
480       break;
481
482     case SIMCALL_COND_WAIT_TIMEOUT:
483       SIMIX_pre_cond_wait_timeout(simcall);
484       break;
485
486     case SIMCALL_COND_BROADCAST:
487       SIMIX_cond_broadcast(simcall->cond_broadcast.cond);
488       SIMIX_simcall_answer(simcall);
489       break;
490
491     case SIMCALL_SEM_INIT:
492       simcall->sem_init.result = SIMIX_sem_init(simcall->sem_init.capacity);
493       SIMIX_simcall_answer(simcall);
494       break;
495
496     case SIMCALL_SEM_DESTROY:
497       SIMIX_sem_destroy(simcall->sem_destroy.sem);
498       SIMIX_simcall_answer(simcall);
499       break;
500
501     case SIMCALL_SEM_RELEASE:
502       SIMIX_sem_release(simcall->sem_release.sem);
503       SIMIX_simcall_answer(simcall);
504       break;
505
506     case SIMCALL_SEM_WOULD_BLOCK:
507       simcall->sem_would_block.result =
508         SIMIX_sem_would_block(simcall->sem_would_block.sem);
509       SIMIX_simcall_answer(simcall);
510       break;
511
512     case SIMCALL_SEM_ACQUIRE:
513       SIMIX_pre_sem_acquire(simcall);
514       break;
515
516     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
517       SIMIX_pre_sem_acquire_timeout(simcall);
518       break;
519
520     case SIMCALL_SEM_GET_CAPACITY:
521       simcall->sem_get_capacity.result = 
522         SIMIX_sem_get_capacity(simcall->sem_get_capacity.sem);
523       SIMIX_simcall_answer(simcall);
524       break;
525
526     case SIMCALL_FILE_READ:
527       SIMIX_pre_file_read(simcall);
528       break;
529
530     case SIMCALL_FILE_WRITE:
531       SIMIX_pre_file_write(simcall);
532       break;
533
534     case SIMCALL_FILE_OPEN:
535       SIMIX_pre_file_open(simcall);
536       break;
537
538     case SIMCALL_FILE_CLOSE:
539       SIMIX_pre_file_close(simcall);
540       break;
541
542     case SIMCALL_FILE_STAT:
543       SIMIX_pre_file_stat(simcall);
544       break;
545
546     case SIMCALL_FILE_UNLINK:
547       SIMIX_pre_file_unlink(simcall);
548       break;
549
550     case SIMCALL_FILE_LS:
551       SIMIX_pre_file_ls(simcall);
552       break;
553
554     case SIMCALL_ASR_GET_PROPERTIES:
555       simcall->asr_get_properties.result =
556         SIMIX_asr_get_properties(simcall->asr_get_properties.name);
557       SIMIX_simcall_answer(simcall);
558       break;
559
560     case SIMCALL_NONE:
561       THROWF(arg_error,0,"Asked to do the noop syscall on %s@%s",
562           SIMIX_process_get_name(simcall->issuer),
563           SIMIX_host_get_name(SIMIX_process_get_host(simcall->issuer))
564           );
565       break;
566   }
567 }
568
569 void SIMIX_simcall_post(smx_action_t action)
570 {
571   switch (action->type) {
572
573     case SIMIX_ACTION_EXECUTE:
574     case SIMIX_ACTION_PARALLEL_EXECUTE:
575       SIMIX_post_host_execute(action);
576       break;
577
578     case SIMIX_ACTION_COMMUNICATE:
579       SIMIX_post_comm(action);
580       break;
581
582     case SIMIX_ACTION_SLEEP:
583       SIMIX_post_process_sleep(action);
584       break;
585
586     case SIMIX_ACTION_SYNCHRO:
587       SIMIX_post_synchro(action);
588       break;
589
590     case SIMIX_ACTION_IO:
591       SIMIX_post_io(action);
592       break;
593   }
594 }