Logo AND Algorithmique Numérique Distribuée

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