Logo AND Algorithmique Numérique Distribuée

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