Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Move some code in a simgrid::surf namespace
[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 surf_callback(void, simgrid::surf::Storage*) storageCreatedCallbacks;
32 surf_callback(void, simgrid::surf::Storage*) storageDestructedCallbacks;
33 surf_callback(void, simgrid::surf::Storage*, e_surf_resource_state_t, e_surf_resource_state_t) storageStateChangedCallbacks;
34 surf_callback(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, props)
62  , p_contentType(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   setState(SURF_RESOURCE_ON);
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, char *content_name,
74                  char *content_type, sg_size_t size, char *attach)
75  :  Resource(model, name, props, lmm_constraint_new(maxminSystem, this, bconnection))
76  , p_contentType(content_type)
77  , m_size(size), m_usedSize(0)
78  , p_typeId(xbt_strdup(type_id))
79  , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL)) {
80   p_content = parseContent(content_name);
81   p_attach = xbt_strdup(attach);
82   setState(SURF_RESOURCE_ON);
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   surf_callback_emit(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(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   FILE *file = NULL;
105
106   file = surf_fopen(filename, "r");
107   if (file == NULL)
108     xbt_die("Cannot open file '%s' (path=%s)", filename,
109             xbt_str_join(surf_path, ":"));
110
111   char *line = NULL;
112   size_t len = 0;
113   ssize_t read;
114   char path[1024];
115   sg_size_t size;
116
117   while ((read = xbt_getline(&line, &len, file)) != -1) {
118     if (read){
119       if(sscanf(line,"%s %llu", path, &size) == 2) {
120         m_usedSize += size;
121         sg_size_t *psize = xbt_new(sg_size_t, 1);
122         *psize = size;
123         xbt_dict_set(parse_content,path,psize,NULL);
124       } else {
125         xbt_die("Be sure of passing a good format for content file.\n");
126       }
127     }
128   }
129   free(line);
130   fclose(file);
131   return parse_content;
132 }
133
134 bool Storage::isUsed()
135 {
136   THROW_UNIMPLEMENTED;
137   return false;
138 }
139
140 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
141 {
142   THROW_UNIMPLEMENTED;
143 }
144
145 void Storage::setState(e_surf_resource_state_t state)
146 {
147   e_surf_resource_state_t old = Resource::getState();
148   Resource::setState(state);
149   surf_callback_emit(storageStateChangedCallbacks, this, old, state);
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(NULL);
157   xbt_dict_cursor_t cursor = NULL;
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,NULL);
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(NULL){
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(NULL){
193   progress = 0;
194 }
195
196 void StorageAction::setState(e_surf_action_state_t state){
197   e_surf_action_state_t old = getState();
198   Action::setState(state);
199   surf_callback_emit(storageActionStateChangedCallbacks, this, old, state);
200 }
201
202 }
203 }