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 int MSG_FILE_LEVEL = -1; // Msg file level
16 xbt_lib_t storage_lib;
17 int SIMIX_STORAGE_LEVEL = -1; // Simix storage level
18 int MSG_STORAGE_LEVEL = -1; // Msg storage level
19 int ROUTING_STORAGE_LEVEL = -1; // Routing for storage level
20 int SURF_STORAGE_LEVEL = -1;
21 xbt_lib_t storage_type_lib;
22 int ROUTING_STORAGE_TYPE_LEVEL = -1; //Routing for storage_type level
23 simgrid::surf::StorageModel *surf_storage_model = nullptr;
32 simgrid::xbt::signal<void(Storage*)> storageCreatedCallbacks;
33 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
34 simgrid::xbt::signal<void(Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
35 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
41 StorageModel::StorageModel(): Model()
43 maxminSystem_ = lmm_system_new(true /* lazy update */);
46 StorageModel::~StorageModel(){
47 lmm_system_free(maxminSystem_);
48 surf_storage_model = nullptr;
55 Storage::Storage(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
56 double bconnection, const char* type_id, const char* content_name, const char* content_type,
57 sg_size_t size, const char* attach)
58 : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
59 , contentType_(xbt_strdup(content_type))
62 , typeId_(xbt_strdup(type_id))
63 , writeActions_(std::vector<StorageAction*>())
65 content_ = parseContent(content_name);
66 attach_ = xbt_strdup(attach);
68 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
69 constraintRead_ = lmm_constraint_new(maxminSystem, this, bread);
70 constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
74 storageDestructedCallbacks(this);
75 xbt_dict_free(&content_);
81 xbt_dict_t Storage::parseContent(const char *filename)
84 if ((!filename) || (strcmp(filename, "") == 0))
87 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
89 FILE *file = surf_fopen(filename, "r");
90 xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
98 while ((read = xbt_getline(&line, &len, file)) != -1) {
100 xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
103 sg_size_t *psize = xbt_new(sg_size_t, 1);
105 xbt_dict_set(parse_content,path,psize,nullptr);
110 return parse_content;
113 bool Storage::isUsed()
119 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
124 void Storage::turnOn() {
127 storageStateChangedCallbacks(this, 0, 1);
130 void Storage::turnOff() {
133 storageStateChangedCallbacks(this, 1, 0);
137 xbt_dict_t Storage::getContent()
139 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
141 xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
142 xbt_dict_cursor_t cursor = nullptr;
146 xbt_dict_foreach(content_, cursor, file, psize){
147 xbt_dict_set(content_dict,file,psize,nullptr);
152 sg_size_t Storage::getSize(){
156 sg_size_t Storage::getFreeSize(){
157 return size_ - usedSize_;
160 sg_size_t Storage::getUsedSize(){
167 StorageAction::StorageAction(Model* model, double cost, bool failed, Storage* storage,
168 e_surf_action_storage_type_t type)
169 : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
174 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, Storage* storage,
175 e_surf_action_storage_type_t type)
176 : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
181 void StorageAction::setState(Action::State state){
182 Action::State old = getState();
183 Action::setState(state);
184 storageActionStateChangedCallbacks(this, old, state);