1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "storage_interface.hpp"
8 #include "surf_private.h"
9 #include "xbt/file.h" /* xbt_getline */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
12 "Logging specific to the SURF storage module");
15 xbt_lib_t storage_lib;
16 int ROUTING_STORAGE_LEVEL; //Routing for storagelevel
17 int ROUTING_STORAGE_HOST_LEVEL;
18 int SURF_STORAGE_LEVEL;
19 xbt_lib_t storage_type_lib;
20 int ROUTING_STORAGE_TYPE_LEVEL; //Routing for storage_type level
21 xbt_dynar_t mount_list = NULL;
22 StorageModel *surf_storage_model = NULL;
28 surf_callback(void, Storage*) storageCreatedCallbacks;
29 surf_callback(void, Storage*) storageDestructedCallbacks;
30 surf_callback(void, Storage*, e_surf_resource_state_t, e_surf_resource_state_t) storageStateChangedCallbacks;
31 surf_callback(void, StorageAction*, e_surf_action_state_t, e_surf_action_state_t) storageActionStateChangedCallbacks;
37 StorageModel::StorageModel()
43 StorageModel::~StorageModel(){
44 lmm_system_free(p_maxminSystem);
46 surf_storage_model = NULL;
48 xbt_dynar_free(&p_storageList);
55 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
56 const char* type_id, char *content_name, char *content_type,
58 : Resource(model, name, props)
59 , p_contentType(content_type)
60 , m_size(size), m_usedSize(0)
61 , p_typeId(xbt_strdup(type_id))
62 , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
64 surf_callback_emit(storageCreatedCallbacks, this);
65 p_content = parseContent(content_name);
66 setState(SURF_RESOURCE_ON);
69 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
70 lmm_system_t maxminSystem, double bread, double bwrite,
71 double bconnection, const char* type_id, char *content_name,
72 char *content_type, sg_size_t size, char *attach)
73 : Resource(model, name, props, lmm_constraint_new(maxminSystem, this, bconnection))
74 , p_contentType(content_type)
75 , m_size(size), m_usedSize(0)
76 , p_typeId(xbt_strdup(type_id))
77 , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL)) {
78 surf_callback_emit(storageCreatedCallbacks, this);
79 p_content = parseContent(content_name);
80 p_attach = xbt_strdup(attach);
81 setState(SURF_RESOURCE_ON);
82 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
83 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
84 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
88 surf_callback_emit(storageDestructedCallbacks, this);
89 xbt_dict_free(&p_content);
90 xbt_dynar_free(&p_writeActions);
96 xbt_dict_t Storage::parseContent(char *filename)
99 if ((!filename) || (strcmp(filename, "") == 0))
102 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
105 file = surf_fopen(filename, "r");
107 xbt_die("Cannot open file '%s' (path=%s)", filename,
108 xbt_str_join(surf_path, ":"));
116 while ((read = xbt_getline(&line, &len, file)) != -1) {
118 if(sscanf(line,"%s %llu", path, &size) == 2) {
120 sg_size_t *psize = xbt_new(sg_size_t, 1);
122 xbt_dict_set(parse_content,path,psize,NULL);
124 xbt_die("Be sure of passing a good format for content file.\n");
130 return parse_content;
133 bool Storage::isUsed()
139 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
144 void Storage::setState(e_surf_resource_state_t state)
146 e_surf_resource_state_t old = Resource::getState();
147 Resource::setState(state);
148 surf_callback_emit(storageStateChangedCallbacks, this, old, state);
151 xbt_dict_t Storage::getContent()
153 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
155 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
156 xbt_dict_cursor_t cursor = NULL;
160 xbt_dict_foreach(p_content, cursor, file, psize){
161 xbt_dict_set(content_dict,file,psize,NULL);
166 sg_size_t Storage::getSize(){
170 sg_size_t Storage::getFreeSize(){
171 return m_size - m_usedSize;
174 sg_size_t Storage::getUsedSize(){
181 StorageAction::StorageAction(Model *model, double cost, bool failed,
182 Storage *storage, e_surf_action_storage_type_t type)
183 : Action(model, cost, failed)
184 , m_type(type), p_storage(storage), p_file(NULL){
188 StorageAction::StorageAction(Model *model, double cost, bool failed, lmm_variable_t var,
189 Storage *storage, e_surf_action_storage_type_t type)
190 : Action(model, cost, failed, var)
191 , m_type(type), p_storage(storage), p_file(NULL){
195 void StorageAction::setState(e_surf_action_state_t state){
196 e_surf_action_state_t old = getState();
197 Action::setState(state);
198 surf_callback_emit(storageActionStateChangedCallbacks, this, old, state);