Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix cpu issues with VMs
[simgrid.git] / src / surf / storage_interface.cpp
1 #include "storage_interface.hpp"
2 #include "surf_private.h"
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
5                                 "Logging specific to the SURF storage module");
6
7 xbt_lib_t storage_lib;
8 int ROUTING_STORAGE_LEVEL;      //Routing for storagelevel
9 int ROUTING_STORAGE_HOST_LEVEL;
10 int SURF_STORAGE_LEVEL;
11 xbt_lib_t storage_type_lib;
12 int ROUTING_STORAGE_TYPE_LEVEL; //Routing for storage_type level
13
14 xbt_dynar_t mount_list = NULL;
15 StorageModelPtr surf_storage_model = NULL;
16
17 /*************
18  * Callbacks *
19  *************/
20
21 surf_callback(void, StoragePtr) storageCreatedCallbacks;
22 surf_callback(void, StoragePtr) storageDestructedCallbacks;
23 surf_callback(void, StoragePtr) storageStateChangedCallbacks;
24 surf_callback(void, StorageActionPtr) storageActionStateChangedCallbacks;
25
26 /*********
27  * Model *
28  *********/
29
30 StorageModel::StorageModel() : Model("Storage") {
31   p_storageList = NULL;
32 }
33
34 StorageModel::~StorageModel(){
35   lmm_system_free(p_maxminSystem);
36
37   surf_storage_model = NULL;
38
39   xbt_dynar_free(&p_storageList);
40 }
41
42 /************
43  * Resource *
44  ************/
45
46 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
47                          const char* type_id, char *content_name, char *content_type, sg_size_t size)
48  : Resource(model, name, props)
49  , p_contentType(content_type)
50  , m_size(size), m_usedSize(0)
51  , p_typeId(xbt_strdup(type_id))
52  , p_writeActions(xbt_dynar_new(sizeof(ActionPtr),NULL))
53 {
54   surf_callback_emit(storageCreatedCallbacks, this);
55   p_content = parseContent(content_name);
56   setState(SURF_RESOURCE_ON);
57 }
58
59 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
60                          lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
61                      const char* type_id, char *content_name, char *content_type, sg_size_t size)
62  :  Resource(model, name, props, lmm_constraint_new(maxminSystem, this, bconnection))
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(ActionPtr),NULL)) {
67   surf_callback_emit(storageCreatedCallbacks, this);
68   p_content = parseContent(content_name);
69   setState(SURF_RESOURCE_ON);
70   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
71   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
72   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
73 }
74
75 Storage::~Storage(){
76   surf_callback_emit(storageDestructedCallbacks, this);
77   xbt_dict_free(&p_content);
78   xbt_dynar_free(&p_writeActions);
79   free(p_typeId);
80   free(p_contentType);
81 }
82
83 xbt_dict_t Storage::parseContent(char *filename)
84 {
85   m_usedSize = 0;
86   if ((!filename) || (strcmp(filename, "") == 0))
87     return NULL;
88
89   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
90   FILE *file = NULL;
91
92   file = surf_fopen(filename, "r");
93   xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
94               xbt_str_join(surf_path, ":"));
95
96   char *line = NULL;
97   size_t len = 0;
98   ssize_t read;
99   char path[1024];
100   sg_size_t size;
101
102
103   while ((read = xbt_getline(&line, &len, file)) != -1) {
104     if (read){
105     if(sscanf(line,"%s %llu", path, &size) == 2) {
106         m_usedSize += size;
107         sg_size_t *psize = xbt_new(sg_size_t, 1);
108         *psize = size;
109         xbt_dict_set(parse_content,path,psize,NULL);
110       } else {
111         xbt_die("Be sure of passing a good format for content file.\n");
112       }
113     }
114   }
115   free(line);
116   fclose(file);
117   return parse_content;
118 }
119
120 bool Storage::isUsed()
121 {
122   THROW_UNIMPLEMENTED;
123   return false;
124 }
125
126 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
127 {
128   THROW_UNIMPLEMENTED;
129 }
130
131 void Storage::setState(e_surf_resource_state_t state)
132 {
133   Resource::setState(state);
134   surf_callback_emit(storageStateChangedCallbacks, this);
135 }
136
137 xbt_dict_t Storage::getContent()
138 {
139   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
140   /*surf_action_t action = storage_action_execute(storage,0, LS);*/
141
142   xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
143   xbt_dict_cursor_t cursor = NULL;
144   char *file;
145   sg_size_t *psize;
146
147   xbt_dict_foreach(p_content, cursor, file, psize){
148     xbt_dict_set(content_dict,file,psize,NULL);
149   }
150   return content_dict;
151 }
152
153 sg_size_t Storage::getSize(){
154   return m_size;
155 }
156
157 /**********
158  * Action *
159  **********/
160 StorageAction::StorageAction(ModelPtr model, double cost, bool failed,
161                                      StoragePtr storage, e_surf_action_storage_type_t type)
162 : Action(model, cost, failed)
163 , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL)
164 {
165 };
166
167 StorageAction::StorageAction(ModelPtr model, double cost, bool failed, lmm_variable_t var,
168                                      StoragePtr storage, e_surf_action_storage_type_t type)
169   : Action(model, cost, failed, var)
170   , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL) {
171 }
172
173 void StorageAction::setState(e_surf_action_state_t state){
174   Action::setState(state);
175   surf_callback_emit(storageActionStateChangedCallbacks, this);
176 }