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.hpp"
11 #include <boost/algorithm/string.hpp>
12 #include <boost/algorithm/string/join.hpp>
13 #include <boost/algorithm/string/split.hpp>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
18 simgrid::surf::StorageModel* surf_storage_model = nullptr;
27 simgrid::xbt::signal<void(StorageImpl*)> storageCreatedCallbacks;
28 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
29 simgrid::xbt::signal<void(StorageImpl*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
30 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
32 /* List of storages */
33 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
34 new std::unordered_map<std::string, StorageImpl*>();
36 StorageImpl* StorageImpl::byName(std::string name)
38 if (storages->find(name) == storages->end())
40 return storages->at(name);
47 StorageModel::StorageModel() : Model()
49 maxminSystem_ = lmm_system_new(true /* lazy update */);
52 StorageModel::~StorageModel()
54 lmm_system_free(maxminSystem_);
55 surf_storage_model = nullptr;
62 StorageImpl::StorageImpl(Model* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
63 std::string type_id, std::string content_name, sg_size_t size, std::string attach)
64 : Resource(model, name.c_str(), lmm_constraint_new(maxminSystem, this, std::max(bread, bwrite)))
70 content_ = parseContent(content_name);
71 StorageImpl::turnOn();
72 XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
73 constraintRead_ = lmm_constraint_new(maxminSystem, this, bread);
74 constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
75 storages->insert({name, this});
78 StorageImpl::~StorageImpl()
80 storageDestructedCallbacks(this);
81 if (content_ != nullptr)
85 std::map<std::string, sg_size_t>* StorageImpl::parseContent(std::string filename)
91 std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
93 std::ifstream* fs = surf_ifsopen(filename);
96 std::vector<std::string> tokens;
98 std::getline(*fs, line);
100 if (line.length() > 0) {
101 boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
102 xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename.c_str(), line.c_str());
103 sg_size_t size = std::stoull(tokens.at(1));
106 parse_content->insert({tokens.front(), size});
108 } while (not fs->eof());
110 return parse_content;
113 bool StorageImpl::isUsed()
119 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
124 void StorageImpl::turnOn()
128 storageStateChangedCallbacks(this, 0, 1);
131 void StorageImpl::turnOff()
135 storageStateChangedCallbacks(this, 1, 0);
139 std::map<std::string, sg_size_t>* StorageImpl::getContent()
141 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
145 sg_size_t StorageImpl::getFreeSize()
147 return size_ - usedSize_;
150 sg_size_t StorageImpl::getUsedSize()
158 void StorageAction::setState(Action::State state)
160 Action::State old = getState();
161 Action::setState(state);
162 storageActionStateChangedCallbacks(this, old, state);