1 /* Java bindings of the s4u::VirtualMachine */
3 /* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 package org.simgrid.msg;
10 public class VM extends Host {
11 // No need to declare a new bind variable: we use the one inherited from the super class Host
13 private Host currentHost;
14 private int coreAmount = 1;
17 * Create a `basic` VM : 1 core and 1GB of RAM.
18 * @param host Host node
19 * @param name name of the machine
21 public VM(Host host, String name) {
22 this(host,name, /*coreAmount*/1, 1024, 0, 0);
26 * Create a VM without useless values (for humans)
27 * @param host Host node
28 * @param name name of the machine
29 * @param coreAmount the amount of cores of the VM
31 public VM(Host host, String name, int coreAmount) {
32 this(host,name, coreAmount, 1024, 0, 0);
36 * Create a VM with 1 core
37 * @param host Host node
38 * @param name name of the machine
39 * @param ramSize size of the RAM that should be allocated (in MBytes)
40 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
41 * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
43 public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
44 this(host, name, /*coreAmount*/1, ramSize, migNetSpeed, dpIntensity);
49 * @param host Host node
50 * @param name name of the machine
51 * @param coreAmount the amount of cores of the VM
52 * @param ramSize size of the RAM that should be allocated (in MBytes)
53 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
54 * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
56 public VM(Host host, String name, int coreAmount, int ramSize, int migNetSpeed, int dpIntensity){
59 this.currentHost = host;
60 this.coreAmount = coreAmount;
61 create(host, name, coreAmount, ramSize, migNetSpeed, dpIntensity);
64 /** Retrieve the list of all existing VMs */
65 public static native VM[] all();
67 /** Retrieve a VM from its name */
68 public static native VM getVMByName(String name);
71 * Make sure that the GC also destroys the C object
72 * @deprecated (from Java9 onwards)
75 protected void finalize() throws Throwable {
78 private native void nativeFinalize();
80 /** Returns whether the given VM is currently suspended */
81 public native int isCreated();
83 /** Returns whether the given VM is currently running */
84 public native int isRunning();
86 /** Returns whether the given VM is currently running */
87 public native int isMigrating();
89 /** Returns whether the given VM is currently suspended */
90 public native int isSuspended();
92 /** Returns the amount of virtual CPUs provided */
93 public int getCoreAmount() {
98 * Natively implemented method create the VM.
99 * @param ramSize size of the RAM that should be allocated (in MB)
100 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
101 * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100], if you don't know put zero ;))
103 private native void create(Host host, String name, int coreAmount, int ramSize, int migNetSpeed, int dpIntensity);
106 * Set a CPU bound for a given VM.
107 * @param bound in flops/s
109 public native void setBound(double bound);
112 public native void start();
115 * Immediately kills all processes within the given VM.
117 * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
119 public native void shutdown();
121 /** Shutdown and unref the VM. */
122 public native void destroy();
124 /** Change the host on which all processes are running
125 * (pre-copy is implemented)
127 public void migrate(Host destination) throws HostFailureException{
129 this.nativeMigration(destination);
130 } catch (Exception e){
131 Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
132 throw new HostFailureException(e.getMessage());
134 // If the migration correcly returned, then we should change the currentHost value.
135 this.currentHost = destination;
137 private native void nativeMigration(Host destination) throws MsgException;
139 /** Immediately suspend the execution of all processes within the given VM
141 * No suspension cost occurs. If you want to simulate this too, you want to use a @ref File.write() before or
142 * after, depending on the exact semantic of VM suspend to you.
144 public native void suspend();
146 /** Immediately resumes the execution of all processes within the given VM
148 * No resume cost occurs. If you want to simulate this too, you want to use a @ref File.read() before or after,
149 * depending on the exact semantic of VM resume to you.
151 public native void resume();
153 /** Class initializer (for JNI), don't do it yourself */
154 private static native void nativeInit();