Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e8e83220aaabfed04456ebfc3bb7256127cc193
[simgrid.git] / src / surf / workstation_interface.cpp
1 #include "workstation_interface.hpp"
2 #include "vm_workstation_interface.hpp"
3 #include "cpu_cas01.hpp"
4 #include "simgrid/sg_config.h"
5
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
7                                 "Logging specific to the SURF workstation module");
8
9 WorkstationModelPtr surf_workstation_model = NULL;
10
11 /*********
12  * Model *
13  *********/
14 WorkstationModel::WorkstationModel(const char *name)
15  : Model(name)
16 {
17   p_cpuModel = surf_cpu_model_pm;
18 }
19
20 WorkstationModel::WorkstationModel()
21 : Model("Workstation") {
22   p_cpuModel = surf_cpu_model_pm;
23 }
24
25 WorkstationModel::~WorkstationModel() {
26 }
27
28
29
30 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
31  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
32  * active task, the dummy CPU action must be deactivated, so that the VM does
33  * not get any CPU share in the PM layer. */
34 void WorkstationModel::adjustWeightOfDummyCpuActions()
35 {
36   /* iterate for all hosts including virtual machines */
37   xbt_lib_cursor_t cursor;
38   char *key;
39   void **ind_host;
40
41   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
42     WorkstationPtr ws = dynamic_cast<WorkstationPtr>(
43                                        static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
44     CpuCas01Ptr cpu_cas01 = dynamic_cast<CpuCas01Ptr>(
45                                static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
46
47     if (!ws)
48       continue;
49     /* skip if it is not a virtual machine */
50     if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
51       continue;
52     xbt_assert(cpu_cas01, "cpu-less workstation");
53
54     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
55     WorkstationVMPtr ws_vm = dynamic_cast<WorkstationVMPtr>(ws);
56
57     int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
58     // int is_active_old = constraint_is_active(cpu_cas01);
59
60     // {
61     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
62     // }
63
64     if (is_active) {
65       /* some tasks exist on this VM */
66       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
67
68       /* FIXME: we shoud use lmm_update_variable_weight() ? */
69       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
70       ws_vm->p_action->setPriority(1);
71
72     } else {
73       /* no task exits on this VM */
74       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
75
76       ws_vm->p_action->setPriority(0);
77     }
78   }
79 }
80
81 /************
82  * Resource *
83  ************/
84 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props,
85                                  xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
86  : Resource(model, name, props)
87  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
88 {}
89
90 Workstation::Workstation(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
91                                          xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
92  : Resource(model, name, props, constraint)
93  , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
94 {}
95
96 int Workstation::getCore(){
97   return p_cpu->getCore();
98 }
99
100 double Workstation::getSpeed(double load){
101   return p_cpu->getSpeed(load);
102 }
103
104 double Workstation::getAvailableSpeed(){
105   return p_cpu->getAvailableSpeed();
106 }
107
108 double Workstation::getCurrentPowerPeak()
109 {
110   return p_cpu->getCurrentPowerPeak();
111 }
112
113 double Workstation::getPowerPeakAt(int pstate_index)
114 {
115   return p_cpu->getPowerPeakAt(pstate_index);
116 }
117
118 int Workstation::getNbPstates()
119 {
120   return p_cpu->getNbPstates();
121 }
122
123 void Workstation::setPowerPeakAt(int pstate_index)
124 {
125         p_cpu->setPowerPeakAt(pstate_index);
126 }
127
128 double Workstation::getConsumedEnergy()
129 {
130   return p_cpu->getConsumedEnergy();
131 }
132
133 xbt_dict_t Workstation::getProperties()
134 {
135   return p_cpu->getProperties();
136 }
137
138
139 StoragePtr Workstation::findStorageOnMountList(const char* mount)
140 {
141   StoragePtr st = NULL;
142   s_mount_t mnt;
143   unsigned int cursor;
144
145   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
146   xbt_dynar_foreach(p_storage,cursor,mnt)
147   {
148     XBT_DEBUG("See '%s'",mnt.name);
149     if(!strcmp(mount,mnt.name)){
150       st = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage));
151       break;
152     }
153   }
154   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
155   return st;
156 }
157
158 xbt_dict_t Workstation::getStorageList()
159 {
160   s_mount_t mnt;
161   unsigned int i;
162   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
163   char *storage_name = NULL;
164
165   xbt_dynar_foreach(p_storage,i,mnt){
166     storage_name = (char *)dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage))->getName();
167     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
168   }
169   return storage_list;
170 }
171
172 ActionPtr Workstation::open(const char* mount, const char* path) {
173   StoragePtr st = findStorageOnMountList(mount);
174   XBT_DEBUG("OPEN on disk '%s'", st->getName());
175   return st->open(mount, path);
176 }
177
178 ActionPtr Workstation::close(surf_file_t fd) {
179   StoragePtr st = findStorageOnMountList(fd->mount);
180   XBT_DEBUG("CLOSE on disk '%s'",st->getName());
181   return st->close(fd);
182 }
183
184 ActionPtr Workstation::read(surf_file_t fd, sg_size_t size) {
185   StoragePtr st = findStorageOnMountList(fd->mount);
186   XBT_DEBUG("READ on disk '%s'",st->getName());
187   return st->read(fd, size);
188 }
189
190 ActionPtr Workstation::write(surf_file_t fd, sg_size_t size) {
191   StoragePtr st = findStorageOnMountList(fd->mount);
192   XBT_DEBUG("WRITE on disk '%s'",st->getName());
193   return st->write(fd, size);
194 }
195
196 int Workstation::unlink(surf_file_t fd) {
197   if (!fd){
198     XBT_WARN("No such file descriptor. Impossible to unlink");
199     return 0;
200   } else {
201 //    XBT_INFO("%s %zu", fd->storage, fd->size);
202     StoragePtr st = findStorageOnMountList(fd->mount);
203     /* Check if the file is on this storage */
204     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
205       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
206           st->getName());
207       return 0;
208     } else {
209       XBT_DEBUG("UNLINK on disk '%s'",st->getName());
210       st->m_usedSize -= fd->size;
211
212       // Remove the file from storage
213       xbt_dict_remove(st->p_content, fd->name);
214
215       free(fd->name);
216       free(fd->mount);
217       xbt_free(fd);
218       return 1;
219     }
220   }
221 }
222
223 ActionPtr Workstation::ls(const char* mount, const char *path){
224   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
225   StoragePtr st = findStorageOnMountList(mount);
226   return st->ls(path);
227 }
228
229 sg_size_t Workstation::getSize(surf_file_t fd){
230   return fd->size;
231 }
232
233 xbt_dynar_t Workstation::getInfo( surf_file_t fd)
234 {
235   StoragePtr st = findStorageOnMountList(fd->mount);
236   sg_size_t *psize = xbt_new(sg_size_t, 1);
237   *psize = fd->size;
238   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
239   xbt_dynar_push_as(info, sg_size_t *, psize);
240   xbt_dynar_push_as(info, void *, fd->mount);
241   xbt_dynar_push_as(info, void *, (void *)st->getName());
242   xbt_dynar_push_as(info, void *, st->p_typeId);
243   xbt_dynar_push_as(info, void *, st->p_contentType);
244
245   return info;
246 }
247
248 sg_size_t Workstation::fileTell(surf_file_t fd){
249   return fd->current_position;
250 }
251
252 int Workstation::fileSeek(surf_file_t fd, sg_size_t offset, int origin){
253
254   switch (origin) {
255   case SEEK_SET:
256     fd->current_position = 0;
257         return MSG_OK;
258   case SEEK_CUR:
259         if(offset > fd->size)
260           offset = fd->size;
261         fd->current_position = offset;
262         return MSG_OK;
263   case SEEK_END:
264         fd->current_position = fd->size;
265         return MSG_OK;
266   default:
267         return MSG_TASK_CANCELED;
268   }
269 }
270
271 sg_size_t Workstation::getFreeSize(const char* name)
272 {
273   StoragePtr st = findStorageOnMountList(name);
274   return st->m_size - st->m_usedSize;
275 }
276
277 sg_size_t Workstation::getUsedSize(const char* name)
278 {
279   StoragePtr st = findStorageOnMountList(name);
280   return st->m_usedSize;
281 }
282
283 xbt_dynar_t Workstation::getVms()
284 {
285   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
286
287   /* iterate for all hosts including virtual machines */
288   xbt_lib_cursor_t cursor;
289   char *key;
290   void **ind_host;
291   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
292     WorkstationPtr ws = dynamic_cast<WorkstationPtr>(static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
293     if (!ws)
294       continue;
295     /* skip if it is not a virtual machine */
296     if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
297       continue;
298
299     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
300     WorkstationVMPtr ws_vm = dynamic_cast<WorkstationVMPtr>(ws);
301     if (this == ws_vm-> p_subWs)
302       xbt_dynar_push(dyn, &ws_vm->p_subWs);
303   }
304
305   return dyn;
306 }
307
308 void Workstation::getParams(ws_params_t params)
309 {
310   memcpy(params, &p_params, sizeof(s_ws_params_t));
311 }
312
313 void Workstation::setParams(ws_params_t params)
314 {
315   /* may check something here. */
316   memcpy(&p_params, params, sizeof(s_ws_params_t));
317 }
318
319 /**********
320  * Action *
321  **********/