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 "storage_interface.hpp"
8 #include "surf_private.h"
9 #include "xbt/file.h" /* xbt_getline */
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 xbt_lib_t storage_lib;
18 int MSG_STORAGE_LEVEL = -1; // Msg storage level
19 int ROUTING_STORAGE_LEVEL = -1; // Routing for storage level
20 int SURF_STORAGE_LEVEL = -1;
21 simgrid::surf::StorageModel *surf_storage_model = nullptr;
30 simgrid::xbt::signal<void(Storage*)> storageCreatedCallbacks;
31 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
32 simgrid::xbt::signal<void(Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
33 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
39 StorageModel::StorageModel(): Model()
41 maxminSystem_ = lmm_system_new(true /* lazy update */);
44 StorageModel::~StorageModel(){
45 lmm_system_free(maxminSystem_);
46 surf_storage_model = nullptr;
53 Storage::Storage(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
54 double bconnection, const char* type_id, const char* content_name, const char* content_type,
55 sg_size_t size, const char* attach)
56 : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
57 , contentType_(xbt_strdup(content_type))
60 , typeId_(xbt_strdup(type_id))
61 , writeActions_(std::vector<StorageAction*>())
63 content_ = parseContent(content_name);
64 attach_ = xbt_strdup(attach);
66 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
67 constraintRead_ = lmm_constraint_new(maxminSystem, this, bread);
68 constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
72 storageDestructedCallbacks(this);
73 if (content_ != nullptr) {
74 for (auto entry : *content_)
83 std::map<std::string, sg_size_t*>* Storage::parseContent(const char* filename)
86 if ((!filename) || (strcmp(filename, "") == 0))
89 std::map<std::string, sg_size_t*>* parse_content = new std::map<std::string, sg_size_t*>();
91 std::ifstream* fs = surf_ifsopen(filename);
94 std::vector<std::string> tokens;
96 std::getline(*fs, line);
98 if (line.length() > 0) {
99 boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
100 xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename, line.c_str());
101 sg_size_t size = std::stoull(tokens.at(1));
104 sg_size_t* psize = new sg_size_t;
106 parse_content->insert({tokens.front(), psize});
108 } while (!fs->eof());
110 return parse_content;
113 bool Storage::isUsed()
119 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
124 void Storage::turnOn() {
127 storageStateChangedCallbacks(this, 0, 1);
130 void Storage::turnOff() {
133 storageStateChangedCallbacks(this, 1, 0);
137 std::map<std::string, sg_size_t*>* Storage::getContent()
139 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
143 sg_size_t Storage::getFreeSize(){
144 return size_ - usedSize_;
147 sg_size_t Storage::getUsedSize(){
154 StorageAction::StorageAction(Model* model, double cost, bool failed, Storage* storage,
155 e_surf_action_storage_type_t type)
156 : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
161 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, Storage* storage,
162 e_surf_action_storage_type_t type)
163 : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
168 void StorageAction::setState(Action::State state){
169 Action::State old = getState();
170 Action::setState(state);
171 storageActionStateChangedCallbacks(this, old, state);