Logo AND Algorithmique Numérique Distribuée

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