Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a bug
[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  * Model *
19  *********/
20
21 StorageModel::StorageModel() : Model("Storage") {
22   p_storageList = NULL;
23 }
24
25 StorageModel::~StorageModel(){
26   lmm_system_free(p_maxminSystem);
27
28   surf_storage_model = NULL;
29
30   xbt_dynar_free(&p_storageList);
31 }
32
33 /************
34  * Resource *
35  ************/
36
37 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
38                          const char* type_id, char *content_name, char *content_type, sg_size_t size)
39  : Resource(model, name, props)
40  , p_contentType(content_type)
41  , m_size(size), m_usedSize(0)
42  , p_typeId(xbt_strdup(type_id))
43  , p_writeActions(xbt_dynar_new(sizeof(ActionPtr),NULL))
44 {
45   p_content = parseContent(content_name);
46   m_stateCurrent = SURF_RESOURCE_ON;
47 }
48
49 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
50                          lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
51                      const char* type_id, char *content_name, char *content_type, sg_size_t size)
52  :  Resource(model, name, props, lmm_constraint_new(maxminSystem, this, bconnection))
53  , p_contentType(content_type)
54  , m_size(size), m_usedSize(0)
55  , p_typeId(xbt_strdup(type_id))
56  , p_writeActions(xbt_dynar_new(sizeof(ActionPtr),NULL)) {
57   p_content = parseContent(content_name);
58   m_stateCurrent = SURF_RESOURCE_ON;
59   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
60   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
61   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
62 }
63
64 Storage::~Storage(){
65   xbt_dict_free(&p_content);
66   xbt_dynar_free(&p_writeActions);
67   free(p_typeId);
68   free(p_contentType);
69 }
70
71 xbt_dict_t Storage::parseContent(char *filename)
72 {
73   m_usedSize = 0;
74   if ((!filename) || (strcmp(filename, "") == 0))
75     return NULL;
76
77   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
78   FILE *file = NULL;
79
80   file = surf_fopen(filename, "r");
81   xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
82               xbt_str_join(surf_path, ":"));
83
84   char *line = NULL;
85   size_t len = 0;
86   ssize_t read;
87   char path[1024];
88   sg_size_t size;
89
90
91   while ((read = xbt_getline(&line, &len, file)) != -1) {
92     if (read){
93     if(sscanf(line,"%s %llu", path, &size) == 2) {
94         m_usedSize += size;
95         sg_size_t *psize = xbt_new(sg_size_t, 1);
96         *psize = size;
97         xbt_dict_set(parse_content,path,psize,NULL);
98       } else {
99         xbt_die("Be sure of passing a good format for content file.\n");
100       }
101     }
102   }
103   free(line);
104   fclose(file);
105   return parse_content;
106 }
107
108 bool Storage::isUsed()
109 {
110   THROW_UNIMPLEMENTED;
111   return false;
112 }
113
114 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
115 {
116   THROW_UNIMPLEMENTED;
117 }
118
119 xbt_dict_t Storage::getContent()
120 {
121   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
122   /*surf_action_t action = storage_action_execute(storage,0, LS);*/
123
124   xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
125   xbt_dict_cursor_t cursor = NULL;
126   char *file;
127   sg_size_t *psize;
128
129   xbt_dict_foreach(p_content, cursor, file, psize){
130     xbt_dict_set(content_dict,file,psize,NULL);
131   }
132   return content_dict;
133 }
134
135 sg_size_t Storage::getSize(){
136   return m_size;
137 }
138
139
140
141 /**********
142  * Action *
143  **********/
144 StorageAction::StorageAction(ModelPtr model, double cost, bool failed,
145                                      StoragePtr storage, e_surf_action_storage_type_t type)
146 : Action(model, cost, failed)
147 , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL)
148 {
149 };
150
151 StorageAction::StorageAction(ModelPtr model, double cost, bool failed, lmm_variable_t var,
152                                      StoragePtr storage, e_surf_action_storage_type_t type)
153   : Action(model, cost, failed, var)
154   , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL) {
155 }
156