-//If you need to log some stuffs, just uncomment these two lines and uses XBT_DEBUG for instance
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
-
-/* **** create a VM **** */
-
-/**
- * \brief Internal function to create a SIMIX host.
- * \param name name of the host to create
- * \param data some user data (may be NULL)
- */
-sg_host_t SIMIX_vm_create(const char *name, sg_host_t ind_phys_host)
-{
- /* Create surf associated resource */
- surf_vm_model_create(name, ind_phys_host);
- sg_host_t host = sg_host_by_name(name);
- SIMIX_host_create(host);
-
- /* We will be able to register the VM to its physical host, so that we can promptly
- * retrieve the list VMs on the physical host. */
-
- return host;
-}
-
-
-/* works for VMs and PMs */
-static long host_get_ramsize(sg_host_t vm, int *overcommit)
-{
- s_vm_params_t params;
- surf_host_get_params(vm, ¶ms);
-
- if (overcommit)
- *overcommit = params.overcommit;
-
- return params.ramsize;
-}
-
-/* **** start a VM **** */
-static int __can_be_started(sg_host_t vm)
-{
- sg_host_t pm = surf_vm_get_pm(vm);
-
- int pm_overcommit = 0;
- long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
- long vm_ramsize = host_get_ramsize(vm, NULL);
-
- if (!pm_ramsize) {
- /* We assume users do not want to care about ramsize. */
- return 1;
- }
-
- if (pm_overcommit) {
- XBT_INFO("%s allows memory overcommit.", pm->key);
- return 1;
- }
-
- long total_ramsize_of_vms = 0;
- xbt_dynar_t dyn_vms = surf_host_get_vms(pm);
- {
- unsigned int cursor = 0;
- sg_host_t another_vm;
- xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
- long another_vm_ramsize = host_get_ramsize(vm, NULL);
- total_ramsize_of_vms += another_vm_ramsize;
- }
- }
-
- if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
- XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
- vm->key, pm->key, vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
- xbt_dynar_free(&dyn_vms);
- return 0;
- }
-
- xbt_dynar_free(&dyn_vms);
- return 1;
-}
-
-void SIMIX_vm_start(sg_host_t ind_vm)
-{
- if (__can_be_started(ind_vm))
- surf_host_set_state(surf_host_resource_priv(ind_vm),
- (e_surf_resource_state_t) SURF_VM_STATE_RUNNING);
- else
- THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
-}
-
-
-int SIMIX_vm_get_state(sg_host_t ind_vm)
-{
- return surf_host_get_state(surf_host_resource_priv(ind_vm));
-}
-
-/**
- * \brief Function to migrate a SIMIX VM host.
- *
- * \param host the vm host to migrate (a sg_host_t)
- */
-void SIMIX_vm_migrate(sg_host_t ind_vm, sg_host_t ind_dst_pm)
-{
- /* precopy migration makes the VM temporally paused */
- xbt_assert(SIMIX_vm_get_state(ind_vm) == SURF_VM_STATE_SUSPENDED);
-
- /* jump to vm_ws_xigrate(). this will update the vm location. */
- surf_vm_migrate(ind_vm, ind_dst_pm);
-}
-
-/**
- * \brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
- * The simcall actually invokes the following calls:
- * simcall_vm_set_affinity(vm, src_pm, 0);
- * simcall_vm_migrate(vm, dst_pm);
- * simcall_vm_resume(vm);
- *
- * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
- *
- * \param vm VM to migrate
- * \param src_pm Source physical host
- * \param dst_pmt Destination physical host
- */
-void SIMIX_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
-{
- /* deinstall the current affinity setting for the CPU */
- SIMIX_vm_set_affinity(vm, src_pm, 0);
-
- /* Update the vm location */
- SIMIX_vm_migrate(vm, dst_pm);
-
- /* Resume the VM */
- smx_process_t self = SIMIX_process_self();
- SIMIX_vm_resume(vm, self->simcall.issuer);
-}
-
-/**
- * \brief Function to get the physical host of the given SIMIX VM host.
- *
- * \param host the vm host to get_phys_host (a sg_host_t)
- */
-void *SIMIX_vm_get_pm(sg_host_t ind_vm)
-{
- /* jump to vm_ws_get_pm(). this will return the vm name. */
- return surf_vm_get_pm(ind_vm);
-}
-
-/**
- * \brief Function to set the CPU bound of the given SIMIX VM host.
- *
- * \param host the vm host (a sg_host_t)
- * \param bound bound (a double)
- */
-void SIMIX_vm_set_bound(sg_host_t ind_vm, double bound)
-{
- /* jump to vm_ws_set_vm_bound(). */
- surf_vm_set_bound(ind_vm, bound);
-}
-
-/**
- * \brief Function to set the CPU affinity of the given SIMIX VM host.
- *
- * \param host the vm host (a sg_host_t)
- * \param host the pm host (a sg_host_t)
- * \param mask affinity mask (a unsigned long)
- */
-void SIMIX_vm_set_affinity(sg_host_t ind_vm, sg_host_t ind_pm, unsigned long mask)
-{
- /* make sure this at the MSG layer. */
- xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
-
- /* jump to vm_ws_set_vm_affinity(). */
- surf_vm_set_affinity(ind_vm, ind_pm, mask);
-}
-