Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #167 from simgrid/smpi_execute_public
[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, const char* content_type,
54                  sg_size_t size, const char* attach)
55     : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
56     , contentType_(xbt_strdup(content_type))
57     , size_(size)
58     , usedSize_(0)
59     , typeId_(xbt_strdup(type_id))
60     , writeActions_(std::vector<StorageAction*>())
61 {
62   content_ = parseContent(content_name);
63   attach_  = xbt_strdup(attach);
64   turnOn();
65   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
66   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
67   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
68 }
69
70 Storage::~Storage(){
71   storageDestructedCallbacks(this);
72   if (content_ != nullptr) {
73     for (auto entry : *content_)
74       delete entry.second;
75     delete content_;
76   }
77   free(typeId_);
78   free(contentType_);
79   free(attach_);
80 }
81
82 std::map<std::string, sg_size_t*>* Storage::parseContent(const char* filename)
83 {
84   usedSize_ = 0;
85   if ((!filename) || (strcmp(filename, "") == 0))
86     return nullptr;
87
88   std::map<std::string, sg_size_t*>* parse_content = new std::map<std::string, sg_size_t*>();
89
90   std::ifstream* fs = surf_ifsopen(filename);
91
92   std::string line;
93   std::vector<std::string> tokens;
94   do {
95     std::getline(*fs, line);
96     boost::trim(line);
97     if (line.length() > 0) {
98       boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
99       xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename, line.c_str());
100       sg_size_t size = std::stoull(tokens.at(1));
101
102       usedSize_ += size;
103       sg_size_t* psize = new sg_size_t;
104       *psize = size;
105       parse_content->insert({tokens.front(), psize});
106     }
107   } while (!fs->eof());
108   delete fs;
109   return parse_content;
110 }
111
112 bool Storage::isUsed()
113 {
114   THROW_UNIMPLEMENTED;
115   return false;
116 }
117
118 void Storage::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
119 {
120   THROW_UNIMPLEMENTED;
121 }
122
123 void Storage::turnOn() {
124   if (isOff()) {
125     Resource::turnOn();
126     storageStateChangedCallbacks(this, 0, 1);
127   }
128 }
129 void Storage::turnOff() {
130   if (isOn()) {
131     Resource::turnOff();
132     storageStateChangedCallbacks(this, 1, 0);
133   }
134 }
135
136 std::map<std::string, sg_size_t*>* Storage::getContent()
137 {
138   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
139   return content_;
140 }
141
142 sg_size_t Storage::getFreeSize(){
143   return size_ - usedSize_;
144 }
145
146 sg_size_t Storage::getUsedSize(){
147   return usedSize_;
148 }
149
150 /**********
151  * Action *
152  **********/
153 StorageAction::StorageAction(Model* model, double cost, bool failed, Storage* storage,
154                              e_surf_action_storage_type_t type)
155     : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
156 {
157   progress_ = 0;
158 };
159
160 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, Storage* storage,
161                              e_surf_action_storage_type_t type)
162     : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
163 {
164   progress_ = 0;
165 }
166
167 void StorageAction::setState(Action::State state){
168   Action::State old = getState();
169   Action::setState(state);
170   storageActionStateChangedCallbacks(this, old, state);
171 }
172
173 }
174 }