Logo AND Algorithmique Numérique Distribuée

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