Logo AND Algorithmique Numérique Distribuée

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