Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
glitch
[simgrid.git] / src / kernel / activity / IoImpl.cpp
index 5daae90..f2c5bdc 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -26,6 +26,11 @@ namespace activity {
 IoImpl::IoImpl()
 {
   piface_ = new s4u::Io(this);
+  actor::ActorImpl* self = actor::ActorImpl::self();
+  if (self) {
+    set_actor(self);
+    self->activities_.emplace_back(this);
+  }
 }
 
 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
@@ -69,11 +74,12 @@ IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
 
 IoImpl* IoImpl::start()
 {
-  state_ = State::RUNNING;
+  set_state(State::RUNNING);
   surf_action_ =
       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
   surf_action_->set_sharing_penalty(sharing_penalty_);
   surf_action_->set_activity(this);
+  set_start_time(surf_action_->get_start_time());
 
   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
 
@@ -85,18 +91,18 @@ void IoImpl::post()
   performed_ioops_ = surf_action_->get_cost();
   if (surf_action_->get_state() == resource::Action::State::FAILED) {
     if (disk_ && not disk_->is_on())
-      state_ = State::FAILED;
+      set_state(State::FAILED);
     else
-      state_ = State::CANCELED;
+      set_state(State::CANCELED);
   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
     if (surf_action_->get_remains() > 0.0) {
       surf_action_->set_state(resource::Action::State::FAILED);
-      state_ = State::TIMEOUT;
+      set_state(State::TIMEOUT);
     } else {
-      state_ = State::DONE;
+      set_state(State::DONE);
     }
   } else {
-    state_ = State::DONE;
+    set_state(State::DONE);
   }
 
   clean_action();
@@ -108,10 +114,29 @@ void IoImpl::post()
   /* Answer all simcalls associated with the synchro */
   finish();
 }
+void IoImpl::set_exception(actor::ActorImpl* issuer)
+{
+  switch (get_state()) {
+    case State::FAILED:
+      issuer->context_->set_wannadie();
+      static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
+      issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
+      break;
+    case State::CANCELED:
+      issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
+      break;
+    case State::TIMEOUT:
+      issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
+      break;
+    default:
+      xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
+                 get_state_str());
+  }
+}
 
 void IoImpl::finish()
 {
-  XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
+  XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
   while (not simcalls_.empty()) {
     smx_simcall_t simcall = simcalls_.front();
     simcalls_.pop_front();
@@ -141,23 +166,7 @@ void IoImpl::finish()
       }
     }
 
-    switch (state_) {
-      case State::FAILED:
-        simcall->issuer_->context_->set_wannadie();
-        piface_->complete(s4u::Activity::State::FAILED);
-        simcall->issuer_->exception_ =
-            std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
-        break;
-      case State::CANCELED:
-        simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
-        break;
-      case State::TIMEOUT:
-        simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
-        break;
-      default:
-        xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
-                   to_c_str(state_));
-    }
+    set_exception(simcall->issuer_);
 
     simcall->issuer_->waiting_synchro_ = nullptr;
     simcall->issuer_->simcall_answer();
@@ -183,7 +192,7 @@ void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>&
     io->simcalls_.push_back(&issuer->simcall_);
 
     /* see if the synchro is already finished */
-    if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
+    if (io->get_state() != State::WAITING && io->get_state() != State::RUNNING) {
       io->finish();
       break;
     }