Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics (+ sleep at the end of test to avoid problems)
[simgrid.git] / src / kernel / activity / CommImpl.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 "src/kernel/activity/CommImpl.hpp"
7
8 #include "simgrid/modelchecker.h"
9 #include "src/mc/mc_replay.h"
10 #include "src/simix/smx_network_private.h"
11 #include "src/surf/surf_interface.hpp"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_network);
14
15 simgrid::kernel::activity::CommImpl::CommImpl(e_smx_comm_type_t _type) : type(_type)
16 {
17   state    = SIMIX_WAITING;
18   src_data = nullptr;
19   dst_data = nullptr;
20   XBT_DEBUG("Create comm activity %p", this);
21 }
22
23 simgrid::kernel::activity::CommImpl::~CommImpl()
24 {
25   XBT_DEBUG("Really free communication %p", this);
26
27   cleanupSurf();
28
29   if (detached && state != SIMIX_DONE) {
30     /* the communication has failed and was detached:
31      * we have to free the buffer */
32     if (clean_fun)
33       clean_fun(src_buff);
34     src_buff = nullptr;
35   }
36
37   if (mbox)
38     mbox->remove(this);
39 }
40
41 void simgrid::kernel::activity::CommImpl::suspend()
42 {
43   /* FIXME: shall we suspend also the timeout synchro? */
44   if (surf_comm)
45     surf_comm->suspend();
46   /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
47 }
48
49 void simgrid::kernel::activity::CommImpl::resume()
50 {
51   /*FIXME: check what happen with the timeouts */
52   if (surf_comm)
53     surf_comm->resume();
54   /* in the other case, the synchro were not really suspended yet, see SIMIX_comm_suspend() and SIMIX_comm_start() */
55 }
56
57 void simgrid::kernel::activity::CommImpl::cancel()
58 {
59   /* if the synchro is a waiting state means that it is still in a mbox */
60   /* so remove from it and delete it */
61   if (state == SIMIX_WAITING) {
62     mbox->remove(this);
63     state = SIMIX_CANCELED;
64     this->unref();
65   } else if (not MC_is_active() /* when running the MC there are no surf actions */
66              && not MC_record_replay_is_active() && (state == SIMIX_READY || state == SIMIX_RUNNING)) {
67
68     surf_comm->cancel();
69   }
70 }
71
72 /**  @brief get the amount remaining from the communication */
73 double simgrid::kernel::activity::CommImpl::remains()
74 {
75   if (state == SIMIX_RUNNING)
76     return surf_comm->getRemains();
77
78   /* FIXME: check what should be returned in the other cases */
79   return 0;
80 }
81
82 /** @brief This is part of the cleanup process, probably an internal command */
83 void simgrid::kernel::activity::CommImpl::cleanupSurf()
84 {
85   if (surf_comm) {
86     surf_comm->unref();
87     surf_comm = nullptr;
88   }
89
90   if (src_timeout) {
91     src_timeout->unref();
92     src_timeout = nullptr;
93   }
94
95   if (dst_timeout) {
96     dst_timeout->unref();
97     dst_timeout = nullptr;
98   }
99 }
100
101 void simgrid::kernel::activity::CommImpl::post()
102 {
103   /* Update synchro state */
104   if (src_timeout && src_timeout->getState() == simgrid::surf::Action::State::done)
105     state = SIMIX_SRC_TIMEOUT;
106   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::done)
107     state = SIMIX_DST_TIMEOUT;
108   else if (src_timeout && src_timeout->getState() == simgrid::surf::Action::State::failed)
109     state = SIMIX_SRC_HOST_FAILURE;
110   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::failed)
111     state = SIMIX_DST_HOST_FAILURE;
112   else if (surf_comm && surf_comm->getState() == simgrid::surf::Action::State::failed) {
113     state = SIMIX_LINK_FAILURE;
114   } else
115     state = SIMIX_DONE;
116
117   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d", this, (int)state, src_proc,
118             dst_proc, detached);
119
120   /* destroy the surf actions associated with the Simix communication */
121   cleanupSurf();
122
123   /* if there are simcalls associated with the synchro, then answer them */
124   if (not simcalls.empty()) {
125     SIMIX_comm_finish(this);
126     this->unref();
127   }
128 }