Logo AND Algorithmique Numérique Distribuée

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