Logo AND Algorithmique Numérique Distribuée

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