Logo AND Algorithmique Numérique Distribuée

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