Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix doxygen comments.
[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 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, char *attach)
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   p_attach = xbt_strdup(attach);
76   setState(SURF_RESOURCE_ON);
77   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
78   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
79   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
80 }
81
82 Storage::~Storage(){
83   surf_callback_emit(storageDestructedCallbacks, this);
84   xbt_dict_free(&p_content);
85   xbt_dynar_free(&p_writeActions);
86   free(p_typeId);
87   free(p_contentType);
88   free(p_attach);
89 }
90
91 xbt_dict_t Storage::parseContent(char *filename)
92 {
93   m_usedSize = 0;
94   if ((!filename) || (strcmp(filename, "") == 0))
95     return NULL;
96
97   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
98   FILE *file = NULL;
99
100   file = surf_fopen(filename, "r");
101   if (file == NULL)
102     xbt_die("Cannot open file '%s' (path=%s)", filename,
103             xbt_str_join(surf_path, ":"));
104
105   char *line = NULL;
106   size_t len = 0;
107   ssize_t read;
108   char path[1024];
109   sg_size_t size;
110
111   while ((read = xbt_getline(&line, &len, file)) != -1) {
112     if (read){
113       if(sscanf(line,"%s %llu", path, &size) == 2) {
114         m_usedSize += size;
115         sg_size_t *psize = xbt_new(sg_size_t, 1);
116         *psize = size;
117         xbt_dict_set(parse_content,path,psize,NULL);
118       } else {
119         xbt_die("Be sure of passing a good format for content file.\n");
120       }
121     }
122   }
123   free(line);
124   fclose(file);
125   return parse_content;
126 }
127
128 bool Storage::isUsed()
129 {
130   THROW_UNIMPLEMENTED;
131   return false;
132 }
133
134 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
135 {
136   THROW_UNIMPLEMENTED;
137 }
138
139 void Storage::setState(e_surf_resource_state_t state)
140 {
141   Resource::setState(state);
142   surf_callback_emit(storageStateChangedCallbacks, this);
143 }
144
145 xbt_dict_t Storage::getContent()
146 {
147   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
148   /*surf_action_t action = storage_action_execute(storage,0, LS);*/
149
150   xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
151   xbt_dict_cursor_t cursor = NULL;
152   char *file;
153   sg_size_t *psize;
154
155   xbt_dict_foreach(p_content, cursor, file, psize){
156     xbt_dict_set(content_dict,file,psize,NULL);
157   }
158   return content_dict;
159 }
160
161 sg_size_t Storage::getSize(){
162   return m_size;
163 }
164
165 /**********
166  * Action *
167  **********/
168 StorageAction::StorageAction(ModelPtr model, double cost, bool failed,
169                                      StoragePtr storage, e_surf_action_storage_type_t type)
170 : Action(model, cost, failed)
171 , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL)
172 {
173 };
174
175 StorageAction::StorageAction(ModelPtr model, double cost, bool failed, lmm_variable_t var,
176                                      StoragePtr storage, e_surf_action_storage_type_t type)
177   : Action(model, cost, failed, var)
178   , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL) {
179 }
180
181 void StorageAction::setState(e_surf_action_state_t state){
182   Action::setState(state);
183   surf_callback_emit(storageActionStateChangedCallbacks, this);
184 }