Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / surf / storage_interface.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "storage_interface.hpp"
8 #include "surf_private.h"
9 #include "xbt/file.h" /* xbt_getline */
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf, "Logging specific to the SURF storage module");
12
13 xbt_lib_t file_lib;
14 int MSG_FILE_LEVEL = -1; // Msg file level
15
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;
24
25 namespace simgrid {
26 namespace surf {
27
28 /*************
29  * Callbacks *
30  *************/
31
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;
36
37 /*********
38  * Model *
39  *********/
40
41 StorageModel::StorageModel(): Model()
42 {
43   maxminSystem_ = lmm_system_new(true /* lazy update */);
44 }
45
46 StorageModel::~StorageModel(){
47   lmm_system_free(maxminSystem_);
48   surf_storage_model = nullptr;
49 }
50
51 /************
52  * Resource *
53  ************/
54
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))
60     , size_(size)
61     , usedSize_(0)
62     , typeId_(xbt_strdup(type_id))
63     , writeActions_(std::vector<StorageAction*>())
64 {
65   content_ = parseContent(content_name);
66   attach_ = xbt_strdup(attach);
67   turnOn();
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);
71 }
72
73 Storage::~Storage(){
74   storageDestructedCallbacks(this);
75   xbt_dict_free(&content_);
76   free(typeId_);
77   free(contentType_);
78   free(attach_);
79 }
80
81 xbt_dict_t Storage::parseContent(const char *filename)
82 {
83   usedSize_ = 0;
84   if ((!filename) || (strcmp(filename, "") == 0))
85     return nullptr;
86
87   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
88
89   FILE *file =  surf_fopen(filename, "r");
90   xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
91
92   char *line = nullptr;
93   size_t len = 0;
94   ssize_t read;
95   char path[1024];
96   sg_size_t size;
97
98   while ((read = xbt_getline(&line, &len, file)) != -1) {
99     if (read){
100       xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
101
102       usedSize_ += size;
103       sg_size_t *psize = xbt_new(sg_size_t, 1);
104       *psize = size;
105       xbt_dict_set(parse_content,path,psize,nullptr);
106     }
107   }
108   free(line);
109   fclose(file);
110   return parse_content;
111 }
112
113 bool Storage::isUsed()
114 {
115   THROW_UNIMPLEMENTED;
116   return false;
117 }
118
119 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
120 {
121   THROW_UNIMPLEMENTED;
122 }
123
124 void Storage::turnOn() {
125   if (isOff()) {
126     Resource::turnOn();
127     storageStateChangedCallbacks(this, 0, 1);
128   }
129 }
130 void Storage::turnOff() {
131   if (isOn()) {
132     Resource::turnOff();
133     storageStateChangedCallbacks(this, 1, 0);
134   }
135 }
136
137 xbt_dict_t Storage::getContent()
138 {
139   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
140
141   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
142   xbt_dict_cursor_t cursor = nullptr;
143   char *file;
144   sg_size_t *psize;
145
146   xbt_dict_foreach(content_, cursor, file, psize){
147     xbt_dict_set(content_dict,file,psize,nullptr);
148   }
149   return content_dict;
150 }
151
152 sg_size_t Storage::getSize(){
153   return size_;
154 }
155
156 sg_size_t Storage::getFreeSize(){
157   return size_ - usedSize_;
158 }
159
160 sg_size_t Storage::getUsedSize(){
161   return usedSize_;
162 }
163
164 /**********
165  * Action *
166  **********/
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)
170 {
171   progress_ = 0;
172 };
173
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)
177 {
178   progress_ = 0;
179 }
180
181 void StorageAction::setState(Action::State state){
182   Action::State old = getState();
183   Action::setState(state);
184   storageActionStateChangedCallbacks(this, old, state);
185 }
186
187 }
188 }