Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d92bfdc6c0ca665193211f09fa1f8b142a5136c0
[simgrid.git] / src / simix / smx_smurf.c
1 #include "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 /* Tables with the requests to handle at the end of this round of scheduling
9  * user processes. There is one for each thread of execution. */
10 static xbt_swag_t *req_lists;
11
12 void SIMIX_request_init(void)
13 {
14   s_smx_req_t req;
15   int i, nthreads = SIMIX_context_get_nthreads();
16
17   req_lists = xbt_new0(xbt_swag_t, nthreads);
18   for(i=0; i < nthreads; i++)
19     req_lists[i] = xbt_swag_new(xbt_swag_offset(req, reqtable_hookup));
20
21 }
22
23 void SIMIX_request_destroy(void)
24 {
25   int i, nthreads = SIMIX_context_get_nthreads();
26
27   for(i=0; i < nthreads; i++)
28     xbt_swag_free(req_lists[i]);
29
30   xbt_free(req_lists);
31 }
32
33 xbt_swag_t SIMIX_request_get_reqlist(int thread_pid)
34 {
35   return req_lists[thread_pid];
36 }
37
38 /* FIXME: we may want to save the initialization of issuer... */
39 XBT_INLINE smx_req_t SIMIX_req_mine() {
40   smx_process_t issuer = SIMIX_process_self();
41   return &issuer->request;
42 }
43
44 void SIMIX_request_push()
45 {
46   xbt_swag_t req_table;
47   smx_process_t issuer = SIMIX_process_self();
48
49   if (issuer != simix_global->maestro_process){
50     issuer->request.issuer = issuer;
51     req_table = SIMIX_request_get_reqlist(SIMIX_context_get_thread_id());
52
53     xbt_swag_insert_at_tail(&issuer->request, req_table);
54
55     XBT_DEBUG("Pushed request %s (%d) of %s",
56         SIMIX_request_name(issuer->request.call), issuer->request.call,
57         issuer->name);
58
59     XBT_DEBUG("Yield process '%s' on request of type %s (%d)", issuer->name,
60         SIMIX_request_name(issuer->request.call), issuer->request.call);
61     SIMIX_process_yield();
62   } else {
63     SIMIX_request_pre(&issuer->request, 0);
64   }
65 }
66
67 smx_req_t SIMIX_request_pop(void)
68 {
69   int i;
70   smx_req_t req = NULL;
71   int nthreads = SIMIX_context_get_nthreads();
72
73   for(i=0; i < nthreads; i++){
74     if((req = xbt_swag_extract(req_lists[i]))){
75       XBT_DEBUG("Popped request %s (%d) of %s",
76           SIMIX_request_name(req->issuer->request.call),
77           req->issuer->request.call,
78           req->issuer->name);
79       return req;
80     }
81   }
82
83   return NULL;
84 }
85
86 void SIMIX_request_answer(smx_req_t req)
87 {
88   if (req->issuer != simix_global->maestro_process){
89     XBT_DEBUG("Answer request %s (%d)", SIMIX_request_name(req->call), req->call);
90     req->issuer->request.call = REQ_NO_REQ;
91     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, req->issuer);
92   }
93 }
94
95 void SIMIX_request_pre(smx_req_t req, int value)
96 {
97   switch (req->call) {
98
99     case REQ_COMM_TEST:
100       SIMIX_pre_comm_test(req);
101       break;
102
103     case REQ_COMM_TESTANY:
104       SIMIX_pre_comm_testany(req, value);
105       break;
106
107     case REQ_COMM_WAIT:
108       SIMIX_pre_comm_wait(req,
109           req->comm_wait.comm,
110           req->comm_wait.timeout,
111           value);
112       break;
113
114     case REQ_COMM_WAITANY:
115       SIMIX_pre_comm_waitany(req, value);
116       break;
117
118     case REQ_COMM_SEND:
119     {
120       smx_action_t comm = SIMIX_comm_isend(
121           req->issuer,
122           req->comm_send.rdv,
123           req->comm_send.task_size,
124           req->comm_send.rate,
125           req->comm_send.src_buff,
126           req->comm_send.src_buff_size,
127           req->comm_send.match_fun,
128           req->comm_send.data,
129           0);
130       SIMIX_pre_comm_wait(req, comm, req->comm_send.timeout, 0);
131       break;
132     }
133
134     case REQ_COMM_ISEND:
135       req->comm_isend.result = SIMIX_comm_isend(
136           req->issuer,
137           req->comm_isend.rdv,
138           req->comm_isend.task_size,
139           req->comm_isend.rate,
140           req->comm_isend.src_buff,
141           req->comm_isend.src_buff_size,
142           req->comm_isend.match_fun,
143           req->comm_isend.data,
144           req->comm_isend.detached);
145       SIMIX_request_answer(req);
146       break;
147
148     case REQ_COMM_RECV:
149     {
150       smx_action_t comm = SIMIX_comm_irecv(
151           req->issuer,
152           req->comm_recv.rdv,
153           req->comm_recv.dst_buff,
154           req->comm_recv.dst_buff_size,
155           req->comm_recv.match_fun,
156           req->comm_recv.data);
157       SIMIX_pre_comm_wait(req, comm, req->comm_recv.timeout, 0);
158       break;
159     }
160
161     case REQ_COMM_IRECV:
162       req->comm_irecv.result = SIMIX_comm_irecv(
163           req->issuer,
164           req->comm_irecv.rdv,
165           req->comm_irecv.dst_buff,
166           req->comm_irecv.dst_buff_size,
167           req->comm_irecv.match_fun,
168           req->comm_irecv.data);
169       SIMIX_request_answer(req);
170       break;
171
172     case REQ_COMM_DESTROY:
173       SIMIX_comm_destroy(req->comm_destroy.comm);
174       SIMIX_request_answer(req);
175       break;
176
177     case REQ_COMM_CANCEL:
178       SIMIX_comm_cancel(req->comm_cancel.comm);
179       SIMIX_request_answer(req);
180       break;
181
182     case REQ_COMM_GET_REMAINS:
183       req->comm_get_remains.result =
184           SIMIX_comm_get_remains(req->comm_get_remains.comm);
185       SIMIX_request_answer(req);
186       break;
187
188     case REQ_COMM_GET_STATE:
189       req->comm_get_state.result =
190           SIMIX_comm_get_state(req->comm_get_state.comm);
191       SIMIX_request_answer(req);
192       break;
193
194     case REQ_COMM_GET_SRC_DATA:
195       req->comm_get_src_data.result = SIMIX_comm_get_src_data(req->comm_get_src_data.comm);
196       SIMIX_request_answer(req);
197       break;
198
199     case REQ_COMM_GET_DST_DATA:
200       req->comm_get_dst_data.result = SIMIX_comm_get_dst_data(req->comm_get_dst_data.comm);
201       SIMIX_request_answer(req);
202       break;
203
204     case REQ_COMM_GET_SRC_PROC:
205       req->comm_get_src_proc.result =
206           SIMIX_comm_get_src_proc(req->comm_get_src_proc.comm);
207       SIMIX_request_answer(req);
208       break;
209
210     case REQ_COMM_GET_DST_PROC:
211       req->comm_get_dst_proc.result =
212           SIMIX_comm_get_dst_proc(req->comm_get_dst_proc.comm);
213       SIMIX_request_answer(req);
214       break;
215
216 #ifdef HAVE_LATENCY_BOUND_TRACKING
217     case REQ_COMM_IS_LATENCY_BOUNDED:
218       req->comm_is_latency_bounded.result =
219           SIMIX_comm_is_latency_bounded(req->comm_is_latency_bounded.comm);
220       SIMIX_request_answer(req);
221       break;
222 #endif
223
224     case REQ_RDV_CREATE:
225       req->rdv_create.result = SIMIX_rdv_create(req->rdv_create.name);
226       SIMIX_request_answer(req);
227       break;
228
229     case REQ_RDV_DESTROY:
230       SIMIX_rdv_destroy(req->rdv_destroy.rdv);
231       SIMIX_request_answer(req);
232       break;
233
234     case REQ_RDV_GEY_BY_NAME:
235       req->rdv_get_by_name.result =
236         SIMIX_rdv_get_by_name(req->rdv_get_by_name.name);
237       SIMIX_request_answer(req);
238       break;
239
240     case REQ_RDV_COMM_COUNT_BY_HOST:
241       req->rdv_comm_count_by_host.result = SIMIX_rdv_comm_count_by_host(
242           req->rdv_comm_count_by_host.rdv,
243           req->rdv_comm_count_by_host.host);
244       SIMIX_request_answer(req);
245       break;
246
247     case REQ_RDV_GET_HEAD:
248       req->rdv_get_head.result = SIMIX_rdv_get_head(req->rdv_get_head.rdv);
249       SIMIX_request_answer(req);
250       break;
251
252     case REQ_HOST_GET_BY_NAME:
253       req->host_get_by_name.result =
254         SIMIX_host_get_by_name(req->host_get_by_name.name);
255       SIMIX_request_answer(req);
256       break;
257
258     case REQ_HOST_GET_NAME:
259       req->host_get_name.result =       SIMIX_host_get_name(req->host_get_name.host);
260       SIMIX_request_answer(req);
261       break;
262
263     case REQ_HOST_GET_PROPERTIES:
264       req->host_get_properties.result =
265         SIMIX_host_get_properties(req->host_get_properties.host);
266       SIMIX_request_answer(req);
267       break;
268
269     case REQ_HOST_GET_SPEED:
270       req->host_get_speed.result = 
271         SIMIX_host_get_speed(req->host_get_speed.host);
272       SIMIX_request_answer(req);
273       break;
274
275     case REQ_HOST_GET_AVAILABLE_SPEED:
276       req->host_get_available_speed.result =
277         SIMIX_host_get_available_speed(req->host_get_available_speed.host);
278       SIMIX_request_answer(req);
279       break;
280
281     case REQ_HOST_GET_STATE:
282       req->host_get_state.result = 
283         SIMIX_host_get_state(req->host_get_state.host);
284       SIMIX_request_answer(req);
285       break;
286
287     case REQ_HOST_GET_DATA:
288       req->host_get_data.result =       SIMIX_host_get_data(req->host_get_data.host);
289       SIMIX_request_answer(req);
290       break;
291
292     case REQ_HOST_SET_DATA:
293       SIMIX_host_set_data(req->host_set_data.host, req->host_set_data.data);
294       SIMIX_request_answer(req);
295       break;
296
297     case REQ_HOST_EXECUTE:
298       req->host_execute.result = SIMIX_host_execute(
299           req->host_execute.name,
300           req->host_execute.host,
301           req->host_execute.computation_amount,
302           req->host_execute.priority);
303       SIMIX_request_answer(req);
304       break;
305
306     case REQ_HOST_PARALLEL_EXECUTE:
307       req->host_parallel_execute.result = SIMIX_host_parallel_execute(
308           req->host_parallel_execute.name,
309           req->host_parallel_execute.host_nb,
310           req->host_parallel_execute.host_list,
311           req->host_parallel_execute.computation_amount,
312           req->host_parallel_execute.communication_amount,
313           req->host_parallel_execute.amount,
314           req->host_parallel_execute.rate);
315       SIMIX_request_answer(req);
316       break;
317
318     case REQ_HOST_EXECUTION_DESTROY:
319       SIMIX_host_execution_destroy(req->host_execution_destroy.execution);
320       SIMIX_request_answer(req);
321       break;
322
323     case REQ_HOST_EXECUTION_CANCEL:
324       SIMIX_host_execution_cancel(req->host_execution_cancel.execution);
325       SIMIX_request_answer(req);
326       break;
327
328     case REQ_HOST_EXECUTION_GET_REMAINS:
329       req->host_execution_get_remains.result =
330         SIMIX_host_execution_get_remains(req->host_execution_get_remains.execution);
331       SIMIX_request_answer(req);
332       break;
333
334     case REQ_HOST_EXECUTION_GET_STATE:
335       req->host_execution_get_state.result =
336         SIMIX_host_execution_get_state(req->host_execution_get_state.execution);
337       SIMIX_request_answer(req);
338       break;
339
340     case REQ_HOST_EXECUTION_SET_PRIORITY:
341       SIMIX_host_execution_set_priority(
342           req->host_execution_set_priority.execution,
343           req->host_execution_set_priority.priority);
344       SIMIX_request_answer(req);
345       break;
346
347     case REQ_HOST_EXECUTION_WAIT:
348       SIMIX_pre_host_execution_wait(req);
349       break;
350
351     case REQ_PROCESS_CREATE:
352       SIMIX_process_create(
353           req->process_create.process,
354           req->process_create.name,
355           req->process_create.code,
356           req->process_create.data,
357           req->process_create.hostname,
358           req->process_create.argc,
359           req->process_create.argv,
360           req->process_create.properties);
361       SIMIX_request_answer(req);
362       break;
363
364     case REQ_PROCESS_KILL:
365       SIMIX_process_kill(req->process_kill.process, req->issuer);
366       SIMIX_request_answer(req);
367       break;
368
369     case REQ_PROCESS_CHANGE_HOST:
370       SIMIX_process_change_host(
371           req->process_change_host.process,
372           req->process_change_host.source,
373           req->process_change_host.dest);
374       SIMIX_request_answer(req);
375       break;
376
377     case REQ_PROCESS_SUSPEND:
378       SIMIX_pre_process_suspend(req);
379       break;
380
381     case REQ_PROCESS_RESUME:
382       SIMIX_process_resume(req->process_resume.process, req->issuer);
383       SIMIX_request_answer(req);
384       break;
385
386     case REQ_PROCESS_COUNT:
387       req->process_count.result = SIMIX_process_count();
388       SIMIX_request_answer(req);
389       break;
390
391     case REQ_PROCESS_GET_DATA:
392       req->process_get_data.result =
393         SIMIX_process_get_data(req->process_get_data.process);
394       SIMIX_request_answer(req);
395       break;
396
397     case REQ_PROCESS_SET_DATA:
398       SIMIX_process_set_data(
399           req->process_set_data.process,
400           req->process_set_data.data);
401       SIMIX_request_answer(req);
402       break;
403
404     case REQ_PROCESS_GET_HOST:
405       req->process_get_host.result = SIMIX_process_get_host(req->process_get_host.process);
406       SIMIX_request_answer(req);
407       break;
408
409     case REQ_PROCESS_GET_NAME:
410       req->process_get_name.result = SIMIX_process_get_name(req->process_get_name.process);
411       SIMIX_request_answer(req);
412       break;
413
414     case REQ_PROCESS_IS_SUSPENDED:
415       req->process_is_suspended.result =
416         SIMIX_process_is_suspended(req->process_is_suspended.process);
417       SIMIX_request_answer(req);
418       break;
419
420     case REQ_PROCESS_GET_PROPERTIES:
421       req->process_get_properties.result =
422         SIMIX_process_get_properties(req->process_get_properties.process);
423       SIMIX_request_answer(req);
424       break;
425
426     case REQ_PROCESS_SLEEP:
427       SIMIX_pre_process_sleep(req);
428       break;
429
430 #ifdef HAVE_TRACING
431     case REQ_SET_CATEGORY:
432       SIMIX_set_category(
433           req->set_category.action,
434           req->set_category.category);
435       SIMIX_request_answer(req);
436       break;
437 #endif
438
439     case REQ_MUTEX_INIT:
440       req->mutex_init.result = SIMIX_mutex_init();
441       SIMIX_request_answer(req);
442       break;
443
444     case REQ_MUTEX_DESTROY:
445       SIMIX_mutex_destroy(req->mutex_destroy.mutex);
446       SIMIX_request_answer(req);
447       break;
448
449     case REQ_MUTEX_LOCK:
450       SIMIX_pre_mutex_lock(req);
451       break;
452
453     case REQ_MUTEX_TRYLOCK:
454       req->mutex_trylock.result =
455               SIMIX_mutex_trylock(req->mutex_trylock.mutex, req->issuer);
456       SIMIX_request_answer(req);
457       break;
458
459     case REQ_MUTEX_UNLOCK:
460       SIMIX_mutex_unlock(req->mutex_unlock.mutex, req->issuer);
461       SIMIX_request_answer(req);
462       break;
463
464     case REQ_COND_INIT:
465       req->cond_init.result = SIMIX_cond_init();
466       SIMIX_request_answer(req);
467       break;
468
469     case REQ_COND_DESTROY:
470       SIMIX_cond_destroy(req->cond_destroy.cond);
471       SIMIX_request_answer(req);
472       break;
473
474     case REQ_COND_SIGNAL:
475       SIMIX_cond_signal(req->cond_signal.cond);
476       SIMIX_request_answer(req);
477       break;
478
479     case REQ_COND_WAIT:
480       SIMIX_pre_cond_wait(req);
481       break;
482
483     case REQ_COND_WAIT_TIMEOUT:
484       SIMIX_pre_cond_wait_timeout(req);
485       break;
486
487     case REQ_COND_BROADCAST:
488       SIMIX_cond_broadcast(req->cond_broadcast.cond);
489       SIMIX_request_answer(req);
490       break;
491
492     case REQ_SEM_INIT:
493       req->sem_init.result = SIMIX_sem_init(req->sem_init.capacity);
494       SIMIX_request_answer(req);
495       break;
496
497     case REQ_SEM_DESTROY:
498       SIMIX_sem_destroy(req->sem_destroy.sem);
499       SIMIX_request_answer(req);
500       break;
501
502     case REQ_SEM_RELEASE:
503       SIMIX_sem_release(req->sem_release.sem);
504       SIMIX_request_answer(req);
505       break;
506
507     case REQ_SEM_WOULD_BLOCK:
508       req->sem_would_block.result =
509         SIMIX_sem_would_block(req->sem_would_block.sem);
510       SIMIX_request_answer(req);
511       break;
512
513     case REQ_SEM_ACQUIRE:
514       SIMIX_pre_sem_acquire(req);
515       break;
516
517     case REQ_SEM_ACQUIRE_TIMEOUT:
518       SIMIX_pre_sem_acquire_timeout(req);
519       break;
520
521     case REQ_SEM_GET_CAPACITY:
522       req->sem_get_capacity.result = 
523         SIMIX_sem_get_capacity(req->sem_get_capacity.sem);
524       SIMIX_request_answer(req);
525       break;
526
527     case REQ_NO_REQ:
528       THROW2(arg_error,0,"Asked to do the noop syscall on %s@%s",
529           SIMIX_process_get_name(req->issuer),
530           SIMIX_host_get_name(SIMIX_process_get_host(req->issuer))
531           );
532       break;
533   }
534 }
535
536 void SIMIX_request_post(smx_action_t action)
537 {
538   switch (action->type) {
539
540     case SIMIX_ACTION_EXECUTE:
541     case SIMIX_ACTION_PARALLEL_EXECUTE:
542       SIMIX_post_host_execute(action);
543       break;
544
545     case SIMIX_ACTION_COMMUNICATE:
546       SIMIX_post_comm(action);
547       break;
548
549     case SIMIX_ACTION_SLEEP:
550       SIMIX_post_process_sleep(action);
551       break;
552
553     case SIMIX_ACTION_SYNCHRO:
554       SIMIX_post_synchro(action);
555       break;
556
557     case SIMIX_ACTION_IO:
558       break;
559   }
560 }