Logo AND Algorithmique Numérique Distribuée

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