Logo AND Algorithmique Numérique Distribuée

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