Logo AND Algorithmique Numérique Distribuée

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