Logo AND Algorithmique Numérique Distribuée

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