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 simgrid::surf::StorageModel *surf_storage_model = NULL;
31 simgrid::surf::signal<void(simgrid::surf::Storage*)> storageCreatedCallbacks;
32 simgrid::surf::signal<void(simgrid::surf::Storage*)> storageDestructedCallbacks;
33 simgrid::surf::signal<void(simgrid::surf::Storage*, e_surf_resource_state_t, e_surf_resource_state_t)> storageStateChangedCallbacks;
34 simgrid::surf::signal<void(simgrid::surf::StorageAction*, e_surf_action_state_t, e_surf_action_state_t)> storageActionStateChangedCallbacks;
40 StorageModel::StorageModel()
46 StorageModel::~StorageModel(){
47 lmm_system_free(p_maxminSystem);
49 surf_storage_model = NULL;
51 xbt_dynar_free(&p_storageList);
58 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
59 const char* type_id, const char *content_name, const char *content_type,
61 : Resource(model, name)
62 , PropertyHolder(props)
63 , p_contentType(xbt_strdup(content_type))
64 , m_size(size), m_usedSize(0)
65 , p_typeId(xbt_strdup(type_id))
66 , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
68 p_content = parseContent(content_name);
69 setState(SURF_RESOURCE_ON);
72 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
73 lmm_system_t maxminSystem, double bread, double bwrite,
74 double bconnection, const char* type_id, const char *content_name,
75 const char *content_type, sg_size_t size, const char *attach)
76 : Storage(model, name, props, type_id, content_name, content_type, size)
78 p_attach = xbt_strdup(attach);
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);
85 storageDestructedCallbacks(this);
86 xbt_dict_free(&p_content);
87 xbt_dynar_free(&p_writeActions);
93 xbt_dict_t Storage::parseContent(const char *filename)
96 if ((!filename) || (strcmp(filename, "") == 0))
99 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
102 file = surf_fopen(filename, "r");
104 xbt_die("Cannot open file '%s' (path=%s)", filename,
105 xbt_str_join(surf_path, ":"));
113 while ((read = xbt_getline(&line, &len, file)) != -1) {
115 if(sscanf(line,"%s %llu", path, &size) == 2) {
117 sg_size_t *psize = xbt_new(sg_size_t, 1);
119 xbt_dict_set(parse_content,path,psize,NULL);
121 xbt_die("Be sure of passing a good format for content file.\n");
127 return parse_content;
130 bool Storage::isUsed()
136 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
141 void Storage::setState(e_surf_resource_state_t state)
143 e_surf_resource_state_t old = Resource::getState();
144 Resource::setState(state);
145 storageStateChangedCallbacks(this, old, state);
148 xbt_dict_t Storage::getContent()
150 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
152 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
153 xbt_dict_cursor_t cursor = NULL;
157 xbt_dict_foreach(p_content, cursor, file, psize){
158 xbt_dict_set(content_dict,file,psize,NULL);
163 sg_size_t Storage::getSize(){
167 sg_size_t Storage::getFreeSize(){
168 return m_size - m_usedSize;
171 sg_size_t Storage::getUsedSize(){
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){
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){
192 void StorageAction::setState(e_surf_action_state_t state){
193 e_surf_action_state_t old = getState();
194 Action::setState(state);
195 storageActionStateChangedCallbacks(this, old, state);