+ /* The workstation_VM2013 struct inherits the workstation_CLM03 struct. We
+ * create a physical workstation resource, but specifying the size of
+ * s_workstation_VM2013_t and the vm workstation model object. */
+ workstation_CLM03_t ws = (workstation_CLM03_t) surf_resource_new(sizeof(s_workstation_VM2013_t),
+ surf_vm_workstation_model, name, NULL);
+
+ /* Currently, we assume a VM has no storage. */
+ ws->storage = NULL;
+
+ /* Currently, a VM uses the network resource of its physical host. In
+ * host_lib, this network resource object is refered from two different keys.
+ * When deregistering the reference that points the network resource object
+ * from the VM name, we have to make sure that the system does not call the
+ * free callback for the network resource object. The network resource object
+ * is still used by the physical machine. */
+ ws->net_elm = xbt_lib_get_or_null(host_lib, sub_ws_name, ROUTING_HOST_LEVEL);
+ xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, ws->net_elm);
+
+ /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
+ * note workstation_VM2013 objects, inheriting the workstation_CLM03
+ * structure, are also saved there.
+ *
+ * If you want to get a workstation_VM2013 object from host_lib, see
+ * ws->generic_resouce.model->type first. If it is
+ * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
+ XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws_name, xbt_dynar_length(ws->storage));
+ xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, ws);
+
+
+ /* We initialize the VM-specific members. */
+ workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
+ vm_ws->sub_ws = sub_ws;
+ vm_ws->current_state = SURF_VM_STATE_CREATED;
+
+
+
+ // //// CPU RELATED STUFF ////
+ // Roughly, create a vcpu resource by using the values of the sub_cpu one.
+ cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
+
+ /* We can assume one core and cas01 cpu for the first step.
+ * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
+ cpu_cas01_create_resource(name, // name
+ sub_cpu->power_peak, // host->power_peak,
+ 1, // host->power_scale,
+ NULL, // host->power_trace,
+ 1, // host->core_amount,
+ SURF_RESOURCE_ON, // host->initial_state,
+ NULL, // host->state_trace,
+ NULL, // host->properties,
+ surf_cpu_model_vm);
+
+
+
+ /* We create cpu_action corresponding to a VM process on the host operating system. */
+ /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
+ // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
+ vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0);
+
+
+ /* TODO:
+ * - check how network requests are scheduled between distinct processes competing for the same card.
+ */
+}
+
+/*
+ * Update the physical host of the given VM
+ */
+static void vm_ws_migrate(void *ind_vm, void *ind_dst_pm)
+{
+ /* ind_phys_workstation equals to smx_host_t */
+ workstation_VM2013_t ws_vm2013 = surf_workstation_resource_priv(ind_vm);
+ workstation_CLM03_t ws_clm03_dst = surf_workstation_resource_priv(ind_dst_pm);
+ const char *vm_name = ws_vm2013->ws.generic_resource.name;
+ const char *pm_name_src = ws_vm2013->sub_ws->generic_resource.name;
+ const char *pm_name_dst = ws_clm03_dst->generic_resource.name;
+
+ xbt_assert(ws_vm2013);
+ xbt_assert(ws_clm03_dst);
+
+ /* do something */
+
+ /* update net_elm with that of the destination physical host */
+ void *old_net_elm = ws_vm2013->ws.net_elm;
+ void *new_net_elm = xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL);
+ xbt_assert(new_net_elm);
+
+ /* Unregister the current net_elm from host_lib. Do not call the free callback. */
+ xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
+
+ /* Then, resister the new one. */
+ ws_vm2013->ws.net_elm = new_net_elm;
+ xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, ws_vm2013->ws.net_elm);
+
+ ws_vm2013->sub_ws = ws_clm03_dst;
+
+ /* Update vcpu's action for the new pm */
+ {
+#if 0
+ XBT_INFO("cpu_action->remains %g", ws_vm2013->cpu_action->remains);
+ XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
+ ws_vm2013->cpu_action->remains,
+ ws_vm2013->cpu_action->start,
+ ws_vm2013->cpu_action->finish
+ );
+ XBT_INFO("cpu_action state %d", surf_action_state_get(ws_vm2013->cpu_action));
+#endif