Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sonar: constructors should only call non-overridable methods.
[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.hpp"
10 #include <algorithm>
11 #include <boost/algorithm/string.hpp>
12 #include <boost/algorithm/string/join.hpp>
13 #include <boost/algorithm/string/split.hpp>
14 #include <fstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
17
18 simgrid::surf::StorageModel* surf_storage_model = nullptr;
19
20 namespace simgrid {
21 namespace surf {
22
23 /*************
24  * Callbacks *
25  *************/
26
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;
31
32 /* List of storages */
33 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
34     new std::unordered_map<std::string, StorageImpl*>();
35
36 StorageImpl* StorageImpl::byName(std::string name)
37 {
38   if (storages->find(name) == storages->end())
39     return nullptr;
40   return storages->at(name);
41 }
42
43 /*********
44  * Model *
45  *********/
46
47 StorageModel::StorageModel() : Model()
48 {
49   maxminSystem_ = lmm_system_new(true /* lazy update */);
50 }
51
52 StorageModel::~StorageModel()
53 {
54   lmm_system_free(maxminSystem_);
55   surf_storage_model = nullptr;
56 }
57
58 /************
59  * Resource *
60  ************/
61
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)))
65     , piface_(this)
66     , typeId_(type_id)
67     , size_(size)
68     , attach_(attach)
69 {
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});
76 }
77
78 StorageImpl::~StorageImpl()
79 {
80   storageDestructedCallbacks(this);
81   if (content_ != nullptr)
82     delete content_;
83 }
84
85 std::map<std::string, sg_size_t>* StorageImpl::parseContent(std::string filename)
86 {
87   usedSize_ = 0;
88   if (filename.empty())
89     return nullptr;
90
91   std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
92
93   std::ifstream* fs = surf_ifsopen(filename);
94
95   std::string line;
96   std::vector<std::string> tokens;
97   do {
98     std::getline(*fs, line);
99     boost::trim(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));
104
105       usedSize_ += size;
106       parse_content->insert({tokens.front(), size});
107     }
108   } while (not fs->eof());
109   delete fs;
110   return parse_content;
111 }
112
113 bool StorageImpl::isUsed()
114 {
115   THROW_UNIMPLEMENTED;
116   return false;
117 }
118
119 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
120 {
121   THROW_UNIMPLEMENTED;
122 }
123
124 void StorageImpl::turnOn()
125 {
126   if (isOff()) {
127     Resource::turnOn();
128     storageStateChangedCallbacks(this, 0, 1);
129   }
130 }
131 void StorageImpl::turnOff()
132 {
133   if (isOn()) {
134     Resource::turnOff();
135     storageStateChangedCallbacks(this, 1, 0);
136   }
137 }
138
139 std::map<std::string, sg_size_t>* StorageImpl::getContent()
140 {
141   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
142   return content_;
143 }
144
145 sg_size_t StorageImpl::getFreeSize()
146 {
147   return size_ - usedSize_;
148 }
149
150 sg_size_t StorageImpl::getUsedSize()
151 {
152   return usedSize_;
153 }
154
155 /**********
156  * Action *
157  **********/
158 void StorageAction::setState(Action::State state)
159 {
160   Action::State old = getState();
161   Action::setState(state);
162   storageActionStateChangedCallbacks(this, old, state);
163 }
164 }
165 }