Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #184 from Takishipp/signals
[simgrid.git] / src / simix / smx_io.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <xbt/ex.hpp>
7 #include <xbt/sysdep.h>
8 #include <xbt/log.h>
9 #include <xbt/dict.h>
10
11 #include "simgrid/s4u/Host.hpp"
12
13 #include <mc/mc.h>
14
15 #include "src/surf/surf_interface.hpp"
16 #include "smx_private.h"
17
18 #include "src/kernel/activity/SynchroIo.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
21
22 //SIMIX FILE READ
23 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
24 {
25   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
26   synchro->simcalls.push_back(simcall);
27   simcall->issuer->waiting_synchro = synchro;
28 }
29
30 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
31 {
32   /* check if the host is active */
33   if (host->isOff())
34     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
35
36   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
37   synchro->host = host;
38   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
39
40   synchro->surf_io->setData(synchro);
41   XBT_DEBUG("Create io synchro %p", synchro);
42
43   return synchro;
44 }
45
46 //SIMIX FILE WRITE
47 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
48 {
49   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
50   synchro->simcalls.push_back(simcall);
51   simcall->issuer->waiting_synchro = synchro;
52 }
53
54 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
55 {
56   if (host->isOff())
57     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
58
59   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
60   synchro->host = host;
61   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
62   synchro->surf_io->setData(synchro);
63   XBT_DEBUG("Create io synchro %p", synchro);
64
65   return synchro;
66 }
67
68 //SIMIX FILE OPEN
69 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
70 {
71   smx_activity_t synchro = SIMIX_file_open(fullpath, host);
72   synchro->simcalls.push_back(simcall);
73   simcall->issuer->waiting_synchro = synchro;
74 }
75
76 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
77 {
78   if (host->isOff())
79     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
80
81   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
82   synchro->host = host;
83   synchro->surf_io = surf_host_open(host, fullpath);
84   synchro->surf_io->setData(synchro);
85   XBT_DEBUG("Create io synchro %p", synchro);
86
87   return synchro;
88 }
89
90 //SIMIX FILE CLOSE
91 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
92 {
93   smx_activity_t synchro = SIMIX_file_close(fd, host);
94   synchro->simcalls.push_back(simcall);
95   simcall->issuer->waiting_synchro = synchro;
96 }
97
98 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
99 {
100   if (host->isOff())
101     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
102
103   simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
104   synchro->host = host;
105   synchro->surf_io = surf_host_close(host, fd->surf_file);
106   synchro->surf_io->setData(synchro);
107   XBT_DEBUG("Create io synchro %p", synchro);
108
109   return synchro;
110 }
111
112 //SIMIX FILE UNLINK
113 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
114 {
115   if (host->isOff())
116     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
117
118   int res = surf_host_unlink(host, fd->surf_file);
119   xbt_free(fd);
120   return res;
121 }
122
123 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
124 {
125   return SIMIX_file_get_size(simcall->issuer, fd);
126 }
127
128 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
129 {
130   sg_host_t host = process->host;
131   return  surf_host_get_size(host, fd->surf_file);
132 }
133
134 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
135 {
136   return SIMIX_file_tell(simcall->issuer, fd);
137 }
138
139 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
140 {
141   sg_host_t host = process->host;
142   return  surf_host_file_tell(host, fd->surf_file);
143 }
144
145
146 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
147 {
148   return SIMIX_file_get_info(simcall->issuer, fd);
149 }
150
151 xbt_dynar_t SIMIX_file_get_info(smx_actor_t process, smx_file_t fd)
152 {
153   sg_host_t host = process->host;
154   return  surf_host_get_info(host, fd->surf_file);
155 }
156
157 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
158 {
159   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
160 }
161
162 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
163 {
164   sg_host_t host = process->host;
165   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
166 }
167
168 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
169 {
170   return SIMIX_file_move(simcall->issuer, file, fullpath);
171 }
172
173 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
174 {
175   sg_host_t host = process->host;
176   return  surf_host_file_move(host, file->surf_file, fullpath);
177 }
178
179 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
180   return surf_storage_get_properties(storage);
181 }
182
183 void SIMIX_io_destroy(smx_activity_t synchro)
184 {
185   simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::Io*>(synchro);
186   XBT_DEBUG("Destroy synchro %p", synchro);
187   if (io->surf_io)
188     io->surf_io->unref();
189   delete io;
190 }
191
192 void SIMIX_io_finish(smx_activity_t synchro)
193 {
194   for (smx_simcall_t simcall : synchro->simcalls) {
195     switch (synchro->state) {
196       case SIMIX_DONE:
197         /* do nothing, synchro done */
198         break;
199       case SIMIX_FAILED:
200         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
201         break;
202       case SIMIX_CANCELED:
203         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
204         break;
205       default:
206         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
207     }
208
209     if (simcall->issuer->host->isOff()) {
210       simcall->issuer->context->iwannadie = 1;
211     }
212
213     simcall->issuer->waiting_synchro = nullptr;
214     SIMIX_simcall_answer(simcall);
215   }
216
217   /* We no longer need it */
218   SIMIX_io_destroy(synchro);
219 }