Logo AND Algorithmique Numérique Distribuée

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