Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 #include "simgrid/s4u/Storage.hpp"
13 #include "src/surf/FileImpl.hpp"
14 #include "src/surf/StorageImpl.hpp"
15
16 #include <mc/mc.h>
17
18 #include "src/surf/surf_interface.hpp"
19 #include "smx_private.h"
20
21 #include "src/kernel/activity/SynchroIo.hpp"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
24
25 //SIMIX FILE READ
26 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
27 {
28   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
29   synchro->simcalls.push_back(simcall);
30   simcall->issuer->waiting_synchro = synchro;
31 }
32
33 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
34 {
35   /* check if the host is active */
36   if (host->isOff())
37     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
38
39   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
40   synchro->host = host;
41   synchro->surf_io = surf_host_read(host, fd->surf_file, size);
42
43   synchro->surf_io->setData(synchro);
44   XBT_DEBUG("Create io synchro %p", synchro);
45
46   return synchro;
47 }
48
49 //SIMIX FILE WRITE
50 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
51 {
52   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
53   synchro->simcalls.push_back(simcall);
54   simcall->issuer->waiting_synchro = synchro;
55 }
56
57 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
58 {
59   if (host->isOff())
60     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
61
62   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
63   synchro->host = host;
64   synchro->surf_io = surf_host_write(host, fd->surf_file, size);
65   synchro->surf_io->setData(synchro);
66   XBT_DEBUG("Create io synchro %p", synchro);
67
68   return synchro;
69 }
70
71 //SIMIX FILE OPEN
72 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* mount, const char* path, sg_storage_t st)
73 {
74   smx_activity_t synchro = SIMIX_file_open(mount, path, st);
75   synchro->simcalls.push_back(simcall);
76   simcall->issuer->waiting_synchro = synchro;
77 }
78
79 smx_activity_t SIMIX_file_open(const char* mount, const char* path, sg_storage_t st)
80 {
81   if (st->host()->isOff())
82     THROWF(host_error, 0, "Host %s failed, you cannot call this function", st->host()->cname());
83
84   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
85   synchro->host                              = st->host();
86   synchro->surf_io                           = st->pimpl_->open(mount, path);
87   synchro->surf_io->setData(synchro);
88   XBT_DEBUG("Create io synchro %p", synchro);
89
90   return synchro;
91 }
92
93 //SIMIX FILE CLOSE
94 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
95 {
96   smx_activity_t synchro = SIMIX_file_close(fd, host);
97   synchro->simcalls.push_back(simcall);
98   simcall->issuer->waiting_synchro = synchro;
99 }
100
101 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
102 {
103   if (host->isOff())
104     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
105
106   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
107   synchro->host = host;
108   synchro->surf_io = surf_host_close(host, fd->surf_file);
109   synchro->surf_io->setData(synchro);
110   XBT_DEBUG("Create io synchro %p", synchro);
111
112   return synchro;
113 }
114
115 //SIMIX FILE UNLINK
116 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
117 {
118   if (host->isOff())
119     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
120
121   return surf_host_unlink(host, fd->surf_file);
122 }
123
124 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
125 {
126   return SIMIX_file_get_size(simcall->issuer, fd);
127 }
128
129 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
130 {
131   return fd->surf_file->size();
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   return fd->surf_file->tell();
142 }
143
144 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
145 {
146   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
147 }
148
149 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
150 {
151   sg_host_t host = process->host;
152   return  surf_host_file_seek(host, fd->surf_file, offset, origin);
153 }
154
155 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
156 {
157   return SIMIX_file_move(simcall->issuer, file, fullpath);
158 }
159
160 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
161 {
162   sg_host_t host = process->host;
163   return  surf_host_file_move(host, file->surf_file, fullpath);
164 }
165
166 void SIMIX_io_destroy(smx_activity_t synchro)
167 {
168   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
169   XBT_DEBUG("Destroy synchro %p", synchro.get());
170   if (io->surf_io)
171     io->surf_io->unref();
172 }
173
174 void SIMIX_io_finish(smx_activity_t synchro)
175 {
176   for (smx_simcall_t simcall : synchro->simcalls) {
177     switch (synchro->state) {
178       case SIMIX_DONE:
179         /* do nothing, synchro done */
180         break;
181       case SIMIX_FAILED:
182         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
183         break;
184       case SIMIX_CANCELED:
185         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
186         break;
187       default:
188         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
189     }
190
191     if (simcall->issuer->host->isOff()) {
192       simcall->issuer->context->iwannadie = 1;
193     }
194
195     simcall->issuer->waiting_synchro = nullptr;
196     SIMIX_simcall_answer(simcall);
197   }
198
199   /* We no longer need it */
200   SIMIX_io_destroy(synchro);
201 }