Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement functions file_read and file_open into simix like fread and fwrite in posix
[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 "smx_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
17 //SIMIX FILE READ
18 void SIMIX_pre_file_read(smx_simcall_t simcall)
19 {
20   smx_action_t action = SIMIX_file_read(simcall->issuer,
21       simcall->file_read.ptr,
22       simcall->file_read.size,
23       simcall->file_read.nmemb,
24       simcall->file_read.stream);
25   xbt_fifo_push(action->simcalls, simcall);
26   simcall->issuer->waiting_action = action;
27 }
28
29 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t* stream)
30 {
31   smx_action_t action;
32   smx_host_t host = process->smx_host;
33
34   /* check if the host is active */
35   if (surf_workstation_model->extension.
36       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
37     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
38            host->name);
39   }
40
41   action = xbt_mallocator_get(simix_global->action_mallocator);
42   action->type = SIMIX_ACTION_IO;
43   action->name = NULL;
44 #ifdef HAVE_TRACING
45   action->category = NULL;
46 #endif
47
48   action->io.host = host;
49   //  TODO in surf model disk???
50   //  action->io.surf_io = surf_workstation_model->extension.disk.read(host->host, name),
51   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 1.0);
52
53   surf_workstation_model->action_data_set(action->io.surf_io, action);
54   XBT_DEBUG("Create io action %p", action);
55
56   return action;
57 }
58
59 //SIMIX FILE WRITE
60 void SIMIX_pre_file_write(smx_simcall_t simcall)
61 {
62   smx_action_t action = SIMIX_file_write(simcall->issuer,
63       simcall->file_write.ptr,
64       simcall->file_write.size,
65       simcall->file_write.nmemb,
66       simcall->file_write.stream);
67   xbt_fifo_push(action->simcalls, simcall);
68   simcall->issuer->waiting_action = action;
69 }
70
71 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t* stream)
72 {
73   smx_action_t action;
74   smx_host_t host = process->smx_host;
75
76   /* check if the host is active */
77   if (surf_workstation_model->extension.
78       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
79     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
80            host->name);
81   }
82
83   action = xbt_mallocator_get(simix_global->action_mallocator);
84   action->type = SIMIX_ACTION_IO;
85   action->name = NULL;
86 #ifdef HAVE_TRACING
87   action->category = NULL;
88 #endif
89
90   action->io.host = host;
91   //  TODO in surf model disk???
92   //  action->io.surf_io = surf_workstation_model->extension.disk.write(host->host, name),
93   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 2.0);
94
95   surf_workstation_model->action_data_set(action->io.surf_io, action);
96   XBT_DEBUG("Create io action %p", action);
97
98   return action;
99 }
100
101 void SIMIX_post_io(smx_action_t action)
102 {
103   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
104
105     case SURF_ACTION_FAILED:
106       action->state = SIMIX_FAILED;
107       break;
108
109     case SURF_ACTION_DONE:
110       action->state = SIMIX_DONE;
111       break;
112
113     default:
114       THROW_IMPOSSIBLE;
115       break;
116   }
117
118   SIMIX_io_finish(action);
119 }
120
121 void SIMIX_io_destroy(smx_action_t action)
122 {
123   XBT_DEBUG("Destroy action %p", action);
124   if (action->io.surf_io)
125     action->io.surf_io->model_type->action_unref(action->io.surf_io);
126   xbt_mallocator_release(simix_global->action_mallocator, action);
127 }
128
129 void SIMIX_io_finish(smx_action_t action)
130 {
131   volatile xbt_fifo_item_t item;
132   smx_simcall_t simcall;
133
134   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
135
136     switch (action->state) {
137
138       case SIMIX_DONE:
139         /* do nothing, action done */
140         break;
141
142       case SIMIX_FAILED:
143         TRY {
144           THROWF(io_error, 0, "IO failed");
145         }
146         CATCH(simcall->issuer->running_ctx->exception) {
147           simcall->issuer->doexception = 1;
148         }
149       break;
150
151       case SIMIX_CANCELED:
152         TRY {
153           THROWF(cancel_error, 0, "Canceled");
154         }
155         CATCH(simcall->issuer->running_ctx->exception) {
156           simcall->issuer->doexception = 1;
157         }
158         break;
159
160       default:
161         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
162             action->state);
163     }
164     simcall->issuer->waiting_action = NULL;
165     SIMIX_simcall_answer(simcall);
166   }
167
168   /* We no longer need it */
169   SIMIX_io_destroy(action);
170 }