1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "storage_n11.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "xbt/utility.hpp"
12 #include <cmath> /*ceil*/
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
19 extern std::map<std::string, simgrid::surf::StorageType*> storage_types;
21 static void check_disk_attachment()
23 for (auto const& s : simgrid::s4u::Engine::getInstance()->getAllStorages()) {
24 simgrid::kernel::routing::NetPoint* host_elm = sg_netpoint_by_name_or_null(s->getImpl()->getHost().c_str());
26 surf_parse_error(std::string("Unable to attach storage ") + s->getCname() + ": host " +
27 s->getImpl()->getHost().c_str() + " does not exist.");
29 s->attached_to_ = sg_host_by_name(s->getImpl()->getHost().c_str());
33 void storage_register_callbacks()
35 simgrid::s4u::onPlatformCreated.connect(check_disk_attachment);
36 instr_routing_define_callbacks();
43 void surf_storage_model_init_default()
45 surf_storage_model = new simgrid::surf::StorageN11Model();
46 all_existing_models->push_back(surf_storage_model);
52 StorageImpl* StorageN11Model::createStorage(std::string id, std::string type_id, std::string content_name,
55 StorageType* storage_type = storage_types.at(type_id);
57 double Bread = surf_parse_get_bandwidth(storage_type->model_properties->at("Bread").c_str(),
58 "property Bread, storage", type_id.c_str());
59 double Bwrite = surf_parse_get_bandwidth(storage_type->model_properties->at("Bwrite").c_str(),
60 "property Bwrite, storage", type_id.c_str());
62 StorageImpl* storage =
63 new StorageN11(this, id, maxminSystem_, Bread, Bwrite, type_id, content_name, storage_type->size, attach);
64 storageCreatedCallbacks(storage);
66 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tBread '%f'\n", id.c_str(), type_id.c_str(),
72 double StorageN11Model::nextOccuringEvent(double now)
74 return StorageModel::nextOccuringEventFull(now);
77 void StorageN11Model::updateActionsState(double /*now*/, double delta)
79 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
80 StorageAction& action = static_cast<StorageAction&>(*it);
81 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
82 action.updateRemains(lrint(action.getVariable()->get_value() * delta));
84 if (action.getMaxDuration() > NO_MAX_DURATION)
85 action.updateMaxDuration(delta);
87 if (((action.getRemainsNoUpdate() <= 0) && (action.getVariable()->get_weight() > 0)) ||
88 ((action.getMaxDuration() > NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
89 action.finish(kernel::resource::Action::State::done);
98 StorageN11::StorageN11(StorageModel* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
99 std::string type_id, std::string content_name, sg_size_t size, std::string attach)
100 : StorageImpl(model, name, maxminSystem, bread, bwrite, type_id, content_name, size, attach)
102 XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
103 simgrid::s4u::Storage::onCreation(this->piface_);
106 StorageAction* StorageN11::read(sg_size_t size)
108 return new StorageN11Action(model(), size, isOff(), this, READ);
111 StorageAction* StorageN11::write(sg_size_t size)
113 return new StorageN11Action(model(), size, isOff(), this, WRITE);
120 StorageN11Action::StorageN11Action(kernel::resource::Model* model, double cost, bool failed, StorageImpl* storage,
121 e_surf_action_storage_type_t type)
122 : StorageAction(model, cost, failed, model->getMaxminSystem()->variable_new(this, 1.0, -1.0, 3), storage, type)
124 XBT_IN("(%s,%g", storage->getCname(), cost);
126 // Must be less than the max bandwidth for all actions
127 model->getMaxminSystem()->expand(storage->constraint(), getVariable(), 1.0);
130 model->getMaxminSystem()->expand(storage->constraintRead_, getVariable(), 1.0);
133 model->getMaxminSystem()->expand(storage->constraintWrite_, getVariable(), 1.0);
141 int StorageN11Action::unref()
145 if (stateSetHook_.is_linked())
146 simgrid::xbt::intrusive_erase(*stateSet_, *this);
148 getModel()->getMaxminSystem()->variable_free(getVariable());
149 xbt_free(getCategory());
156 void StorageN11Action::cancel()
158 setState(Action::State::failed);
161 void StorageN11Action::suspend()
163 XBT_IN("(%p)", this);
164 if (suspended_ != Action::SuspendStates::sleeping) {
165 getModel()->getMaxminSystem()->update_variable_weight(getVariable(), 0.0);
166 suspended_ = Action::SuspendStates::suspended;
171 void StorageN11Action::resume()
176 bool StorageN11Action::isSuspended()
178 return suspended_ == Action::SuspendStates::suspended;
181 void StorageN11Action::setMaxDuration(double /*duration*/)
186 void StorageN11Action::setSharingWeight(double /*priority*/)