Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification: we don't use non-LMM resources
[simgrid.git] / src / surf / storage_interface.cpp
1 /* Copyright (c) 2013-2015. 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 "xbt/file.h" /* xbt_getline */
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
12
13 xbt_lib_t file_lib;
14 xbt_lib_t storage_lib;
15 int ROUTING_STORAGE_LEVEL = -1;      //Routing for storagelevel
16 int ROUTING_STORAGE_HOST_LEVEL = -1;
17 int SURF_STORAGE_LEVEL = -1;
18 xbt_lib_t storage_type_lib;
19 int ROUTING_STORAGE_TYPE_LEVEL = -1; //Routing for storage_type level
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(Storage*)> storageCreatedCallbacks;
30 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
31 simgrid::xbt::signal<void(Storage*, 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   lmm_system_free(maxminSystem_);
45   surf_storage_model = nullptr;
46 }
47
48 /************
49  * Resource *
50  ************/
51
52 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
53                  lmm_system_t maxminSystem, double bread, double bwrite,
54                  double bconnection, const char* type_id, const char *content_name,
55                  const char *content_type, sg_size_t size, const char *attach)
56  : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
57  , PropertyHolder(props)
58  , contentType_(xbt_strdup(content_type))
59  , size_(size), usedSize_(0)
60  , typeId_(xbt_strdup(type_id))
61  , writeActions_(std::vector<StorageAction*>())
62 {
63   content_ = parseContent(content_name);
64   attach_ = xbt_strdup(attach);
65   turnOn();
66   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
67   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
68   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
69 }
70
71 Storage::~Storage(){
72   storageDestructedCallbacks(this);
73   xbt_dict_free(&content_);
74   free(typeId_);
75   free(contentType_);
76   free(attach_);
77 }
78
79 xbt_dict_t Storage::parseContent(const char *filename)
80 {
81   usedSize_ = 0;
82   if ((!filename) || (strcmp(filename, "") == 0))
83     return nullptr;
84
85   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
86
87   FILE *file =  surf_fopen(filename, "r");
88   xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
89
90   char *line = nullptr;
91   size_t len = 0;
92   ssize_t read;
93   char path[1024];
94   sg_size_t size;
95
96   while ((read = xbt_getline(&line, &len, file)) != -1) {
97     if (read){
98       xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
99
100       usedSize_ += size;
101       sg_size_t *psize = xbt_new(sg_size_t, 1);
102       *psize = size;
103       xbt_dict_set(parse_content,path,psize,nullptr);
104     }
105   }
106   free(line);
107   fclose(file);
108   return parse_content;
109 }
110
111 bool Storage::isUsed()
112 {
113   THROW_UNIMPLEMENTED;
114   return false;
115 }
116
117 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
118 {
119   THROW_UNIMPLEMENTED;
120 }
121
122 void Storage::turnOn() {
123   if (isOff()) {
124     Resource::turnOn();
125     storageStateChangedCallbacks(this, 0, 1);
126   }
127 }
128 void Storage::turnOff() {
129   if (isOn()) {
130     Resource::turnOff();
131     storageStateChangedCallbacks(this, 1, 0);
132   }
133 }
134
135 xbt_dict_t Storage::getContent()
136 {
137   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
138
139   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
140   xbt_dict_cursor_t cursor = nullptr;
141   char *file;
142   sg_size_t *psize;
143
144   xbt_dict_foreach(content_, cursor, file, psize){
145     xbt_dict_set(content_dict,file,psize,nullptr);
146   }
147   return content_dict;
148 }
149
150 sg_size_t Storage::getSize(){
151   return size_;
152 }
153
154 sg_size_t Storage::getFreeSize(){
155   return size_ - usedSize_;
156 }
157
158 sg_size_t Storage::getUsedSize(){
159   return usedSize_;
160 }
161
162 /**********
163  * Action *
164  **********/
165 StorageAction::StorageAction(Model* model, double cost, bool failed, Storage* storage,
166                              e_surf_action_storage_type_t type)
167     : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
168 {
169   progress_ = 0;
170 };
171
172 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, Storage* storage,
173                              e_surf_action_storage_type_t type)
174     : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
175 {
176   progress_ = 0;
177 }
178
179 void StorageAction::setState(Action::State state){
180   Action::State old = getState();
181   Action::setState(state);
182   storageActionStateChangedCallbacks(this, old, state);
183 }
184
185 }
186 }