Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug leaks
[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 simgrid::surf::StorageModel* surf_storage_model = nullptr;
18
19 namespace simgrid {
20 namespace surf {
21
22 /*************
23  * Callbacks *
24  *************/
25
26 simgrid::xbt::signal<void(StorageImpl*)> storageCreatedCallbacks;
27 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
28 simgrid::xbt::signal<void(StorageImpl*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
29 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
30
31 /* List of storages */
32 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
33     new std::unordered_map<std::string, StorageImpl*>();
34
35 StorageImpl* StorageImpl::byName(const char* name)
36 {
37   if (storages->find(name) == storages->end())
38     return nullptr;
39   return storages->at(name);
40 }
41
42 /*********
43  * Model *
44  *********/
45
46 StorageModel::StorageModel() : Model()
47 {
48   maxminSystem_ = lmm_system_new(true /* lazy update */);
49 }
50
51 StorageModel::~StorageModel()
52 {
53   lmm_system_free(maxminSystem_);
54   surf_storage_model = nullptr;
55 }
56
57 /************
58  * Resource *
59  ************/
60
61 StorageImpl::StorageImpl(Model* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
62                          std::string type_id, std::string content_name, sg_size_t size, std::string attach)
63     : Resource(model, name.c_str(), lmm_constraint_new(maxminSystem, this, MAX(bread, bwrite)))
64     , piface_(this)
65     , typeId_(type_id)
66     , size_(size)
67     , attach_(attach)
68 {
69   content_ = parseContent(content_name);
70   turnOn();
71   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
72   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
73   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
74   storages->insert({name, this});
75 }
76
77 StorageImpl::~StorageImpl()
78 {
79   storageDestructedCallbacks(this);
80   if (content_ != nullptr)
81     delete content_;
82 }
83
84 std::map<std::string, sg_size_t>* StorageImpl::parseContent(std::string filename)
85 {
86   usedSize_ = 0;
87   if (filename.empty())
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.c_str());
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.c_str(), line.c_str());
102       sg_size_t size = std::stoull(tokens.at(1));
103
104       usedSize_ += size;
105       parse_content->insert({tokens.front(), size});
106     }
107   } while (not fs->eof());
108   delete fs;
109   return parse_content;
110 }
111
112 bool StorageImpl::isUsed()
113 {
114   THROW_UNIMPLEMENTED;
115   return false;
116 }
117
118 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
119 {
120   THROW_UNIMPLEMENTED;
121 }
122
123 void StorageImpl::turnOn()
124 {
125   if (isOff()) {
126     Resource::turnOn();
127     storageStateChangedCallbacks(this, 0, 1);
128   }
129 }
130 void StorageImpl::turnOff()
131 {
132   if (isOn()) {
133     Resource::turnOff();
134     storageStateChangedCallbacks(this, 1, 0);
135   }
136 }
137
138 std::map<std::string, sg_size_t>* StorageImpl::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 StorageImpl::getFreeSize()
145 {
146   return size_ - usedSize_;
147 }
148
149 sg_size_t StorageImpl::getUsedSize()
150 {
151   return usedSize_;
152 }
153
154 /**********
155  * Action *
156  **********/
157 void StorageAction::setState(Action::State state)
158 {
159   Action::State old = getState();
160   Action::setState(state);
161   storageActionStateChangedCallbacks(this, old, state);
162 }
163 }
164 }