Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #193 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 #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 SIMIX_file_get_size(smx_file_t fd)
125 {
126   return fd->surf_file->size();
127 }
128
129 sg_size_t SIMIX_file_tell(smx_file_t fd)
130 {
131   return fd->surf_file->tell();
132 }
133
134 int SIMIX_file_seek(smx_file_t fd, sg_offset_t offset, int origin)
135 {
136   return fd->surf_file->seek(offset, origin);
137 }
138
139 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
140 {
141   return SIMIX_file_move(simcall->issuer, file, fullpath);
142 }
143
144 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
145 {
146   sg_host_t host = process->host;
147   return  surf_host_file_move(host, file->surf_file, fullpath);
148 }
149
150 void SIMIX_io_destroy(smx_activity_t synchro)
151 {
152   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
153   XBT_DEBUG("Destroy synchro %p", synchro.get());
154   if (io->surf_io)
155     io->surf_io->unref();
156 }
157
158 void SIMIX_io_finish(smx_activity_t synchro)
159 {
160   for (smx_simcall_t simcall : synchro->simcalls) {
161     switch (synchro->state) {
162       case SIMIX_DONE:
163         /* do nothing, synchro done */
164         break;
165       case SIMIX_FAILED:
166         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
167         break;
168       case SIMIX_CANCELED:
169         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
170         break;
171       default:
172         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
173     }
174
175     if (simcall->issuer->host->isOff()) {
176       simcall->issuer->context->iwannadie = 1;
177     }
178
179     simcall->issuer->waiting_synchro = nullptr;
180     SIMIX_simcall_answer(simcall);
181   }
182
183   /* We no longer need it */
184   SIMIX_io_destroy(synchro);
185 }