Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move some of the file mgmt logic out of the storage model
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/plugins/vm/VirtualMachineImpl.hpp"
7 #include "src/surf/FileImpl.hpp"
8 #include <string>
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
11
12 simgrid::surf::HostModel *surf_host_model = nullptr;
13
14 /*************
15  * Callbacks *
16  *************/
17
18 namespace simgrid {
19 namespace surf {
20
21 /*********
22  * Model *
23  *********/
24
25 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
26  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
27  * active task, the dummy CPU action must be deactivated, so that the VM does
28  * not get any CPU share in the PM layer. */
29 void HostModel::ignoreEmptyVmInPmLMM()
30 {
31   /* iterate for all virtual machines */
32   for (s4u::VirtualMachine* ws_vm : vm::VirtualMachineImpl::allVms_) {
33     Cpu* cpu = ws_vm->pimpl_cpu;
34     int active_tasks = lmm_constraint_get_variable_amount(cpu->constraint());
35
36     /* The impact of the VM over its PM is the min between its vCPU amount and the amount of tasks it contains */
37     int impact = std::min(active_tasks, ws_vm->pimpl_vm_->coreAmount());
38
39     XBT_DEBUG("set the weight of the dummy CPU action of VM%p on PM to %d (#tasks: %d)", ws_vm, impact, active_tasks);
40     if (impact > 0)
41       ws_vm->pimpl_vm_->action_->setSharingWeight(1. / impact);
42     else
43       ws_vm->pimpl_vm_->action_->setSharingWeight(0.);
44   }
45 }
46
47 /* Helper function for executeParallelTask */
48 static inline double has_cost(double* array, int pos)
49 {
50   if (array)
51     return array[pos];
52   else
53     return -1.0;
54 }
55
56 Action* HostModel::executeParallelTask(int host_nb, simgrid::s4u::Host** host_list, double* flops_amount,
57     double* bytes_amount, double rate)
58 {
59   Action* action = nullptr;
60   if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
61     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
62   } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
63     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
64   } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
65     int nb = 0;
66     double value = 0.0;
67
68     for (int i = 0; i < host_nb * host_nb; i++) {
69       if (has_cost(bytes_amount, i) > 0.0) {
70         nb++;
71         value = has_cost(bytes_amount, i);
72       }
73     }
74     if (nb == 1) {
75       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
76     } else if (nb == 0) {
77       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
78           "ptask model");
79     } else {
80       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
81           "using the ptask model");
82     }
83   } else
84     xbt_die(
85         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
86         " - execution with one host only and no communication\n"
87         " - Self-comms with one host only\n"
88         " - Communications with two hosts and no computation");
89   xbt_free(host_list);
90   return action;
91 }
92
93 /************
94  * Resource *
95  ************/
96 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
97 {
98   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
99   delete piface_->pimpl_;
100   piface_->pimpl_ = this;
101 }
102
103 simgrid::surf::StorageImpl* HostImpl::findStorageOnMountList(const char* mount)
104 {
105   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, piface_->getCname());
106   if (storage_.find(mount) == storage_.end())
107     xbt_die("Can't find mount '%s' for '%s'", mount, piface_->getCname());
108
109   return storage_.at(mount);
110 }
111
112 void HostImpl::getAttachedStorageList(std::vector<const char*>* storages)
113 {
114   for (auto s : storage_)
115     if (s.second->attach_ == piface_->getCname())
116       storages->push_back(s.second->piface_.getName());
117 }
118
119 Action* HostImpl::read(surf_file_t fd, sg_size_t size)
120 {
121   simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount());
122   XBT_DEBUG("READ %s on disk '%s'", fd->cname(), st->cname());
123   if (fd->tell() + size > fd->size()) {
124     if (fd->tell() > fd->size()) {
125       size = 0;
126     } else {
127       size = fd->size() - fd->tell();
128     }
129     fd->setPosition(fd->size());
130   } else
131     fd->incrPosition(size);
132
133   return st->read(size);
134 }
135
136 Action* HostImpl::write(surf_file_t fd, sg_size_t size)
137 {
138   simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount());
139   XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", fd->cname(), st->cname(), size, fd->size());
140
141   StorageAction* action = st->write(size);
142   action->file_         = fd;
143   /* Substract the part of the file that might disappear from the used sized on the storage element */
144   st->usedSize_ -= (fd->size() - fd->tell());
145   // If the storage is full before even starting to write
146   if (st->usedSize_ >= st->size_) {
147     action->setState(Action::State::failed);
148   }
149   return action;
150 }
151
152 }
153 }