Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[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 #include "surf/maxmin.hpp"
9 #include "surf_private.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
12
13 simgrid::surf::StorageModel* surf_storage_model = nullptr;
14
15 namespace simgrid {
16 namespace surf {
17
18 /*************
19  * Callbacks *
20  *************/
21
22 simgrid::xbt::signal<void(StorageImpl*)> storageCreatedCallbacks;
23 simgrid::xbt::signal<void(StorageImpl*)> storageDestructedCallbacks;
24 simgrid::xbt::signal<void(StorageImpl*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
25 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
26
27 /* List of storages */
28 std::unordered_map<std::string, StorageImpl*>* StorageImpl::storages =
29     new std::unordered_map<std::string, StorageImpl*>();
30
31 StorageImpl* StorageImpl::byName(std::string name)
32 {
33   if (storages->find(name) == storages->end())
34     return nullptr;
35   return storages->at(name);
36 }
37
38 /*********
39  * Model *
40  *********/
41
42 StorageModel::StorageModel() : Model()
43 {
44   maxminSystem_ = new s_lmm_system_t(true /* lazy update */);
45 }
46
47 StorageModel::~StorageModel()
48 {
49   delete maxminSystem_;
50   surf_storage_model = nullptr;
51 }
52
53 /************
54  * Resource *
55  ************/
56
57 StorageImpl::StorageImpl(Model* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
58                          std::string type_id, std::string content_name, sg_size_t size, std::string attach)
59     : Resource(model, name.c_str(), maxminSystem->constraint_new(this, std::max(bread, bwrite)))
60     , piface_(this)
61     , typeId_(type_id)
62     , content_name(content_name)
63     , size_(size)
64     , attach_(attach)
65 {
66   StorageImpl::turnOn();
67   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
68   constraintRead_  = maxminSystem->constraint_new(this, bread);
69   constraintWrite_ = maxminSystem->constraint_new(this, bwrite);
70   storages->insert({name, this});
71 }
72
73 StorageImpl::~StorageImpl()
74 {
75   storageDestructedCallbacks(this);
76 }
77
78
79 bool StorageImpl::isUsed()
80 {
81   THROW_UNIMPLEMENTED;
82   return false;
83 }
84
85 void StorageImpl::apply_event(tmgr_trace_event_t /*event*/, double /*value*/)
86 {
87   THROW_UNIMPLEMENTED;
88 }
89
90 void StorageImpl::turnOn()
91 {
92   if (isOff()) {
93     Resource::turnOn();
94     storageStateChangedCallbacks(this, 0, 1);
95   }
96 }
97 void StorageImpl::turnOff()
98 {
99   if (isOn()) {
100     Resource::turnOff();
101     storageStateChangedCallbacks(this, 1, 0);
102   }
103 }
104
105 /**********
106  * Action *
107  **********/
108 void StorageAction::setState(Action::State state)
109 {
110   Action::State old = getState();
111   Action::setState(state);
112   storageActionStateChangedCallbacks(this, old, state);
113 }
114 }
115 }