Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
call sg_instr_new_host via signal call
[simgrid.git] / src / surf / StorageImpl.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 "StorageImpl.hpp"
8
9 #include "surf_private.h"
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 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(StorageImpl*)> storageCreatedCallbacks;
30 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
31 simgrid::xbt::signal<void(StorageImpl*, 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 {
45   lmm_system_free(maxminSystem_);
46   surf_storage_model = nullptr;
47 }
48
49 /************
50  * Resource *
51  ************/
52
53 StorageImpl::StorageImpl(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
54                          const char* type_id, const char* content_name, sg_size_t size, const char* attach)
55     : Resource(model, name, lmm_constraint_new(maxminSystem, this, MAX(bread, bwrite)))
56     , size_(size)
57     , usedSize_(0)
58     , typeId_(xbt_strdup(type_id))
59     , writeActions_(std::vector<StorageAction*>())
60 {
61   content_ = parseContent(content_name);
62   attach_  = xbt_strdup(attach);
63   turnOn();
64   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
65   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
66   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
67 }
68
69 StorageImpl::~StorageImpl()
70 {
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(attach_);
79 }
80
81 std::map<std::string, sg_size_t*>* StorageImpl::parseContent(const char* filename)
82 {
83   usedSize_ = 0;
84   if ((not filename) || (strcmp(filename, "") == 0))
85     return nullptr;
86
87   std::map<std::string, sg_size_t*>* parse_content = new std::map<std::string, sg_size_t*>();
88
89   std::ifstream* fs = surf_ifsopen(filename);
90
91   std::string line;
92   std::vector<std::string> tokens;
93   do {
94     std::getline(*fs, line);
95     boost::trim(line);
96     if (line.length() > 0) {
97       boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
98       xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename, line.c_str());
99       sg_size_t size = std::stoull(tokens.at(1));
100
101       usedSize_ += size;
102       sg_size_t* psize = new sg_size_t;
103       *psize           = size;
104       parse_content->insert({tokens.front(), psize});
105     }
106   } while (not fs->eof());
107   delete fs;
108   return parse_content;
109 }
110
111 bool StorageImpl::isUsed()
112 {
113   THROW_UNIMPLEMENTED;
114   return false;
115 }
116
117 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
118 {
119   THROW_UNIMPLEMENTED;
120 }
121
122 void StorageImpl::turnOn()
123 {
124   if (isOff()) {
125     Resource::turnOn();
126     storageStateChangedCallbacks(this, 0, 1);
127   }
128 }
129 void StorageImpl::turnOff()
130 {
131   if (isOn()) {
132     Resource::turnOff();
133     storageStateChangedCallbacks(this, 1, 0);
134   }
135 }
136
137 std::map<std::string, sg_size_t*>* StorageImpl::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 StorageImpl::getFreeSize()
144 {
145   return size_ - usedSize_;
146 }
147
148 sg_size_t StorageImpl::getUsedSize()
149 {
150   return usedSize_;
151 }
152
153 /**********
154  * Action *
155  **********/
156 StorageAction::StorageAction(Model* model, double cost, bool failed, StorageImpl* storage,
157                              e_surf_action_storage_type_t type)
158     : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
159 {
160   progress_ = 0;
161 };
162
163 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, StorageImpl* storage,
164                              e_surf_action_storage_type_t type)
165     : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
166 {
167   progress_ = 0;
168 }
169
170 void StorageAction::setState(Action::State state)
171 {
172   Action::State old = getState();
173   Action::setState(state);
174   storageActionStateChangedCallbacks(this, old, state);
175 }
176 }
177 }