Logo AND Algorithmique Numérique Distribuée

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