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, "Logging specific to the SURF storage module");
14 xbt_lib_t storage_lib;
15 int ROUTING_STORAGE_LEVEL = -1; //Routing for storagelevel
16 int ROUTING_STORAGE_HOST_LEVEL = -1;
17 int SURF_STORAGE_LEVEL = -1;
18 xbt_lib_t storage_type_lib;
19 int ROUTING_STORAGE_TYPE_LEVEL = -1; //Routing for storage_type level
20 simgrid::surf::StorageModel *surf_storage_model = nullptr;
29 simgrid::xbt::signal<void(Storage*)> storageCreatedCallbacks;
30 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
31 simgrid::xbt::signal<void(Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
32 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
38 StorageModel::StorageModel()
41 p_storageList = nullptr;
44 StorageModel::~StorageModel(){
45 lmm_system_free(maxminSystem_);
46 surf_storage_model = nullptr;
47 xbt_dynar_free(&p_storageList);
54 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
55 const char* type_id, const char *content_name, const char *content_type,
57 : Resource(model, name)
58 , PropertyHolder(props)
59 , p_contentType(xbt_strdup(content_type))
60 , m_size(size), m_usedSize(0)
61 , p_typeId(xbt_strdup(type_id))
62 , p_writeActions(xbt_dynar_new(sizeof(Action*),nullptr))
64 p_content = parseContent(content_name);
68 Storage::Storage(Model *model, const char *name, xbt_dict_t props,
69 lmm_system_t maxminSystem, double bread, double bwrite,
70 double bconnection, const char* type_id, const char *content_name,
71 const char *content_type, sg_size_t size, const char *attach)
72 : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
73 , PropertyHolder(props)
74 , p_contentType(xbt_strdup(content_type))
75 , m_size(size), m_usedSize(0)
76 , p_typeId(xbt_strdup(type_id))
77 , p_writeActions(xbt_dynar_new(sizeof(Action*),nullptr))
79 p_content = parseContent(content_name);
80 p_attach = xbt_strdup(attach);
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 storageDestructedCallbacks(this);
89 xbt_dict_free(&p_content);
90 xbt_dynar_free(&p_writeActions);
96 xbt_dict_t Storage::parseContent(const char *filename)
99 if ((!filename) || (strcmp(filename, "") == 0))
102 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
104 FILE *file = surf_fopen(filename, "r");
105 xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
107 char *line = nullptr;
113 while ((read = xbt_getline(&line, &len, file)) != -1) {
115 xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
118 sg_size_t *psize = xbt_new(sg_size_t, 1);
120 xbt_dict_set(parse_content,path,psize,nullptr);
125 return parse_content;
128 bool Storage::isUsed()
134 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
139 void Storage::turnOn() {
142 storageStateChangedCallbacks(this, 0, 1);
145 void Storage::turnOff() {
148 storageStateChangedCallbacks(this, 1, 0);
152 xbt_dict_t Storage::getContent()
154 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
156 xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
157 xbt_dict_cursor_t cursor = nullptr;
161 xbt_dict_foreach(p_content, cursor, file, psize){
162 xbt_dict_set(content_dict,file,psize,nullptr);
167 sg_size_t Storage::getSize(){
171 sg_size_t Storage::getFreeSize(){
172 return m_size - m_usedSize;
175 sg_size_t Storage::getUsedSize(){
182 StorageAction::StorageAction(Model *model, double cost, bool failed,
183 Storage *storage, e_surf_action_storage_type_t type)
184 : Action(model, cost, failed)
185 , m_type(type), p_storage(storage), p_file(nullptr){
189 StorageAction::StorageAction(Model *model, double cost, bool failed, lmm_variable_t var,
190 Storage *storage, e_surf_action_storage_type_t type)
191 : Action(model, cost, failed, var)
192 , m_type(type), p_storage(storage), p_file(nullptr){
196 void StorageAction::setState(Action::State state){
197 Action::State old = getState();
198 Action::setState(state);
199 storageActionStateChangedCallbacks(this, old, state);