Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9d6ccb2e06adb03aa8af5f8228c2c7a47241cac8
[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
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/s4u/Storage.hpp"
12 #include "src/surf/FileImpl.hpp"
13 #include "src/surf/HostImpl.hpp"
14 #include "src/surf/StorageImpl.hpp"
15 #include "surf/surf.h"
16
17 #include "src/surf/surf_interface.hpp"
18 #include "smx_private.h"
19
20 #include "src/kernel/activity/SynchroIo.hpp"
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
23
24 //SIMIX FILE READ
25 void simcall_HANDLER_file_read(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
26 {
27   smx_activity_t synchro = SIMIX_file_read(fd, size, host);
28   synchro->simcalls.push_back(simcall);
29   simcall->issuer->waiting_synchro = synchro;
30 }
31
32 smx_activity_t SIMIX_file_read(surf_file_t file, sg_size_t size, sg_host_t host)
33 {
34   /* check if the host is active */
35   if (host->isOff())
36     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
37
38   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
39   synchro->surf_io                           = file->read(size);
40
41   synchro->surf_io->setData(synchro);
42   XBT_DEBUG("Create io synchro %p", synchro);
43
44   return synchro;
45 }
46
47 //SIMIX FILE WRITE
48 void simcall_HANDLER_file_write(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
49 {
50   smx_activity_t synchro = SIMIX_file_write(fd,  size, host);
51   synchro->simcalls.push_back(simcall);
52   simcall->issuer->waiting_synchro = synchro;
53 }
54
55 smx_activity_t SIMIX_file_write(surf_file_t file, sg_size_t size, sg_host_t host)
56 {
57   if (host->isOff())
58     THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
59
60   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
61   synchro->surf_io                           = file->write(size);
62   synchro->surf_io->setData(synchro);
63   XBT_DEBUG("Create io synchro %p", synchro);
64
65   return synchro;
66 }
67
68 void SIMIX_io_destroy(smx_activity_t synchro)
69 {
70   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
71   XBT_DEBUG("Destroy synchro %p", synchro.get());
72   if (io->surf_io)
73     io->surf_io->unref();
74 }
75
76 void SIMIX_io_finish(smx_activity_t synchro)
77 {
78   for (smx_simcall_t simcall : synchro->simcalls) {
79     switch (synchro->state) {
80       case SIMIX_DONE:
81         /* do nothing, synchro done */
82         break;
83       case SIMIX_FAILED:
84         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
85         break;
86       case SIMIX_CANCELED:
87         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
88         break;
89       default:
90         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
91     }
92
93     if (simcall->issuer->host->isOff()) {
94       simcall->issuer->context->iwannadie = 1;
95     }
96
97     simcall->issuer->waiting_synchro = nullptr;
98     SIMIX_simcall_answer(simcall);
99   }
100
101   /* We no longer need it */
102   SIMIX_io_destroy(synchro);
103 }