Logo AND Algorithmique Numérique Distribuée

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