1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "host_interface.hpp"
9 #include <simgrid/Host.hpp>
11 #include "src/simix/smx_private.h"
12 #include "cpu_cas01.hpp"
13 #include "simgrid/sg_config.h"
15 #include "network_interface.hpp"
16 #include "virtual_machine.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf,
19 "Logging specific to the SURF host module");
21 simgrid::surf::HostModel *surf_host_model = NULL;
23 void host_add_traces(){
24 surf_host_model->addTraces();
34 simgrid::xbt::Extension<simgrid::Host, Host> Host::EXTENSION_ID;
40 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
41 * constraint (capacity) of the VM in the PM layer. If the VM does not have any
42 * active task, the dummy CPU action must be deactivated, so that the VM does
43 * not get any CPU share in the PM layer. */
44 void HostModel::adjustWeightOfDummyCpuActions()
46 /* iterate for all virtual machines */
47 for (VMModel::vm_list_t::iterator iter =
48 VMModel::ws_vms.begin();
49 iter != VMModel::ws_vms.end(); ++iter) {
51 VirtualMachine *ws_vm = &*iter;
52 CpuCas01 *cpu_cas01 = static_cast<CpuCas01*>(ws_vm->p_cpu);
53 xbt_assert(cpu_cas01, "cpu-less host");
55 int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
56 // int is_active_old = constraint_is_active(cpu_cas01);
59 /* some tasks exist on this VM */
60 XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
62 /* FIXME: we should use lmm_update_variable_weight() ? */
63 /* FIXME: If we assign 1.05 and 0.05, the system makes apparently wrong values. */
64 ws_vm->p_action->setPriority(1);
67 /* no task exits on this VM */
68 XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
70 ws_vm->p_action->setPriority(0);
78 simgrid::surf::signal<void(simgrid::surf::Host*)> Host::onCreation;
79 simgrid::surf::signal<void(simgrid::surf::Host*)> Host::onDestruction;
80 simgrid::surf::signal<void(simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t)> Host::onStateChange;
82 static void host_destroy(void *h){
83 static_cast<simgrid::surf::Host*>(h)->destroy();
86 void Host::classInit()
88 if (!EXTENSION_ID.valid()) {
89 EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>(host_destroy);
90 SURF_HOST_LEVEL = EXTENSION_ID.id(); // FIXME: KILLME
94 Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props,
95 xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
96 : Resource(model, name)
97 , PropertyHolder(props)
98 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
100 p_params.ramsize = 0;
103 Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
104 xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
105 : Resource(model, name, constraint)
106 , PropertyHolder(props)
107 , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
109 p_params.ramsize = 0;
112 /** @brief use destroy() instead of this destructor */
115 xbt_assert(currentlyDestroying_, "Don't delete Hosts directly. Call destroy() instead.");
117 /** @brief Fire the require callbacks and destroy the object
119 * Don't delete directly an Host, call h->destroy() instead.
123 if (!currentlyDestroying_) {
124 currentlyDestroying_ = true;
130 void Host::attach(simgrid::Host* host)
132 if (p_host != nullptr)
133 xbt_die("Already attached to host %s", host->id().c_str());
134 host->extension_set(this);
139 void Host::setState(e_surf_resource_state_t state){
140 e_surf_resource_state_t old = Resource::getState();
141 Resource::setState(state);
142 onStateChange(this, old, state);
143 p_cpu->setState(state);
146 simgrid::surf::Storage *Host::findStorageOnMountList(const char* mount)
148 simgrid::surf::Storage *st = NULL;
152 XBT_DEBUG("Search for storage name '%s' on '%s'", mount, getName());
153 xbt_dynar_foreach(p_storage,cursor,mnt)
155 XBT_DEBUG("See '%s'",mnt.name);
156 if(!strcmp(mount,mnt.name)){
157 st = static_cast<simgrid::surf::Storage*>(mnt.storage);
161 if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, getName());
165 xbt_dict_t Host::getMountedStorageList()
169 xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
170 char *storage_name = NULL;
172 xbt_dynar_foreach(p_storage,i,mnt){
173 storage_name = (char *)static_cast<simgrid::surf::Storage*>(mnt.storage)->getName();
174 xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
179 xbt_dynar_t Host::getAttachedStorageList()
181 xbt_lib_cursor_t cursor;
184 xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
185 xbt_lib_foreach(storage_lib, cursor, key, data) {
186 if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
187 simgrid::surf::Storage *storage = static_cast<simgrid::surf::Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
188 if(!strcmp((const char*)storage->p_attach,this->getName())){
189 xbt_dynar_push_as(result, void *, (void*)storage->getName());
196 Action *Host::open(const char* fullpath) {
198 simgrid::surf::Storage *st = NULL;
201 size_t longest_prefix_length = 0;
203 char *file_mount_name = NULL;
204 char *mount_name = NULL;
206 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, getName());
207 xbt_dynar_foreach(p_storage,cursor,mnt)
209 XBT_DEBUG("See '%s'",mnt.name);
210 file_mount_name = (char *) xbt_malloc ((strlen(mnt.name)+1));
211 strncpy(file_mount_name,fullpath,strlen(mnt.name)+1);
212 file_mount_name[strlen(mnt.name)] = '\0';
214 if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
215 {/* The current mount name is found in the full path and is bigger than the previous*/
216 longest_prefix_length = strlen(mnt.name);
217 st = static_cast<simgrid::surf::Storage*>(mnt.storage);
219 free(file_mount_name);
221 if(longest_prefix_length>0)
222 { /* Mount point found, split fullpath into mount_name and path+filename*/
223 path = (char *) xbt_malloc ((strlen(fullpath)-longest_prefix_length+1));
224 mount_name = (char *) xbt_malloc ((longest_prefix_length+1));
225 strncpy(mount_name, fullpath, longest_prefix_length+1);
226 strncpy(path, fullpath+longest_prefix_length, strlen(fullpath)-longest_prefix_length+1);
227 path[strlen(fullpath)-longest_prefix_length] = '\0';
228 mount_name[longest_prefix_length] = '\0';
231 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, getName());
233 XBT_DEBUG("OPEN %s on disk '%s'",path, st->getName());
234 Action *action = st->open((const char*)mount_name, (const char*)path);
236 free((char*)mount_name);
240 Action *Host::close(surf_file_t fd) {
241 simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
242 XBT_DEBUG("CLOSE %s on disk '%s'",fd->name, st->getName());
243 return st->close(fd);
246 Action *Host::read(surf_file_t fd, sg_size_t size) {
247 simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
248 XBT_DEBUG("READ %s on disk '%s'",fd->name, st->getName());
249 return st->read(fd, size);
252 Action *Host::write(surf_file_t fd, sg_size_t size) {
253 simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
254 XBT_DEBUG("WRITE %s on disk '%s'",fd->name, st->getName());
255 return st->write(fd, size);
258 int Host::unlink(surf_file_t fd) {
260 XBT_WARN("No such file descriptor. Impossible to unlink");
264 simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
265 /* Check if the file is on this storage */
266 if (!xbt_dict_get_or_null(st->p_content, fd->name)){
267 XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
271 XBT_DEBUG("UNLINK %s on disk '%s'",fd->name, st->getName());
272 st->m_usedSize -= fd->size;
274 // Remove the file from storage
275 xbt_dict_remove(st->p_content, fd->name);
285 sg_size_t Host::getSize(surf_file_t fd){
289 xbt_dynar_t Host::getInfo( surf_file_t fd)
291 simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
292 sg_size_t *psize = xbt_new(sg_size_t, 1);
294 xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
295 xbt_dynar_push_as(info, sg_size_t *, psize);
296 xbt_dynar_push_as(info, void *, fd->mount);
297 xbt_dynar_push_as(info, void *, (void *)st->getName());
298 xbt_dynar_push_as(info, void *, st->p_typeId);
299 xbt_dynar_push_as(info, void *, st->p_contentType);
304 sg_size_t Host::fileTell(surf_file_t fd){
305 return fd->current_position;
308 int Host::fileSeek(surf_file_t fd, sg_offset_t offset, int origin){
312 fd->current_position = offset;
315 fd->current_position += offset;
318 fd->current_position = fd->size + offset;
325 int Host::fileMove(surf_file_t fd, const char* fullpath){
326 /* Check if the new full path is on the same mount point */
327 if(!strncmp((const char*)fd->mount, fullpath, strlen(fd->mount))) {
328 sg_size_t *psize, *new_psize;
330 xbt_dict_get_or_null(findStorageOnMountList(fd->mount)->p_content,
332 new_psize = xbt_new(sg_size_t, 1);
334 if (psize){// src file exists
335 xbt_dict_remove(findStorageOnMountList(fd->mount)->p_content, fd->name);
336 char *path = (char *) xbt_malloc ((strlen(fullpath)-strlen(fd->mount)+1));
337 strncpy(path, fullpath+strlen(fd->mount),
338 strlen(fullpath)-strlen(fd->mount)+1);
339 xbt_dict_set(findStorageOnMountList(fd->mount)->p_content, path,
341 XBT_DEBUG("Move file from %s to %s, size '%llu'",fd->name, fullpath, *psize);
345 XBT_WARN("File %s doesn't exist", fd->name);
349 XBT_WARN("New full path %s is not on the same mount point: %s. Action has been canceled.",
350 fullpath, fd->mount);
355 xbt_dynar_t Host::getVms()
357 xbt_dynar_t dyn = xbt_dynar_new(sizeof(simgrid::surf::VirtualMachine*), NULL);
359 /* iterate for all virtual machines */
360 for (simgrid::surf::VMModel::vm_list_t::iterator iter =
361 simgrid::surf::VMModel::ws_vms.begin();
362 iter != simgrid::surf::VMModel::ws_vms.end(); ++iter) {
364 simgrid::surf::VirtualMachine *ws_vm = &*iter;
365 if (this == ws_vm->p_subWs)
366 xbt_dynar_push(dyn, &ws_vm);
372 void Host::getParams(vm_params_t params)
377 void Host::setParams(vm_params_t params)
379 /* may check something here. */
386 simgrid::surf::signal<void(simgrid::surf::HostAction*, e_surf_action_state_t, e_surf_action_state_t)> HostAction::onStateChange;
388 void HostAction::setState(e_surf_action_state_t state){
389 e_surf_action_state_t old = getState();
390 Action::setState(state);
391 onStateChange(this, old, state);