Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4771d97823ee28e1db6dccfa02fbea5503f3ebf5
[simgrid.git] / src / surf / workstation.cpp
1 #include "workstation.hpp"
2 #include "vm_workstation.hpp"
3 #include "cpu_cas01.hpp"
4 #include "simgrid/sg_config.h"
5
6 extern "C" {
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_workstation, surf,
8                                 "Logging specific to the SURF workstation module");
9 }
10
11 WorkstationModelPtr surf_workstation_model = NULL;
12
13 //FIXME:Faire hériter ou composer de cup et network
14
15 /*************
16  * CallBacks *
17  *************/
18
19 static void workstation_new(sg_platf_host_cbarg_t host){
20   surf_workstation_model->createResource(host->id);
21 }
22
23 /*********
24  * Model *
25  *********/
26
27 void surf_workstation_model_init_current_default(void)
28 {
29   surf_workstation_model = new WorkstationModel();
30   xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", "yes");
31   surf_cpu_model_init_Cas01();
32   surf_network_model_init_LegrandVelho();
33
34   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
35   xbt_dynar_push(model_list, &model);
36   xbt_dynar_push(model_list_invoke, &model);
37   sg_platf_host_add_cb(workstation_new);
38 }
39
40 void surf_workstation_model_init_compound()
41 {
42
43   xbt_assert(surf_cpu_model_pm, "No CPU model defined yet!");
44   xbt_assert(surf_network_model, "No network model defined yet!");
45   surf_workstation_model = new WorkstationModel();
46
47   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
48   xbt_dynar_push(model_list, &model);
49   xbt_dynar_push(model_list_invoke, &model);
50   sg_platf_host_add_cb(workstation_new);
51 }
52
53 WorkstationModel::WorkstationModel() : Model("Workstation") {
54 }
55
56 WorkstationModel::~WorkstationModel() {
57 }
58
59 void WorkstationModel::parseInit(sg_platf_host_cbarg_t host){
60   createResource(host->id);
61 }
62
63 WorkstationCLM03Ptr WorkstationModel::createResource(string name){
64
65   WorkstationCLM03Ptr workstation = new WorkstationCLM03(surf_workstation_model, name.c_str(), NULL,
66                   (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name.c_str(), ROUTING_STORAGE_HOST_LEVEL),
67                   (RoutingEdgePtr)xbt_lib_get_or_null(host_lib, name.c_str(), ROUTING_HOST_LEVEL),
68                   dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(host_lib, name.c_str(), SURF_CPU_LEVEL))));
69   XBT_DEBUG("Create workstation %s with %ld mounted disks", name.c_str(), xbt_dynar_length(workstation->p_storage));
70   xbt_lib_set(host_lib, name.c_str(), SURF_WKS_LEVEL, static_cast<ResourcePtr>(workstation));
71   return workstation;
72 }
73
74 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
75  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
76  * active task, the dummy CPU action must be deactivated, so that the VM does
77  * not get any CPU share in the PM layer. */
78 void WorkstationModel::adjustWeightOfDummyCpuActions()
79 {
80   /* iterate for all hosts including virtual machines */
81   xbt_lib_cursor_t cursor;
82   char *key;
83   void **ind_host;
84
85   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
86     WorkstationCLM03Ptr ws_clm03 = dynamic_cast<WorkstationCLM03Ptr>(
87                                        static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
88     CpuCas01LmmPtr cpu_cas01 = dynamic_cast<CpuCas01LmmPtr>(
89                                static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
90
91     if (!ws_clm03)
92       continue;
93     /* skip if it is not a virtual machine */
94     if (ws_clm03->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
95       continue;
96     xbt_assert(cpu_cas01, "cpu-less workstation");
97
98     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
99     WorkstationVM2013Ptr ws_vm2013 = dynamic_cast<WorkstationVM2013Ptr>(ws_clm03);
100
101     int is_active = lmm_constraint_used(cpu_cas01->p_model->p_maxminSystem, cpu_cas01->p_constraint);
102     // int is_active_old = constraint_is_active(cpu_cas01);
103
104     // {
105     //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
106     // }
107
108     if (is_active) {
109       /* some tasks exist on this VM */
110       XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
111
112       /* FIXME: we shoud use lmm_update_variable_weight() ? */
113       /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
114       surf_action_set_priority(ws_vm2013->p_action, 1);
115
116     } else {
117       /* no task exits on this VM */
118       XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
119
120       surf_action_set_priority(ws_vm2013->p_action, 0);
121     }
122   }
123 }
124
125 double WorkstationModel::shareResources(double now){
126   adjustWeightOfDummyCpuActions();
127
128   double min_by_cpu = surf_cpu_model_pm->shareResources(now);
129   double min_by_net = surf_network_model->shareResources(now);
130
131   XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f",
132       this, surf_cpu_model_pm->m_name.c_str(), min_by_cpu, surf_network_model->m_name.c_str(), min_by_net);
133
134   if (min_by_cpu >= 0.0 && min_by_net >= 0.0)
135     return min(min_by_cpu, min_by_net);
136   else if (min_by_cpu >= 0.0)
137     return min_by_cpu;
138   else if (min_by_net >= 0.0)
139     return min_by_net;
140   else
141     return min_by_cpu;  /* probably min_by_cpu == min_by_net == -1 */
142 }
143
144 void WorkstationModel::updateActionsState(double now, double delta){
145   return;
146 }
147
148 ActionPtr WorkstationModel::executeParallelTask(int workstation_nb,
149                                         void **workstation_list,
150                                         double *computation_amount,
151                                         double *communication_amount,
152                                         double rate){
153 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
154   if ((workstation_nb == 1)
155       && (cost_or_zero(communication_amount, 0) == 0.0))
156     return ((WorkstationCLM03Ptr)workstation_list[0])->execute(computation_amount[0]);
157   else if ((workstation_nb == 1)
158            && (cost_or_zero(computation_amount, 0) == 0.0))
159     return communicate((WorkstationCLM03Ptr)workstation_list[0], (WorkstationCLM03Ptr)workstation_list[0],communication_amount[0], rate);
160   else if ((workstation_nb == 2)
161              && (cost_or_zero(computation_amount, 0) == 0.0)
162              && (cost_or_zero(computation_amount, 1) == 0.0)) {
163     int i,nb = 0;
164     double value = 0.0;
165
166     for (i = 0; i < workstation_nb * workstation_nb; i++) {
167       if (cost_or_zero(communication_amount, i) > 0.0) {
168         nb++;
169         value = cost_or_zero(communication_amount, i);
170       }
171     }
172     if (nb == 1)
173       return communicate((WorkstationCLM03Ptr)workstation_list[0], (WorkstationCLM03Ptr)workstation_list[1],value, rate);
174   }
175 #undef cost_or_zero
176
177   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks */
178   return NULL;
179 }
180
181 /* returns an array of network_link_CM02_t */
182 xbt_dynar_t WorkstationModel::getRoute(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst)
183 {
184   XBT_DEBUG("ws_get_route");
185   return surf_network_model->getRoute(src->p_netElm, dst->p_netElm);
186 }
187
188 ActionPtr WorkstationModel::communicate(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst, double size, double rate){
189   return surf_network_model->communicate(src->p_netElm, dst->p_netElm, size, rate);
190 }
191
192
193
194 /************
195  * Resource *
196  ************/
197 WorkstationCLM03::WorkstationCLM03(WorkstationModelPtr model, const char* name, xbt_dict_t properties, xbt_dynar_t storage, RoutingEdgePtr netElm, CpuPtr cpu)
198   : Resource(model, name, properties), p_storage(storage), p_netElm(netElm), p_cpu(cpu) {}
199
200 bool WorkstationCLM03::isUsed(){
201   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
202   return -1;
203 }
204
205 void WorkstationCLM03::updateState(tmgr_trace_event_t event_type, double value, double date){
206   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
207 }
208
209 ActionPtr WorkstationCLM03::execute(double size) {
210   return p_cpu->execute(size);
211 }
212
213 ActionPtr WorkstationCLM03::sleep(double duration) {
214   return p_cpu->sleep(duration);
215 }
216
217 e_surf_resource_state_t WorkstationCLM03::getState() {
218   return p_cpu->getState();
219 }
220
221 int WorkstationCLM03::getCore(){
222   return p_cpu->getCore();
223 }
224
225 double WorkstationCLM03::getSpeed(double load){
226   return p_cpu->getSpeed(load);
227 }
228
229 double WorkstationCLM03::getAvailableSpeed(){
230   return p_cpu->getAvailableSpeed();
231 }
232
233 double WorkstationCLM03::getCurrentPowerPeak()
234 {
235   return p_cpu->getCurrentPowerPeak();
236 }
237
238 double WorkstationCLM03::getPowerPeakAt(int pstate_index)
239 {
240   return p_cpu->getPowerPeakAt(pstate_index);
241 }
242
243 int WorkstationCLM03::getNbPstates()
244 {
245   return p_cpu->getNbPstates();
246 }
247
248 void WorkstationCLM03::setPowerPeakAt(int pstate_index)
249 {
250         p_cpu->setPowerPeakAt(pstate_index);
251 }
252
253 double WorkstationCLM03::getConsumedEnergy()
254 {
255   return p_cpu->getConsumedEnergy();
256 }
257
258
259 xbt_dict_t WorkstationCLM03::getProperties()
260 {
261   return p_cpu->m_properties;
262 }
263
264
265 StoragePtr WorkstationCLM03::findStorageOnMountList(const char* mount)
266 {
267   StoragePtr st = NULL;
268   s_mount_t mnt;
269   unsigned int cursor;
270
271   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, m_name);
272   xbt_dynar_foreach(p_storage,cursor,mnt)
273   {
274     XBT_DEBUG("See '%s'",mnt.name);
275     if(!strcmp(mount,mnt.name)){
276       st = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage));
277       break;
278     }
279   }
280   if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, m_name);
281   return st;
282 }
283
284 xbt_dict_t WorkstationCLM03::getStorageList()
285 {
286   s_mount_t mnt;
287   unsigned int i;
288   xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
289   char *storage_name = NULL;
290
291   xbt_dynar_foreach(p_storage,i,mnt){
292     storage_name = (char *)dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage))->m_name;
293     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
294   }
295   return storage_list;
296 }
297
298 ActionPtr WorkstationCLM03::open(const char* mount, const char* path) {
299   StoragePtr st = findStorageOnMountList(mount);
300   XBT_DEBUG("OPEN on disk '%s'", st->m_name);
301   return st->open(mount, path);
302 }
303
304 ActionPtr WorkstationCLM03::close(surf_file_t fd) {
305   StoragePtr st = findStorageOnMountList(fd->mount);
306   XBT_DEBUG("CLOSE on disk '%s'",st->m_name);
307   return st->close(fd);
308 }
309
310 ActionPtr WorkstationCLM03::read(surf_file_t fd, sg_storage_size_t size) {
311   StoragePtr st = findStorageOnMountList(fd->mount);
312   XBT_DEBUG("READ on disk '%s'",st->m_name);
313   return st->read(fd, size);
314 }
315
316 ActionPtr WorkstationCLM03::write(surf_file_t fd, sg_storage_size_t size) {
317   StoragePtr st = findStorageOnMountList(fd->mount);
318   XBT_DEBUG("WRITE on disk '%s'",st->m_name);
319   return st->write(fd, size);
320 }
321
322 int WorkstationCLM03::unlink(surf_file_t fd) {
323   if (!fd){
324     XBT_WARN("No such file descriptor. Impossible to unlink");
325     return 0;
326   } else {
327 //    XBT_INFO("%s %zu", fd->storage, fd->size);
328     StoragePtr st = findStorageOnMountList(fd->mount);
329     /* Check if the file is on this storage */
330     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
331       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
332           st->m_name);
333       return 0;
334     } else {
335       XBT_DEBUG("UNLINK on disk '%s'",st->m_name);
336       st->m_usedSize -= fd->size;
337
338       // Remove the file from storage
339       xbt_dict_remove(st->p_content, fd->name);
340
341       free(fd->name);
342       free(fd->mount);
343       xbt_free(fd);
344       return 1;
345     }
346   }
347 }
348
349 ActionPtr WorkstationCLM03::ls(const char* mount, const char *path){
350   XBT_DEBUG("LS on mount '%s' and file '%s'", mount, path);
351   StoragePtr st = findStorageOnMountList(mount);
352   return st->ls(path);
353 }
354
355 sg_storage_size_t WorkstationCLM03::getSize(surf_file_t fd){
356   return fd->size;
357 }
358
359 xbt_dynar_t WorkstationCLM03::getInfo( surf_file_t fd)
360 {
361   StoragePtr st = findStorageOnMountList(fd->mount);
362   sg_storage_size_t *psize = xbt_new(sg_storage_size_t, 1);
363   *psize = fd->size;
364   xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
365   xbt_dynar_push_as(info, sg_storage_size_t *, psize);
366   xbt_dynar_push_as(info, void *, fd->mount);
367   xbt_dynar_push_as(info, void *, (void *)st->m_name);
368   xbt_dynar_push_as(info, void *, st->p_typeId);
369   xbt_dynar_push_as(info, void *, st->p_contentType);
370
371   return info;
372 }
373
374 sg_storage_size_t WorkstationCLM03::getFreeSize(const char* name)
375 {
376   StoragePtr st = findStorageOnMountList(name);
377   return st->m_size - st->m_usedSize;
378 }
379
380 sg_storage_size_t WorkstationCLM03::getUsedSize(const char* name)
381 {
382   StoragePtr st = findStorageOnMountList(name);
383   return st->m_usedSize;
384 }
385
386 e_surf_resource_state_t WorkstationCLM03Lmm::getState() {
387   return WorkstationCLM03::getState();
388 }
389
390 xbt_dynar_t WorkstationCLM03::getVms()
391 {
392   xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
393
394   /* iterate for all hosts including virtual machines */
395   xbt_lib_cursor_t cursor;
396   char *key;
397   void **ind_host;
398   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
399     WorkstationCLM03Ptr ws_clm03 = dynamic_cast<WorkstationCLM03Ptr>(static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
400     if (!ws_clm03)
401       continue;
402     /* skip if it is not a virtual machine */
403     if (ws_clm03->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
404       continue;
405
406     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
407     WorkstationVM2013Ptr ws_vm2013 = dynamic_cast<WorkstationVM2013Ptr>(ws_clm03);
408     if (this == ws_vm2013-> p_subWs)
409       xbt_dynar_push(dyn, &ws_vm2013->p_subWs);
410   }
411
412   return dyn;
413 }
414
415 void WorkstationCLM03::getParams(ws_params_t params)
416 {
417   memcpy(params, &p_params, sizeof(s_ws_params_t));
418 }
419
420 void WorkstationCLM03::setParams(ws_params_t params)
421 {
422   /* may check something here. */
423   memcpy(&p_params, params, sizeof(s_ws_params_t));
424 }
425 /**********
426  * Action *
427  **********/