Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
55a6093c199dc1da6fb8769cf209c9ca49aedebb
[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_DESTROY:
127       SIMIX_comm_destroy(simcall->comm_destroy.comm);
128       SIMIX_simcall_answer(simcall);
129       break;
130
131     case SIMCALL_COMM_CANCEL:
132       SIMIX_comm_cancel(simcall->comm_cancel.comm);
133       SIMIX_simcall_answer(simcall);
134       break;
135
136     case SIMCALL_COMM_GET_REMAINS:
137       simcall->comm_get_remains.result =
138           SIMIX_comm_get_remains(simcall->comm_get_remains.comm);
139       SIMIX_simcall_answer(simcall);
140       break;
141
142     case SIMCALL_COMM_GET_STATE:
143       simcall->comm_get_state.result =
144           SIMIX_comm_get_state(simcall->comm_get_state.comm);
145       SIMIX_simcall_answer(simcall);
146       break;
147
148     case SIMCALL_COMM_GET_SRC_DATA:
149       simcall->comm_get_src_data.result = SIMIX_comm_get_src_data(simcall->comm_get_src_data.comm);
150       SIMIX_simcall_answer(simcall);
151       break;
152
153     case SIMCALL_COMM_GET_DST_DATA:
154       simcall->comm_get_dst_data.result = SIMIX_comm_get_dst_data(simcall->comm_get_dst_data.comm);
155       SIMIX_simcall_answer(simcall);
156       break;
157
158     case SIMCALL_COMM_GET_SRC_PROC:
159       simcall->comm_get_src_proc.result =
160           SIMIX_comm_get_src_proc(simcall->comm_get_src_proc.comm);
161       SIMIX_simcall_answer(simcall);
162       break;
163
164     case SIMCALL_COMM_GET_DST_PROC:
165       simcall->comm_get_dst_proc.result =
166           SIMIX_comm_get_dst_proc(simcall->comm_get_dst_proc.comm);
167       SIMIX_simcall_answer(simcall);
168       break;
169
170 #ifdef HAVE_LATENCY_BOUND_TRACKING
171     case SIMCALL_COMM_IS_LATENCY_BOUNDED:
172       simcall->comm_is_latency_bounded.result =
173           SIMIX_comm_is_latency_bounded(simcall->comm_is_latency_bounded.comm);
174       SIMIX_simcall_answer(simcall);
175       break;
176 #endif
177
178     case SIMCALL_RDV_CREATE:
179       simcall->rdv_create.result = SIMIX_rdv_create(simcall->rdv_create.name);
180       SIMIX_simcall_answer(simcall);
181       break;
182
183     case SIMCALL_RDV_DESTROY:
184       SIMIX_rdv_destroy(simcall->rdv_destroy.rdv);
185       SIMIX_simcall_answer(simcall);
186       break;
187
188     case SIMCALL_RDV_GEY_BY_NAME:
189       simcall->rdv_get_by_name.result =
190         SIMIX_rdv_get_by_name(simcall->rdv_get_by_name.name);
191       SIMIX_simcall_answer(simcall);
192       break;
193
194     case SIMCALL_RDV_COMM_COUNT_BY_HOST:
195       simcall->rdv_comm_count_by_host.result = SIMIX_rdv_comm_count_by_host(
196           simcall->rdv_comm_count_by_host.rdv,
197           simcall->rdv_comm_count_by_host.host);
198       SIMIX_simcall_answer(simcall);
199       break;
200
201     case SIMCALL_RDV_GET_HEAD:
202       simcall->rdv_get_head.result = SIMIX_rdv_get_head(simcall->rdv_get_head.rdv);
203       SIMIX_simcall_answer(simcall);
204       break;
205
206     case SIMCALL_HOST_GET_BY_NAME:
207       simcall->host_get_by_name.result =
208         SIMIX_host_get_by_name(simcall->host_get_by_name.name);
209       SIMIX_simcall_answer(simcall);
210       break;
211
212     case SIMCALL_HOST_GET_NAME:
213       simcall->host_get_name.result =  SIMIX_host_get_name(simcall->host_get_name.host);
214       SIMIX_simcall_answer(simcall);
215       break;
216
217     case SIMCALL_HOST_GET_PROPERTIES:
218       simcall->host_get_properties.result =
219         SIMIX_host_get_properties(simcall->host_get_properties.host);
220       SIMIX_simcall_answer(simcall);
221       break;
222
223     case SIMCALL_HOST_GET_SPEED:
224       simcall->host_get_speed.result = 
225         SIMIX_host_get_speed(simcall->host_get_speed.host);
226       SIMIX_simcall_answer(simcall);
227       break;
228
229     case SIMCALL_HOST_GET_AVAILABLE_SPEED:
230       simcall->host_get_available_speed.result =
231         SIMIX_host_get_available_speed(simcall->host_get_available_speed.host);
232       SIMIX_simcall_answer(simcall);
233       break;
234
235     case SIMCALL_HOST_GET_STATE:
236       simcall->host_get_state.result = 
237         SIMIX_host_get_state(simcall->host_get_state.host);
238       SIMIX_simcall_answer(simcall);
239       break;
240
241     case SIMCALL_HOST_GET_DATA:
242       simcall->host_get_data.result =  SIMIX_host_get_data(simcall->host_get_data.host);
243       SIMIX_simcall_answer(simcall);
244       break;
245
246     case SIMCALL_HOST_SET_DATA:
247       SIMIX_host_set_data(simcall->host_set_data.host, simcall->host_set_data.data);
248       SIMIX_simcall_answer(simcall);
249       break;
250
251     case SIMCALL_HOST_EXECUTE:
252       simcall->host_execute.result = SIMIX_host_execute(
253     simcall->host_execute.name,
254     simcall->host_execute.host,
255     simcall->host_execute.computation_amount,
256     simcall->host_execute.priority);
257       SIMIX_simcall_answer(simcall);
258       break;
259
260     case SIMCALL_HOST_PARALLEL_EXECUTE:
261       simcall->host_parallel_execute.result = SIMIX_host_parallel_execute(
262     simcall->host_parallel_execute.name,
263     simcall->host_parallel_execute.host_nb,
264     simcall->host_parallel_execute.host_list,
265     simcall->host_parallel_execute.computation_amount,
266     simcall->host_parallel_execute.communication_amount,
267     simcall->host_parallel_execute.amount,
268     simcall->host_parallel_execute.rate);
269       SIMIX_simcall_answer(simcall);
270       break;
271
272     case SIMCALL_HOST_EXECUTION_DESTROY:
273       SIMIX_host_execution_destroy(simcall->host_execution_destroy.execution);
274       SIMIX_simcall_answer(simcall);
275       break;
276
277     case SIMCALL_HOST_EXECUTION_CANCEL:
278       SIMIX_host_execution_cancel(simcall->host_execution_cancel.execution);
279       SIMIX_simcall_answer(simcall);
280       break;
281
282     case SIMCALL_HOST_EXECUTION_GET_REMAINS:
283       simcall->host_execution_get_remains.result =
284         SIMIX_host_execution_get_remains(simcall->host_execution_get_remains.execution);
285       SIMIX_simcall_answer(simcall);
286       break;
287
288     case SIMCALL_HOST_EXECUTION_GET_STATE:
289       simcall->host_execution_get_state.result =
290         SIMIX_host_execution_get_state(simcall->host_execution_get_state.execution);
291       SIMIX_simcall_answer(simcall);
292       break;
293
294     case SIMCALL_HOST_EXECUTION_SET_PRIORITY:
295       SIMIX_host_execution_set_priority(
296     simcall->host_execution_set_priority.execution,
297     simcall->host_execution_set_priority.priority);
298       SIMIX_simcall_answer(simcall);
299       break;
300
301     case SIMCALL_HOST_EXECUTION_WAIT:
302       SIMIX_pre_host_execution_wait(simcall);
303       break;
304
305     case SIMCALL_PROCESS_CREATE:
306       SIMIX_process_create(
307           simcall->process_create.process,
308     simcall->process_create.name,
309     simcall->process_create.code,
310     simcall->process_create.data,
311     simcall->process_create.hostname,
312     simcall->process_create.kill_time,
313     simcall->process_create.argc,
314     simcall->process_create.argv,
315     simcall->process_create.properties,
316     simcall->process_create.auto_restart);
317       SIMIX_simcall_answer(simcall);
318       break;
319
320     case SIMCALL_PROCESS_KILL:
321       SIMIX_process_kill(simcall->process_kill.process,simcall->issuer);
322       SIMIX_simcall_answer(simcall);
323       break;
324
325     case SIMCALL_PROCESS_KILLALL:
326       SIMIX_process_killall(simcall->issuer);
327       SIMIX_simcall_answer(simcall);
328       break;
329
330     case SIMCALL_PROCESS_CLEANUP:
331       SIMIX_process_cleanup(simcall->process_cleanup.process);
332       SIMIX_simcall_answer(simcall);
333       break;
334
335     case SIMCALL_PROCESS_CHANGE_HOST:
336       SIMIX_pre_process_change_host(
337           simcall->process_change_host.process,
338           simcall->process_change_host.dest);
339       SIMIX_simcall_answer(simcall);
340       break;
341
342     case SIMCALL_PROCESS_SUSPEND:
343       SIMIX_pre_process_suspend(simcall);
344       break;
345
346     case SIMCALL_PROCESS_RESUME:
347       SIMIX_process_resume(simcall->process_resume.process, simcall->issuer);
348       SIMIX_simcall_answer(simcall);
349       break;
350
351     case SIMCALL_PROCESS_COUNT:
352       simcall->process_count.result = SIMIX_process_count();
353       SIMIX_simcall_answer(simcall);
354       break;
355
356     case SIMCALL_PROCESS_GET_DATA:
357       simcall->process_get_data.result =
358         SIMIX_process_get_data(simcall->process_get_data.process);
359       SIMIX_simcall_answer(simcall);
360       break;
361     case SIMCALL_PROCESS_ON_EXIT:
362       SIMIX_process_on_exit(simcall->process_on_exit.process,
363                             simcall->process_on_exit.fun,
364                             simcall->process_on_exit.data);
365       SIMIX_simcall_answer(simcall);
366     break;
367     case SIMCALL_PROCESS_RESTART:
368       simcall->process_restart.result = SIMIX_process_restart(simcall->process_restart.process, simcall->issuer);
369       SIMIX_simcall_answer(simcall);
370     break;
371     case SIMCALL_PROCESS_AUTO_RESTART_SET:
372       SIMIX_process_auto_restart_set(simcall->process_auto_restart.process,simcall->process_auto_restart.auto_restart);
373       SIMIX_simcall_answer(simcall);
374     break;
375     case SIMCALL_PROCESS_SET_DATA:
376       SIMIX_process_set_data(
377     simcall->process_set_data.process,
378     simcall->process_set_data.data);
379       SIMIX_simcall_answer(simcall);
380       break;
381
382     case SIMCALL_PROCESS_GET_HOST:
383       simcall->process_get_host.result = SIMIX_process_get_host(simcall->process_get_host.process);
384       SIMIX_simcall_answer(simcall);
385       break;
386
387     case SIMCALL_PROCESS_GET_NAME:
388       simcall->process_get_name.result = SIMIX_process_get_name(simcall->process_get_name.process);
389       SIMIX_simcall_answer(simcall);
390       break;
391
392     case SIMCALL_PROCESS_IS_SUSPENDED:
393       simcall->process_is_suspended.result =
394         SIMIX_process_is_suspended(simcall->process_is_suspended.process);
395       SIMIX_simcall_answer(simcall);
396       break;
397
398     case SIMCALL_PROCESS_GET_PROPERTIES:
399       simcall->process_get_properties.result =
400         SIMIX_process_get_properties(simcall->process_get_properties.process);
401       SIMIX_simcall_answer(simcall);
402       break;
403
404     case SIMCALL_PROCESS_SLEEP:
405       SIMIX_pre_process_sleep(simcall);
406       break;
407
408 #ifdef HAVE_TRACING
409     case SIMCALL_SET_CATEGORY:
410       SIMIX_set_category(
411           simcall->set_category.action,
412           simcall->set_category.category);
413       SIMIX_simcall_answer(simcall);
414       break;
415 #endif
416
417     case SIMCALL_MUTEX_INIT:
418       simcall->mutex_init.result = SIMIX_mutex_init();
419       SIMIX_simcall_answer(simcall);
420       break;
421
422     case SIMCALL_MUTEX_DESTROY:
423       SIMIX_mutex_destroy(simcall->mutex_destroy.mutex);
424       SIMIX_simcall_answer(simcall);
425       break;
426
427     case SIMCALL_MUTEX_LOCK:
428       SIMIX_pre_mutex_lock(simcall);
429       break;
430
431     case SIMCALL_MUTEX_TRYLOCK:
432       simcall->mutex_trylock.result =
433         SIMIX_mutex_trylock(simcall->mutex_trylock.mutex, simcall->issuer);
434       SIMIX_simcall_answer(simcall);
435       break;
436
437     case SIMCALL_MUTEX_UNLOCK:
438       SIMIX_mutex_unlock(simcall->mutex_unlock.mutex, simcall->issuer);
439       SIMIX_simcall_answer(simcall);
440       break;
441
442     case SIMCALL_COND_INIT:
443       simcall->cond_init.result = SIMIX_cond_init();
444       SIMIX_simcall_answer(simcall);
445       break;
446
447     case SIMCALL_COND_DESTROY:
448       SIMIX_cond_destroy(simcall->cond_destroy.cond);
449       SIMIX_simcall_answer(simcall);
450       break;
451
452     case SIMCALL_COND_SIGNAL:
453       SIMIX_cond_signal(simcall->cond_signal.cond);
454       SIMIX_simcall_answer(simcall);
455       break;
456
457     case SIMCALL_COND_WAIT:
458       SIMIX_pre_cond_wait(simcall);
459       break;
460
461     case SIMCALL_COND_WAIT_TIMEOUT:
462       SIMIX_pre_cond_wait_timeout(simcall);
463       break;
464
465     case SIMCALL_COND_BROADCAST:
466       SIMIX_cond_broadcast(simcall->cond_broadcast.cond);
467       SIMIX_simcall_answer(simcall);
468       break;
469
470     case SIMCALL_SEM_INIT:
471       simcall->sem_init.result = SIMIX_sem_init(simcall->sem_init.capacity);
472       SIMIX_simcall_answer(simcall);
473       break;
474
475     case SIMCALL_SEM_DESTROY:
476       SIMIX_sem_destroy(simcall->sem_destroy.sem);
477       SIMIX_simcall_answer(simcall);
478       break;
479
480     case SIMCALL_SEM_RELEASE:
481       SIMIX_sem_release(simcall->sem_release.sem);
482       SIMIX_simcall_answer(simcall);
483       break;
484
485     case SIMCALL_SEM_WOULD_BLOCK:
486       simcall->sem_would_block.result =
487         SIMIX_sem_would_block(simcall->sem_would_block.sem);
488       SIMIX_simcall_answer(simcall);
489       break;
490
491     case SIMCALL_SEM_ACQUIRE:
492       SIMIX_pre_sem_acquire(simcall);
493       break;
494
495     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
496       SIMIX_pre_sem_acquire_timeout(simcall);
497       break;
498
499     case SIMCALL_SEM_GET_CAPACITY:
500       simcall->sem_get_capacity.result = 
501         SIMIX_sem_get_capacity(simcall->sem_get_capacity.sem);
502       SIMIX_simcall_answer(simcall);
503       break;
504
505     case SIMCALL_FILE_READ:
506       SIMIX_pre_file_read(simcall);
507       break;
508
509     case SIMCALL_FILE_WRITE:
510       SIMIX_pre_file_write(simcall);
511       break;
512
513     case SIMCALL_FILE_OPEN:
514       SIMIX_pre_file_open(simcall);
515       break;
516
517     case SIMCALL_FILE_CLOSE:
518       SIMIX_pre_file_close(simcall);
519       break;
520
521     case SIMCALL_FILE_STAT:
522       SIMIX_pre_file_stat(simcall);
523       break;
524
525     case SIMCALL_FILE_UNLINK:
526       SIMIX_pre_file_unlink(simcall);
527       break;
528
529     case SIMCALL_NONE:
530       THROWF(arg_error,0,"Asked to do the noop syscall on %s@%s",
531           SIMIX_process_get_name(simcall->issuer),
532           SIMIX_host_get_name(SIMIX_process_get_host(simcall->issuer))
533           );
534       break;
535   }
536 }
537
538 void SIMIX_simcall_post(smx_action_t action)
539 {
540   switch (action->type) {
541
542     case SIMIX_ACTION_EXECUTE:
543     case SIMIX_ACTION_PARALLEL_EXECUTE:
544       SIMIX_post_host_execute(action);
545       break;
546
547     case SIMIX_ACTION_COMMUNICATE:
548       SIMIX_post_comm(action);
549       break;
550
551     case SIMIX_ACTION_SLEEP:
552       SIMIX_post_process_sleep(action);
553       break;
554
555     case SIMIX_ACTION_SYNCHRO:
556       SIMIX_post_synchro(action);
557       break;
558
559     case SIMIX_ACTION_IO:
560       SIMIX_post_io(action);
561       break;
562   }
563 }