Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Starting simix mecanism for io.
[simgrid.git] / src / simix / smx_io.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14                                 "Logging specific to SIMIX (io)");
15
16 void SIMIX_pre_file_read(smx_req_t req)
17 {
18   smx_action_t action = SIMIX_file_read(req->issuer, req->file_read.name);
19   xbt_fifo_push(action->request_list, req);
20   req->issuer->waiting_action = action;
21 }
22
23 smx_action_t SIMIX_file_read(smx_process_t process, char* name)
24 {
25   smx_action_t action;
26   smx_host_t host = process->smx_host;
27
28   /* check if the host is active */
29   if (surf_workstation_model->extension.
30       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
31     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
32            host->name);
33   }
34
35   action = xbt_mallocator_get(simix_global->action_mallocator);
36   action->type = SIMIX_ACTION_IO;
37   action->name = NULL;
38 #ifdef HAVE_TRACING
39   action->category = NULL;
40 #endif
41
42   action->io.host = host;
43   //  TODO in surf model disk???
44   //  action->io.surf_io = surf_workstation_model->extension.disk.read(host->host, name),
45     action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 1.0);
46
47   surf_workstation_model->action_data_set(action->io.surf_io, action);
48   XBT_DEBUG("Create io action %p", action);
49
50   return action;
51 }
52
53 void SIMIX_post_file_read(smx_action_t action)
54 {
55   smx_req_t req;
56
57   while ((req = xbt_fifo_shift(action->request_list))) {
58
59     switch(surf_workstation_model->action_state_get(action->io.surf_io)){
60     case SURF_ACTION_FAILED:
61       action->state = SIMIX_FAILED;
62       break;
63
64     case SURF_ACTION_DONE:
65       action->state = SIMIX_DONE;
66       break;
67
68     default:
69       THROW_IMPOSSIBLE;
70       break;
71     }
72   }
73   /* If there are requests associated with the action, then answer them */
74   if (xbt_fifo_size(action->request_list))
75           SIMIX_io_finish(action);
76 }
77
78 void SIMIX_io_destroy(smx_action_t action)
79 {
80   XBT_DEBUG("Destroy action %p", action);
81   if (action->io.surf_io)
82     action->io.surf_io->model_type->action_unref(action->io.surf_io);
83   xbt_mallocator_release(simix_global->action_mallocator, action);
84 }
85
86 void SIMIX_io_finish(smx_action_t action)
87 {
88   volatile xbt_fifo_item_t item;
89   smx_req_t req;
90
91   xbt_fifo_foreach(action->request_list, item, req, smx_req_t) {
92
93     switch (action->state) {
94
95       case SIMIX_DONE:
96         /* do nothing, action done */
97         break;
98
99       case SIMIX_FAILED:
100         TRY {
101           THROWF(io_error, 0, "IO failed");
102         }
103         CATCH(req->issuer->running_ctx->exception) {
104           req->issuer->doexception = 1;
105         }
106       break;
107
108       case SIMIX_CANCELED:
109         TRY {
110           THROWF(cancel_error, 0, "Canceled");
111         }
112         CATCH(req->issuer->running_ctx->exception) {
113           req->issuer->doexception = 1;
114         }
115         break;
116
117       default:
118         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
119             action->state);
120     }
121     req->issuer->waiting_action = NULL;
122     SIMIX_request_answer(req);
123   }
124
125   /* We no longer need it */
126   SIMIX_io_destroy(action);
127 }