Logo AND Algorithmique Numérique Distribuée

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