1 /* Copyright (c) 2013-2014. 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"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
11 "Logging specific to the SURF storage module");
14 xbt_lib_t storage_lib;
15 int ROUTING_STORAGE_LEVEL; //Routing for storagelevel
16 int ROUTING_STORAGE_HOST_LEVEL;
17 int SURF_STORAGE_LEVEL;
18 xbt_lib_t storage_type_lib;
19 int ROUTING_STORAGE_TYPE_LEVEL; //Routing for storage_type level
20 xbt_dynar_t mount_list = NULL;
21 StorageModelPtr surf_storage_model = NULL;
27 surf_callback(void, StoragePtr) storageCreatedCallbacks;
28 surf_callback(void, StoragePtr) storageDestructedCallbacks;
29 surf_callback(void, StoragePtr) storageStateChangedCallbacks;
30 surf_callback(void, StorageActionPtr) storageActionStateChangedCallbacks;
36 StorageModel::StorageModel() : Model("Storage") {
40 StorageModel::~StorageModel(){
41 lmm_system_free(p_maxminSystem);
43 surf_storage_model = NULL;
45 xbt_dynar_free(&p_storageList);
52 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
53 const char* type_id, char *content_name, char *content_type, sg_size_t size)
54 : Resource(model, name, props)
55 , p_contentType(content_type)
56 , m_size(size), m_usedSize(0)
57 , p_typeId(xbt_strdup(type_id))
58 , p_writeActions(xbt_dynar_new(sizeof(ActionPtr),NULL))
60 surf_callback_emit(storageCreatedCallbacks, this);
61 p_content = parseContent(content_name);
62 setState(SURF_RESOURCE_ON);
65 Storage::Storage(ModelPtr model, const char *name, xbt_dict_t props,
66 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
67 const char* type_id, char *content_name, char *content_type, sg_size_t size, char *attach)
68 : Resource(model, name, props, lmm_constraint_new(maxminSystem, this, bconnection))
69 , p_contentType(content_type)
70 , m_size(size), m_usedSize(0)
71 , p_typeId(xbt_strdup(type_id))
72 , p_writeActions(xbt_dynar_new(sizeof(ActionPtr),NULL)) {
73 surf_callback_emit(storageCreatedCallbacks, this);
74 p_content = parseContent(content_name);
75 p_attach = xbt_strdup(attach);
76 setState(SURF_RESOURCE_ON);
77 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
78 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
79 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
83 surf_callback_emit(storageDestructedCallbacks, this);
84 xbt_dict_free(&p_content);
85 xbt_dynar_free(&p_writeActions);
91 xbt_dict_t Storage::parseContent(char *filename)
94 if ((!filename) || (strcmp(filename, "") == 0))
97 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
100 file = surf_fopen(filename, "r");
101 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
102 xbt_str_join(surf_path, ":"));
111 while ((read = xbt_getline(&line, &len, file)) != -1) {
113 if(sscanf(line,"%s %llu", path, &size) == 2) {
115 sg_size_t *psize = xbt_new(sg_size_t, 1);
117 xbt_dict_set(parse_content,path,psize,NULL);
119 xbt_die("Be sure of passing a good format for content file.\n");
125 return parse_content;
128 bool Storage::isUsed()
134 void Storage::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/)
139 void Storage::setState(e_surf_resource_state_t state)
141 Resource::setState(state);
142 surf_callback_emit(storageStateChangedCallbacks, this);
145 xbt_dict_t Storage::getContent()
147 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
148 /*surf_action_t action = storage_action_execute(storage,0, LS);*/
150 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
151 xbt_dict_cursor_t cursor = NULL;
155 xbt_dict_foreach(p_content, cursor, file, psize){
156 xbt_dict_set(content_dict,file,psize,NULL);
161 sg_size_t Storage::getSize(){
168 StorageAction::StorageAction(ModelPtr model, double cost, bool failed,
169 StoragePtr storage, e_surf_action_storage_type_t type)
170 : Action(model, cost, failed)
171 , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL)
175 StorageAction::StorageAction(ModelPtr model, double cost, bool failed, lmm_variable_t var,
176 StoragePtr storage, e_surf_action_storage_type_t type)
177 : Action(model, cost, failed, var)
178 , m_type(type), p_storage(storage), p_file(NULL), p_lsDict(NULL) {
181 void StorageAction::setState(e_surf_action_state_t state){
182 Action::setState(state);
183 surf_callback_emit(storageActionStateChangedCallbacks, this);