Logo AND Algorithmique Numérique Distribuée

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