Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar
[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 ROUTING_STORAGE_HOST_LEVEL = -1;
21 int SURF_STORAGE_LEVEL = -1;
22 xbt_lib_t storage_type_lib;
23 int ROUTING_STORAGE_TYPE_LEVEL = -1; //Routing for storage_type level
24 simgrid::surf::StorageModel *surf_storage_model = nullptr;
25
26 namespace simgrid {
27 namespace surf {
28
29 /*************
30  * Callbacks *
31  *************/
32
33 simgrid::xbt::signal<void(Storage*)> storageCreatedCallbacks;
34 simgrid::xbt::signal<void(Storage*)> storageDestructedCallbacks;
35 simgrid::xbt::signal<void(Storage*, int, int)> storageStateChangedCallbacks; // signature: wasOn, isOn
36 simgrid::xbt::signal<void(StorageAction*, Action::State, Action::State)> storageActionStateChangedCallbacks;
37
38 /*********
39  * Model *
40  *********/
41
42 StorageModel::StorageModel(): Model()
43 {
44   maxminSystem_ = lmm_system_new(true /* lazy update */);
45 }
46
47 StorageModel::~StorageModel(){
48   lmm_system_free(maxminSystem_);
49   surf_storage_model = nullptr;
50 }
51
52 /************
53  * Resource *
54  ************/
55
56 Storage::Storage(Model* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
57                  double bconnection, const char* type_id, const char* content_name, const char* content_type,
58                  sg_size_t size, const char* attach)
59     : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection))
60     , contentType_(xbt_strdup(content_type))
61     , size_(size)
62     , usedSize_(0)
63     , typeId_(xbt_strdup(type_id))
64     , writeActions_(std::vector<StorageAction*>())
65 {
66   content_ = parseContent(content_name);
67   attach_ = xbt_strdup(attach);
68   turnOn();
69   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
70   constraintRead_  = lmm_constraint_new(maxminSystem, this, bread);
71   constraintWrite_ = lmm_constraint_new(maxminSystem, this, bwrite);
72 }
73
74 Storage::~Storage(){
75   storageDestructedCallbacks(this);
76   xbt_dict_free(&content_);
77   free(typeId_);
78   free(contentType_);
79   free(attach_);
80 }
81
82 xbt_dict_t Storage::parseContent(const char *filename)
83 {
84   usedSize_ = 0;
85   if ((!filename) || (strcmp(filename, "") == 0))
86     return nullptr;
87
88   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
89
90   FILE *file =  surf_fopen(filename, "r");
91   xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
92
93   char *line = nullptr;
94   size_t len = 0;
95   ssize_t read;
96   char path[1024];
97   sg_size_t size;
98
99   while ((read = xbt_getline(&line, &len, file)) != -1) {
100     if (read){
101       xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
102
103       usedSize_ += size;
104       sg_size_t *psize = xbt_new(sg_size_t, 1);
105       *psize = size;
106       xbt_dict_set(parse_content,path,psize,nullptr);
107     }
108   }
109   free(line);
110   fclose(file);
111   return parse_content;
112 }
113
114 bool Storage::isUsed()
115 {
116   THROW_UNIMPLEMENTED;
117   return false;
118 }
119
120 void Storage::apply_event(tmgr_trace_iterator_t /*event*/, double /*value*/)
121 {
122   THROW_UNIMPLEMENTED;
123 }
124
125 void Storage::turnOn() {
126   if (isOff()) {
127     Resource::turnOn();
128     storageStateChangedCallbacks(this, 0, 1);
129   }
130 }
131 void Storage::turnOff() {
132   if (isOn()) {
133     Resource::turnOff();
134     storageStateChangedCallbacks(this, 1, 0);
135   }
136 }
137
138 xbt_dict_t Storage::getContent()
139 {
140   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
141
142   xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
143   xbt_dict_cursor_t cursor = nullptr;
144   char *file;
145   sg_size_t *psize;
146
147   xbt_dict_foreach(content_, cursor, file, psize){
148     xbt_dict_set(content_dict,file,psize,nullptr);
149   }
150   return content_dict;
151 }
152
153 sg_size_t Storage::getSize(){
154   return size_;
155 }
156
157 sg_size_t Storage::getFreeSize(){
158   return size_ - usedSize_;
159 }
160
161 sg_size_t Storage::getUsedSize(){
162   return usedSize_;
163 }
164
165 /**********
166  * Action *
167  **********/
168 StorageAction::StorageAction(Model* model, double cost, bool failed, Storage* storage,
169                              e_surf_action_storage_type_t type)
170     : Action(model, cost, failed), type_(type), storage_(storage), file_(nullptr)
171 {
172   progress_ = 0;
173 };
174
175 StorageAction::StorageAction(Model* model, double cost, bool failed, lmm_variable_t var, Storage* storage,
176                              e_surf_action_storage_type_t type)
177     : Action(model, cost, failed, var), type_(type), storage_(storage), file_(nullptr)
178 {
179   progress_ = 0;
180 }
181
182 void StorageAction::setState(Action::State state){
183   Action::State old = getState();
184   Action::setState(state);
185   storageActionStateChangedCallbacks(this, old, state);
186 }
187
188 }
189 }