Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d15ab37d50a6d3da4d4f8f9ae3b094c362b59553
[simgrid.git] / src / surf / StorageImpl.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "StorageImpl.hpp"
8
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>
13 #include <fstream>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
16
17 xbt_lib_t storage_lib;
18 int MSG_STORAGE_LEVEL                           = -1; // Msg storage level
19 int SURF_STORAGE_LEVEL                          = -1;
20 simgrid::surf::StorageModel* surf_storage_model = nullptr;
21
22 namespace simgrid {
23 namespace surf {
24
25 /*************
26  * Callbacks *
27  *************/
28
29 simgrid::xbt::signal<void(StorageImpl*)> storageCreatedCallbacks;
30 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
31 simgrid::xbt::signal<void(StorageImpl*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
32 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
33
34 /* List of storages */
35 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
36     new std::unordered_map<std::string, StorageImpl*>();
37
38 StorageImpl* StorageImpl::byName(const char* name)
39 {
40   if (storages->find(name) == storages->end())
41     return nullptr;
42   return storages->at(name);
43 }
44
45 /*********
46  * Model *
47  *********/
48
49 StorageModel::StorageModel() : Model()
50 {
51   maxminSystem_ = lmm_system_new(true /* lazy update */);
52 }
53
54 StorageModel::~StorageModel()
55 {
56   lmm_system_free(maxminSystem_);
57   surf_storage_model = nullptr;
58 }
59
60 /************
61  * Resource *
62  ************/
63
64 StorageImpl::StorageImpl(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
65                          const char* type_id, const char* content_name, sg_size_t size, const char* attach)
66     : Resource(model, name, lmm_constraint_new(maxminSystem, this, MAX(bread, bwrite)))
67     , piface_(this)
68     , size_(size)
69     , usedSize_(0)
70     , typeId_(xbt_strdup(type_id))
71     , writeActions_(std::vector<StorageAction*>())
72 {
73   content_ = parseContent(content_name);
74   attach_  = xbt_strdup(attach);
75   turnOn();
76   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
77   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
78   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
79   storages->insert({name, this});
80 }
81
82 StorageImpl::~StorageImpl()
83 {
84   storageDestructedCallbacks(this);
85   if (content_ != nullptr) {
86     for (auto entry : *content_)
87       delete entry.second;
88     delete content_;
89   }
90   free(typeId_);
91   free(attach_);
92 }
93
94 std::map<std::string, sg_size_t*>* StorageImpl::parseContent(const char* filename)
95 {
96   usedSize_ = 0;
97   if ((not filename) || (strcmp(filename, "") == 0))
98     return nullptr;
99
100   std::map<std::string, sg_size_t*>* parse_content = new std::map<std::string, sg_size_t*>();
101
102   std::ifstream* fs = surf_ifsopen(filename);
103
104   std::string line;
105   std::vector<std::string> tokens;
106   do {
107     std::getline(*fs, line);
108     boost::trim(line);
109     if (line.length() > 0) {
110       boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
111       xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename, line.c_str());
112       sg_size_t size = std::stoull(tokens.at(1));
113
114       usedSize_ += size;
115       sg_size_t* psize = new sg_size_t;
116       *psize           = size;
117       parse_content->insert({tokens.front(), psize});
118     }
119   } while (not fs->eof());
120   delete fs;
121   return parse_content;
122 }
123
124 bool StorageImpl::isUsed()
125 {
126   THROW_UNIMPLEMENTED;
127   return false;
128 }
129
130 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
131 {
132   THROW_UNIMPLEMENTED;
133 }
134
135 void StorageImpl::turnOn()
136 {
137   if (isOff()) {
138     Resource::turnOn();
139     storageStateChangedCallbacks(this, 0, 1);
140   }
141 }
142 void StorageImpl::turnOff()
143 {
144   if (isOn()) {
145     Resource::turnOff();
146     storageStateChangedCallbacks(this, 1, 0);
147   }
148 }
149
150 std::map<std::string, sg_size_t*>* StorageImpl::getContent()
151 {
152   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
153   return content_;
154 }
155
156 sg_size_t StorageImpl::getFreeSize()
157 {
158   return size_ - usedSize_;
159 }
160
161 sg_size_t StorageImpl::getUsedSize()
162 {
163   return usedSize_;
164 }
165
166 /**********
167  * Action *
168  **********/
169 StorageAction::StorageAction(Model* model, double cost, bool failed, StorageImpl* storage,
170                              e_surf_action_storage_type_t type)
171     : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
172 {
173   progress_ = 0;
174 };
175
176 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, StorageImpl* storage,
177                              e_surf_action_storage_type_t type)
178     : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
179 {
180   progress_ = 0;
181 }
182
183 void StorageAction::setState(Action::State state)
184 {
185   Action::State old = getState();
186   Action::setState(state);
187   storageActionStateChangedCallbacks(this, old, state);
188 }
189 }
190 }