1 /* Copyright (c) 2015-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 "simgrid/datatypes.h"
7 #include "simgrid/s4u/VirtualMachine.hpp"
8 #include "simgrid/s4u/host.hpp"
9 #include "simgrid/simix.hpp"
10 #include "src/instr/instr_private.h"
11 #include "src/plugins/vm/VirtualMachineImpl.hpp"
12 #include "src/simix/smx_host_private.h"
13 #include "src/surf/HostImpl.hpp"
14 #include "src/surf/cpu_cas01.hpp"
15 #include "xbt/asserts.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_vm, "S4U virtual machines");
22 VirtualMachine::VirtualMachine(const char* name, s4u::Host* pm) : Host(name)
24 XBT_DEBUG("Create VM %s", name);
26 pimpl_vm_ = new surf::VirtualMachineImpl(this, pm);
27 /* Currently, a VM uses the network resource of its physical host */
28 pimpl_netcard = pm->pimpl_netcard;
29 // Create a VCPU for this VM
30 surf::CpuCas01* sub_cpu = dynamic_cast<surf::CpuCas01*>(pm->pimpl_cpu);
32 pimpl_cpu = surf_cpu_model_vm->createCpu(this, sub_cpu->getSpeedPeakList(), 1 /*cores*/);
33 if (sub_cpu->getPState() != 0)
34 pimpl_cpu->setPState(sub_cpu->getPState());
36 /* Make a process container */
37 extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
39 if (TRACE_msg_vm_is_enabled()) {
40 container_t host_container = PJ_container_get(sg_host_get_name(pm));
41 PJ_container_new(name, INSTR_MSG_VM, host_container);
45 VirtualMachine::~VirtualMachine()
49 XBT_DEBUG("destroy %s", name().c_str());
51 /* FIXME: this is really strange that everything fails if the next line is removed.
52 * This is as if we shared these data with the PM, which definitely should not be the case...
54 * We need to test that suspending a VM does not suspends the processes running on its PM, for example.
55 * Or we need to simplify this code enough to make it actually readable (but this sounds harder than testing)
57 extension_set<simgrid::simix::Host>(nullptr);
59 /* Don't free these things twice: they are the ones of my physical host */
60 pimpl_netcard = nullptr;
63 bool VirtualMachine::isMigrating()
65 return static_cast<surf::VirtualMachineImpl*>(pimpl_)->isMigrating;
68 /** @brief Retrieve a copy of the parameters of that VM/PM
69 * @details The ramsize and overcommit fields are used on the PM too */
70 void VirtualMachine::parameters(vm_params_t params)
72 static_cast<surf::VirtualMachineImpl*>(pimpl_)->getParams(params);
74 /** @brief Sets the params of that VM/PM */
75 void VirtualMachine::setParameters(vm_params_t params)
77 simgrid::simix::kernelImmediate([&]() { static_cast<surf::VirtualMachineImpl*>(pimpl_)->setParams(params); });
80 } // namespace simgrid