1 /* Copyright (c) 2013-2016. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/surf/HostImpl.hpp"
7 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
12 simgrid::surf::HostModel *surf_host_model = nullptr;
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::adjustWeightOfDummyCpuActions()
31 /* iterate for all virtual machines */
32 for (s4u::VirtualMachine* ws_vm : vm::VirtualMachineImpl::allVms_) {
34 Cpu* cpu = ws_vm->pimpl_cpu;
36 int is_active = lmm_constraint_used(cpu->model()->getMaxminSystem(), cpu->constraint());
39 /* some tasks exist on this VM */
40 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
42 /* FIXME: we should use lmm_update_variable_weight() ? */
43 /* FIXME: If we assign 1.05 and 0.05, the system makes apparently wrong values. */
44 ws_vm->pimpl_vm_->action_->setPriority(1);
47 /* no task exits on this VM */
48 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
50 ws_vm->pimpl_vm_->action_->setPriority(0);
55 /* Helper function for executeParallelTask */
56 static inline double has_cost(double* array, int pos)
63 Action* HostModel::executeParallelTask(int host_nb, simgrid::s4u::Host** host_list, double* flops_amount,
64 double* bytes_amount, double rate)
66 Action* action = nullptr;
67 if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
68 action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
69 } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
70 action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
71 } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
75 for (int i = 0; i < host_nb * host_nb; i++) {
76 if (has_cost(bytes_amount, i) > 0.0) {
78 value = has_cost(bytes_amount, i);
82 action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
84 xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
87 xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
88 "using the ptask model");
92 "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
93 " - execution with one host only and no communication\n"
94 " - Self-comms with one host only\n"
95 " - Communications with two hosts and no computation");
103 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
105 /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
106 delete piface_->pimpl_;
107 piface_->pimpl_ = this;
110 /** @brief use destroy() instead of this destructor */
111 HostImpl::~HostImpl() = default;
113 simgrid::surf::Storage* HostImpl::findStorageOnMountList(const char* mount)
115 simgrid::surf::Storage* st = nullptr;
119 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, piface_->cname());
120 xbt_dynar_foreach (storage_, cursor, mnt) {
121 XBT_DEBUG("See '%s'", mnt.name);
122 if (!strcmp(mount, mnt.name)) {
123 st = static_cast<simgrid::surf::Storage*>(mnt.storage);
128 xbt_die("Can't find mount '%s' for '%s'", mount, piface_->cname());
132 xbt_dict_t HostImpl::getMountedStorageList()
136 xbt_dict_t storage_list = xbt_dict_new_homogeneous(nullptr);
137 char* storage_name = nullptr;
139 xbt_dynar_foreach (storage_, i, mnt) {
140 storage_name = (char*)static_cast<simgrid::surf::Storage*>(mnt.storage)->cname();
141 xbt_dict_set(storage_list, mnt.name, storage_name, nullptr);
146 xbt_dynar_t HostImpl::getAttachedStorageList()
148 xbt_lib_cursor_t cursor;
151 xbt_dynar_t result = xbt_dynar_new(sizeof(void*), nullptr);
152 xbt_lib_foreach(storage_lib, cursor, key, data)
154 if (xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != nullptr) {
155 simgrid::surf::Storage* storage = static_cast<simgrid::surf::Storage*>(
156 xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
157 if (!strcmp(static_cast<const char*>(storage->attach_), piface_->cname())) {
158 xbt_dynar_push_as(result, void*, (void*)storage->cname());
165 Action* HostImpl::open(const char* fullpath)
168 simgrid::surf::Storage* st = nullptr;
171 size_t longest_prefix_length = 0;
173 std::string mount_name;
175 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, piface_->cname());
176 xbt_dynar_foreach (storage_, cursor, mnt) {
177 XBT_DEBUG("See '%s'", mnt.name);
178 std::string file_mount_name = std::string(fullpath).substr(0, strlen(mnt.name));
180 if (!strcmp(file_mount_name.c_str(), mnt.name) &&
181 strlen(mnt.name) > longest_prefix_length) { /* The current mount name is found in the full path and is
182 bigger than the previous*/
183 longest_prefix_length = strlen(mnt.name);
184 st = static_cast<simgrid::surf::Storage*>(mnt.storage);
187 if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
188 mount_name = std::string(fullpath).substr(0, longest_prefix_length);
189 path = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath));
191 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, piface_->cname());
193 XBT_DEBUG("OPEN %s on disk '%s'", path.c_str(), st->cname());
194 Action* action = st->open(mount_name.c_str(), path.c_str());
198 Action* HostImpl::close(surf_file_t fd)
200 simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
201 XBT_DEBUG("CLOSE %s on disk '%s'", fd->name, st->cname());
202 return st->close(fd);
205 Action* HostImpl::read(surf_file_t fd, sg_size_t size)
207 simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
208 XBT_DEBUG("READ %s on disk '%s'", fd->name, st->cname());
209 return st->read(fd, size);
212 Action* HostImpl::write(surf_file_t fd, sg_size_t size)
214 simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
215 XBT_DEBUG("WRITE %s on disk '%s'", fd->name, st->cname());
216 return st->write(fd, size);
219 int HostImpl::unlink(surf_file_t fd)
222 XBT_WARN("No such file descriptor. Impossible to unlink");
226 simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
227 /* Check if the file is on this storage */
228 if (!xbt_dict_get_or_null(st->content_, fd->name)) {
229 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name, st->cname());
232 XBT_DEBUG("UNLINK %s on disk '%s'", fd->name, st->cname());
233 st->usedSize_ -= fd->size;
235 // Remove the file from storage
236 xbt_dict_remove(st->content_, fd->name);
246 sg_size_t HostImpl::getSize(surf_file_t fd)
251 xbt_dynar_t HostImpl::getInfo(surf_file_t fd)
253 simgrid::surf::Storage* st = findStorageOnMountList(fd->mount);
254 sg_size_t* psize = xbt_new(sg_size_t, 1);
256 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), nullptr);
257 xbt_dynar_push_as(info, sg_size_t*, psize);
258 xbt_dynar_push_as(info, void*, fd->mount);
259 xbt_dynar_push_as(info, void*, (void*)st->cname());
260 xbt_dynar_push_as(info, void*, st->typeId_);
261 xbt_dynar_push_as(info, void*, st->contentType_);
266 sg_size_t HostImpl::fileTell(surf_file_t fd)
268 return fd->current_position;
271 int HostImpl::fileSeek(surf_file_t fd, sg_offset_t offset, int origin)
276 fd->current_position = offset;
279 fd->current_position += offset;
282 fd->current_position = fd->size + offset;
289 int HostImpl::fileMove(surf_file_t fd, const char* fullpath)
291 /* Check if the new full path is on the same mount point */
292 if (!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
293 sg_size_t* psize = (sg_size_t*)xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->content_, fd->name);
294 if (psize) { // src file exists
295 sg_size_t* new_psize = xbt_new(sg_size_t, 1);
297 xbt_dict_remove(findStorageOnMountList(fd->mount)->content_, fd->name);
298 std::string path = std::string(fullpath).substr(strlen(fd->mount), strlen(fullpath));
299 xbt_dict_set(findStorageOnMountList(fd->mount)->content_, path.c_str(), new_psize, nullptr);
300 XBT_DEBUG("Move file from %s to %s, size '%llu'", fd->name, fullpath, *psize);
303 XBT_WARN("File %s doesn't exist", fd->name);
307 XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.", fullpath, fd->mount);