1 /* Copyright (c) 2013-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "StorageImpl.hpp"
9 #include "surf_private.h"
10 #include <boost/algorithm/string.hpp>
11 #include <boost/algorithm/string/join.hpp>
12 #include <boost/algorithm/string/split.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
17 simgrid::surf::StorageModel* surf_storage_model = nullptr;
26 simgrid::xbt::signal<void(StorageImpl*)> storageCreatedCallbacks;
27 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
28 simgrid::xbt::signal<void(StorageImpl*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
29 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
31 /* List of storages */
32 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
33 new std::unordered_map<std::string, StorageImpl*>();
35 StorageImpl* StorageImpl::byName(const char* name)
37 if (storages->find(name) == storages->end())
39 return storages->at(name);
46 StorageModel::StorageModel() : Model()
48 maxminSystem_ = lmm_system_new(true /* lazy update */);
51 StorageModel::~StorageModel()
53 lmm_system_free(maxminSystem_);
54 surf_storage_model = nullptr;
61 StorageImpl::StorageImpl(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
62 const char* type_id, const char* content_name, sg_size_t size, const char* attach)
63 : Resource(model, name, lmm_constraint_new(maxminSystem, this, MAX(bread, bwrite)))
69 content_ = parseContent(content_name);
71 XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
72 constraintRead_ = lmm_constraint_new(maxminSystem, this, bread);
73 constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
74 storages->insert({name, this});
77 StorageImpl::~StorageImpl()
79 storageDestructedCallbacks(this);
80 if (content_ != nullptr)
84 std::map<std::string, sg_size_t>* StorageImpl::parseContent(const char* filename)
87 if ((not filename) || (strcmp(filename, "") == 0))
90 std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
92 std::ifstream* fs = surf_ifsopen(filename);
95 std::vector<std::string> tokens;
97 std::getline(*fs, line);
99 if (line.length() > 0) {
100 boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
101 xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename, line.c_str());
102 sg_size_t size = std::stoull(tokens.at(1));
105 parse_content->insert({tokens.front(), size});
107 } while (not fs->eof());
109 return parse_content;
112 bool StorageImpl::isUsed()
118 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
123 void StorageImpl::turnOn()
127 storageStateChangedCallbacks(this, 0, 1);
130 void StorageImpl::turnOff()
134 storageStateChangedCallbacks(this, 1, 0);
138 std::map<std::string, sg_size_t>* StorageImpl::getContent()
140 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
144 sg_size_t StorageImpl::getFreeSize()
146 return size_ - usedSize_;
149 sg_size_t StorageImpl::getUsedSize()
157 StorageAction::StorageAction(Model* model, double cost, bool failed, StorageImpl* storage,
158 e_surf_action_storage_type_t type)
159 : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
164 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, StorageImpl* storage,
165 e_surf_action_storage_type_t type)
166 : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
171 void StorageAction::setState(Action::State state)
173 Action::State old = getState();
174 Action::setState(state);
175 storageActionStateChangedCallbacks(this, old, state);