Logo AND Algorithmique Numérique Distribuée

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