Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
separate the energy plugin from surf in the doc + improvements
[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 <string>
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
10
11 simgrid::surf::HostModel *surf_host_model = nullptr;
12
13 /*************
14  * Callbacks *
15  *************/
16
17 namespace simgrid {
18 namespace surf {
19
20 /*********
21  * Model *
22  *********/
23
24 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
25  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
26  * active task, the dummy CPU action must be deactivated, so that the VM does
27  * not get any CPU share in the PM layer. */
28 void HostModel::ignoreEmptyVmInPmLMM()
29 {
30   /* iterate for all virtual machines */
31   for (s4u::VirtualMachine* ws_vm : vm::VirtualMachineImpl::allVms_) {
32     Cpu* cpu = ws_vm->pimpl_cpu;
33     int active_tasks = lmm_constraint_get_variable_amount(cpu->constraint());
34
35     /* The impact of the VM over its PM is the min between its vCPU amount and the amount of tasks it contains */
36     int impact = std::min(active_tasks, ws_vm->pimpl_vm_->coreAmount());
37
38     XBT_DEBUG("set the weight of the dummy CPU action of VM%p on PM to %d (#tasks: %d)", ws_vm, impact, active_tasks);
39     ws_vm->pimpl_vm_->action_->setPriority(impact);
40   }
41 }
42
43 /* Helper function for executeParallelTask */
44 static inline double has_cost(double* array, int pos)
45 {
46   if (array)
47     return array[pos];
48   else
49     return -1.0;
50 }
51
52 Action* HostModel::executeParallelTask(int host_nb, simgrid::s4u::Host** host_list, double* flops_amount,
53     double* bytes_amount, double rate)
54 {
55   Action* action = nullptr;
56   if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
57     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
58   } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
59     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
60   } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
61     int nb = 0;
62     double value = 0.0;
63
64     for (int i = 0; i < host_nb * host_nb; i++) {
65       if (has_cost(bytes_amount, i) > 0.0) {
66         nb++;
67         value = has_cost(bytes_amount, i);
68       }
69     }
70     if (nb == 1) {
71       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
72     } else if (nb == 0) {
73       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
74           "ptask model");
75     } else {
76       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
77           "using the ptask model");
78     }
79   } else
80     xbt_die(
81         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
82         " - execution with one host only and no communication\n"
83         " - Self-comms with one host only\n"
84         " - Communications with two hosts and no computation");
85   xbt_free(host_list);
86   return action;
87 }
88
89 /************
90  * Resource *
91  ************/
92 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
93 {
94   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
95   delete piface_->pimpl_;
96   piface_->pimpl_ = this;
97 }
98
99 simgrid::surf::StorageImpl* HostImpl::findStorageOnMountList(const char* mount)
100 {
101   XBT_DEBUG("Search for storage name '%s' on '%s'", mount, piface_->cname());
102   if (storage_.find(mount) == storage_.end())
103     xbt_die("Can't find mount '%s' for '%s'", mount, piface_->cname());
104
105   return storage_.at(mount);
106 }
107
108 void HostImpl::getAttachedStorageList(std::vector<const char*>* storages)
109 {
110   for (auto s : storage_)
111     if (s.second->attach_ == piface_->cname())
112       storages->push_back(s.second->piface_.name());
113 }
114
115 Action* HostImpl::close(surf_file_t fd)
116 {
117   simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount);
118   XBT_DEBUG("CLOSE %s on disk '%s'", fd->name, st->cname());
119   return st->close(fd);
120 }
121
122 Action* HostImpl::read(surf_file_t fd, sg_size_t size)
123 {
124   simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount);
125   XBT_DEBUG("READ %s on disk '%s'", fd->name, st->cname());
126   return st->read(fd, size);
127 }
128
129 Action* HostImpl::write(surf_file_t fd, sg_size_t size)
130 {
131   simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount);
132   XBT_DEBUG("WRITE %s on disk '%s'", fd->name, st->cname());
133   return st->write(fd, size);
134 }
135
136 int HostImpl::unlink(surf_file_t fd)
137 {
138   if (not fd) {
139     XBT_WARN("No such file descriptor. Impossible to unlink");
140     return -1;
141   } else {
142
143     simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount);
144     /* Check if the file is on this storage */
145     if (st->content_->find(fd->name) == st->content_->end()) {
146       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name, st->cname());
147       return -1;
148     } else {
149       XBT_DEBUG("UNLINK %s on disk '%s'", fd->name, st->cname());
150       st->usedSize_ -= fd->size;
151
152       // Remove the file from storage
153       st->content_->erase(fd->name);
154
155       xbt_free(fd->name);
156       xbt_free(fd->mount);
157       xbt_free(fd);
158       return 0;
159     }
160   }
161 }
162
163 sg_size_t HostImpl::getSize(surf_file_t fd)
164 {
165   return fd->size;
166 }
167
168 sg_size_t HostImpl::fileTell(surf_file_t fd)
169 {
170   return fd->current_position;
171 }
172
173 int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin)
174 {
175   switch (origin) {
176   case SEEK_SET:
177     fd->current_position = offset;
178     return 0;
179   case SEEK_CUR:
180     fd->current_position += offset;
181     return 0;
182   case SEEK_END:
183     fd->current_position = fd->size + offset;
184     return 0;
185   default:
186     return -1;
187   }
188 }
189
190 int HostImpl::fileMove(surf_file_t fd, const char* fullpath)
191 {
192   /* Check if the new full path is on the same mount point */
193   if (not strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
194     std::map<std::string, sg_size_t>* content = findStorageOnMountList(fd->mount)->content_;
195     if (content->find(fd->name) != content->end()) { // src file exists
196       sg_size_t new_size = content->at(std::string(fd->name));
197       content->erase(fd->name);
198       std::string path = std::string(fullpath).substr(strlen(fd->mount), strlen(fullpath));
199       content->insert({path.c_str(), new_size});
200       XBT_DEBUG("Move file from %s to %s, size '%llu'", fd->name, fullpath, new_size);
201       return 0;
202     } else {
203       XBT_WARN("File %s doesn't exist", fd->name);
204       return -1;
205     }
206   } else {
207     XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);
208     return -1;
209   }
210 }
211
212 }
213 }