Logo AND Algorithmique Numérique Distribuée

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