Logo AND Algorithmique Numérique Distribuée

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