Logo AND Algorithmique Numérique Distribuée

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