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 simgrid::surf::StorageModel *surf_storage_model = NULL;
30 simgrid::xbt::signal<void(Storage*)> storageCreatedCallbacks;
31 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
32 simgrid::xbt::signal<void(Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
33 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
39 StorageModel::StorageModel()
45 StorageModel::~StorageModel(){
46 lmm_system_free(maxminSystem_);
47 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, const char *content_name, const char *content_type,
58 : Resource(model, name)
59 , PropertyHolder(props)
60 , p_contentType(xbt_strdup(content_type))
61 , m_size(size), m_usedSize(0)
62 , p_typeId(xbt_strdup(type_id))
63 , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
65 p_content = parseContent(content_name);
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, const char *content_name,
72 const char *content_type, sg_size_t size, const char *attach)
73 : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
74 , PropertyHolder(props)
75 , p_contentType(xbt_strdup(content_type))
76 , m_size(size), m_usedSize(0)
77 , p_typeId(xbt_strdup(type_id))
78 , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL))
80 p_content = parseContent(content_name);
81 p_attach = xbt_strdup(attach);
83 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
84 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
85 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
89 storageDestructedCallbacks(this);
90 xbt_dict_free(&p_content);
91 xbt_dynar_free(&p_writeActions);
97 xbt_dict_t Storage::parseContent(const char *filename)
100 if ((!filename) || (strcmp(filename, "") == 0))
103 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
105 FILE *file = surf_fopen(filename, "r");
106 xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
114 while ((read = xbt_getline(&line, &len, file)) != -1) {
116 xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
119 sg_size_t *psize = xbt_new(sg_size_t, 1);
121 xbt_dict_set(parse_content,path,psize,NULL);
126 return parse_content;
129 bool Storage::isUsed()
135 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
140 void Storage::turnOn() {
143 storageStateChangedCallbacks(this, 0, 1);
146 void Storage::turnOff() {
149 storageStateChangedCallbacks(this, 1, 0);
153 xbt_dict_t Storage::getContent()
155 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
157 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
158 xbt_dict_cursor_t cursor = NULL;
162 xbt_dict_foreach(p_content, cursor, file, psize){
163 xbt_dict_set(content_dict,file,psize,NULL);
168 sg_size_t Storage::getSize(){
172 sg_size_t Storage::getFreeSize(){
173 return m_size - m_usedSize;
176 sg_size_t Storage::getUsedSize(){
183 StorageAction::StorageAction(Model *model, double cost, bool failed,
184 Storage *storage, e_surf_action_storage_type_t type)
185 : Action(model, cost, failed)
186 , m_type(type), p_storage(storage), p_file(NULL){
190 StorageAction::StorageAction(Model *model, double cost, bool failed, lmm_variable_t var,
191 Storage *storage, e_surf_action_storage_type_t type)
192 : Action(model, cost, failed, var)
193 , m_type(type), p_storage(storage), p_file(NULL){
197 void StorageAction::setState(Action::State state){
198 Action::State old = getState();
199 Action::setState(state);
200 storageActionStateChangedCallbacks(this, old, state);