Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
gosh, this shitty code is even dupplicated!
[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,
12                                 "Logging specific to the SURF storage module");
13
14 xbt_lib_t file_lib;
15 xbt_lib_t storage_lib;
16 int ROUTING_STORAGE_LEVEL;      //Routing for storagelevel
17 int ROUTING_STORAGE_HOST_LEVEL;
18 int SURF_STORAGE_LEVEL;
19 xbt_lib_t storage_type_lib;
20 int ROUTING_STORAGE_TYPE_LEVEL; //Routing for storage_type level
21 simgrid::surf::StorageModel *surf_storage_model = NULL;
22
23 namespace simgrid {
24 namespace surf {
25
26 /*************
27  * Callbacks *
28  *************/
29
30 simgrid::xbt::signal<void(simgrid::surf::Storage*)> storageCreatedCallbacks;
31 simgrid::xbt::signal<void(simgrid::surf::Storage*)> storageDestructedCallbacks;
32 simgrid::xbt::signal<void(simgrid::surf::Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
33 simgrid::xbt::signal<void(simgrid::surf::StorageAction*, e_surf_action_state_t, e_surf_action_state_t)> storageActionStateChangedCallbacks;
34
35 /*********
36  * Model *
37  *********/
38
39 StorageModel::StorageModel()
40   : Model()
41 {
42   p_storageList = NULL;
43 }
44
45 StorageModel::~StorageModel(){
46   lmm_system_free(p_maxminSystem);
47
48   surf_storage_model = NULL;
49
50   xbt_dynar_free(&p_storageList);
51 }
52
53 /************
54  * Resource *
55  ************/
56
57 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
58                  const char* type_id, const char *content_name, const char *content_type,
59                  sg_size_t size)
60  : Resource(model, name)
61  , PropertyHolder(props)
62  , p_contentType(xbt_strdup(content_type))
63  , m_size(size), m_usedSize(0)
64  , p_typeId(xbt_strdup(type_id))
65  , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
66 {
67   p_content = parseContent(content_name);
68   turnOn();
69 }
70
71 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
72                  lmm_system_t maxminSystem, double bread, double bwrite,
73                  double bconnection, const char* type_id, const char *content_name,
74                  const char *content_type, sg_size_t size, const char *attach)
75  : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
76  , PropertyHolder(props)
77  , p_contentType(xbt_strdup(content_type))
78  , m_size(size), m_usedSize(0)
79  , p_typeId(xbt_strdup(type_id))
80  , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
81 {
82   p_content = parseContent(content_name);
83   p_attach = xbt_strdup(attach);
84   turnOn();
85   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
86   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
87   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
88 }
89
90 Storage::~Storage(){
91   storageDestructedCallbacks(this);
92   xbt_dict_free(&p_content);
93   xbt_dynar_free(&p_writeActions);
94   free(p_typeId);
95   free(p_contentType);
96   free(p_attach);
97 }
98
99 xbt_dict_t Storage::parseContent(const char *filename)
100 {
101   m_usedSize = 0;
102   if ((!filename) || (strcmp(filename, "") == 0))
103     return NULL;
104
105   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
106   FILE *file = NULL;
107
108   file = surf_fopen(filename, "r");
109   if (file == NULL)
110     xbt_die("Cannot open file '%s' (path=%s)", filename,
111             xbt_str_join(surf_path, ":"));
112
113   char *line = NULL;
114   size_t len = 0;
115   ssize_t read;
116   char path[1024];
117   sg_size_t size;
118
119   while ((read = xbt_getline(&line, &len, file)) != -1) {
120     if (read){
121       if(sscanf(line,"%s %llu", path, &size) == 2) {
122         m_usedSize += size;
123         sg_size_t *psize = xbt_new(sg_size_t, 1);
124         *psize = size;
125         xbt_dict_set(parse_content,path,psize,NULL);
126       } else {
127         xbt_die("Be sure of passing a good format for content file.\n");
128       }
129     }
130   }
131   free(line);
132   fclose(file);
133   return parse_content;
134 }
135
136 bool Storage::isUsed()
137 {
138   THROW_UNIMPLEMENTED;
139   return false;
140 }
141
142 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
143 {
144   THROW_UNIMPLEMENTED;
145 }
146
147 void Storage::turnOn() {
148   if (isOff()) {
149     Resource::turnOn();
150     storageStateChangedCallbacks(this, 0, 1);
151   }
152 }
153 void Storage::turnOff() {
154   if (isOn()) {
155     Resource::turnOff();
156     storageStateChangedCallbacks(this, 1, 0);
157   }
158 }
159
160 xbt_dict_t Storage::getContent()
161 {
162   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
163
164   xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
165   xbt_dict_cursor_t cursor = NULL;
166   char *file;
167   sg_size_t *psize;
168
169   xbt_dict_foreach(p_content, cursor, file, psize){
170     xbt_dict_set(content_dict,file,psize,NULL);
171   }
172   return content_dict;
173 }
174
175 sg_size_t Storage::getSize(){
176   return m_size;
177 }
178
179 sg_size_t Storage::getFreeSize(){
180   return m_size - m_usedSize;
181 }
182
183 sg_size_t Storage::getUsedSize(){
184   return m_usedSize;
185 }
186
187 /**********
188  * Action *
189  **********/
190 StorageAction::StorageAction(Model *model, double cost, bool failed,
191                              Storage *storage, e_surf_action_storage_type_t type)
192 : Action(model, cost, failed)
193 , m_type(type), p_storage(storage), p_file(NULL){
194   progress = 0;
195 };
196
197 StorageAction::StorageAction(Model *model, double cost, bool failed, lmm_variable_t var,
198                              Storage *storage, e_surf_action_storage_type_t type)
199   : Action(model, cost, failed, var)
200   , m_type(type), p_storage(storage), p_file(NULL){
201   progress = 0;
202 }
203
204 void StorageAction::setState(e_surf_action_state_t state){
205   e_surf_action_state_t old = getState();
206   Action::setState(state);
207   storageActionStateChangedCallbacks(this, old, state);
208 }
209
210 }
211 }