Logo AND Algorithmique Numérique Distribuée

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