Logo AND Algorithmique Numérique Distribuée

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