Logo AND Algorithmique Numérique Distribuée

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